blog/backend/feature/auth/src/adapter/delivery/auth_controller.rs
SquidSpirit 32b3d47715 BLOG-85 feat: implement OIDC authentication flow
- Added OIDC authentication support with new modules for handling OIDC login and callback.
- Introduced `AuthController`, `AuthOidcService`, and related DTOs for managing authentication state and user responses.
- Implemented session management using `actix-session` with Redis for storing authentication state.
- Created configuration management for OIDC settings, including provider metadata and client credentials.
- Updated server configuration to initialize OIDC services and session management.
- Refactored existing code to integrate new authentication features and ensure proper dependency management.
2025-07-30 03:00:36 +08:00

67 lines
1.7 KiB
Rust

use std::sync::Arc;
use async_trait::async_trait;
use crate::{
adapter::delivery::{
oidc_callback_query_dto::OidcCallbackQueryDto, user_response_dto::UserResponseDto,
},
application::{
error::auth_error::AuthError,
use_case::{
exchange_auth_code_use_case::ExchangeAuthCodeUseCase,
get_auth_url_use_case::{AuthUrl, GetAuthUrlUseCase},
},
},
};
#[async_trait]
pub trait AuthController: Send + Sync {
fn oidc_login(&self) -> Result<AuthUrl, AuthError>;
async fn oidc_callback(
&self,
query: OidcCallbackQueryDto,
expected_state: &str,
expected_nonce: &str,
) -> Result<UserResponseDto, AuthError>;
}
pub struct AuthControllerImpl {
get_auth_url_use_case: Arc<dyn GetAuthUrlUseCase>,
exchange_auth_code_use_case: Arc<dyn ExchangeAuthCodeUseCase>,
}
impl AuthControllerImpl {
pub fn new(
get_auth_url_use_case: Arc<dyn GetAuthUrlUseCase>,
exchange_auth_code_use_case: Arc<dyn ExchangeAuthCodeUseCase>,
) -> Self {
Self {
get_auth_url_use_case,
exchange_auth_code_use_case,
}
}
}
#[async_trait]
impl AuthController for AuthControllerImpl {
fn oidc_login(&self) -> Result<AuthUrl, AuthError> {
self.get_auth_url_use_case.execute()
}
async fn oidc_callback(
&self,
query: OidcCallbackQueryDto,
expected_state: &str,
expected_nonce: &str,
) -> Result<UserResponseDto, AuthError> {
let result = self
.exchange_auth_code_use_case
.execute(&query.code, &query.state, expected_state, expected_nonce)
.await;
result.map(|user| UserResponseDto::from(user))
}
}