|
1 | | -use aws_sdk_cognitoidentityprovider::types::AuthFlowType; |
| 1 | +use anyhow::{Error, Result, anyhow}; |
| 2 | +use aws_sdk_cognitoidentityprovider::{ |
| 3 | + error::SdkError, operation::admin_initiate_auth::AdminInitiateAuthError, types::AuthFlowType, |
| 4 | +}; |
2 | 5 | use axum_extra::extract::{CookieJar, Host}; |
3 | 6 | use http::Method; |
4 | 7 | use openapi::{ |
5 | 8 | apis::auth::{Auth, AuthLoginPostResponse}, |
6 | | - models::AuthLoginPostRequest, |
| 9 | + models::{AuthLoginPost200Response, AuthLoginPostRequest}, |
7 | 10 | }; |
8 | 11 |
|
9 | | -use crate::api_impl::{api::ApiImpl, error::ApiError}; |
| 12 | +use crate::api_impl::api::ApiImpl; |
10 | 13 |
|
11 | 14 | #[async_trait::async_trait] |
12 | | -impl Auth<ApiError> for ApiImpl { |
| 15 | +impl Auth<Error> for ApiImpl { |
13 | 16 | async fn auth_login_post( |
14 | 17 | &self, |
15 | 18 |
|
16 | 19 | _method: &Method, |
17 | 20 | _host: &Host, |
18 | 21 | _cookies: &CookieJar, |
19 | | - _body: &AuthLoginPostRequest, |
20 | | - ) -> Result<AuthLoginPostResponse, ApiError> { |
21 | | - // self.cognito_client |
22 | | - // .admin_initiate_auth() |
23 | | - // .user_pool_id(self.cognito_user_pool_id.clone()) |
24 | | - // .client_id(self.cognito_client_id.clone()) |
25 | | - // .auth_flow(AuthFlowType::AdminUserPasswordAuth) |
26 | | - // .send() |
27 | | - // .await?; |
28 | | - |
29 | | - todo!() |
| 22 | + body: &AuthLoginPostRequest, |
| 23 | + ) -> Result<AuthLoginPostResponse> { |
| 24 | + let admin_initiate_auth_result = self |
| 25 | + .cognito_client |
| 26 | + .admin_initiate_auth() |
| 27 | + .user_pool_id(&self.cognito_user_pool_id) |
| 28 | + .client_id(&self.cognito_client_id) |
| 29 | + .auth_flow(AuthFlowType::AdminUserPasswordAuth) |
| 30 | + .auth_parameters("USERNAME", &body.email) |
| 31 | + .auth_parameters("PASSWORD", &body.password) |
| 32 | + .send() |
| 33 | + .await; |
| 34 | + |
| 35 | + let admin_initiate_auth_output = match admin_initiate_auth_result { |
| 36 | + Ok(output) => output, |
| 37 | + Err(SdkError::ServiceError(err)) |
| 38 | + if matches!( |
| 39 | + err.err(), |
| 40 | + AdminInitiateAuthError::UserNotFoundException { .. } |
| 41 | + | AdminInitiateAuthError::NotAuthorizedException { .. } |
| 42 | + ) => |
| 43 | + { |
| 44 | + return Ok(AuthLoginPostResponse::Status401_InvalidCredentials( |
| 45 | + "Invalid email or password".to_string(), |
| 46 | + )); |
| 47 | + } |
| 48 | + Err(err) => { |
| 49 | + return Err(anyhow!( |
| 50 | + "Error during Cognito authentication. for user {:?}: {:?}", |
| 51 | + body.email, |
| 52 | + err |
| 53 | + )); |
| 54 | + } |
| 55 | + }; |
| 56 | + |
| 57 | + let auth_result = admin_initiate_auth_output |
| 58 | + .authentication_result() |
| 59 | + .ok_or(anyhow!( |
| 60 | + "Authentication result missing from Cognito response" |
| 61 | + ))?; |
| 62 | + |
| 63 | + let token = auth_result |
| 64 | + .access_token() |
| 65 | + .map(|at| at.to_string()) |
| 66 | + .ok_or(anyhow!("Access token missing from Cognito response"))?; |
| 67 | + |
| 68 | + let user_id = auth_result |
| 69 | + .id_token() |
| 70 | + .map(|id| id.to_string()) |
| 71 | + .ok_or(anyhow!("ID token missing from Cognito response"))?; |
| 72 | + |
| 73 | + Ok(AuthLoginPostResponse::Status200_LoginSuccessful( |
| 74 | + AuthLoginPost200Response { token, user_id }, |
| 75 | + )) |
30 | 76 | } |
31 | 77 | } |
0 commit comments