ForgeFlux/src/forge/auth/adapter/input/web/login.rs

61 lines
2 KiB
Rust

use std::str::FromStr;
use actix_web::{get, http::header, post, web, HttpResponse};
use crate::forge::auth::adapter::out::forge::SupportedForges;
use crate::forge::auth::application::port::input::ui::{
errors::*, login::RequestAuthorizationInterface,
};
use crate::forge::auth::application::services::request_authorization::command::RequestAuthorizationCommand;
use super::{templates::login::LoginCtxFactory, ServiceFactory, WebCtx};
use crate::ActixCtx;
#[async_trait::async_trait]
impl RequestAuthorizationInterface for WebCtx {
#[tracing::instrument(name = "web adapter request_oauth_authorization", skip(self))]
async fn request_oauth_authorization(&self, forge_name: String) -> InUIResult<HttpResponse> {
let service = ServiceFactory::request_authorization(
SupportedForges::from_str(&forge_name).map_err(|_| InUIError::BadRequest)?,
self,
)?;
log::info!("service found");
let cmd = RequestAuthorizationCommand::new_command(forge_name)?;
let auth_page = service.request_authorization(cmd).await?;
Ok(HttpResponse::Found()
.insert_header((header::LOCATION, auth_page.as_str()))
.finish())
}
}
#[get("/login")]
async fn login_page(ctx: ActixCtx) -> InUIResult<HttpResponse> {
let template_ctx =
LoginCtxFactory::get_ctx(ctx.adapters.forges.get_supported_forges(), &ctx.routes);
let page = ctx
.templates
.login_page
.get_login_page(template_ctx)
.unwrap();
Ok(HttpResponse::Ok()
.append_header((header::CONTENT_TYPE, "text/html; charset=UTF-8"))
.body(page))
}
#[post("/oauth/{forge}/login")]
#[tracing::instrument(name = "web handler request_oauth_authorization", skip(ctx))]
async fn request_oauth_authorization(
ctx: ActixCtx,
forge_name: web::Path<String>,
) -> InUIResult<HttpResponse> {
ctx.request_oauth_authorization(forge_name.into_inner())
.await
}
pub fn services(cfg: &mut web::ServiceConfig) {
cfg.service(login_page);
cfg.service(request_oauth_authorization);
}