Implement server #3

Merged
pierre merged 5 commits from feat-server into master 2023-04-28 07:15:10 +00:00
6 changed files with 30 additions and 13 deletions
Showing only changes of commit e36eb7c74b - Show all commits

1
Cargo.lock generated
View File

@ -1197,6 +1197,7 @@ dependencies = [
"rand", "rand",
"reqwest", "reqwest",
"serde", "serde",
"serde_json",
"urlencoding", "urlencoding",
] ]

View File

@ -13,6 +13,7 @@ lazy_static = "1.4.0"
actix-web = "4.3.1" actix-web = "4.3.1"
askama = "0.12.0" askama = "0.12.0"
serde = { version = "1.0.160", features = ["derive"] } serde = { version = "1.0.160", features = ["derive"] }
serde_json = "1.0.96"
reqwest = { version = "0.11.16", features = ["json"] } reqwest = { version = "0.11.16", features = ["json"] }
urlencoding = "2.1.2" urlencoding = "2.1.2"
futures-util = "0.3.28" futures-util = "0.3.28"

View File

@ -26,6 +26,10 @@ body {
} }
main {
overflow-y: scroll;
}
/* /*
* Header * Header
*/ */

View File

@ -1,6 +1,6 @@
use actix_web::middleware::Logger;
use actix_web::{get, web, App, HttpResponse, HttpServer}; use actix_web::{get, web, App, HttpResponse, HttpServer};
use askama::Template; use askama::Template;
use actix_web::middleware::Logger;
use oidc_test_client::app_config::AppConfig; use oidc_test_client::app_config::AppConfig;
use oidc_test_client::openid_primitives::OpenIDConfig; use oidc_test_client::openid_primitives::OpenIDConfig;
@ -112,7 +112,7 @@ async fn redirect(remote_ip: RemoteIP, query: web::Query<RedirectQuery>) -> Http
}; };
// Query token endpoint // Query token endpoint
let token = match config let (token, token_str) = match config
.request_token( .request_token(
&AppConfig::get().client_id, &AppConfig::get().client_id,
&AppConfig::get().client_secret, &AppConfig::get().client_secret,
@ -129,7 +129,7 @@ async fn redirect(remote_ip: RemoteIP, query: web::Query<RedirectQuery>) -> Http
}; };
// Query userinfo endpoint // 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, Ok(t) => t,
Err(e) => { Err(e) => {
log::error!("Failed to retrieve user info! {}", e); log::error!("Failed to retrieve user info! {}", e);
@ -139,8 +139,14 @@ async fn redirect(remote_ip: RemoteIP, query: web::Query<RedirectQuery>) -> Http
HttpResponse::Ok().content_type("text/html").body( HttpResponse::Ok().content_type("text/html").body(
ResultTemplate { ResultTemplate {
token: format!("{:#?}", token), token: serde_json::to_string_pretty(
user_info: format!("{:#?}", user_info), &serde_json::from_str::<serde_json::Value>(&token_str).unwrap(),
)
.unwrap(),
user_info: serde_json::to_string_pretty(
&serde_json::from_str::<serde_json::Value>(&user_info_str).unwrap(),
)
.unwrap(),
} }
.render() .render()
.unwrap(), .unwrap(),

View File

@ -62,7 +62,7 @@ impl OpenIDConfig {
client_secret: &str, client_secret: &str,
code: &str, code: &str,
redirect_uri: &str, redirect_uri: &str,
) -> Res<TokenResponse> { ) -> Res<(TokenResponse, String)> {
let authorization = BASE64_STANDARD.encode(format!("{}:{}", client_id, client_secret)); let authorization = BASE64_STANDARD.encode(format!("{}:{}", client_id, client_secret));
let mut params = HashMap::new(); let mut params = HashMap::new();
@ -70,24 +70,28 @@ impl OpenIDConfig {
params.insert("code", code); params.insert("code", code);
params.insert("redirect_uri", redirect_uri); params.insert("redirect_uri", redirect_uri);
Ok(reqwest::Client::new() let response = reqwest::Client::new()
.post(&self.token_endpoint) .post(&self.token_endpoint)
.header("Authorization", format!("Basic {authorization}")) .header("Authorization", format!("Basic {authorization}"))
.form(&params) .form(&params)
.send() .send()
.await? .await?
.json() .text()
.await?) .await?;
Ok((serde_json::from_str(&response)?, response))
} }
/// Query the UserInfo endpoint /// Query the UserInfo endpoint
pub async fn request_user_info(&self, token: &TokenResponse) -> Res<UserInfo> { pub async fn request_user_info(&self, token: &TokenResponse) -> Res<(UserInfo, String)> {
Ok(reqwest::Client::new() let response = reqwest::Client::new()
.get(&self.userinfo_endpoint) .get(&self.userinfo_endpoint)
.header("Authorization", format!("Bearer {}", token.access_token)) .header("Authorization", format!("Bearer {}", token.access_token))
.send() .send()
.await? .await?
.json() .text()
.await?) .await?;
Ok((serde_json::from_str(&response)?, response))
} }
} }

View File

@ -4,6 +4,7 @@
<style> <style>
.card { .card {
text-align: left; text-align: left;
margin-bottom: 20px;
} }
</style> </style>