Make request architecture and response signatures more consistent
This commit is contained in:
parent
5b3323f39c
commit
3ba2d9c26f
5 changed files with 65 additions and 36 deletions
|
@ -24,8 +24,12 @@ pub async fn account_creation_post_handler(
|
|||
|
||||
account_creation_request(pool, user_id, request)
|
||||
.await
|
||||
.map(|(status_code, response)| {
|
||||
(status_code, ApiResponse::new(response).into_json_response()).into_response()
|
||||
.map(|response| {
|
||||
(
|
||||
StatusCode::CREATED,
|
||||
ApiResponse::new(response).into_json_response(),
|
||||
)
|
||||
.into_response()
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -33,7 +37,7 @@ async fn account_creation_request(
|
|||
pool: &DbPool,
|
||||
user_id: i32,
|
||||
request: AccountCreationRequest,
|
||||
) -> Result<(StatusCode, AccountCreationResponse), AppError> {
|
||||
) -> Result<AccountCreationResponse, AppError> {
|
||||
let AccountCreationRequest {
|
||||
r#type: account_type,
|
||||
name,
|
||||
|
@ -52,5 +56,5 @@ async fn account_creation_request(
|
|||
.await
|
||||
.map(|response| AccountCreationResponse::from(response))?;
|
||||
|
||||
Ok((StatusCode::CREATED, response))
|
||||
Ok(response)
|
||||
}
|
||||
|
|
|
@ -1,25 +1,25 @@
|
|||
use std::time::SystemTime;
|
||||
|
||||
use axum::{
|
||||
debug_handler,
|
||||
Json, debug_handler,
|
||||
extract::State,
|
||||
response::{IntoResponse, Response},
|
||||
Json,
|
||||
};
|
||||
use http::StatusCode;
|
||||
use pasetors::{keys::SymmetricKey, version4::V4};
|
||||
use tracing::debug;
|
||||
|
||||
use crate::{
|
||||
db::{get_username_and_password_by_email, DbPool, UserIdAndHashedPasswordEntity},
|
||||
db::{DbPool, UserIdAndHashedPasswordEntity, get_username_and_password_by_email},
|
||||
models::{ApiResponse, AppError, Session},
|
||||
requests::{
|
||||
auth::login::models::{AuthLoginResponse, AuthLoginTokenData},
|
||||
AppState,
|
||||
auth::login::models::{AuthLoginResponse, AuthLoginTokenData},
|
||||
},
|
||||
services::{
|
||||
CachePool,
|
||||
auth_token::{generate_auth_token, generate_session_token, store_user_auth_token},
|
||||
user_session, verify_password, CachePool,
|
||||
user_session, verify_password,
|
||||
},
|
||||
};
|
||||
|
||||
|
@ -33,7 +33,15 @@ pub async fn auth_login_post_handler(
|
|||
let db_pool = state.db_pool();
|
||||
let cache_pool = state.cache_pool();
|
||||
let token_key = state.config().secrets().token_key();
|
||||
auth_login_request(db_pool, cache_pool, token_key, body).await
|
||||
auth_login_request(db_pool, cache_pool, token_key, body)
|
||||
.await
|
||||
.map(|response| {
|
||||
(
|
||||
StatusCode::OK,
|
||||
ApiResponse::new(response).into_json_response(),
|
||||
)
|
||||
.into_response()
|
||||
})
|
||||
}
|
||||
|
||||
async fn auth_login_request(
|
||||
|
@ -41,7 +49,7 @@ async fn auth_login_request(
|
|||
cache_pool: &CachePool,
|
||||
token_key: &SymmetricKey<V4>,
|
||||
body: AuthLoginRequest,
|
||||
) -> Result<Response, AppError> {
|
||||
) -> Result<AuthLoginResponse, AppError> {
|
||||
debug!(?body);
|
||||
|
||||
let AuthLoginRequest { email, password } = body;
|
||||
|
@ -75,11 +83,7 @@ async fn auth_login_request(
|
|||
},
|
||||
};
|
||||
|
||||
Ok((
|
||||
StatusCode::OK,
|
||||
ApiResponse::new(response).into_json_response(),
|
||||
)
|
||||
.into_response())
|
||||
Ok(response)
|
||||
}
|
||||
|
||||
pub async fn generate_login_auth_and_session_tokens(
|
||||
|
|
|
@ -6,11 +6,13 @@ use axum::{
|
|||
response::{IntoResponse, Response},
|
||||
};
|
||||
use http::{HeaderMap, StatusCode};
|
||||
use pasetors::{keys::SymmetricKey, version4::V4};
|
||||
|
||||
use crate::{
|
||||
models::{ApiResponse, AppError, Session},
|
||||
requests::AppState,
|
||||
services::{
|
||||
CachePool,
|
||||
auth_token::{self, generate_session_token, get_if_auth_token_exists, verify_token},
|
||||
user_session,
|
||||
},
|
||||
|
@ -25,9 +27,25 @@ pub async fn auth_session_get_handler(
|
|||
) -> Result<Response, AppError> {
|
||||
let cache_pool = state.cache_pool();
|
||||
let token_key = state.config().secrets().token_key();
|
||||
let raw_token_str = auth_token::extract_token_string_from_http_headers(&headers)?;
|
||||
|
||||
let auth_token_str = auth_token::extract_token_string_from_http_headers(&headers)?;
|
||||
let auth_token = verify_token(token_key, auth_token_str, None)?;
|
||||
auth_session_handler(cache_pool, token_key, raw_token_str)
|
||||
.await
|
||||
.map(|response| {
|
||||
(
|
||||
StatusCode::CREATED,
|
||||
ApiResponse::new(response).into_json_response(),
|
||||
)
|
||||
.into_response()
|
||||
})
|
||||
}
|
||||
|
||||
async fn auth_session_handler(
|
||||
cache_pool: &CachePool,
|
||||
token_key: &SymmetricKey<V4>,
|
||||
raw_token_str: &str,
|
||||
) -> Result<AuthSessionResponse, AppError> {
|
||||
let auth_token = verify_token(token_key, raw_token_str, None)?;
|
||||
|
||||
let user_id = auth_token
|
||||
.payload_claims()
|
||||
|
@ -42,7 +60,7 @@ pub async fn auth_session_get_handler(
|
|||
.unwrap();
|
||||
|
||||
let auth_token_exists =
|
||||
get_if_auth_token_exists(cache_pool, user_id, auth_token_str.to_string().as_str()).await?;
|
||||
get_if_auth_token_exists(cache_pool, user_id, raw_token_str.to_string().as_str()).await?;
|
||||
|
||||
if !auth_token_exists {
|
||||
return Err(AppError::no_session_found());
|
||||
|
@ -64,13 +82,8 @@ pub async fn auth_session_get_handler(
|
|||
user_session::store_user_session(cache_pool, session_token_id, new_session, Some(expiration))
|
||||
.await?;
|
||||
|
||||
Ok((
|
||||
StatusCode::CREATED,
|
||||
ApiResponse::new(AuthSessionResponse {
|
||||
Ok(AuthSessionResponse {
|
||||
token: session_token,
|
||||
expiration: session_token_expiration,
|
||||
})
|
||||
.into_json_response(),
|
||||
)
|
||||
.into_response())
|
||||
}
|
||||
|
|
|
@ -37,8 +37,12 @@ pub async fn user_registration_post_handler(
|
|||
mail_sender,
|
||||
)
|
||||
.await
|
||||
.map(|(status_code, response)| {
|
||||
(status_code, ApiResponse::new(response).into_json_response()).into_response()
|
||||
.map(|response| {
|
||||
(
|
||||
StatusCode::CREATED,
|
||||
ApiResponse::new(response).into_json_response(),
|
||||
)
|
||||
.into_response()
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -49,7 +53,7 @@ async fn register_new_user_request(
|
|||
signing_key: &SymmetricKey<V4>,
|
||||
send_verification_email: bool,
|
||||
email_sender: &Sender<UserConfirmationMessage>,
|
||||
) -> Result<(StatusCode, UserRegistrationResponse), AppError> {
|
||||
) -> Result<UserRegistrationResponse, AppError> {
|
||||
debug!(?body, send_verification_email);
|
||||
|
||||
let UserRegistrationRequest {
|
||||
|
@ -123,5 +127,5 @@ async fn register_new_user_request(
|
|||
}
|
||||
};
|
||||
|
||||
Ok((StatusCode::CREATED, response_body))
|
||||
Ok(response_body)
|
||||
}
|
||||
|
|
|
@ -34,8 +34,12 @@ pub async fn user_verification_get_handler(
|
|||
let UserVerifyGetParams { verification_token } = query;
|
||||
verify_new_user_request(db_pool, cache_pool, verification_token, token_key)
|
||||
.await
|
||||
.map(|(status_code, response)| {
|
||||
(status_code, ApiResponse::new(response).into_json_response()).into_response()
|
||||
.map(|response| {
|
||||
(
|
||||
StatusCode::OK,
|
||||
ApiResponse::new(response).into_json_response(),
|
||||
)
|
||||
.into_response()
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -44,7 +48,7 @@ async fn verify_new_user_request(
|
|||
cache_pool: &CachePool,
|
||||
verification_token: String,
|
||||
token_key: &SymmetricKey<V4>,
|
||||
) -> Result<(StatusCode, UserVerifyGetResponse), AppError> {
|
||||
) -> Result<UserVerifyGetResponse, AppError> {
|
||||
let validation_rules = {
|
||||
let mut rules = ClaimsValidationRules::new();
|
||||
rules.validate_audience_with(format!("/user/verify").as_str());
|
||||
|
@ -110,5 +114,5 @@ async fn verify_new_user_request(
|
|||
},
|
||||
};
|
||||
|
||||
Ok((StatusCode::OK, response))
|
||||
Ok(response)
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue