remove OIDC/Postgres, replace ApiError w/ AppError, move params to api-types
This commit is contained in:
@@ -1,14 +1,14 @@
|
||||
use axum::extract::FromRequestParts;
|
||||
use axum::http::request::Parts;
|
||||
use domain::User;
|
||||
use domain::{DomainError, User};
|
||||
|
||||
use crate::errors::ApiError;
|
||||
use crate::errors::AppError;
|
||||
use crate::state::AppState;
|
||||
|
||||
pub struct CurrentUser(pub User);
|
||||
|
||||
impl FromRequestParts<AppState> for CurrentUser {
|
||||
type Rejection = ApiError;
|
||||
type Rejection = AppError;
|
||||
|
||||
async fn from_request_parts(
|
||||
parts: &mut Parts,
|
||||
@@ -25,9 +25,9 @@ impl FromRequestParts<AppState> for CurrentUser {
|
||||
#[cfg(not(feature = "auth-jwt"))]
|
||||
{
|
||||
let _ = (parts, state);
|
||||
Err(ApiError::Unauthorized(
|
||||
Err(AppError(DomainError::Unauthenticated(
|
||||
"No authentication backend configured".to_string(),
|
||||
))
|
||||
)))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -35,7 +35,7 @@ impl FromRequestParts<AppState> for CurrentUser {
|
||||
pub struct OptionalCurrentUser(pub Option<User>);
|
||||
|
||||
impl FromRequestParts<AppState> for OptionalCurrentUser {
|
||||
type Rejection = ApiError;
|
||||
type Rejection = AppError;
|
||||
|
||||
async fn from_request_parts(
|
||||
parts: &mut Parts,
|
||||
@@ -69,7 +69,7 @@ impl FromRequestParts<AppState> for OptionalCurrentUser {
|
||||
pub struct AdminUser(pub User);
|
||||
|
||||
impl FromRequestParts<AppState> for AdminUser {
|
||||
type Rejection = ApiError;
|
||||
type Rejection = AppError;
|
||||
|
||||
async fn from_request_parts(
|
||||
parts: &mut Parts,
|
||||
@@ -77,64 +77,64 @@ impl FromRequestParts<AppState> for AdminUser {
|
||||
) -> Result<Self, Self::Rejection> {
|
||||
let CurrentUser(user) = CurrentUser::from_request_parts(parts, state).await?;
|
||||
if !user.is_admin() {
|
||||
return Err(ApiError::Forbidden("Admin access required".to_string()));
|
||||
return Err(AppError(DomainError::Forbidden("Admin access required".to_string())));
|
||||
}
|
||||
Ok(AdminUser(user))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "auth-jwt")]
|
||||
async fn try_jwt_auth(parts: &mut Parts, state: &AppState) -> Result<User, ApiError> {
|
||||
async fn try_jwt_auth(parts: &mut Parts, state: &AppState) -> Result<User, AppError> {
|
||||
use axum::http::header::AUTHORIZATION;
|
||||
|
||||
let auth_header = parts
|
||||
.headers
|
||||
.get(AUTHORIZATION)
|
||||
.ok_or_else(|| ApiError::Unauthorized("Missing Authorization header".to_string()))?;
|
||||
.ok_or_else(|| AppError(DomainError::Unauthenticated("Missing Authorization header".to_string())))?;
|
||||
|
||||
let auth_str = auth_header
|
||||
.to_str()
|
||||
.map_err(|_| ApiError::Unauthorized("Invalid Authorization header encoding".to_string()))?;
|
||||
.map_err(|_| AppError(DomainError::Unauthenticated("Invalid Authorization header encoding".to_string())))?;
|
||||
|
||||
let token = auth_str.strip_prefix("Bearer ").ok_or_else(|| {
|
||||
ApiError::Unauthorized("Authorization header must use Bearer scheme".to_string())
|
||||
AppError(DomainError::Unauthenticated("Authorization header must use Bearer scheme".to_string()))
|
||||
})?;
|
||||
|
||||
validate_jwt_token(token, state).await
|
||||
}
|
||||
|
||||
#[cfg(feature = "auth-jwt")]
|
||||
pub(crate) async fn validate_jwt_token(token: &str, state: &AppState) -> Result<User, ApiError> {
|
||||
pub(crate) async fn validate_jwt_token(token: &str, state: &AppState) -> Result<User, AppError> {
|
||||
let validator = state
|
||||
.jwt_validator
|
||||
.as_ref()
|
||||
.ok_or_else(|| ApiError::Internal("JWT validator not configured".to_string()))?;
|
||||
.ok_or_else(|| AppError(DomainError::InfrastructureError("JWT validator not configured".to_string())))?;
|
||||
|
||||
let claims = validator.validate_access_token(token).map_err(|e| {
|
||||
tracing::debug!("JWT validation failed: {:?}", e);
|
||||
match e {
|
||||
adapter_auth::JwtError::Expired => {
|
||||
ApiError::Unauthorized("Token expired".to_string())
|
||||
AppError(DomainError::Unauthenticated("Token expired".to_string()))
|
||||
}
|
||||
adapter_auth::JwtError::InvalidFormat => {
|
||||
ApiError::Unauthorized("Invalid token format".to_string())
|
||||
AppError(DomainError::Unauthenticated("Invalid token format".to_string()))
|
||||
}
|
||||
_ => ApiError::Unauthorized("Token validation failed".to_string()),
|
||||
_ => AppError(DomainError::Unauthenticated("Token validation failed".to_string())),
|
||||
}
|
||||
})?;
|
||||
|
||||
let user_id: uuid::Uuid = claims
|
||||
.sub
|
||||
.parse()
|
||||
.map_err(|_| ApiError::Unauthorized("Invalid user ID in token".to_string()))?;
|
||||
.map_err(|_| AppError(DomainError::Unauthenticated("Invalid user ID in token".to_string())))?;
|
||||
|
||||
let user = state
|
||||
.auth_deps
|
||||
.user_query
|
||||
.find_by_id(domain::UserId::from(user_id))
|
||||
.await
|
||||
.map_err(|e| ApiError::Internal(format!("Failed to fetch user: {}", e)))?
|
||||
.ok_or_else(|| ApiError::Unauthorized("User not found".to_string()))?;
|
||||
.map_err(|e| AppError(DomainError::InfrastructureError(format!("Failed to fetch user: {}", e))))?
|
||||
.ok_or_else(|| AppError(DomainError::Unauthenticated("User not found".to_string())))?;
|
||||
|
||||
Ok(user)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user