Skip to content

Commit 9a56ce4

Browse files
committed
Update OAuthUser to have an id property of type Identifer to make Fluent integration simple
1 parent f032e2c commit 9a56ce4

23 files changed

Lines changed: 69 additions & 48 deletions

Sources/OAuth/DefaultImplementations/EmptyCodeManager.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
import Node
2+
13
public struct EmptyCodeManager: CodeManager {
24
public init() {}
35

46
public func getCode(_ code: String) -> OAuthCode? {
57
return nil
68
}
79

8-
public func generateCode(userID: String, clientID: String, redirectURI: String, scopes: [String]?) throws -> String {
10+
public func generateCode(userID: Identifier, clientID: String, redirectURI: String, scopes: [String]?) throws -> String {
911
return ""
1012
}
1113

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1+
import Node
2+
13
public struct EmptyUserManager: UserManager {
24

35
public init() {}
46

5-
public func getUser(id: String) -> OAuthUser? {
7+
public func getUser(id: Identifier) -> OAuthUser? {
68
return nil
79
}
810

9-
public func authenticateUser(username: String, password: String) -> String? {
11+
public func authenticateUser(username: String, password: String) -> Identifier? {
1012
return nil
1113
}
1214
}

Sources/OAuth/Models/OAuthCode.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
import Foundation
22
import Core
3+
import Node
34

45
public final class OAuthCode {
56
public let codeID: String
67
public let clientID: String
78
public let redirectURI: String
8-
public let userID: String
9+
public let userID: Identifier
910
public let expiryDate: Date
1011
public let scopes: [String]?
1112

1213
public var extend: [String: Any] = [:]
1314

14-
public init(codeID: String, clientID: String, redirectURI: String, userID: String, expiryDate: Date, scopes: [String]?) {
15+
public init(codeID: String, clientID: String, redirectURI: String, userID: Identifier, expiryDate: Date, scopes: [String]?) {
1516
self.codeID = codeID
1617
self.clientID = clientID
1718
self.redirectURI = redirectURI
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
import Authentication
22
import Core
3+
import Node
34

45
public final class OAuthUser: Authenticatable, Extendable {
56
public let username: String
67
public let emailAddress: String?
78
public let password: Bytes
8-
public let userID: String?
9+
public var id: Identifier?
910

1011
public var extend: [String: Any] = [:]
1112

12-
public init(userID: String?, username: String, emailAddress: String?, password: Bytes) {
13+
public init(id: Identifier? = nil, username: String, emailAddress: String?, password: Bytes) {
1314
self.username = username
1415
self.emailAddress = emailAddress
1516
self.password = password
16-
self.userID = userID
17+
self.id = id
1718
}
1819
}

Sources/OAuth/Models/Tokens/AccessToken.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import Foundation
22
import Core
3+
import Node
34

45
public final class AccessToken: Extendable {
56
public let tokenString: String
67
public let clientID: String
7-
public let userID: String?
8+
public let userID: Identifier?
89
public let scopes: [String]?
910
public let expiryTime: Date
1011

1112
public var extend: [String: Any] = [:]
1213

13-
public init(tokenString: String, clientID: String, userID: String?, scopes: [String]? = nil, expiryTime: Date) {
14+
public init(tokenString: String, clientID: String, userID: Identifier?, scopes: [String]? = nil, expiryTime: Date) {
1415
self.tokenString = tokenString
1516
self.clientID = clientID
1617
self.userID = userID

Sources/OAuth/Models/Tokens/RefreshToken.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import Core
2+
import Node
23

34
public final class RefreshToken: Extendable {
45
public let tokenString: String
56
public let clientID: String
6-
public let userID: String?
7+
public let userID: Identifier?
78
public var scopes: [String]?
89

910
public var extend: [String: Any] = [:]
1011

11-
public init(tokenString: String, clientID: String, userID: String?, scopes: [String]? = nil) {
12+
public init(tokenString: String, clientID: String, userID: Identifier?, scopes: [String]? = nil) {
1213
self.tokenString = tokenString
1314
self.clientID = clientID
1415
self.userID = userID

Sources/OAuth/OAuth2Provider.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ struct OAuth2Provider {
104104
throw Abort.unauthorized
105105
}
106106

107-
guard let userID = user.userID else {
107+
guard let userID = user.id else {
108108
throw Abort.unauthorized
109109
}
110110

@@ -157,7 +157,7 @@ struct OAuth2Provider {
157157

158158
if approveApplication {
159159
if responseType == ResponseType.token {
160-
let accessToken = try tokenManager.generateAccessToken(clientID: clientID, userID: user.userID, scopes: scopes, expiryTime: 3600)
160+
let accessToken = try tokenManager.generateAccessToken(clientID: clientID, userID: user.id, scopes: scopes, expiryTime: 3600)
161161
redirectURI += "#token_type=bearer&access_token=\(accessToken.tokenString)&expires_in=3600"
162162
}
163163
else if responseType == ResponseType.code {

Sources/OAuth/Protocols/CodeManager.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import Node
2+
13
public protocol CodeManager {
2-
func generateCode(userID: String, clientID: String, redirectURI: String, scopes: [String]?) throws -> String
4+
func generateCode(userID: Identifier, clientID: String, redirectURI: String, scopes: [String]?) throws -> String
35
func getCode(_ code: String) -> OAuthCode?
46

57
// This is explicit to ensure that the code is marked as used or deleted (it could be implied that this is done when you call

Sources/OAuth/Protocols/TokenManager.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import Node
2+
13
public protocol TokenManager {
2-
func generateAccessRefreshTokens(clientID: String, userID: String?, scopes: [String]?, accessTokenExpiryTime: Int) throws -> (AccessToken, RefreshToken)
3-
func generateAccessToken(clientID: String, userID: String?, scopes: [String]?, expiryTime: Int) throws -> AccessToken
4+
func generateAccessRefreshTokens(clientID: String, userID: Identifier?, scopes: [String]?, accessTokenExpiryTime: Int) throws -> (AccessToken, RefreshToken)
5+
func generateAccessToken(clientID: String, userID: Identifier?, scopes: [String]?, expiryTime: Int) throws -> AccessToken
46
func getRefreshToken(_ refreshToken: String) -> RefreshToken?
57
func getAccessToken(_ accessToken: String) -> AccessToken?
68
func updateRefreshToken(_ refreshToken: RefreshToken, scopes: [String])
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import Node
2+
13
public protocol UserManager {
2-
func authenticateUser(username: String, password: String) -> String?
3-
func getUser(id: String) -> OAuthUser?
4+
func authenticateUser(username: String, password: String) -> Identifier?
5+
func getUser(id: Identifier) -> OAuthUser?
46
}

0 commit comments

Comments
 (0)