Skip to content

Commit 0f524e9

Browse files
committed
add cognito functions as dependencies for testing
1 parent 205ee2d commit 0f524e9

File tree

7 files changed

+34
-10
lines changed

7 files changed

+34
-10
lines changed

Sources/App/Controllers/Manage/Cognito.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ struct Cognito {
9292
)
9393
req.application.cognito.authenticatable = CognitoAuthenticatable(configuration: awsCognitoConfiguration)
9494
try await req.application.cognito.authenticatable.confirmSignUp(username: username, confirmationCode: confirmationCode)
95+
try awsClient.syncShutdown()
9596
}
9697

9798
@Sendable

Sources/App/Controllers/Manage/DeleteAccountController.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Foundation
2+
import Dependencies
23
import Fluent
34
import Plot
45
import Vapor
@@ -9,8 +10,9 @@ import SotoCognitoIdentity
910
enum DeleteAccountController {
1011
@Sendable
1112
static func deleteAccount(req: Request) async throws -> Response {
13+
@Dependency(\.cognito) var cognito
1214
do {
13-
try await Cognito.deleteUser(req: req, accessToken: req.auth.require(AuthenticatedUser.self).sessionID)
15+
try await cognito.deleteUser(req: req, accessToken: req.auth.require(AuthenticatedUser.self).sessionID)
1416
req.auth.logout(AuthenticatedUser.self)
1517
req.session.unauthenticate(AuthenticatedUser.self)
1618
req.session.destroy()

Sources/App/Controllers/Manage/ForgotPasswordController.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Fluent
2+
import Dependencies
23
import Plot
34
import Vapor
45
import SotoCognitoAuthentication
@@ -13,12 +14,13 @@ enum ForgotPasswordController {
1314

1415
@Sendable
1516
static func forgotPasswordEmail(req: Request) async throws -> HTML {
17+
@Dependency(\.cognito) var cognito
1618
struct Credentials: Content {
1719
var email: String
1820
}
1921
do {
2022
let user = try req.content.decode(Credentials.self)
21-
try await Cognito.forgotPassword(req: req, username: user.email)
23+
try await cognito.forgotPassword(req: req, username: user.email)
2224
return Reset.View(path: SiteURL.resetPassword.relativeURL(), model: Reset.Model(email: user.email)).document()
2325
} catch {
2426
return ForgotPassword.View(path: req.url.path, model: ForgotPassword.Model(errorMessage: "An error occurred: \(error.localizedDescription)")).document()

Sources/App/Controllers/Manage/ResetController.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Fluent
2+
import Dependencies
23
import Plot
34
import Vapor
45
import SotoCognitoAuthentication
@@ -13,18 +14,20 @@ enum ResetController {
1314

1415
@Sendable
1516
static func resetPassword(req: Request) async throws -> HTML {
17+
@Dependency(\.cognito) var cognito
1618
struct UserInfo: Content {
1719
var email: String
1820
var password: String
1921
var confirmationCode: String
2022
}
2123
do {
2224
let user = try req.content.decode(UserInfo.self)
23-
try await Cognito.resetPassword(req: req, username: user.email, password: user.password, confirmationCode: user.confirmationCode)
25+
try await cognito.resetPassword(req: req, username: user.email, password: user.password, confirmationCode: user.confirmationCode)
2426
let model = SuccessfulChange.Model(successMessage: "Successfully changed password")
2527
return SuccessfulChange.View(path: req.url.path, model: model).document()
2628
} catch let error as AWSErrorType {
27-
let model = Reset.Model(errorMessage: error.message ?? "There was an error.")
29+
let errorMessage = (error.message != nil) ? "There was an error: \(error.message)" : "There was an error: \(error.localizedDescription)"
30+
let model = Reset.Model(errorMessage: errorMessage)
2831
return Reset.View(path: req.url.path, model: model).document()
2932
} catch {
3033
let model = Reset.Model(errorMessage: "An unknown error occurred: \(error.localizedDescription)")

Sources/App/Controllers/Manage/SessionAuthentication.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Vapor
2+
import Dependencies
23
import SotoCognitoAuthentication
34
import SotoCognitoIdentityProvider
45
import SotoCognitoIdentity
@@ -16,11 +17,12 @@ extension AuthenticatedUser: SessionAuthenticatable {
1617

1718
struct UserSessionAuthenticator: AsyncSessionAuthenticator {
1819
func authenticate(sessionID: String, for request: Vapor.Request) async throws {
20+
@Dependency(\.cognito) var cognito
1921
do {
2022
// TODO: handle response, refresh token
21-
let response = try await request.application.cognito.authenticatable.authenticate(accessToken: sessionID, on: request.eventLoop)
23+
try await cognito.authenticateToken(req: request, sessionID: sessionID, accessToken: sessionID, eventLoop: request.eventLoop)
2224
request.auth.login(User(accessToken: sessionID))
23-
} catch let error as SotoCognitoError { // TODO: handle error
25+
} catch let error as SotoCognitoError { // TODO: handle error
2426
return
2527
}
2628
}

Sources/App/Controllers/Manage/VerifyController.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Vapor
55
import SotoCognitoAuthentication
66
import SotoCognitoIdentityProvider
77
import SotoCognitoIdentity
8+
import Dependencies
89

910
enum VerifyController {
1011
@Sendable
@@ -14,18 +15,20 @@ enum VerifyController {
1415

1516
@Sendable
1617
static func verify(req: Request) async throws -> HTML {
18+
@Dependency(\.cognito) var cognito
1719
struct VerifyInformation: Content {
1820
var email: String
1921
var confirmationCode: String
2022
}
2123
do {
2224
let info = try req.content.decode(VerifyInformation.self)
23-
try await Cognito.confirmSignUp(req: req, username: info.email, confirmationCode: info.confirmationCode)
25+
try await cognito.confirmSignUp(req: req, username: info.email, confirmationCode: info.confirmationCode)
2426
let model = SuccessfulChange.Model(successMessage: "Successfully confirmed signup")
2527
return SuccessfulChange.View(path: req.url.path, model: model).document()
2628
} catch let error as AWSErrorType {
2729
let info = try req.content.decode(VerifyInformation.self)
28-
let model = Verify.Model(email: info.email, errorMessage: error.message ?? "There was an error.")
30+
let errorMessage = (error.message != nil) ? "There was an error: \(error.message)" : "There was an error: \(error.localizedDescription)"
31+
let model = Verify.Model(email: info.email, errorMessage: errorMessage)
2932
return Verify.View(path: req.url.path, model: model).document()
3033
} catch {
3134
let info = try req.content.decode(VerifyInformation.self)

Sources/App/Core/Dependencies/CognitoClient.swift

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,29 @@
1515
import Dependencies
1616
import DependenciesMacros
1717
import Vapor
18+
import SotoCognitoAuthenticationKit
1819

1920
@DependencyClient
2021
struct CognitoClient {
21-
var authenticate: @Sendable (_ req: Request, _ username: String, _ password: String) async throws -> Void
22+
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
2224
var signup: @Sendable (_ req: Request, _ username: String, _ password: String) async throws -> Void
25+
var resetPassword: @Sendable (_ req: Request, _ username: String, _ password: String, _ confirmationCode: String) async throws -> Void
26+
var forgotPassword: @Sendable (_ req: Request, _ username: String) async throws -> Void
27+
var confirmSignUp: @Sendable (_ req: Request, _ username: String, _ confirmationCode: String) async throws -> Void
28+
var deleteUser: @Sendable (_ req: Request, _ accessToken: String) async throws -> Void
2329
}
2430

2531
extension CognitoClient: DependencyKey {
2632
static var liveValue: CognitoClient {
2733
.init(
2834
authenticate: { req, username, password in try await Cognito.authenticate(req: req, username: username, password: password) },
29-
signup : { req, username, password in try await Cognito.signup(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)},
36+
signup : { req, username, password in try await Cognito.signup(req: req, username: username, password: password) },
37+
resetPassword : { req, username, password, confirmationCode in try await Cognito.resetPassword(req: req, username: username, password: password, confirmationCode: confirmationCode) },
38+
forgotPassword: { req, username in try await Cognito.forgotPassword(req: req, username: username) },
39+
confirmSignUp: { req, username, confirmationCode in try await Cognito.confirmSignUp(req: req, username: username, confirmationCode: confirmationCode) },
40+
deleteUser: { req, accessToken in try await Cognito.deleteUser(req: req, accessToken: accessToken) }
3041
)
3142
}
3243
}

0 commit comments

Comments
 (0)