Files
Oxicloud/src/interfaces/api/handlers/auth_handler.rs
T

476 lines
15 KiB
Rust
Raw Normal View History

2025-03-20 09:22:31 +01:00
use axum::{
Router,
2026-02-14 01:29:34 +01:00
extract::{Json, Query, State},
http::{HeaderMap, StatusCode, header},
response::{IntoResponse, Redirect},
2026-02-14 01:29:34 +01:00
routing::{get, post, put},
2025-03-20 09:22:31 +01:00
};
2026-02-14 01:29:34 +01:00
use std::sync::Arc;
2025-03-20 09:22:31 +01:00
use crate::application::dtos::user_dto::{
2026-02-14 01:29:34 +01:00
ChangePasswordDto, LoginDto, OidcCallbackQueryDto, OidcExchangeDto, OidcProviderInfoDto,
RefreshTokenDto, RegisterDto,
2025-03-20 09:22:31 +01:00
};
2026-02-14 01:29:34 +01:00
use crate::common::di::AppState;
use crate::interfaces::errors::AppError;
2025-03-20 09:22:31 +01:00
pub fn auth_routes() -> Router<Arc<AppState>> {
// Routes that do NOT require authentication
2026-02-03 17:59:04 +01:00
let public_routes = Router::new()
2025-03-20 09:22:31 +01:00
.route("/register", post(register))
.route("/login", post(login))
.route("/refresh", post(refresh_token))
.route("/status", get(get_system_status))
// OIDC endpoints (all public)
.route("/oidc/providers", get(oidc_providers))
.route("/oidc/authorize", get(oidc_authorize))
.route("/oidc/callback", get(oidc_callback))
.route("/oidc/exchange", post(oidc_exchange));
2026-02-14 01:29:34 +01:00
// Routes that DO require authentication - we use route_layer to apply middleware
// The middleware will use the state passed with .with_state() from main.rs
2026-02-03 17:59:04 +01:00
let protected_routes = Router::new()
2025-03-20 09:22:31 +01:00
.route("/me", get(get_current_user))
.route("/change-password", put(change_password))
2026-02-03 17:59:04 +01:00
.route("/logout", post(logout));
2026-02-14 01:29:34 +01:00
// Combine public and protected routes
2026-02-03 17:59:04 +01:00
public_routes.merge(protected_routes)
2025-03-20 09:22:31 +01:00
}
async fn register(
State(state): State<Arc<AppState>>,
Json(dto): Json<RegisterDto>,
) -> Result<impl IntoResponse, AppError> {
2025-03-23 22:44:18 +01:00
// Add detailed logging for debugging
tracing::info!("Registration attempt for user: {}", dto.username);
2026-02-14 01:29:34 +01:00
2025-03-23 22:44:18 +01:00
// Verify auth service exists
let auth_service = match state.auth_service.as_ref() {
Some(service) => {
tracing::info!("Auth service found, proceeding with registration");
service
2026-02-14 01:29:34 +01:00
}
2025-03-23 22:44:18 +01:00
None => {
tracing::error!("Auth service not configured");
2026-02-14 01:29:34 +01:00
return Err(AppError::internal_error(
"Authentication service not configured",
));
2025-03-23 22:44:18 +01:00
}
};
// Fix #5: Block password registration when OIDC-only mode is active
2026-02-14 01:29:34 +01:00
if auth_service
.auth_application_service
.password_login_disabled()
{
return Err(AppError::new(
StatusCode::FORBIDDEN,
"Password registration is disabled. Please use SSO/OIDC to sign in.",
"PasswordRegistrationDisabled",
));
}
// Check if public registration has been disabled by the admin
if let Some(admin_svc) = state.admin_settings_service.as_ref()
2026-02-14 01:29:34 +01:00
&& !admin_svc.get_registration_enabled().await
{
return Err(AppError::new(
StatusCode::FORBIDDEN,
"Public registration has been disabled by the administrator.",
"RegistrationDisabled",
));
}
// Registration logic (admin detection, fresh-install handling, duplicate
// checks) is all inside the service layer. Call it directly.
2026-02-14 01:29:34 +01:00
match auth_service
.auth_application_service
.register(dto.clone())
.await
{
2025-03-23 22:44:18 +01:00
Ok(user) => {
tracing::info!("Registration successful for user: {}", dto.username);
Ok((StatusCode::CREATED, Json(user)))
2026-02-14 01:29:34 +01:00
}
2025-03-23 22:44:18 +01:00
Err(err) => {
tracing::error!("Registration failed for user {}: {}", dto.username, err);
Err(err.into())
}
}
2025-03-20 09:22:31 +01:00
}
async fn login(
State(state): State<Arc<AppState>>,
Json(dto): Json<LoginDto>,
) -> Result<impl IntoResponse, AppError> {
2025-03-23 22:44:18 +01:00
// Add detailed logging for debugging
tracing::info!("Login attempt for user: {}", dto.username);
2026-02-14 01:29:34 +01:00
// Verify auth service exists
2025-03-23 22:44:18 +01:00
let auth_service = match state.auth_service.as_ref() {
Some(service) => {
tracing::info!("Auth service found, proceeding with login");
service
2026-02-14 01:29:34 +01:00
}
2025-03-23 22:44:18 +01:00
None => {
tracing::error!("Auth service not configured");
2026-02-14 01:29:34 +01:00
return Err(AppError::internal_error(
"Authentication service not configured",
));
2025-03-23 22:44:18 +01:00
}
};
// Check if password login is disabled (OIDC-only mode)
2026-02-14 01:29:34 +01:00
if auth_service
.auth_application_service
.password_login_disabled()
{
return Err(AppError::unauthorized(
2026-02-14 01:29:34 +01:00
"Password login is disabled. Please use SSO/OIDC to sign in.",
));
}
2026-02-14 01:29:34 +01:00
2025-03-23 22:44:18 +01:00
// Try the normal login process
2026-02-14 01:29:34 +01:00
match auth_service
.auth_application_service
.login(dto.clone())
.await
{
2025-03-23 22:44:18 +01:00
Ok(auth_response) => {
tracing::info!("Login successful for user: {}", dto.username);
// Log the response structure for debugging
tracing::debug!("Auth response: {:?}", &auth_response);
2026-02-14 01:29:34 +01:00
// Ensure the response has the expected fields
if auth_response.access_token.is_empty() || auth_response.refresh_token.is_empty() {
2026-02-14 01:29:34 +01:00
tracing::error!(
"Login response contains empty tokens for user: {}",
dto.username
);
return Err(AppError::internal_error(
"Error generating authentication tokens",
));
}
2026-02-14 01:29:34 +01:00
2025-03-23 22:44:18 +01:00
Ok((StatusCode::OK, Json(auth_response)))
2026-02-14 01:29:34 +01:00
}
2025-03-23 22:44:18 +01:00
Err(err) => {
tracing::error!("Login failed for user {}: {}", dto.username, err);
Err(err.into())
}
}
2025-03-20 09:22:31 +01:00
}
async fn refresh_token(
State(state): State<Arc<AppState>>,
Json(dto): Json<RefreshTokenDto>,
) -> Result<impl IntoResponse, AppError> {
2025-03-31 06:20:15 +02:00
// Add rate limiting for token refresh to prevent refresh loops
// Check if this refresh token is being used too frequently
2026-02-14 01:29:34 +01:00
2025-03-31 06:20:15 +02:00
// Log the refresh attempt for debugging
tracing::info!("Token refresh requested");
2026-02-14 01:29:34 +01:00
2025-03-31 06:20:15 +02:00
// Normal process for real tokens
2026-02-14 01:29:34 +01:00
let auth_service = state
.auth_service
.as_ref()
.ok_or_else(|| AppError::internal_error("Authentication service not configured"))?;
2026-02-14 01:29:34 +01:00
let auth_response = auth_service
.auth_application_service
.refresh_token(dto)
.await?;
2025-03-31 06:20:15 +02:00
// Log successful token refresh
tracing::info!("Token refresh successful, new token issued");
2026-02-14 01:29:34 +01:00
2025-03-20 09:22:31 +01:00
Ok((StatusCode::OK, Json(auth_response)))
}
async fn get_current_user(
State(state): State<Arc<AppState>>,
2026-02-03 17:59:04 +01:00
headers: HeaderMap,
2025-03-20 09:22:31 +01:00
) -> Result<impl IntoResponse, AppError> {
2025-03-28 06:15:09 +01:00
// Normal process for all users
2026-02-14 01:29:34 +01:00
let auth_service = state
.auth_service
.as_ref()
.ok_or_else(|| AppError::internal_error("Authentication service not configured"))?;
2026-02-14 01:29:34 +01:00
// Extract and validate the token directly
2026-02-03 17:59:04 +01:00
let token = headers
.get(header::AUTHORIZATION)
.and_then(|value| value.to_str().ok())
.and_then(|value| value.strip_prefix("Bearer "))
.ok_or_else(|| AppError::unauthorized("Authorization token not found"))?;
2026-02-14 01:29:34 +01:00
// Validate the token and get claims
2026-02-14 01:29:34 +01:00
let claims = auth_service
.token_service
.validate_token(token)
.map_err(|e| AppError::unauthorized(format!("Invalid token: {}", e)))?;
2026-02-14 01:29:34 +01:00
2026-02-03 17:59:04 +01:00
let user_id = claims.sub;
2026-02-14 01:29:34 +01:00
// First, update the storage usage statistics
// IMPORTANT: We await the calculation to return updated data
2025-04-09 00:21:20 +02:00
if let Some(storage_usage_service) = state.storage_usage_service.as_ref() {
// Calculate storage synchronously (we await the result)
2026-02-14 01:29:34 +01:00
match storage_usage_service
.update_user_storage_usage(&user_id)
.await
{
2026-02-03 17:59:04 +01:00
Ok(usage) => {
2026-02-14 01:29:34 +01:00
tracing::info!(
"Updated storage usage for user {}: {} bytes",
user_id,
usage
);
}
2026-02-03 17:59:04 +01:00
Err(e) => {
// Only log a warning, don't fail the entire request
2026-02-03 17:59:04 +01:00
tracing::warn!("Failed to update storage usage for user {}: {}", user_id, e);
2025-04-09 00:21:20 +02:00
}
2026-02-03 17:59:04 +01:00
}
2025-04-09 00:21:20 +02:00
}
2026-02-14 01:29:34 +01:00
// Now get the user data WITH the updated storage
2026-02-14 01:29:34 +01:00
let user = auth_service
.auth_application_service
.get_user_by_id(&user_id)
.await?;
2025-03-20 09:22:31 +01:00
Ok((StatusCode::OK, Json(user)))
}
async fn change_password(
State(state): State<Arc<AppState>>,
2026-02-03 17:59:04 +01:00
headers: HeaderMap,
2025-03-20 09:22:31 +01:00
Json(dto): Json<ChangePasswordDto>,
) -> Result<impl IntoResponse, AppError> {
2026-02-14 01:29:34 +01:00
let auth_service = state
.auth_service
.as_ref()
.ok_or_else(|| AppError::internal_error("Authentication service not configured"))?;
2026-02-14 01:29:34 +01:00
// Extract and validate the token directly
2026-02-03 17:59:04 +01:00
let token = headers
.get(header::AUTHORIZATION)
.and_then(|value| value.to_str().ok())
.and_then(|value| value.strip_prefix("Bearer "))
.ok_or_else(|| AppError::unauthorized("Authorization token not found"))?;
2026-02-14 01:29:34 +01:00
// Validate the token and get claims
2026-02-14 01:29:34 +01:00
let claims = auth_service
.token_service
.validate_token(token)
.map_err(|e| AppError::unauthorized(format!("Invalid token: {}", e)))?;
2026-02-14 01:29:34 +01:00
auth_service
.auth_application_service
.change_password(&claims.sub, dto)
.await?;
2025-03-20 09:22:31 +01:00
Ok(StatusCode::OK)
}
async fn logout(
State(state): State<Arc<AppState>>,
headers: HeaderMap,
) -> Result<impl IntoResponse, AppError> {
2026-02-14 01:29:34 +01:00
let auth_service = state
.auth_service
.as_ref()
.ok_or_else(|| AppError::internal_error("Authentication service not configured"))?;
2026-02-14 01:29:34 +01:00
// Extract and validate the token directly
2026-02-03 17:59:04 +01:00
let token = headers
2025-03-20 09:22:31 +01:00
.get(header::AUTHORIZATION)
.and_then(|value| value.to_str().ok())
.and_then(|value| value.strip_prefix("Bearer "))
.ok_or_else(|| AppError::unauthorized("Authorization token not found"))?;
2026-02-14 01:29:34 +01:00
// Validate the token and get claims
2026-02-14 01:29:34 +01:00
let claims = auth_service
.token_service
.validate_token(token)
.map_err(|e| AppError::unauthorized(format!("Invalid token: {}", e)))?;
2026-02-14 01:29:34 +01:00
2026-02-03 17:59:04 +01:00
// Use access token for logout (we don't have refresh token in headers)
2026-02-14 01:29:34 +01:00
auth_service
.auth_application_service
.logout(&claims.sub, token)
.await?;
2025-03-20 09:22:31 +01:00
Ok(StatusCode::OK)
}
2026-02-03 17:59:04 +01:00
/// Get system status - returns whether admin is configured
/// This is a public endpoint used to determine if setup is needed
#[derive(serde::Serialize)]
struct SystemStatus {
/// Whether the system has been set up with an admin
initialized: bool,
/// Number of admin users in the system
admin_count: i64,
/// Whether registration is allowed (only if admin exists)
registration_allowed: bool,
}
async fn get_system_status(
State(state): State<Arc<AppState>>,
) -> Result<impl IntoResponse, AppError> {
2026-02-14 01:29:34 +01:00
let auth_service = state
.auth_service
.as_ref()
.ok_or_else(|| AppError::internal_error("Authentication service not configured"))?;
2026-02-14 01:29:34 +01:00
2026-02-03 17:59:04 +01:00
// Count admin users to determine if system is initialized
2026-02-14 01:29:34 +01:00
let admin_count = auth_service
.auth_application_service
.count_admin_users()
.await
2026-02-03 17:59:04 +01:00
.unwrap_or(0);
2026-02-14 01:29:34 +01:00
2026-02-03 17:59:04 +01:00
let status = SystemStatus {
initialized: admin_count > 0,
admin_count,
registration_allowed: admin_count > 0, // Only allow registration if admin exists
};
2026-02-14 01:29:34 +01:00
tracing::info!(
"System status check: initialized={}, admin_count={}",
status.initialized,
status.admin_count
);
2026-02-03 17:59:04 +01:00
Ok((StatusCode::OK, Json(status)))
}
// ============================================================================
// OIDC Handlers
// ============================================================================
/// GET /api/auth/oidc/providers — Returns OIDC provider info for the UI
2026-02-14 01:29:34 +01:00
async fn oidc_providers(State(state): State<Arc<AppState>>) -> Result<impl IntoResponse, AppError> {
let auth_service = state
.auth_service
.as_ref()
.ok_or_else(|| AppError::internal_error("Auth service not configured"))?;
let auth_app = &auth_service.auth_application_service;
if !auth_app.oidc_enabled() {
return Ok(Json(OidcProviderInfoDto {
enabled: false,
provider_name: String::new(),
authorize_endpoint: String::new(),
password_login_enabled: true,
}));
}
let config = auth_app.oidc_config().unwrap();
Ok(Json(OidcProviderInfoDto {
enabled: true,
provider_name: config.provider_name.clone(),
authorize_endpoint: "/api/auth/oidc/authorize".to_string(),
password_login_enabled: !config.disable_password_login,
}))
}
/// GET /api/auth/oidc/authorize — Redirects user to the OIDC provider
2026-02-14 01:29:34 +01:00
async fn oidc_authorize(State(state): State<Arc<AppState>>) -> Result<impl IntoResponse, AppError> {
let auth_service = state
.auth_service
.as_ref()
.ok_or_else(|| AppError::internal_error("Auth service not configured"))?;
let auth_app = &auth_service.auth_application_service;
if !auth_app.oidc_enabled() {
return Err(AppError::new(
StatusCode::NOT_FOUND,
"OIDC is not enabled",
"OidcDisabled",
));
}
// Prepare OIDC authorization flow (generates CSRF state, PKCE pair, nonce)
let authorize_url = auth_app.prepare_oidc_authorize().await?;
tracing::info!("OIDC authorize redirect generated");
Ok(Redirect::temporary(&authorize_url))
}
/// GET /api/auth/oidc/callback?code=...&state=... — Handles OIDC callback
async fn oidc_callback(
State(state): State<Arc<AppState>>,
Query(query): Query<OidcCallbackQueryDto>,
) -> Result<impl IntoResponse, AppError> {
2026-02-14 01:29:34 +01:00
let auth_service = state
.auth_service
.as_ref()
.ok_or_else(|| AppError::internal_error("Auth service not configured"))?;
let auth_app = &auth_service.auth_application_service;
if !auth_app.oidc_enabled() {
return Err(AppError::new(
StatusCode::NOT_FOUND,
"OIDC is not enabled",
"OidcDisabled",
));
}
tracing::info!("OIDC callback received with code");
// Exchange code, validate state/nonce/PKCE, authenticate user
2026-02-14 01:29:34 +01:00
let exchange_code = auth_app
.oidc_callback(&query.code, &query.state)
.await
.map_err(|e| {
tracing::error!("OIDC callback failed: {}", e);
AppError::from(e)
})?;
// Redirect to frontend with one-time exchange code (NOT raw tokens)
let config = auth_app.oidc_config().unwrap();
let frontend_url = config.frontend_url.trim_end_matches('/');
2026-02-14 01:29:34 +01:00
let redirect_url = format!("{}/?oidc_code={}", frontend_url, exchange_code,);
tracing::info!("OIDC login successful, redirecting with exchange code");
Ok(Redirect::temporary(&redirect_url))
}
/// POST /api/auth/oidc/exchange — Exchange one-time code for auth tokens
/// Request body: { "code": "<one_time_code>" }
async fn oidc_exchange(
State(state): State<Arc<AppState>>,
Json(body): Json<OidcExchangeDto>,
) -> Result<impl IntoResponse, AppError> {
2026-02-14 01:29:34 +01:00
let auth_service = state
.auth_service
.as_ref()
.ok_or_else(|| AppError::internal_error("Auth service not configured"))?;
2026-02-14 01:29:34 +01:00
let auth_response = auth_service
.auth_application_service
.exchange_oidc_token(&body.code)
.map_err(|e| {
tracing::warn!("OIDC token exchange failed: {}", e);
AppError::from(e)
})?;
2026-02-14 01:29:34 +01:00
tracing::info!(
"OIDC token exchange successful for user: {}",
auth_response.user.username
);
Ok((StatusCode::OK, Json(auth_response)))
}