Skip to content

Commit

Permalink
Separate the clientID from the index
Browse files Browse the repository at this point in the history
  • Loading branch information
0xTim committed Aug 2, 2017
1 parent 6f430fe commit 9643d3e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 26 deletions.
4 changes: 2 additions & 2 deletions Sources/OAuthFluent/Managers/FluentClientRetriever.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
30 changes: 11 additions & 19 deletions Sources/OAuthFluent/Models/FluentOAuthClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions Tests/OAuthFluentTests/OAuthFluentTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "[email protected]"
let username = "han"
Expand Down Expand Up @@ -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()
}

Expand All @@ -76,9 +78,7 @@ class OAuthFluentTests: XCTestCase {
#endif
}

func testThatAuthCodeFlowWorksAsExpectedWithFluentModels() throws {
let clientID = oauthClient.clientID

func testThatAuthCodeFlowWorksAsExpectedWithFluentModels() throws {
// Get Auth Code
let state = "jfeiojo382497329"

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 9643d3e

Please sign in to comment.