diff --git a/Sources/OAuthFluent/Managers/FluentClientRetriever.swift b/Sources/OAuthFluent/Managers/FluentClientRetriever.swift index e13a333..25881e4 100644 --- a/Sources/OAuthFluent/Managers/FluentClientRetriever.swift +++ b/Sources/OAuthFluent/Managers/FluentClientRetriever.swift @@ -5,7 +5,7 @@ public struct FluentClientRetriever: ClientRetriever { public init() {} - public func getClient(clientID: String) -> OAuthClient? { - return (try? FluentOAuthClient.find(clientID)) ?? nil + public func getClient(clientID: String) -> OAuthClient? { + return (try? FluentOAuthClient.makeQuery().filter(FluentOAuthClient.Properties.clientID, clientID).first()) ?? nil } } diff --git a/Sources/OAuthFluent/Models/FluentOAuthClient.swift b/Sources/OAuthFluent/Models/FluentOAuthClient.swift index 221a792..9ebb2ca 100644 --- a/Sources/OAuthFluent/Models/FluentOAuthClient.swift +++ b/Sources/OAuthFluent/Models/FluentOAuthClient.swift @@ -4,8 +4,9 @@ import FluentProvider public final class FluentOAuthClient: OAuthClient, Model { struct Properties { - static let redirectURIs = "redirect_uris" + static let clientID = "client_id" static let clientSecret = "client_secret" + static let redirectURIs = "redirect_uris" static let scopes = "scopes" static let confidentialClient = "confidential_client" static let firstParty = "first_party" @@ -15,8 +16,9 @@ public final class FluentOAuthClient: OAuthClient, Model { public let storage = Storage() public init(row: Row) throws { - let redirectURIsString: String? = try? row.get(Properties.redirectURIs) + let clientID: String = try row.get(Properties.clientID) let clientSecret: String? = try? row.get(Properties.clientSecret) + let redirectURIsString: String? = try? row.get(Properties.redirectURIs) let scopeString: String? = try row.get(Properties.scopes) let confidentalClient: Bool? = try? row.get(Properties.confidentialClient) let firstParty: Bool = try row.get(Properties.firstParty) @@ -56,18 +58,20 @@ public final class FluentOAuthClient: OAuthClient, Model { allowedGrantTypes = nil } - super.init(clientID: "ID", redirectURIs: redirectURIs, clientSecret: clientSecret, validScopes: scopes, confidential: confidentalClient, firstParty: firstParty, allowedGrantTypes: allowedGrantTypes) + super.init(clientID: clientID, redirectURIs: redirectURIs, clientSecret: clientSecret, validScopes: scopes, confidential: confidentalClient, firstParty: firstParty, allowedGrantTypes: allowedGrantTypes) } - public init (redirectURIs: [String]?, clientSecret: String? = nil, validScopes: [String]? = nil, confidential: Bool? = nil, firstParty: Bool = false, allowedGrantTypes: [OAuthFlowType]? = nil) { - super.init(clientID: "", redirectURIs: redirectURIs, clientSecret: clientSecret, validScopes: validScopes, confidential: confidential, 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) } public func makeRow() throws -> Row { var row = Row() - try row.set(Properties.redirectURIs, redirectURIs?.joined(separator: " ")) + try row.set(Properties.clientID, clientID) try row.set(Properties.clientSecret, clientSecret) + try row.set(Properties.redirectURIs, redirectURIs?.joined(separator: " ")) try row.set(Properties.scopes, validScopes?.joined(separator: " ")) try row.set(Properties.confidentialClient, confidentialClient) try row.set(Properties.firstParty, firstParty) @@ -77,25 +81,13 @@ public final class FluentOAuthClient: OAuthClient, Model { return row } - - override public var clientID: String { - get { - guard let storageIDNode = try? id.makeNode(in: nil), let storageID = storageIDNode.string else { - return "IDENTIFIER" - } - - return storageID - } - set { - self.id = Identifier(newValue) - } - } } extension FluentOAuthClient: Preparation { public static func prepare(_ database: Database) throws { try database.create(self) { builder in builder.id() + builder.string(Properties.clientID) builder.string(Properties.redirectURIs, optional: true) builder.string(Properties.clientSecret, optional: true) builder.string(Properties.scopes, optional: true) diff --git a/Tests/OAuthFluentTests/OAuthFluentTests.swift b/Tests/OAuthFluentTests/OAuthFluentTests.swift index 339fe11..5ad8130 100644 --- a/Tests/OAuthFluentTests/OAuthFluentTests.swift +++ b/Tests/OAuthFluentTests/OAuthFluentTests.swift @@ -22,6 +22,7 @@ class OAuthFluentTests: XCTestCase { let capturingAuthHandler = CapturingAuthHandler() let scope = "email" let redirectURI = "https://api.brokenhands.io/callback" + let clientID = "ABCDEFG" let clientSecret = "1234" let email = "han@therebelalliance.com" let username = "han" @@ -60,7 +61,8 @@ class OAuthFluentTests: XCTestCase { user = FluentOAuthUser(username: username, emailAddress: email, password: passwordHash) try! user.save() - oauthClient = FluentOAuthClient(redirectURIs: [redirectURI], clientSecret: clientSecret, validScopes: [scope], confidential: true, firstParty: true) + oauthClient = FluentOAuthClient(clientID: clientID, redirectURIs: [redirectURI], clientSecret: clientSecret, validScopes: [scope], confidential: true, firstParty: true) + try! oauthClient.save() } @@ -76,9 +78,7 @@ class OAuthFluentTests: XCTestCase { #endif } - func testThatAuthCodeFlowWorksAsExpectedWithFluentModels() throws { - let clientID = oauthClient.clientID - + func testThatAuthCodeFlowWorksAsExpectedWithFluentModels() throws { // Get Auth Code let state = "jfeiojo382497329" @@ -235,7 +235,7 @@ class OAuthFluentTests: XCTestCase { var tokenRequestData = Node([:], in: nil) try tokenRequestData.set("grant_type", "password") - try tokenRequestData.set("client_id", oauthClient.clientID) + try tokenRequestData.set("client_id", clientID) try tokenRequestData.set("client_secret", clientSecret) try tokenRequestData.set("scope", scope) try tokenRequestData.set("username", username)