more refactoring

This commit is contained in:
pavel 2026-02-11 01:41:07 +01:00
commit 7268d49b4a
12 changed files with 319 additions and 189 deletions

View file

@ -1,7 +1,7 @@
use axum::{
Json, RequestPartsExt,
extract::{FromRef, FromRequestParts, Query, State},
http::{StatusCode, request::Parts},
http::request::Parts,
response::IntoResponse,
};
use axum_extra::{
@ -12,10 +12,9 @@ use axum_extra::{
use std::sync::Arc;
use super::AppState;
use crate::domain::auth::Claims;
use crate::error::{AppError, AppResult};
#[allow(dead_code)]
pub struct AuthenticatedUser(pub Claims);
pub struct AuthenticatedUser(pub crate::domain::auth::Claims);
#[axum::async_trait]
impl<S> FromRequestParts<S> for AuthenticatedUser
@ -23,7 +22,7 @@ where
Arc<AppState>: axum::extract::FromRef<S>,
S: Send + Sync,
{
type Rejection = (StatusCode, String);
type Rejection = AppError;
async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
let app_state = Arc::<AppState>::from_ref(state);
@ -33,22 +32,22 @@ where
{
Some(bearer.token().to_string())
} else {
let jar = parts.extract::<CookieJar>().await.unwrap();
let jar = parts
.extract::<CookieJar>()
.await
.map_err(|e| AppError::Internal(e.to_string()))?;
jar.get("access_token")
.map(|cookie| cookie.value().to_string())
};
let token = token.ok_or((
StatusCode::UNAUTHORIZED,
"Missing or invalid access token".to_string(),
))?;
let token = token
.ok_or_else(|| AppError::Unauthorized("Missing or invalid access token".into()))?;
let claims = app_state.verifier.verify(&token).await.map_err(|e| {
(
StatusCode::UNAUTHORIZED,
format!("Token verification failed: {}", e),
)
})?;
let claims = app_state
.verifier
.verify(&token)
.await
.map_err(|e| AppError::Unauthorized(format!("Token verification failed: {}", e)))?;
Ok(AuthenticatedUser(claims))
}
@ -69,7 +68,7 @@ pub async fn auth_refresh(
State(state): State<Arc<AppState>>,
jar: CookieJar,
Json(payload): Json<RefreshRequest>,
) -> Result<impl IntoResponse, (StatusCode, String)> {
) -> AppResult<impl IntoResponse> {
let refresh_token = payload
.refresh_token
.filter(|token| !token.is_empty())
@ -77,16 +76,13 @@ pub async fn auth_refresh(
jar.get("refresh_token")
.map(|cookie| cookie.value().to_string())
})
.ok_or((
StatusCode::UNAUTHORIZED,
"Missing refresh token".to_string(),
))?;
.ok_or_else(|| AppError::Unauthorized("Missing refresh token".into()))?;
let data = state
.authenticator
.refresh_token(refresh_token)
.await
.map_err(|e| (StatusCode::UNAUTHORIZED, e.to_string()))?;
.map_err(|e| AppError::Unauthorized(e.to_string()))?;
let jar = update_auth_cookies(jar, &data, &state.config);
Ok((jar, Json(data)))
@ -96,17 +92,12 @@ pub async fn auth_callback(
State(state): State<Arc<AppState>>,
jar: CookieJar,
Query(query): Query<AuthCallbackQuery>,
) -> Result<impl IntoResponse, (StatusCode, String)> {
) -> AppResult<impl IntoResponse> {
let data = state
.authenticator
.exchange_code(query.code, query.redirect_uri)
.await
.map_err(|e| {
(
StatusCode::INTERNAL_SERVER_ERROR,
format!("Token exchange failed: {}", e),
)
})?;
.map_err(|e| AppError::Internal(format!("Token exchange failed: {}", e)))?;
let jar = update_auth_cookies(jar, &data, &state.config);
Ok((jar, Json(data)))
@ -114,7 +105,7 @@ pub async fn auth_callback(
pub async fn auth_logout(State(state): State<Arc<AppState>>, jar: CookieJar) -> impl IntoResponse {
let jar = clear_auth_cookies(jar, &state.config);
(jar, StatusCode::NO_CONTENT)
(jar, axum::http::StatusCode::NO_CONTENT)
}
pub async fn auth_session(user: AuthenticatedUser) -> Json<serde_json::Value> {