aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2024-08-22 18:27:00 +0200
committermetamuffin <metamuffin@disroot.org>2024-08-22 18:27:00 +0200
commitf04bd4b788ddecdcb040379c70bc0bc9670c9605 (patch)
tree7eeeba4cd6e74d5fcd38b78e3068ddfef9fa3a36 /src
parent79f59dc9f2f82e2d21dd4738375c53a9f43a4d26 (diff)
downloadgnix-f04bd4b788ddecdcb040379c70bc0bc9670c9605.tar
gnix-f04bd4b788ddecdcb040379c70bc0bc9670c9605.tar.bz2
gnix-f04bd4b788ddecdcb040379c70bc0bc9670c9605.tar.zst
got some more random tokens
Diffstat (limited to 'src')
-rw-r--r--src/modules/auth/openid.rs47
1 files changed, 36 insertions, 11 deletions
diff --git a/src/modules/auth/openid.rs b/src/modules/auth/openid.rs
index fb1fbcb..a8d9d6e 100644
--- a/src/modules/auth/openid.rs
+++ b/src/modules/auth/openid.rs
@@ -40,7 +40,8 @@ impl NodeKind for OpenIDAuthKind {
#[derive(Deserialize)]
pub struct OpenIDAuth {
client_id: String,
- provider: String,
+ authorize_endpoint: String,
+ token_endpoint: String,
next: DynNode,
}
@@ -92,8 +93,13 @@ impl Node for OpenIDAuth {
};
let redirect_uri = redirect_uri(&request)?.to_string();
- token_request(
- &self.provider,
+ let OAuthTokenResponse {
+ access_token,
+ expires_in,
+ token_type,
+ id_token,
+ } = token_request(
+ &self.token_endpoint,
&self.client_id,
&redirect_uri,
&code,
@@ -102,8 +108,18 @@ impl Node for OpenIDAuth {
.await?;
let mut r = Response::new(BoxBody::<_, ServiceError>::new(
- format!("state={state:?}\ncode={code:?}\nreturn_path={return_path:?}")
- .map_err(|_| unreachable!()),
+ format!(
+ r#"Response:
+
+state={state:?}
+code={code:?}
+return_path={return_path:?}
+access_token={access_token:?}
+token_type={token_type:?}
+expires_in={expires_in:?}
+id_token={id_token:?}"#
+ )
+ .map_err(|_| unreachable!()),
));
r.headers_mut()
.insert(CONTENT_TYPE, HeaderValue::from_static("text/plain"));
@@ -137,8 +153,8 @@ impl Node for OpenIDAuth {
let redirect_uri = redirect_uri(&request)?.to_string();
let uri = format!(
- "{}/authorize?client_id={}&redirect_uri={}&state={}_{}&code_challenge={}&code_challenge_method=S256&response_type=code&scope=openid profile email",
- self.provider,
+ "{}?client_id={}&redirect_uri={}&state={}_{}&code_challenge={}&code_challenge_method=S256&response_type=code&scope=openid magic",
+ self.authorize_endpoint,
utf8_percent_encode(&self.client_id, NON_ALPHANUMERIC),
utf8_percent_encode(&redirect_uri, NON_ALPHANUMERIC),
hex::encode(verif_cipher),
@@ -180,8 +196,8 @@ async fn token_request(
redirect_uri: &str,
code: &str,
verifier: &str,
-) -> Result<(), ServiceError> {
- let url = Uri::from_str(&format!("{provider}/token")).unwrap();
+) -> Result<OAuthTokenResponse, ServiceError> {
+ let url = Uri::from_str(provider).unwrap();
let body = format!(
"client_id={}&redirect_uri={}&code={}&code_verifier={}&grant_type=authorization_code",
utf8_percent_encode(client_id, NON_ALPHANUMERIC),
@@ -214,6 +230,15 @@ async fn token_request(
let body = res.collect().await.unwrap().aggregate();
let mut buf = String::new();
body.reader().read_to_string(&mut buf).unwrap();
- eprintln!("{buf:?}");
- Ok(())
+ eprintln!("{buf}");
+ Ok(serde_json::from_str(&buf)
+ .map_err(|_| ServiceError::CustomStatic("invalid token response"))?)
+}
+
+#[derive(Debug, Deserialize)]
+struct OAuthTokenResponse {
+ access_token: String,
+ expires_in: i64,
+ token_type: String,
+ id_token: String,
}