Skip to content

Commit

Permalink
Merge pull request #2 from brokenhandsio/FluentExtensions
Browse files Browse the repository at this point in the history
Fluent extensions
  • Loading branch information
0xTim authored Aug 3, 2017
2 parents 9643d3e + 7149412 commit c55355b
Show file tree
Hide file tree
Showing 11 changed files with 138 additions and 116 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ You can choose which implementations to use, or write your custom ones. For inst

# Models Included

The following models are included with this repository:
The following models have Fluent extensions included with this repository:

* FluentAccessToken
* FluentRefreshToken
* FluentOAuthCode
* FluentOAuthUser
* FluentOAuthClient
* AccessToken
* RefreshToken
* OAuthCode
* OAuthUser
* OAuthClient

**Note** you will need to add these models to your preparations if you wish to use any of these.

Expand Down
2 changes: 1 addition & 1 deletion Sources/OAuthFluent/Managers/FluentClientRetriever.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ public struct FluentClientRetriever: ClientRetriever {
public init() {}

public func getClient(clientID: String) -> OAuthClient? {
return (try? FluentOAuthClient.makeQuery().filter(FluentOAuthClient.Properties.clientID, clientID).first()) ?? nil
return (try? OAuthClient.makeQuery().filter(OAuthClient.Properties.clientID, clientID).first()) ?? nil
}
}
12 changes: 4 additions & 8 deletions Sources/OAuthFluent/Managers/FluentCodeManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,22 @@ public struct FluentCodeManager: CodeManager {

public init() {}

public func generateCode(userID: String, clientID: String, redirectURI: String, scopes: [String]?) throws -> String {
public func generateCode(userID: Identifier, clientID: String, redirectURI: String, scopes: [String]?) throws -> String {
let codeString = try Random.bytes(count: 32).hexString
let fluentCode = FluentOAuthCode(codeID: codeString, clientID: clientID, redirectURI: redirectURI, userID: userID, expiryDate: Date().addingTimeInterval(60), scopes: scopes)
let fluentCode = OAuthCode(codeID: codeString, clientID: clientID, redirectURI: redirectURI, userID: userID, expiryDate: Date().addingTimeInterval(60), scopes: scopes)
try fluentCode.save()
return codeString
}

public func getCode(_ code: String) -> OAuthCode? {
do {
return try FluentOAuthCode.makeQuery().filter(FluentOAuthCode.Properties.codeString, code).first()
return try OAuthCode.makeQuery().filter(OAuthCode.Properties.codeString, code).first()
} catch {
return nil
}
}

public func codeUsed(_ code: OAuthCode) {
guard let fluentCode = code as? FluentOAuthCode else {
return
}

try? fluentCode.delete()
try? code.delete()
}
}
22 changes: 9 additions & 13 deletions Sources/OAuthFluent/Managers/FluentTokenManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,45 +9,41 @@ public struct FluentTokenManager: TokenManager {

public func getAccessToken(_ accessToken: String) -> AccessToken? {
do {
return try FluentAccessToken.makeQuery().filter(FluentAccessToken.Properties.tokenString, accessToken).first()
return try AccessToken.makeQuery().filter(AccessToken.Properties.tokenString, accessToken).first()
} catch {
return nil
}
}

public func getRefreshToken(_ refreshToken: String) -> RefreshToken? {
do {
return try FluentRefreshToken.makeQuery().filter(FluentRefreshToken.Properties.tokenString, refreshToken).first()
return try RefreshToken.makeQuery().filter(RefreshToken.Properties.tokenString, refreshToken).first()
} catch {
return nil
}
}

public func generateAccessToken(clientID: String, userID: String?, scopes: [String]?, expiryTime: Int) throws -> AccessToken {
public func generateAccessToken(clientID: String, userID: Identifier?, scopes: [String]?, expiryTime: Int) throws -> AccessToken {
let accessTokenString = try Random.bytes(count: 32).hexString
let accessToken = FluentAccessToken(tokenString: accessTokenString, clientID: clientID, userID: userID, scopes: scopes, expiryTime: Date().addingTimeInterval(TimeInterval(expiryTime)))
let accessToken = AccessToken(tokenString: accessTokenString, clientID: clientID, userID: userID, scopes: scopes, expiryTime: Date().addingTimeInterval(TimeInterval(expiryTime)))
try accessToken.save()
return accessToken
}

public func generateAccessRefreshTokens(clientID: String, userID: String?, scopes: [String]?, accessTokenExpiryTime: Int) throws -> (AccessToken, RefreshToken) {
public func generateAccessRefreshTokens(clientID: String, userID: Identifier?, scopes: [String]?, accessTokenExpiryTime: Int) throws -> (AccessToken, RefreshToken) {
let accessTokenString = try Random.bytes(count: 32).hexString
let accessToken = FluentAccessToken(tokenString: accessTokenString, clientID: clientID, userID: userID, scopes: scopes, expiryTime: Date().addingTimeInterval(TimeInterval(accessTokenExpiryTime)))
let accessToken = AccessToken(tokenString: accessTokenString, clientID: clientID, userID: userID, scopes: scopes, expiryTime: Date().addingTimeInterval(TimeInterval(accessTokenExpiryTime)))
try accessToken.save()

let refreshTokenString = try Random.bytes(count: 32).hexString
let refreshToken = FluentRefreshToken(tokenString: refreshTokenString, clientID: clientID, userID: userID, scopes: scopes)
let refreshToken = RefreshToken(tokenString: refreshTokenString, clientID: clientID, userID: userID, scopes: scopes)
try refreshToken.save()

return (accessToken, refreshToken)
}

public func updateRefreshToken(_ refreshToken: RefreshToken, scopes: [String]) {
guard let refreshTokenToUpdate = refreshToken as? FluentRefreshToken else {
return
}

refreshTokenToUpdate.scopes = scopes
try? refreshTokenToUpdate.save()
refreshToken.scopes = scopes
try? refreshToken.save()
}
}
10 changes: 5 additions & 5 deletions Sources/OAuthFluent/Managers/FluentUserManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ public struct FluentUserManager: UserManager {

public init() {}

public func authenticateUser(username: String, password: String) -> String? {
public func authenticateUser(username: String, password: String) -> Identifier? {
let credentials = Password(username: username, password: password)
let user = try? FluentOAuthUser.authenticate(credentials)
return user?.userID
let user = try? OAuthUser.authenticate(credentials)
return user?.id
}

public func getUser(id: String) -> OAuthUser? {
return (try? FluentOAuthUser.find(id)) ?? nil
public func getUser(id: Identifier) -> OAuthUser? {
return (try? OAuthUser.find(id)) ?? nil
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import FluentProvider
import OAuth
import Foundation

public final class FluentAccessToken: AccessToken, Model {
extension AccessToken: Model {

struct Properties {
static let tokenString = "token_string"
Expand All @@ -12,12 +12,23 @@ public final class FluentAccessToken: AccessToken, Model {
static let scopes = "scopes"
}

public let storage = Storage()
public var storage: Storage {
get {
if let storage = extend["fluent-storage"] as? Storage {
return storage
}
else {
let storage = Storage()
extend["fluent-storage"] = storage
return storage
}
}
}

public init(row: Row) throws {
public convenience init(row: Row) throws {
let tokenString: String = try row.get(Properties.tokenString)
let clientID: String = try row.get(Properties.clientID)
let userID: String? = try? row.get(Properties.userID)
let userID: Identifier? = try? row.get(Properties.userID)
let expiryTime: Date = try row.get(Properties.expiryTime)
let scopesString: String? = try? row.get(Properties.scopes)

Expand All @@ -30,11 +41,7 @@ public final class FluentAccessToken: AccessToken, Model {
scopes = nil
}

super.init(tokenString: tokenString, clientID: clientID, userID: userID, scopes: scopes, expiryTime: expiryTime)
}

override public init(tokenString: String, clientID: String, userID: String?, scopes: [String]?, expiryTime: Date) {
super.init(tokenString: tokenString, clientID: clientID, userID: userID, scopes: scopes, expiryTime: expiryTime)
self.init(tokenString: tokenString, clientID: clientID, userID: userID, scopes: scopes, expiryTime: expiryTime)
}

public func makeRow() throws -> Row {
Expand All @@ -48,7 +55,7 @@ public final class FluentAccessToken: AccessToken, Model {
}
}

extension FluentAccessToken: Preparation {
extension AccessToken: Preparation {
public static func prepare(_ database: Database) throws {
try database.create(self) { builder in
builder.id()
Expand All @@ -58,6 +65,8 @@ extension FluentAccessToken: Preparation {
builder.date(Properties.expiryTime)
builder.string(Properties.scopes, optional: true)
}

try database.index(Properties.tokenString, for: AccessToken.self)
}

public static func revert(_ database: Database) throws {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import OAuth
import FluentProvider

public final class FluentOAuthClient: OAuthClient, Model {
extension OAuthClient: Model {

struct Properties {
static let clientID = "client_id"
static let clientSecret = "client_secret"
Expand All @@ -13,9 +13,20 @@ public final class FluentOAuthClient: OAuthClient, Model {
static let allowGrantTypes = "allowed_grant_types"
}

public let storage = Storage()
public var storage: Storage {
get {
if let storage = extend["fluent-storage"] as? Storage {
return storage
}
else {
let storage = Storage()
extend["fluent-storage"] = storage
return storage
}
}
}

public init(row: Row) throws {
public convenience init(row: Row) throws {
let clientID: String = try row.get(Properties.clientID)
let clientSecret: String? = try? row.get(Properties.clientSecret)
let redirectURIsString: String? = try? row.get(Properties.redirectURIs)
Expand Down Expand Up @@ -58,12 +69,7 @@ public final class FluentOAuthClient: OAuthClient, Model {
allowedGrantTypes = nil
}

super.init(clientID: clientID, redirectURIs: redirectURIs, clientSecret: clientSecret, validScopes: scopes, confidential: confidentalClient, firstParty: firstParty, allowedGrantTypes: allowedGrantTypes)
}


public override init(clientID: String, redirectURIs: [String]?, clientSecret: String? = nil, validScopes: [String]? = nil, confidential: Bool? = nil, firstParty: Bool = false, allowedGrantTypes: [OAuthFlowType]? = nil) {
super.init(clientID: clientID, redirectURIs: redirectURIs, clientSecret: clientSecret, validScopes: validScopes, confidential: confidential, firstParty: firstParty, allowedGrantTypes: allowedGrantTypes)
self.init(clientID: clientID, redirectURIs: redirectURIs, clientSecret: clientSecret, validScopes: scopes, confidential: confidentalClient, firstParty: firstParty, allowedGrantTypes: allowedGrantTypes)
}

public func makeRow() throws -> Row {
Expand All @@ -83,7 +89,7 @@ public final class FluentOAuthClient: OAuthClient, Model {
}
}

extension FluentOAuthClient: Preparation {
extension OAuthClient: Preparation {
public static func prepare(_ database: Database) throws {
try database.create(self) { builder in
builder.id()
Expand All @@ -95,6 +101,8 @@ extension FluentOAuthClient: Preparation {
builder.bool(Properties.firstParty)
builder.string(Properties.allowGrantTypes, optional: true)
}

try database.index(Properties.clientID, for: OAuthClient.self)
}

public static func revert(_ database: Database) throws {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import OAuth
import FluentProvider

public final class FluentOAuthCode: OAuthCode, Model {
extension OAuthCode: Model {

struct Properties {
static let codeString = "code_string"
Expand All @@ -12,22 +12,29 @@ public final class FluentOAuthCode: OAuthCode, Model {
static let scopes = "scopes"
}

public let storage = Storage()
public var storage: Storage {
get {
if let storage = extend["fluent-storage"] as? Storage {
return storage
}
else {
let storage = Storage()
extend["fluent-storage"] = storage
return storage
}
}
}

public init(row: Row) throws {
public convenience init(row: Row) throws {
let codeString: String = try row.get(Properties.codeString)
let clientID: String = try row.get(Properties.clientID)
let redirectURI: String = try row.get(Properties.redirectURI)
let userID: String = try row.get(Properties.userID)
let userID: Identifier = try row.get(Properties.userID)
let expiryDate: Date = try row.get(Properties.expiryDate)
let scopesString: String? = try? row.get(Properties.scopes)
let scopes = scopesString?.components(separatedBy: " ")

super.init(codeID: codeString, clientID: clientID, redirectURI: redirectURI, userID: userID, expiryDate: expiryDate, scopes: scopes)
}

override public init(codeID: String, clientID: String, redirectURI: String, userID: String, expiryDate: Date, scopes: [String]?) {
super.init(codeID: codeID, clientID: clientID, redirectURI: redirectURI, userID: userID, expiryDate: expiryDate, scopes: scopes)
self.init(codeID: codeString, clientID: clientID, redirectURI: redirectURI, userID: userID, expiryDate: expiryDate, scopes: scopes)
}

public func makeRow() throws -> Row {
Expand All @@ -42,7 +49,7 @@ public final class FluentOAuthCode: OAuthCode, Model {
}
}

extension FluentOAuthCode: Preparation {
extension OAuthCode: Preparation {
public static func prepare(_ database: Database) throws {
try database.create(self) { builder in
builder.id()
Expand All @@ -53,6 +60,8 @@ extension FluentOAuthCode: Preparation {
builder.date(Properties.expiryDate)
builder.string(Properties.scopes, optional: true)
}

try database.index(Properties.codeString, for: OAuthCode.self)
}

public static func revert(_ database: Database) throws {
Expand Down
Loading

0 comments on commit c55355b

Please sign in to comment.