update: fixed cookies and set non tutors page

This commit is contained in:
Maix0 2024-09-28 14:24:11 +02:00
parent c98f01ebf7
commit 7a130a06ff
2 changed files with 26 additions and 13 deletions

View file

@ -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<AppState> 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#"
<h1>Hello TUTORS ONLY :D</h1>
"#,
)
}
// 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<String, StatusCode> {
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

View file

@ -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)
}
}