Skip to content

Commit 79339fa

Browse files
committed
simplification of authenticateToken + address refresh
1 parent 1f0a0d7 commit 79339fa

File tree

4 files changed

+7
-9
lines changed

4 files changed

+7
-9
lines changed

Sources/App/Controllers/Manage/Cognito.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ struct Cognito {
2121
}
2222

2323
@Sendable
24-
static func authenticateToken(req: Request, sessionID: String, accessToken: String, on eventLoop: EventLoop) async throws -> Void {
24+
static func authenticateToken(req: Request, sessionID: String, accessToken: String) async throws -> Void {
2525
let awsClient = AWSClient(httpClientProvider: .shared(req.application.http.client.shared))
2626
let awsCognitoConfiguration = CognitoConfiguration(
2727
userPoolId: Environment.get("AWS_COGNITO_POOL_ID")!,

Sources/App/Controllers/Manage/LoginController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ enum LoginController {
2525
let response = try await cognito.authenticate(req: req, username: user.email, password: user.password)
2626
switch response {
2727
case .authenticated(let authenticatedResponse):
28-
let user = AuthenticatedUser(accessToken: authenticatedResponse.accessToken!, refreshToken: authenticatedResponse.refreshToken!)
28+
let user = AuthenticatedUser(accessToken: authenticatedResponse.accessToken!)
2929
req.auth.login(user)
3030
case .challenged(let challengedResponse): // with the current pool configuration, a challenge response is not expected
3131
break

Sources/App/Controllers/Manage/SessionAuthentication.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import SotoCognitoIdentity
66

77
struct AuthenticatedUser {
88
var accessToken: String
9-
var refreshToken: String?
109
}
1110

1211
extension AuthenticatedUser: SessionAuthenticatable {
@@ -19,11 +18,10 @@ struct UserSessionAuthenticator: AsyncSessionAuthenticator {
1918
func authenticate(sessionID: String, for request: Vapor.Request) async throws {
2019
@Dependency(\.cognito) var cognito
2120
do {
22-
// TODO: handle response, refresh token
23-
try await cognito.authenticateToken(req: request, sessionID: sessionID, accessToken: sessionID, eventLoop: request.eventLoop)
21+
try await cognito.authenticateToken(req: request, sessionID: sessionID, accessToken: sessionID)
2422
request.auth.login(User(accessToken: sessionID))
25-
} catch let error as SotoCognitoError { // TODO: handle error
26-
return
23+
} catch let error as SotoCognitoError {
24+
// .unauthorized SotoCognitoError with reason "invalid token", attempt to refresh using req.application.cognito.authenticatable.refresh(), which requires the username and refresh token, both returned upon initial successful login
2725
}
2826
}
2927
typealias User = AuthenticatedUser

Sources/App/Core/Dependencies/CognitoClient.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import SotoCognitoAuthenticationKit
2020
@DependencyClient
2121
struct CognitoClient {
2222
var authenticate: @Sendable (_ req: Request, _ username: String, _ password: String) async throws -> CognitoAuthenticateResponse
23-
var authenticateToken: @Sendable (_ req: Request, _ sessionID: String, _ accessToken: String, _ eventLoop: EventLoop) async throws -> Void
23+
var authenticateToken: @Sendable (_ req: Request, _ sessionID: String, _ accessToken: String) async throws -> Void
2424
var signup: @Sendable (_ req: Request, _ username: String, _ password: String) async throws -> Void
2525
var resetPassword: @Sendable (_ req: Request, _ username: String, _ password: String, _ confirmationCode: String) async throws -> Void
2626
var forgotPassword: @Sendable (_ req: Request, _ username: String) async throws -> Void
@@ -32,7 +32,7 @@ extension CognitoClient: DependencyKey {
3232
static var liveValue: CognitoClient {
3333
.init(
3434
authenticate: { req, username, password in try await Cognito.authenticate(req: req, username: username, password: password) },
35-
authenticateToken: { req, sessionID, accessToken, eventLoop in try await Cognito.authenticateToken(req: req, sessionID: sessionID, accessToken: accessToken, on: eventLoop)},
35+
authenticateToken: { req, sessionID, accessToken in try await Cognito.authenticateToken(req: req, sessionID: sessionID, accessToken: accessToken)},
3636
signup : { req, username, password in try await Cognito.signup(req: req, username: username, password: password) },
3737
resetPassword : { req, username, password, confirmationCode in try await Cognito.resetPassword(req: req, username: username, password: password, confirmationCode: confirmationCode) },
3838
forgotPassword: { req, username in try await Cognito.forgotPassword(req: req, username: username) },

0 commit comments

Comments
 (0)