Skip to content

Commit c55355b

Browse files
authored
Merge pull request #2 from brokenhandsio/FluentExtensions
Fluent extensions
2 parents 9643d3e + 7149412 commit c55355b

11 files changed

+138
-116
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ You can choose which implementations to use, or write your custom ones. For inst
3434

3535
# Models Included
3636

37-
The following models are included with this repository:
37+
The following models have Fluent extensions included with this repository:
3838

39-
* FluentAccessToken
40-
* FluentRefreshToken
41-
* FluentOAuthCode
42-
* FluentOAuthUser
43-
* FluentOAuthClient
39+
* AccessToken
40+
* RefreshToken
41+
* OAuthCode
42+
* OAuthUser
43+
* OAuthClient
4444

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

Sources/OAuthFluent/Managers/FluentClientRetriever.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ public struct FluentClientRetriever: ClientRetriever {
66
public init() {}
77

88
public func getClient(clientID: String) -> OAuthClient? {
9-
return (try? FluentOAuthClient.makeQuery().filter(FluentOAuthClient.Properties.clientID, clientID).first()) ?? nil
9+
return (try? OAuthClient.makeQuery().filter(OAuthClient.Properties.clientID, clientID).first()) ?? nil
1010
}
1111
}

Sources/OAuthFluent/Managers/FluentCodeManager.swift

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,22 @@ public struct FluentCodeManager: CodeManager {
66

77
public init() {}
88

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

1616
public func getCode(_ code: String) -> OAuthCode? {
1717
do {
18-
return try FluentOAuthCode.makeQuery().filter(FluentOAuthCode.Properties.codeString, code).first()
18+
return try OAuthCode.makeQuery().filter(OAuthCode.Properties.codeString, code).first()
1919
} catch {
2020
return nil
2121
}
2222
}
2323

2424
public func codeUsed(_ code: OAuthCode) {
25-
guard let fluentCode = code as? FluentOAuthCode else {
26-
return
27-
}
28-
29-
try? fluentCode.delete()
25+
try? code.delete()
3026
}
3127
}

Sources/OAuthFluent/Managers/FluentTokenManager.swift

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,45 +9,41 @@ public struct FluentTokenManager: TokenManager {
99

1010
public func getAccessToken(_ accessToken: String) -> AccessToken? {
1111
do {
12-
return try FluentAccessToken.makeQuery().filter(FluentAccessToken.Properties.tokenString, accessToken).first()
12+
return try AccessToken.makeQuery().filter(AccessToken.Properties.tokenString, accessToken).first()
1313
} catch {
1414
return nil
1515
}
1616
}
1717

1818
public func getRefreshToken(_ refreshToken: String) -> RefreshToken? {
1919
do {
20-
return try FluentRefreshToken.makeQuery().filter(FluentRefreshToken.Properties.tokenString, refreshToken).first()
20+
return try RefreshToken.makeQuery().filter(RefreshToken.Properties.tokenString, refreshToken).first()
2121
} catch {
2222
return nil
2323
}
2424
}
2525

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

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

3838
let refreshTokenString = try Random.bytes(count: 32).hexString
39-
let refreshToken = FluentRefreshToken(tokenString: refreshTokenString, clientID: clientID, userID: userID, scopes: scopes)
39+
let refreshToken = RefreshToken(tokenString: refreshTokenString, clientID: clientID, userID: userID, scopes: scopes)
4040
try refreshToken.save()
4141

4242
return (accessToken, refreshToken)
4343
}
4444

4545
public func updateRefreshToken(_ refreshToken: RefreshToken, scopes: [String]) {
46-
guard let refreshTokenToUpdate = refreshToken as? FluentRefreshToken else {
47-
return
48-
}
49-
50-
refreshTokenToUpdate.scopes = scopes
51-
try? refreshTokenToUpdate.save()
46+
refreshToken.scopes = scopes
47+
try? refreshToken.save()
5248
}
5349
}

