@@ -5,7 +5,7 @@ import SotoCognitoIdentity
55
66struct Cognito {
77 @Sendable
8- static func authenticate( req: Request , username: String , password: String ) async throws {
8+ static func authenticate( req: Request , username: String , password: String ) async throws -> CognitoAuthenticateResponse {
99 let awsClient = AWSClient ( httpClientProvider: . shared( req. application. http. client. shared) )
1010 let awsCognitoConfiguration = CognitoConfiguration (
1111 userPoolId: Environment . get ( " AWS_COGNITO_POOL_ID " ) !,
@@ -16,13 +16,22 @@ struct Cognito {
1616 )
1717 req. application. cognito. authenticatable = CognitoAuthenticatable ( configuration: awsCognitoConfiguration)
1818 let response = try await req. application. cognito. authenticatable. authenticate ( username: username, password: password)
19- switch response {
20- case . authenticated( let authenticatedResponse) :
21- let user = AuthenticatedUser ( accessToken: authenticatedResponse. accessToken!, refreshToken: authenticatedResponse. refreshToken!)
22- req. auth. login ( user)
23- case . challenged( let challengedResponse) : // TODO: handle challenge
24- break
25- }
19+ try awsClient. syncShutdown ( )
20+ return response
21+ }
22+
23+ @Sendable
24+ static func authenticateToken( req: Request , sessionID: String , accessToken: String , on eventLoop: EventLoop ) async throws -> Void {
25+ let awsClient = AWSClient ( httpClientProvider: . shared( req. application. http. client. shared) )
26+ let awsCognitoConfiguration = CognitoConfiguration (
27+ userPoolId: Environment . get ( " AWS_COGNITO_POOL_ID " ) !,
28+ clientId: Environment . get ( " AWS_COGNITO_CLIENT_ID " ) !,
29+ clientSecret: Environment . get ( " AWS_COGNITO_CLIENT_SECRET " ) !,
30+ cognitoIDP: CognitoIdentityProvider ( client: awsClient, region: . useast2) ,
31+ adminClient: true
32+ )
33+ req. application. cognito. authenticatable = CognitoAuthenticatable ( configuration: awsCognitoConfiguration)
34+ let _ = try await req. application. cognito. authenticatable. authenticate ( accessToken: sessionID, on: req. eventLoop)
2635 try awsClient. syncShutdown ( )
2736 }
2837
@@ -40,4 +49,34 @@ struct Cognito {
4049 try await req. application. cognito. authenticatable. signUp ( username: username, password: password, attributes: [ : ] , on: req. eventLoop)
4150 try awsClient. syncShutdown ( )
4251 }
52+
53+ @Sendable
54+ static func forgotPassword( req: Request , username: String ) async throws {
55+ let awsClient = AWSClient ( httpClientProvider: . shared( req. application. http. client. shared) )
56+ let awsCognitoConfiguration = CognitoConfiguration (
57+ userPoolId: Environment . get ( " AWS_COGNITO_POOL_ID " ) !,
58+ clientId: Environment . get ( " AWS_COGNITO_CLIENT_ID " ) !,
59+ clientSecret: Environment . get ( " AWS_COGNITO_CLIENT_SECRET " ) !,
60+ cognitoIDP: CognitoIdentityProvider ( client: awsClient, region: . useast2) ,
61+ adminClient: true
62+ )
63+ req. application. cognito. authenticatable = CognitoAuthenticatable ( configuration: awsCognitoConfiguration)
64+ try await req. application. cognito. authenticatable. forgotPassword ( username: username)
65+ try awsClient. syncShutdown ( )
66+ }
67+
68+ @Sendable
69+ static func resetPassword( req: Request , username: String , password: String , confirmationCode: String ) async throws {
70+ let awsClient = AWSClient ( httpClientProvider: . shared( req. application. http. client. shared) )
71+ let awsCognitoConfiguration = CognitoConfiguration (
72+ userPoolId: Environment . get ( " AWS_COGNITO_POOL_ID " ) !,
73+ clientId: Environment . get ( " AWS_COGNITO_CLIENT_ID " ) !,
74+ clientSecret: Environment . get ( " AWS_COGNITO_CLIENT_SECRET " ) !,
75+ cognitoIDP: CognitoIdentityProvider ( client: awsClient, region: . useast2) ,
76+ adminClient: true
77+ )
78+ req. application. cognito. authenticatable = CognitoAuthenticatable ( configuration: awsCognitoConfiguration)
79+ try await req. application. cognito. authenticatable. confirmForgotPassword ( username: username, newPassword: password, confirmationCode: confirmationCode)
80+ try awsClient. syncShutdown ( )
81+ }
4382}
0 commit comments