From e36eb7c74bde7fc9712351cb1dee675722f7f17d Mon Sep 17 00:00:00 2001 From: Pierre Hubert Date: Fri, 28 Apr 2023 09:05:27 +0200 Subject: [PATCH] Improve result page appearance --- Cargo.lock | 1 + Cargo.toml | 1 + assets/cover.css | 4 ++++ src/main.rs | 16 +++++++++++----- src/openid_primitives.rs | 20 ++++++++++++-------- templates/result.html | 1 + 6 files changed, 30 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 71c2f64..e29d1e5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1197,6 +1197,7 @@ dependencies = [ "rand", "reqwest", "serde", + "serde_json", "urlencoding", ] diff --git a/Cargo.toml b/Cargo.toml index cd6f9d5..56f1f42 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,7 @@ lazy_static = "1.4.0" actix-web = "4.3.1" askama = "0.12.0" serde = { version = "1.0.160", features = ["derive"] } +serde_json = "1.0.96" reqwest = { version = "0.11.16", features = ["json"] } urlencoding = "2.1.2" futures-util = "0.3.28" diff --git a/assets/cover.css b/assets/cover.css index 9039d1f..4343926 100644 --- a/assets/cover.css +++ b/assets/cover.css @@ -26,6 +26,10 @@ body { } +main { + overflow-y: scroll; +} + /* * Header */ diff --git a/src/main.rs b/src/main.rs index f9849c0..7faee55 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ +use actix_web::middleware::Logger; use actix_web::{get, web, App, HttpResponse, HttpServer}; use askama::Template; -use actix_web::middleware::Logger; use oidc_test_client::app_config::AppConfig; use oidc_test_client::openid_primitives::OpenIDConfig; @@ -112,7 +112,7 @@ async fn redirect(remote_ip: RemoteIP, query: web::Query) -> Http }; // Query token endpoint - let token = match config + let (token, token_str) = match config .request_token( &AppConfig::get().client_id, &AppConfig::get().client_secret, @@ -129,7 +129,7 @@ async fn redirect(remote_ip: RemoteIP, query: web::Query) -> Http }; // Query userinfo endpoint - let user_info = match config.request_user_info(&token).await { + let (_user_info, user_info_str) = match config.request_user_info(&token).await { Ok(t) => t, Err(e) => { log::error!("Failed to retrieve user info! {}", e); @@ -139,8 +139,14 @@ async fn redirect(remote_ip: RemoteIP, query: web::Query) -> Http HttpResponse::Ok().content_type("text/html").body( ResultTemplate { - token: format!("{:#?}", token), - user_info: format!("{:#?}", user_info), + token: serde_json::to_string_pretty( + &serde_json::from_str::(&token_str).unwrap(), + ) + .unwrap(), + user_info: serde_json::to_string_pretty( + &serde_json::from_str::(&user_info_str).unwrap(), + ) + .unwrap(), } .render() .unwrap(), diff --git a/src/openid_primitives.rs b/src/openid_primitives.rs index 80e178a..7518012 100644 --- a/src/openid_primitives.rs +++ b/src/openid_primitives.rs @@ -62,7 +62,7 @@ impl OpenIDConfig { client_secret: &str, code: &str, redirect_uri: &str, - ) -> Res { + ) -> Res<(TokenResponse, String)> { let authorization = BASE64_STANDARD.encode(format!("{}:{}", client_id, client_secret)); let mut params = HashMap::new(); @@ -70,24 +70,28 @@ impl OpenIDConfig { params.insert("code", code); params.insert("redirect_uri", redirect_uri); - Ok(reqwest::Client::new() + let response = reqwest::Client::new() .post(&self.token_endpoint) .header("Authorization", format!("Basic {authorization}")) .form(¶ms) .send() .await? - .json() - .await?) + .text() + .await?; + + Ok((serde_json::from_str(&response)?, response)) } /// Query the UserInfo endpoint - pub async fn request_user_info(&self, token: &TokenResponse) -> Res { - Ok(reqwest::Client::new() + pub async fn request_user_info(&self, token: &TokenResponse) -> Res<(UserInfo, String)> { + let response = reqwest::Client::new() .get(&self.userinfo_endpoint) .header("Authorization", format!("Bearer {}", token.access_token)) .send() .await? - .json() - .await?) + .text() + .await?; + + Ok((serde_json::from_str(&response)?, response)) } } diff --git a/templates/result.html b/templates/result.html index 755eb75..9761c42 100644 --- a/templates/result.html +++ b/templates/result.html @@ -4,6 +4,7 @@