Files
Oxicloud/src/infrastructure/auth_factory.rs
T

72 lines
2.5 KiB
Rust
Raw Normal View History

2025-03-20 09:22:31 +01:00
use anyhow::Result;
use sqlx::PgPool;
2026-02-14 01:29:34 +01:00
use std::sync::Arc;
2025-03-20 09:22:31 +01:00
use crate::application::ports::auth_ports::TokenServicePort;
2025-03-20 09:22:31 +01:00
use crate::application::services::auth_application_service::AuthApplicationService;
2025-03-23 22:44:18 +01:00
use crate::application::services::folder_service::FolderService;
2025-03-20 09:22:31 +01:00
use crate::common::config::AppConfig;
use crate::common::di::AuthServices;
2026-02-14 01:29:34 +01:00
use crate::infrastructure::repositories::{SessionPgRepository, UserPgRepository};
use crate::infrastructure::services::jwt_service::JwtTokenService;
use crate::infrastructure::services::oidc_service::OidcService;
use crate::infrastructure::services::password_hasher::Argon2PasswordHasher;
2025-03-20 09:22:31 +01:00
2025-03-23 22:44:18 +01:00
pub async fn create_auth_services(
2026-02-14 01:29:34 +01:00
config: &AppConfig,
2025-03-23 22:44:18 +01:00
pool: Arc<PgPool>,
2026-02-14 01:29:34 +01:00
folder_service: Option<Arc<FolderService>>,
2025-03-23 22:44:18 +01:00
) -> Result<AuthServices> {
// Create JWT token service (TokenServicePort implementation)
let token_service: Arc<dyn TokenServicePort> = Arc::new(JwtTokenService::new(
2025-03-20 09:22:31 +01:00
config.auth.jwt_secret.clone(),
config.auth.access_token_expiry_secs,
config.auth.refresh_token_expiry_secs,
));
2026-02-14 01:29:34 +01:00
// Create password hashing service
let password_hasher = Arc::new(Argon2PasswordHasher::new());
2026-02-14 01:29:34 +01:00
// Create PostgreSQL repositories
2025-03-20 09:22:31 +01:00
let user_repository = Arc::new(UserPgRepository::new(pool.clone()));
let session_repository = Arc::new(SessionPgRepository::new(pool.clone()));
2026-02-14 01:29:34 +01:00
// Create authentication application service
2025-03-23 22:44:18 +01:00
let mut auth_app_service = AuthApplicationService::new(
2025-03-20 09:22:31 +01:00
user_repository,
session_repository,
password_hasher,
token_service.clone(),
config.storage_path.clone(),
2025-03-23 22:44:18 +01:00
);
2026-02-14 01:29:34 +01:00
// Configure folder service if available
2025-03-23 22:44:18 +01:00
if let Some(folder_svc) = folder_service {
auth_app_service = auth_app_service.with_folder_service(folder_svc);
}
// Configure OIDC service if enabled
if config.oidc.enabled {
2026-02-14 01:29:34 +01:00
tracing::info!(
"Initializing OIDC service (provider: {}, issuer: {})",
config.oidc.provider_name,
config.oidc.issuer_url
);
let oidc_service = Arc::new(OidcService::new(config.oidc.clone()));
auth_app_service = auth_app_service.with_oidc(oidc_service, config.oidc.clone());
if config.oidc.disable_password_login {
tracing::warn!("Password login is DISABLED — only OIDC authentication is allowed");
}
}
2026-02-14 01:29:34 +01:00
// Package service in Arc
2025-03-23 22:44:18 +01:00
let auth_application_service = Arc::new(auth_app_service);
2026-02-14 01:29:34 +01:00
2025-03-20 09:22:31 +01:00
Ok(AuthServices {
token_service,
2025-03-20 09:22:31 +01:00
auth_application_service,
})
2026-02-14 01:29:34 +01:00
}