From 71fc110570b4204ab7bdde3c47a6ea9312932393 Mon Sep 17 00:00:00 2001 From: Frank Elsinga Date: Wed, 17 Jan 2024 18:30:54 +0100 Subject: [PATCH] refactored the oauth implementation --- server/main-api/src/calendar/mod.rs | 39 +++++++++++++---------------- server/main-api/src/main.rs | 8 +++--- 2 files changed, 21 insertions(+), 26 deletions(-) diff --git a/server/main-api/src/calendar/mod.rs b/server/main-api/src/calendar/mod.rs index 41bcd9d10..3a6d4bbfa 100644 --- a/server/main-api/src/calendar/mod.rs +++ b/server/main-api/src/calendar/mod.rs @@ -12,6 +12,7 @@ use sqlx::PgPool; use std::env; use std::error::Error; use std::ops::Sub; +use reqwest::Url; fn has_to_refetch(last_requests: &DateTime) -> bool { let one_hour = FixedOffset::east_opt(60 * 60).expect("time travel is impossible and chronos is 2038-save"); @@ -35,36 +36,30 @@ async fn delete_events( } async fn fetch_oauth_token() -> Result> { - let oauth2_client = BasicClient::new( - ClientId::new(env::var("TUMONLINE_OAUTH_CLIENT_ID")?), - Some(ClientSecret::new(env::var( - "TUMONLINE_OAUTH_CLIENT_SECRET", - )?)), - AuthUrl::new( - "https://review.campus.tum.de/RSYSTEM/co/public/sec/auth/realms/CAMPUSonline" - .to_string(), - )?, - Some(TokenUrl::new("https://example.com/token".to_string())?), - ); + let client_id = env::var("TUMONLINE_OAUTH_CLIENT_ID").expect("please configure the environment variable TUMONLINE_OAUTH_CLIENT_ID to use this endpoint"); + let client_secret = env::var("TUMONLINE_OAUTH_CLIENT_SECRET").expect("please configure the environment variable TUMONLINE_OAUTH_CLIENT_SECRET to use this endpoint"); - let token = oauth2_client - .exchange_client_credentials() + // for urls see https://review.campus.tum.de/RSYSTEM/co/public/sec/auth/realms/CAMPUSonline/.well-known/openid-configuration + let auth_url = Url::parse("https://review.campus.tum.de/RSYSTEM/co/public/sec/auth/realms/CAMPUSonline/protocol/openid-connect/auth")?; + let token_url = Url::parse("https://review.campus.tum.de/RSYSTEM/co/public/sec/auth/realms/CAMPUSonline/protocol/openid-connect/token")?; + + let token = BasicClient::new( + ClientId::new(client_id), + Some(ClientSecret::new(client_secret)), + AuthUrl::from_url(auth_url), + Some(TokenUrl::from_url(token_url)), + ).exchange_client_credentials() .add_scope(Scope::new("connectum-rooms.read".into())) .request_async(async_http_client) - .await?; // not directly returned for typing issues - Ok(token) + .await; + Ok(token?) // not directly returned for typing issues } -async fn refetch_calendar_for( - id: &str, - pool: &PgPool, -) -> Result<(DateTime, Vec), Box> { +async fn refetch_calendar_for(id: &str, pool: &PgPool) -> Result<(DateTime, Vec), Box> { // Make OAuth2 secured request let oauth_token = fetch_oauth_token().await?; let events: Vec = reqwest::Client::new() - .get(format!( - "https://review.campus.tum.de/RSYSTEM/co/connectum/api/rooms/{id}/calendar" - )) + .get(format!("https://review.campus.tum.de/RSYSTEM/co/connectum/api/rooms/{id}/calendars")) .bearer_auth(oauth_token.access_token().secret().clone()) .send() .await? diff --git a/server/main-api/src/main.rs b/server/main-api/src/main.rs index 1abfae1d8..4228133e3 100644 --- a/server/main-api/src/main.rs +++ b/server/main-api/src/main.rs @@ -61,10 +61,10 @@ async fn main() -> Result<(), Box> { .init(); let uri = connection_string(); let pool = PgPoolOptions::new().connect(&uri).await?; - info!("setting up the database"); - setup::database::setup_database(&pool).await?; - info!("setting up meilisearch"); - setup::meilisearch::setup_meilisearch().await?; + //info!("setting up the database"); + //setup::database::setup_database(&pool).await?; + //info!("setting up meilisearch"); + //setup::meilisearch::setup_meilisearch().await?; debug!("setting up metrics"); let labels = HashMap::from([(