From adb0b7582c708fbaaf928392898e7a7f04321342 Mon Sep 17 00:00:00 2001 From: aumetra Date: Wed, 11 Dec 2024 23:04:00 +0100 Subject: [PATCH] prog --- Cargo.lock | 1 + lib/komainu/Cargo.toml | 1 + lib/komainu/src/authorize.rs | 0 lib/komainu/src/lib.rs | 19 +++++++++++++------ 4 files changed, 15 insertions(+), 6 deletions(-) create mode 100644 lib/komainu/src/authorize.rs diff --git a/Cargo.lock b/Cargo.lock index 816aaa2d8..9f4b17227 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4003,6 +4003,7 @@ dependencies = [ "strum", "thiserror 2.0.6", "tracing", + "url", ] [[package]] diff --git a/lib/komainu/Cargo.toml b/lib/komainu/Cargo.toml index 62cec1e0c..40311450e 100644 --- a/lib/komainu/Cargo.toml +++ b/lib/komainu/Cargo.toml @@ -15,6 +15,7 @@ sonic-rs.workspace = true strum.workspace = true thiserror.workspace = true tracing.workspace = true +url.workspace = true [dev-dependencies] serde_test.workspace = true diff --git a/lib/komainu/src/authorize.rs b/lib/komainu/src/authorize.rs new file mode 100644 index 000000000..e69de29bb diff --git a/lib/komainu/src/lib.rs b/lib/komainu/src/lib.rs index cd2bcfa59..7e3075624 100644 --- a/lib/komainu/src/lib.rs +++ b/lib/komainu/src/lib.rs @@ -9,6 +9,7 @@ use strum::AsRefStr; pub use self::error::{Error, Result}; pub use self::params::ParamStorage; +mod authorize; mod error; mod params; @@ -121,7 +122,6 @@ where let state = query.get("state").map(|state| &**state); let client = self.client_extractor.extract(client_id, None).await?; - if client.redirect_uri != *redirect_uri { debug!(?client_id, "redirect uri doesn't match"); return Err(Error::Unauthorized); @@ -168,17 +168,24 @@ where } pub async fn accept(self, user_id: AI::UserId, scopes: &[&str]) -> http::Response<()> { - // TODO: Call an issuer to issue an access token for a particular user - // Construct the callback url - // Construct a redirect HTTP response UwU - let code = self .auth_issuer .issue_code(user_id, self.client.client_id, scopes) .await .unwrap(); - todo!(); + let mut url = url::Url::parse(&self.client.redirect_uri).unwrap(); + url.query_pairs_mut().append_pair("code", &code); + + if let Some(state) = self.state { + url.query_pairs_mut().append_pair("state", state); + } + + http::Response::builder() + .header(http::header::LOCATION, url.as_str()) + .status(http::StatusCode::FOUND) + .body(()) + .unwrap() } pub async fn deny(self) -> http::Response<()> {