Sources/OAuthFluent/Managers/FluentUserManager.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ public struct FluentUserManager: UserManager {
66

77
public init() {}
88

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

15-
public func getUser(id: String) -> OAuthUser? {
16-
return (try? FluentOAuthUser.find(id)) ?? nil
15+
public func getUser(id: Identifier) -> OAuthUser? {
16+
return (try? OAuthUser.find(id)) ?? nil
1717
}
1818
}

Sources/OAuthFluent/Models/FluentAccessToken.swift renamed to Sources/OAuthFluent/Models/AccessToken+Fluent.swift

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import FluentProvider
22
import OAuth
33
import Foundation
44

5-
public final class FluentAccessToken: AccessToken, Model {
5+
extension AccessToken: Model {
66

77
struct Properties {
88
static let tokenString = "token_string"
@@ -12,12 +12,23 @@ public final class FluentAccessToken: AccessToken, Model {
1212
static let scopes = "scopes"
1313
}
1414

15-
public let storage = Storage()
15+
public var storage: Storage {
16+
get {
17+
if let storage = extend["fluent-storage"] as? Storage {
18+
return storage
19+
}
20+
else {
21+
let storage = Storage()
22+
extend["fluent-storage"] = storage
23+
return storage
24+
}
25+
}
26+
}
1627

17-
public init(row: Row) throws {
28+
public convenience init(row: Row) throws {
1829
let tokenString: String = try row.get(Properties.tokenString)
1930
let clientID: String = try row.get(Properties.clientID)
20-
let userID: String? = try? row.get(Properties.userID)
31+
let userID: Identifier? = try? row.get(Properties.userID)
2132
let expiryTime: Date = try row.get(Properties.expiryTime)
2233
let scopesString: String? = try? row.get(Properties.scopes)
2334

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

33-
super.init(tokenString: tokenString, clientID: clientID, userID: userID, scopes: scopes, expiryTime: expiryTime)
34-
}
35-
36-
override public init(tokenString: String, clientID: String, userID: String?, scopes: [String]?, expiryTime: Date) {
37-
super.init(tokenString: tokenString, clientID: clientID, userID: userID, scopes: scopes, expiryTime: expiryTime)
44+
self.init(tokenString: tokenString, clientID: clientID, userID: userID, scopes: scopes, expiryTime: expiryTime)
3845
}
3946

4047
public func makeRow() throws -> Row {
@@ -48,7 +55,7 @@ public final class FluentAccessToken: AccessToken, Model {
4855
}
4956
}
5057

51-
extension FluentAccessToken: Preparation {
58+
extension AccessToken: Preparation {
5259
public static func prepare(_ database: Database) throws {
5360
try database.create(self) { builder in
5461
builder.id()
@@ -58,6 +65,8 @@ extension FluentAccessToken: Preparation {
5865
builder.date(Properties.expiryTime)
5966
builder.string(Properties.scopes, optional: true)
6067
}
68+
69+
try database.index(Properties.tokenString, for: AccessToken.self)
6170
}
6271

6372
public static func revert(_ database: Database) throws {

Sources/OAuthFluent/Models/FluentOAuthClient.swift renamed to Sources/OAuthFluent/Models/OAuthClient+Fluent.swift

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import OAuth
22
import FluentProvider
33

4-
public final class FluentOAuthClient: OAuthClient, Model {
5-
4+
extension OAuthClient: Model {
5+
66
struct Properties {
77
static let clientID = "client_id"
88
static let clientSecret = "client_secret"
@@ -13,9 +13,20 @@ public final class FluentOAuthClient: OAuthClient, Model {
1313
static let allowGrantTypes = "allowed_grant_types"
1414
}
1515

16-
public let storage = Storage()
16+
public var storage: Storage {
17+
get {
18+
if let storage = extend["fluent-storage"] as? Storage {
19+
return storage
20+
}
21+
else {
22+
let storage = Storage()
23+
extend["fluent-storage"] = storage
24+
return storage
25+
}
26+
}
27+
}
1728

18-
public init(row: Row) throws {
29+
public convenience init(row: Row) throws {
1930
let clientID: String = try row.get(Properties.clientID)
2031
let clientSecret: String? = try? row.get(Properties.clientSecret)
2132
let redirectURIsString: String? = try? row.get(Properties.redirectURIs)
@@ -58,12 +69,7 @@ public final class FluentOAuthClient: OAuthClient, Model {
5869
allowedGrantTypes = nil
5970
}
6071

61-
super.init(clientID: clientID, redirectURIs: redirectURIs, clientSecret: clientSecret, validScopes: scopes, confidential: confidentalClient, firstParty: firstParty, allowedGrantTypes: allowedGrantTypes)
62-
}
63-
64-
65-
public override init(clientID: String, redirectURIs: [String]?, clientSecret: String? = nil, validScopes: [String]? = nil, confidential: Bool? = nil, firstParty: Bool = false, allowedGrantTypes: [OAuthFlowType]? = nil) {
66-
super.init(clientID: clientID, redirectURIs: redirectURIs, clientSecret: clientSecret, validScopes: validScopes, confidential: confidential, firstParty: firstParty, allowedGrantTypes: allowedGrantTypes)
72+
self.init(clientID: clientID, redirectURIs: redirectURIs, clientSecret: clientSecret, validScopes: scopes, confidential: confidentalClient, firstParty: firstParty, allowedGrantTypes: allowedGrantTypes)
6773
}
6874

6975
public func makeRow() throws -> Row {
@@ -83,7 +89,7 @@ public final class FluentOAuthClient: OAuthClient, Model {
8389
}
8490
}
8591

86-
extension FluentOAuthClient: Preparation {
92+
extension OAuthClient: Preparation {
8793
public static func prepare(_ database: Database) throws {
8894
try database.create(self) { builder in
8995
builder.id()
@@ -95,6 +101,8 @@ extension FluentOAuthClient: Preparation {
95101
builder.bool(Properties.firstParty)
96102
builder.string(Properties.allowGrantTypes, optional: true)
97103
}
104+
105+
try database.index(Properties.clientID, for: OAuthClient.self)
98106
}
99107

100108
public static func revert(_ database: Database) throws {

Sources/OAuthFluent/Models/FluentOAuthCode.swift renamed to Sources/OAuthFluent/Models/OAuthCode+Fluent.swift

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import OAuth
22
import FluentProvider
33

4-
public final class FluentOAuthCode: OAuthCode, Model {
4+
extension OAuthCode: Model {
55

66
struct Properties {
77
static let codeString = "code_string"
@@ -12,22 +12,29 @@ public final class FluentOAuthCode: OAuthCode, Model {
1212
static let scopes = "scopes"
1313
}
1414

15-
public let storage = Storage()
15+
public var storage: Storage {
16+
get {
17+
if let storage = extend["fluent-storage"] as? Storage {
18+
return storage
19+
}
20+
else {
21+
let storage = Storage()
22+
extend["fluent-storage"] = storage
23+
return storage
24+
}
25+
}
26+
}
1627

17-
public init(row: Row) throws {
28+
public convenience init(row: Row) throws {
1829
let codeString: String = try row.get(Properties.codeString)
1930
let clientID: String = try row.get(Properties.clientID)
2031
let redirectURI: String = try row.get(Properties.redirectURI)
21-
let userID: String = try row.get(Properties.userID)
32+
let userID: Identifier = try row.get(Properties.userID)
2233
let expiryDate: Date = try row.get(Properties.expiryDate)
2334
let scopesString: String? = try? row.get(Properties.scopes)
2435
let scopes = scopesString?.components(separatedBy: " ")
2536

26-
super.init(codeID: codeString, clientID: clientID, redirectURI: redirectURI, userID: userID, expiryDate: expiryDate, scopes: scopes)
27-
}
28-
29-
override public init(codeID: String, clientID: String, redirectURI: String, userID: String, expiryDate: Date, scopes: [String]?) {
30-
super.init(codeID: codeID, clientID: clientID, redirectURI: redirectURI, userID: userID, expiryDate: expiryDate, scopes: scopes)
37+
self.init(codeID: codeString, clientID: clientID, redirectURI: redirectURI, userID: userID, expiryDate: expiryDate, scopes: scopes)
3138
}
3239

3340
public func makeRow() throws -> Row {
@@ -42,7 +49,7 @@ public final class FluentOAuthCode: OAuthCode, Model {
4249
}
4350
}
4451

45-
extension FluentOAuthCode: Preparation {
52+
extension OAuthCode: Preparation {
4653
public static func prepare(_ database: Database) throws {
4754
try database.create(self) { builder in
4855
builder.id()
@@ -53,6 +60,8 @@ extension FluentOAuthCode: Preparation {
5360
builder.date(Properties.expiryDate)
5461
builder.string(Properties.scopes, optional: true)
5562
}
63+
64+
try database.index(Properties.codeString, for: OAuthCode.self)
5665
}
5766

5867
public static func revert(_ database: Database) throws {

0 commit comments

Comments
 (0)