Skip to content

Commit

Permalink
Introduce openid callback
Browse files Browse the repository at this point in the history
  • Loading branch information
lucemans committed Jul 22, 2024
1 parent 6ca5a18 commit d78203e
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 19 deletions.
8 changes: 8 additions & 0 deletions engine/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ edition = "2021"

[dependencies]
axum = "0.7.3"
dotenv = "0.15.0"
dotenvy = "0.15.7"
openid = "0.14.0"
reqwest = "0.12.5"
serde = "1.0.204"
Expand Down
3 changes: 3 additions & 0 deletions engine/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod database;
mod permissions;
mod routes;
mod state;
mod openid;

#[tokio::main]
async fn main() {
Expand All @@ -15,6 +16,8 @@ async fn main() {
.render();
println!("{}", banner);

dotenvy::dotenv().ok();

tracing_subscriber::fmt::init();

info!("Starting v3x-property");
Expand Down
3 changes: 3 additions & 0 deletions engine/src/openid.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
use openid::{Client, Discovered, StandardClaims};

pub type OpenIDClient = Client<Discovered, StandardClaims>;
61 changes: 44 additions & 17 deletions engine/src/routes/auth.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
use axum::response::{IntoResponse, Redirect};
use openid::{DiscoveredClient, Options};
use std::{borrow::BorrowMut, ops::Deref, sync::Arc};

pub async fn login() -> impl IntoResponse {
use axum::{
extract::{Query, State},
response::{IntoResponse, Redirect},
};
use openid::{Options, Token};
use serde::Deserialize;
use tracing::info;

use crate::state::AppState;

pub async fn login(state: State<Arc<AppState>>) -> impl IntoResponse {
// let discovery_url = "http://localhost:8080/realms/master/.well-known/openid-configuration";

// let http_client = reqwest::Client::new();
Expand All @@ -12,25 +21,43 @@ pub async fn login() -> impl IntoResponse {
// .json()
// .await.unwrap();

// Create the OpenID client
let client_id = "devclient";
let client_secret = Some("wavt7wfi7VXkv5ex9PMFKOGBBnVhfZzy");
let redirect_url = "http://localhost:3000/callback";

let client = DiscoveredClient::discover(
client_id.to_string(),
client_secret.map(|s| s.to_string()),
Some(redirect_url.to_string()),
"http://localhost:8080/realms/master".parse().unwrap()
// discovery_response.issuer.parse().unwrap()
)
.await.unwrap();
let options = Options {
scope: Some("openid email profile".to_string()),
..Default::default()
};

// Generate the authorization URL
let authorize_url = client.auth_url(&Options::default());
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())
}

#[derive(Deserialize)]
pub struct MyQuery {
pub session_state: Option<String>,
pub iss: Option<String>,
pub code: String,
pub prompt: Option<String>,
}

pub async fn callback(query: Query<MyQuery>, state: State<Arc<AppState>>) -> impl IntoResponse {
let mut token = state.openid.request_token(&query.code).await.unwrap();

// let mut id_token = (&token.id_token).clone().unwrap().clone();

let mut token = Token::from(token);

let mut id_token = token.id_token.take().unwrap();

state.openid.decode_token(&mut id_token).unwrap();
state.openid.validate_token(&id_token, None, None).unwrap();

// info!("Token: {:?}", id_token);

let x = state.openid.request_userinfo(&token).await.unwrap();

format!("Hello {:?}", x)
}
1 change: 1 addition & 0 deletions engine/src/routes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub async fn serve(state: AppState) -> Result<(), axum::Error> {
let app = Router::new()
.route("/", get(root))
.route("/login", get(auth::login))
.route("/callback", get(auth::callback))
// .route("/devices", get(routes::devices::get))
.with_state(Arc::new(state));

Expand Down
4 changes: 2 additions & 2 deletions engine/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::env;

use crate::database::Database;
use crate::{database::Database, openid::OpenIDClient};
use openid::DiscoveredClient;

pub struct AppState {
pub database: Database,
pub openid: DiscoveredClient,
pub openid: OpenIDClient,
}

impl AppState {
Expand Down

0 comments on commit d78203e

Please sign in to comment.