82 lines
2.3 KiB
Rust
82 lines
2.3 KiB
Rust
use std::sync::Arc;
|
|
|
|
use actix_web::web::{self, Data};
|
|
use url::Url;
|
|
|
|
use crate::forge::auth::adapter::out::db::DBAdapter;
|
|
use crate::forge::auth::adapter::out::forge::{ForgeRepository, SupportedForges};
|
|
use crate::forge::auth::application::port::input::ui::errors::{InUIError, InUIResult};
|
|
use crate::forge::auth::application::services::request_authorization::service::RequestAuthorizationService;
|
|
use crate::forge::auth::application::services::request_authorization::RequestAuthorizationUserCase;
|
|
use crate::settings::Settings;
|
|
|
|
pub mod login;
|
|
mod routes;
|
|
mod templates;
|
|
|
|
use routes::RoutesRepository;
|
|
|
|
pub type ArcCtx = Arc<WebCtx>;
|
|
pub type ActixCtx = Data<ArcCtx>;
|
|
|
|
#[derive(Clone)]
|
|
pub struct WebCtx {
|
|
pub routes: Arc<RoutesRepository>,
|
|
pub adapters: Adapters,
|
|
pub templates: templates::Templates,
|
|
pub settings: Settings,
|
|
}
|
|
|
|
impl WebCtx {
|
|
pub fn new_actix_ctx(forges: ForgeRepository, db: DBAdapter, settings: Settings) -> ActixCtx {
|
|
let routes = Arc::new(RoutesRepository::default());
|
|
Data::new(Arc::new(Self {
|
|
routes: routes.clone(),
|
|
adapters: Adapters::new(forges, db),
|
|
templates: templates::Templates::default(),
|
|
settings,
|
|
}))
|
|
}
|
|
}
|
|
|
|
#[derive(Clone)]
|
|
pub struct Adapters {
|
|
pub forges: ForgeRepository,
|
|
pub db: DBAdapter,
|
|
}
|
|
|
|
impl Adapters {
|
|
pub fn new(forges: ForgeRepository, db: DBAdapter) -> Self {
|
|
Self { forges, db }
|
|
}
|
|
}
|
|
|
|
pub struct ServiceFactory;
|
|
|
|
impl ServiceFactory {
|
|
pub fn request_authorization(
|
|
forge_name: SupportedForges,
|
|
ctx: &WebCtx,
|
|
) -> InUIResult<Arc<dyn RequestAuthorizationUserCase>> {
|
|
if let Some(forge) = ctx.adapters.forges.get_forge(&forge_name) {
|
|
Ok(Arc::new(RequestAuthorizationService::new(
|
|
ctx.adapters.db.save_oauth_state_adapter.clone(),
|
|
forge.get_redirect_uri_adapter.clone(),
|
|
Url::parse(&format!(
|
|
"{}://{}{}",
|
|
"http",
|
|
&ctx.settings.server.domain,
|
|
&ctx.routes.process_oauth_authorization_response(&forge_name)
|
|
))
|
|
.map_err(|_| InUIError::InternalServerError)?,
|
|
)))
|
|
} else {
|
|
Err(InUIError::BadRequest)
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn services(cfg: &mut web::ServiceConfig) {
|
|
cfg.configure(login::services);
|
|
}
|