Skip to content

Commit

Permalink
Update OAuthUser to have an id property of type Identifer to make Flu…
Browse files Browse the repository at this point in the history
…ent integration simple
  • Loading branch information
0xTim committed Aug 3, 2017
1 parent f032e2c commit 9a56ce4
Show file tree
Hide file tree
Showing 23 changed files with 69 additions and 48 deletions.
4 changes: 3 additions & 1 deletion Sources/OAuth/DefaultImplementations/EmptyCodeManager.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import Node

public struct EmptyCodeManager: CodeManager {
public init() {}

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

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 {
return ""
}

Expand Down
6 changes: 4 additions & 2 deletions Sources/OAuth/DefaultImplementations/EmptyUserManager.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import Node

public struct EmptyUserManager: UserManager {

public init() {}

public func getUser(id: String) -> OAuthUser? {
public func getUser(id: Identifier) -> OAuthUser? {
return nil
}

public func authenticateUser(username: String, password: String) -> String? {
public func authenticateUser(username: String, password: String) -> Identifier? {
return nil
}
}
5 changes: 3 additions & 2 deletions Sources/OAuth/Models/OAuthCode.swift
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import Foundation
import Core
import Node

public final class OAuthCode {
public let codeID: String
public let clientID: String
public let redirectURI: String
public let userID: String
public let userID: Identifier
public let expiryDate: Date
public let scopes: [String]?

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

public init(codeID: String, clientID: String, redirectURI: String, userID: String, expiryDate: Date, scopes: [String]?) {
public init(codeID: String, clientID: String, redirectURI: String, userID: Identifier, expiryDate: Date, scopes: [String]?) {
self.codeID = codeID
self.clientID = clientID
self.redirectURI = redirectURI
Expand Down
7 changes: 4 additions & 3 deletions Sources/OAuth/Models/OAuthUser.swift
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import Authentication
import Core
import Node

public final class OAuthUser: Authenticatable, Extendable {
public let username: String
public let emailAddress: String?
public let password: Bytes
public let userID: String?
public var id: Identifier?

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

public init(userID: String?, username: String, emailAddress: String?, password: Bytes) {
public init(id: Identifier? = nil, username: String, emailAddress: String?, password: Bytes) {
self.username = username
self.emailAddress = emailAddress
self.password = password
self.userID = userID
self.id = id
}
}
5 changes: 3 additions & 2 deletions Sources/OAuth/Models/Tokens/AccessToken.swift
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import Foundation
import Core
import Node

public final class AccessToken: Extendable {
public let tokenString: String
public let clientID: String
public let userID: String?
public let userID: Identifier?
public let scopes: [String]?
public let expiryTime: Date

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

public init(tokenString: String, clientID: String, userID: String?, scopes: [String]? = nil, expiryTime: Date) {
public init(tokenString: String, clientID: String, userID: Identifier?, scopes: [String]? = nil, expiryTime: Date) {
self.tokenString = tokenString
self.clientID = clientID
self.userID = userID
Expand Down
5 changes: 3 additions & 2 deletions Sources/OAuth/Models/Tokens/RefreshToken.swift
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import Core
import Node

public final class RefreshToken: Extendable {
public let tokenString: String
public let clientID: String
public let userID: String?
public let userID: Identifier?
public var scopes: [String]?

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

public init(tokenString: String, clientID: String, userID: String?, scopes: [String]? = nil) {
public init(tokenString: String, clientID: String, userID: Identifier?, scopes: [String]? = nil) {
self.tokenString = tokenString
self.clientID = clientID
self.userID = userID
Expand Down
4 changes: 2 additions & 2 deletions Sources/OAuth/OAuth2Provider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ struct OAuth2Provider {
throw Abort.unauthorized
}

guard let userID = user.userID else {
guard let userID = user.id else {
throw Abort.unauthorized
}

Expand Down Expand Up @@ -157,7 +157,7 @@ struct OAuth2Provider {

if approveApplication {
if responseType == ResponseType.token {
let accessToken = try tokenManager.generateAccessToken(clientID: clientID, userID: user.userID, scopes: scopes, expiryTime: 3600)
let accessToken = try tokenManager.generateAccessToken(clientID: clientID, userID: user.id, scopes: scopes, expiryTime: 3600)
redirectURI += "#token_type=bearer&access_token=\(accessToken.tokenString)&expires_in=3600"
}
else if responseType == ResponseType.code {
Expand Down
4 changes: 3 additions & 1 deletion Sources/OAuth/Protocols/CodeManager.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import Node

public protocol CodeManager {
func generateCode(userID: String, clientID: String, redirectURI: String, scopes: [String]?) throws -> String
func generateCode(userID: Identifier, clientID: String, redirectURI: String, scopes: [String]?) throws -> String
func getCode(_ code: String) -> OAuthCode?

// 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
Expand Down
6 changes: 4 additions & 2 deletions Sources/OAuth/Protocols/TokenManager.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import Node

public protocol TokenManager {
func generateAccessRefreshTokens(clientID: String, userID: String?, scopes: [String]?, accessTokenExpiryTime: Int) throws -> (AccessToken, RefreshToken)
func generateAccessToken(clientID: String, userID: String?, scopes: [String]?, expiryTime: Int) throws -> AccessToken
func generateAccessRefreshTokens(clientID: String, userID: Identifier?, scopes: [String]?, accessTokenExpiryTime: Int) throws -> (AccessToken, RefreshToken)
func generateAccessToken(clientID: String, userID: Identifier?, scopes: [String]?, expiryTime: Int) throws -> AccessToken
func getRefreshToken(_ refreshToken: String) -> RefreshToken?
func getAccessToken(_ accessToken: String) -> AccessToken?
func updateRefreshToken(_ refreshToken: RefreshToken, scopes: [String])
Expand Down
6 changes: 4 additions & 2 deletions Sources/OAuth/Protocols/UserManager.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import Node

public protocol UserManager {
func authenticateUser(username: String, password: String) -> String?
func getUser(id: String) -> OAuthUser?
func authenticateUser(username: String, password: String) -> Identifier?
func getUser(id: Identifier) -> OAuthUser?
}
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ class AuthorizationResponseTests: XCTestCase {
return
}

XCTAssertEqual(code.userID, user.userID)
XCTAssertEqual(code.userID, user.id)
}

func testThatClientIDSetOnCode() throws {
Expand Down
3 changes: 2 additions & 1 deletion Tests/OAuthTests/Fakes/FakeCodeManager.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import OAuth
import Foundation
import Node

class FakeCodeManager: CodeManager {

Expand All @@ -11,7 +12,7 @@ class FakeCodeManager: CodeManager {
return codes[code]
}

func generateCode(userID: String, clientID: String, redirectURI: String, scopes: [String]?) throws -> String {
func generateCode(userID: Identifier, clientID: String, redirectURI: String, scopes: [String]?) throws -> String {
let code = OAuthCode(codeID: generatedCode, clientID: clientID, redirectURI: redirectURI, userID: userID, expiryDate: Date().addingTimeInterval(60), scopes: scopes)
codes[generatedCode] = code
return generatedCode
Expand Down
5 changes: 3 additions & 2 deletions Tests/OAuthTests/Fakes/FakeTokenManager.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import OAuth
import Foundation
import Node

class FakeTokenManager: TokenManager {

Expand All @@ -17,7 +18,7 @@ class FakeTokenManager: TokenManager {
return accessTokens[accessToken]
}

func generateAccessRefreshTokens(clientID: String, userID: String?, scopes: [String]?, accessTokenExpiryTime: Int) throws -> (AccessToken, RefreshToken) {
func generateAccessRefreshTokens(clientID: String, userID: Identifier?, scopes: [String]?, accessTokenExpiryTime: Int) throws -> (AccessToken, RefreshToken) {
let accessToken = AccessToken(tokenString: accessTokenToReturn, clientID: clientID, userID: userID, scopes: scopes, expiryTime: currentTime.addingTimeInterval(TimeInterval(accessTokenExpiryTime)))
let refreshToken = RefreshToken(tokenString: refreshTokenToReturn, clientID: clientID, userID: userID, scopes: scopes)

Expand All @@ -26,7 +27,7 @@ class FakeTokenManager: TokenManager {
return (accessToken, refreshToken)
}

func generateAccessToken(clientID: String, userID: String?, scopes: [String]?, expiryTime: Int) throws -> AccessToken {
func generateAccessToken(clientID: String, userID: Identifier?, scopes: [String]?, expiryTime: Int) throws -> AccessToken {
let accessToken = AccessToken(tokenString: accessTokenToReturn, clientID: clientID, userID: userID, scopes: scopes, expiryTime: currentTime.addingTimeInterval(TimeInterval(expiryTime)))
accessTokens[accessTokenToReturn] = accessToken
return accessToken
Expand Down
9 changes: 5 additions & 4 deletions Tests/OAuthTests/Fakes/FakeUserManager.swift
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
import OAuth
import Node

class FakeUserManager: UserManager {
var users: [OAuthUser] = []

func authenticateUser(username: String, password: String) -> String? {
func authenticateUser(username: String, password: String) -> Identifier? {
for user in users {
if user.username == username {
if user.password.makeString() == password {
return user.userID
return user.id
}
}
}

return nil
}

func getUser(id: String) -> OAuthUser? {
func getUser(id: Identifier) -> OAuthUser? {
for user in users {
if user.userID == id {
if user.id == id {
return user
}
}
Expand Down
3 changes: 2 additions & 1 deletion Tests/OAuthTests/Fakes/StubCodeManager.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import OAuth
import Node

class StubCodeManager: CodeManager {

var codeToReturn = "ABCDEFHIJKLMNO"

func generateCode(userID: String, clientID: String, redirectURI: String, scopes: [String]?) throws -> String {
func generateCode(userID: Identifier, clientID: String, redirectURI: String, scopes: [String]?) throws -> String {
return codeToReturn
}

Expand Down
5 changes: 3 additions & 2 deletions Tests/OAuthTests/Fakes/StubTokenManager.swift
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import OAuth
import Foundation
import Node

class StubTokenManager: TokenManager {

var accessToken = "ABCDEF"
var refreshToken = "GHIJKL"

func generateAccessRefreshTokens(clientID: String, userID: String?, scopes: [String]?, accessTokenExpiryTime: Int) throws -> (AccessToken, RefreshToken) {
func generateAccessRefreshTokens(clientID: String, userID: Identifier?, scopes: [String]?, accessTokenExpiryTime: Int) throws -> (AccessToken, RefreshToken) {
let access = AccessToken(tokenString: accessToken, clientID: clientID, userID: userID, scopes: scopes, expiryTime: Date())
let refresh = RefreshToken(tokenString: refreshToken, clientID: clientID, userID: nil, scopes: scopes)
return (access, refresh)
}

func generateAccessToken(clientID: String, userID: String?, scopes: [String]?, expiryTime: Int) throws -> AccessToken {
func generateAccessToken(clientID: String, userID: Identifier?, scopes: [String]?, expiryTime: Int) throws -> AccessToken {
return AccessToken(tokenString: accessToken, clientID: clientID, userID: userID, scopes: scopes, expiryTime: Date())
}

Expand Down
5 changes: 3 additions & 2 deletions Tests/OAuthTests/Fakes/StubUserManager.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import OAuth
import Node

struct StubUserManager: UserManager {
func authenticateUser(username: String, password: String) -> String? {
func authenticateUser(username: String, password: String) -> Identifier? {
return nil
}

func getUser(id: String) -> OAuthUser? {
func getUser(id: Identifier) -> OAuthUser? {
return nil
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class AuthorizationCodeTokenTests: XCTestCase {
let testClientSecret = "ABCDEFGHIJK"
let testClientRedirectURI = "https://api.brokenhands.io/callback"
let testCodeID = "12345ABCD"
let userID = "the-user-id"
let userID: Identifier = "the-user-id"
let scopes = ["email", "create"]

// MARK: - Overrides
Expand Down
4 changes: 2 additions & 2 deletions Tests/OAuthTests/GrantTests/ImplicitGrantTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -381,10 +381,10 @@ class ImplicitGrantTests: XCTestCase {
}

func testThatUserIDIsSetOnToken() throws {
let userID = "abcdef-123453-cbdhe"
let userID: Identifier = "abcdef-123453-cbdhe"
let accessToken = "IMPLICIT-GRANT-ACCESS-TOKEN"
fakeTokenManager.accessTokenToReturn = accessToken
let user = OAuthUser(userID: userID, username: "luke", emailAddress: "[email protected]", password: "obiwan".makeBytes())
let user = OAuthUser(id: userID, username: "luke", emailAddress: "[email protected]", password: "obiwan".makeBytes())
_ = try getImplicitGrantResponse(user: user)

guard let token = fakeTokenManager.getAccessToken(accessToken) else {
Expand Down
4 changes: 2 additions & 2 deletions Tests/OAuthTests/GrantTests/PasswordGrantTokenTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class PasswordGrantTokenTests: XCTestCase {
let testClientSecret = "01234567890"
let testUsername = "testUser"
let testPassword = "testPassword"
let testUserID = "ABCD-FJUH-31232"
let testUserID: Identifier = "ABCD-FJUH-31232"
let accessToken = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
let refreshToken = "ABCDEFGHIJLMNOP1234567890"
let scope1 = "email"
Expand All @@ -60,7 +60,7 @@ class PasswordGrantTokenTests: XCTestCase {

let testClient = OAuthClient(clientID: testClientID, redirectURIs: nil, clientSecret: testClientSecret, validScopes: [scope1, scope2], firstParty: true)
fakeClientGetter.validClients[testClientID] = testClient
let testUser = OAuthUser(userID: testUserID, username: testUsername, emailAddress: nil, password: testPassword.makeBytes())
let testUser = OAuthUser(id: testUserID, username: testUsername, emailAddress: nil, password: testPassword.makeBytes())
fakeUserManager.users.append(testUser)
fakeTokenManager.accessTokenToReturn = accessToken
fakeTokenManager.refreshTokenToReturn = refreshToken
Expand Down
2 changes: 1 addition & 1 deletion Tests/OAuthTests/GrantTests/TokenRefreshTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ class TokenRefreshTests: XCTestCase {
}

func testUserIDIsSetOnAccessTokenIfRefreshTokenHasOne() throws {
let userID = "abcdefg-123456"
let userID: Identifier = "abcdefg-123456"
let accessToken = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
let userIDRefreshTokenString = "ASHFUIEWHFIHEWIUF"
let userIDRefreshToken = RefreshToken(tokenString: userIDRefreshTokenString, clientID: testClientID, userID: userID, scopes: [scope1, scope2])
Expand Down
5 changes: 3 additions & 2 deletions Tests/OAuthTests/Helpers/TestDataBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import OAuth
import Vapor
import Cookies
import Sessions
import Node

class TestDataBuilder
{
Expand Down Expand Up @@ -171,8 +172,8 @@ class TestDataBuilder
return try drop.respond(to: authRequest)
}

static let anyUserID = "12345-asbdsadi"
static let anyUserID: Identifier = "12345-asbdsadi"
static func anyOAuthUser() -> OAuthUser {
return OAuthUser(userID: TestDataBuilder.anyUserID, username: "hansolo", emailAddress: "[email protected]", password: "leia".makeBytes())
return OAuthUser(id: TestDataBuilder.anyUserID, username: "hansolo", emailAddress: "[email protected]", password: "leia".makeBytes())
}
}
Loading

0 comments on commit 9a56ce4

Please sign in to comment.