Skip to content

Commit

Permalink
Merge pull request #9 from Brett-Best/feature/user-manager-async
Browse files Browse the repository at this point in the history
  • Loading branch information
0xTim authored Feb 19, 2023
2 parents 074f3c2 + b44c79b commit b4cc076
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ public struct EmptyUserManager: UserManager {

public init() {}

public func getUser(userID: String) -> OAuthUser? {
public func getUser(userID: String) async throws -> OAuthUser? {
return nil
}

public func authenticateUser(username: String, password: String) -> String? {
public func authenticateUser(username: String, password: String) async throws -> String? {
return nil
}
}
2 changes: 1 addition & 1 deletion Sources/VaporOAuth/Helper/OAuthHelper+local.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ extension OAuthHelper {
throw Abort(.unauthorized)
}

guard let user = userManager.getUser(userID: userID) else {
guard let user = try await userManager.getUser(userID: userID) else {
throw Abort(.unauthorized)
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/VaporOAuth/Protocols/UserManager.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
public protocol UserManager {
func authenticateUser(username: String, password: String) -> String?
func getUser(userID: String) -> OAuthUser?
func authenticateUser(username: String, password: String) async throws -> String?
func getUser(userID: String) async throws -> OAuthUser?
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ struct PasswordTokenHandler {
}
}

guard let userID = userManager.authenticateUser(username: username, password: password) else {
guard let userID = try await userManager.authenticateUser(username: username, password: password) else {
logger.warning("LOGIN WARNING: Invalid login attempt for user \(username)")
return try tokenResponseGenerator.createResponse(error: OAuthResponseParameters.ErrorType.invalidGrant,
description: "Request had invalid credentials")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ struct TokenIntrospectionHandler {
var user: OAuthUser? = nil

if let userID = token.userID {
if let tokenUser = userManager.getUser(userID: userID) {
if let tokenUser = try await userManager.getUser(userID: userID) {
user = tokenUser
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@ class DefaultImplementationTests: XCTestCase {
XCTAssertNil(emptyResourceServerRetriever.getServer("some username"))
}

func testThatEmptyUserManagerReturnsNilWhenAttemptingToAuthenticate() {
func testThatEmptyUserManagerReturnsNilWhenAttemptingToAuthenticate() async throws {
let emptyUserManager = EmptyUserManager()
XCTAssertNil(emptyUserManager.authenticateUser(username: "username", password: "password"))
let token = try await emptyUserManager.authenticateUser(username: "username", password: "password")
XCTAssertNil(token)
}

func testThatEmptyUserManagerReturnsNilWhenTryingToGetUser() {
func testThatEmptyUserManagerReturnsNilWhenTryingToGetUser() async throws {
let emptyUserManager = EmptyUserManager()
let id = "some-id"
XCTAssertNil(emptyUserManager.getUser(userID: id))
let user = try await emptyUserManager.getUser(userID: id)
XCTAssertNil(user)
}

func testThatEmptyAuthHandlerReturnsEmptyStringWhenHandlingAuthError() async throws {
Expand Down

0 comments on commit b4cc076

Please sign in to comment.