From bb1c08b3d960eb7b5f2db0d3e9f56523af41a2d0 Mon Sep 17 00:00:00 2001 From: Luc Date: Wed, 4 Dec 2024 16:40:34 +0100 Subject: [PATCH] Update login & CI --- .github/workflows/build.yaml | 2 +- engine/src/routes/mod.rs | 3 +- engine/src/routes/oauth/login.rs | 82 ++++++++++++++++++-------------- 3 files changed, 50 insertions(+), 37 deletions(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 3458109..11245fd 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -8,7 +8,7 @@ on: env: REGISTRY: ghcr.io - IMAGE_NAME: v3x-property/engine + IMAGE_NAME: v3x-property-engine jobs: deploy: diff --git a/engine/src/routes/mod.rs b/engine/src/routes/mod.rs index b159987..19e76af 100644 --- a/engine/src/routes/mod.rs +++ b/engine/src/routes/mod.rs @@ -4,6 +4,7 @@ use instance::InstanceApi; use items::ItemsApi; use me::MeApi; use media::MediaApi; +use oauth::login::LoginApi; use poem::{ get, handler, listener::TcpListener, middleware::Cors, web::Html, EndpointExt, Route, Server, }; @@ -50,6 +51,7 @@ fn get_api() -> impl OpenApi { UserApi, SessionsApi, InstanceApi, + LoginApi, ) } @@ -67,7 +69,6 @@ pub async fn serve(state: AppState) -> Result<(), poem::Error> { let state = Arc::new(state); let app = Route::new() - .at("/login", get(oauth::login::login)) .at("/callback", get(oauth::callback::callback)) .nest("/api", api_service) .nest("/openapi.json", spec) diff --git a/engine/src/routes/oauth/login.rs b/engine/src/routes/oauth/login.rs index 5835c0f..86c3d70 100644 --- a/engine/src/routes/oauth/login.rs +++ b/engine/src/routes/oauth/login.rs @@ -2,44 +2,56 @@ use std::{collections::HashSet, sync::Arc}; use openid::{Options, Prompt}; use poem::{ - handler, - web::{Data, Query, Redirect}, - IntoResponse, + handler, http::Response, web::{headers::Header, Data, Redirect}, IntoResponse }; +use poem_openapi::{param::Query, payload::{PlainText}, ApiResponse, OpenApi}; use serde::Deserialize; use crate::state::AppState; -#[handler] -pub async fn login( - redirect: Query>, - state: Data<&Arc>, -) -> impl IntoResponse { - // let discovery_url = "http://localhost:8080/realms/master/.well-known/openid-configuration"; - - // let http_client = reqwest::Client::new(); - // let discovery_response: DiscoveryResponse = http_client - // .get(discovery_url) - // .send() - // .await.unwrap() - // .json() - // .await.unwrap(); - - // scopes, for calendar for example https://www.googleapis.com/auth/calendar.events - let scope = "openid email profile".to_string(); - - let options = Options { - scope: Some(scope), - state: redirect.0.clone(), - prompt: Some(HashSet::from([Prompt::SelectAccount])), - ..Default::default() - }; - - // Generate the authorization URL - let authorize_url = state.openid.auth_url(&options); - - println!("OpenID Connect Authorization URL: {}", authorize_url); - - // redirect to the authorization URL - Redirect::temporary(authorize_url.as_str()) +pub struct LoginApi; + +#[derive(ApiResponse)] +enum RedirectResponse { + #[oai(status = 302)] + Redirect(PlainText), +} + +#[OpenApi] +impl LoginApi { + + #[oai(path = "/login", method = "get")] + pub async fn login( + &self, + redirect: Query>, + state: Data<&Arc>, + ) -> RedirectResponse { + // let discovery_url = "http://localhost:8080/realms/master/.well-known/openid-configuration"; + + // let http_client = reqwest::Client::new(); + // let discovery_response: DiscoveryResponse = http_client + // .get(discovery_url) + // .send() + // .await.unwrap() + // .json() + // .await.unwrap(); + + // scopes, for calendar for example https://www.googleapis.com/auth/calendar.events + let scope = "openid email profile".to_string(); + + let options = Options { + scope: Some(scope), + state: redirect.0.clone(), + prompt: Some(HashSet::from([Prompt::SelectAccount])), + ..Default::default() + }; + + // Generate the authorization URL + let authorize_url = state.openid.auth_url(&options); + + println!("OpenID Connect Authorization URL: {}", authorize_url); + + // redirect to the authorization URL + RedirectResponse::Redirect(PlainText(authorize_url.as_str().to_string())) + } }