Skip to content

Commit

Permalink
Update login & CI
Browse files Browse the repository at this point in the history
  • Loading branch information
lucemans committed Dec 4, 2024
1 parent cfccbe2 commit bb1c08b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 37 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:

env:
REGISTRY: ghcr.io
IMAGE_NAME: v3x-property/engine
IMAGE_NAME: v3x-property-engine

jobs:
deploy:
Expand Down
3 changes: 2 additions & 1 deletion engine/src/routes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -50,6 +51,7 @@ fn get_api() -> impl OpenApi {
UserApi,
SessionsApi,
InstanceApi,
LoginApi,
)
}

Expand All @@ -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)
Expand Down
82 changes: 47 additions & 35 deletions engine/src/routes/oauth/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Option<String>>,
state: Data<&Arc<AppState>>,
) -> 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<String>),
}

#[OpenApi]
impl LoginApi {

#[oai(path = "/login", method = "get")]
pub async fn login(
&self,
redirect: Query<Option<String>>,
state: Data<&Arc<AppState>>,
) -> 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()))
}
}

0 comments on commit bb1c08b

Please sign in to comment.