From 7a130a06ff538dc7801e1c8530348fa82d249147 Mon Sep 17 00:00:00 2001 From: Maix0 Date: Sat, 28 Sep 2024 14:24:11 +0200 Subject: [PATCH] update: fixed cookies and set non tutors page --- src/main.rs | 16 +++++++++++++--- src/oauth2.rs | 23 +++++++++++++---------- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/src/main.rs b/src/main.rs index 7ba60e0..a2c30b7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -144,6 +144,7 @@ async fn main() { .route("/pull", get(git_pull)) .route("/auth/callback", get(oauth2_callback)) .route("/auth/login", get(oauth2_login)) + .route("/auth/error", get(auth_error)) .with_state(state); // run our app with hyper @@ -203,7 +204,7 @@ async fn oauth2_callback( let mut cookie = Cookie::new("token", res.id.to_string()); cookie.set_same_site(SameSite::None); - cookie.set_secure(false); + cookie.set_secure(true); cookie.set_path("/"); // cookie.set_domain("localhost:3000"); // cookie.set_http_only(Some(false)); @@ -257,11 +258,20 @@ impl FromRequestParts for UserLoggedIn { Err(( StatusCode::TEMPORARY_REDIRECT, jar, - Redirect::to("/auth/login"), + Redirect::to("/auth/error"), )) } } } +// basic handler that responds with a static string +async fn auth_error() -> Html<&'static str> { + info!("Request auth_error page"); + Html( + r#" +

Hello TUTORS ONLY :D

+ "#, + ) +} // basic handler that responds with a static string async fn root(_user: UserLoggedIn) -> Html<&'static str> { @@ -313,7 +323,7 @@ async fn stop(_user: UserLoggedIn) -> Redirect { async fn status() -> Result { info!("Requested status"); let mut output = tokio::process::Command::new("journalctl") - .args(["-xeu", "botloc"]) + .args(["--user", "-xeu", "botloc"]) .output() .await // let mut output = child.wait_with_output().await diff --git a/src/oauth2.rs b/src/oauth2.rs index 10c0182..816a85f 100644 --- a/src/oauth2.rs +++ b/src/oauth2.rs @@ -47,13 +47,15 @@ impl OauthClient { form_data.insert("grant_type", "client_credentials"); form_data.insert("client_id", uid); form_data.insert("client_secret", secret); - let response = client + let res = client .post("https://api.intra.42.fr/oauth/token") .form(&form_data) .send() .await .wrap_err("Sending request to fetch 42 API token")?; - let json: Token = response.json().await.wrap_err("API response to json")?; + let text = res.text().await.wrap_err("API reponse to text")?; + let json: Token = serde_json::from_str(&text) + .wrap_err_with(|| format!("API response to json: {text}"))?; Ok(json) } pub async fn new( @@ -89,7 +91,7 @@ impl OauthClient { )) .build() .wrap_err("Failed to build URI")?; - Ok(uri) + Ok(dbg!(uri)) } pub async fn get_user_token( @@ -107,14 +109,16 @@ impl OauthClient { form_data.insert("client_secret", &self.client_secret); form_data.insert("redirect_uri", &self.redirect_uri); form_data.insert("grant_type", "authorization_code"); - let response = self + let res = self .http .post("https://api.intra.42.fr/oauth/token") .form(&form_data) .send() .await .wrap_err("Failed to get token for user")?; - let json: Token = response.json().await.wrap_err("API response to json")?; + let text = res.text().await.wrap_err("API reponse to text")?; + let json: Token = serde_json::from_str(&text) + .wrap_err_with(|| format!("API response to json: {text}"))?; Ok(json) } @@ -128,7 +132,7 @@ impl OauthClient { let token = token .map(IntoToken::get_token) .unwrap_or_else(|| self.token.get_token()); - let req = self + let res = self .http .get(url) .query(qs) @@ -136,10 +140,9 @@ impl OauthClient { .send() .await .wrap_err("Failed to send request")?; - let json = req - .json() - .await - .wrap_err("Failed to Deserialize response")?; + let text = res.text().await.wrap_err("API reponse to text")?; + let json = serde_json::from_str(&text) + .wrap_err_with(|| format!("API response to json: {text}"))?; Ok(json) } }