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("/pull", get(git_pull))
.route("/auth/callback", get(oauth2_callback)) .route("/auth/callback", get(oauth2_callback))
.route("/auth/login", get(oauth2_login)) .route("/auth/login", get(oauth2_login))
.route("/auth/error", get(auth_error))
.with_state(state); .with_state(state);
// run our app with hyper // run our app with hyper
@ -203,7 +204,7 @@ async fn oauth2_callback(
let mut cookie = Cookie::new("token", res.id.to_string()); let mut cookie = Cookie::new("token", res.id.to_string());
cookie.set_same_site(SameSite::None); cookie.set_same_site(SameSite::None);
cookie.set_secure(false); cookie.set_secure(true);
cookie.set_path("/"); cookie.set_path("/");
// cookie.set_domain("localhost:3000"); // cookie.set_domain("localhost:3000");
// cookie.set_http_only(Some(false)); // cookie.set_http_only(Some(false));
@ -257,11 +258,20 @@ impl FromRequestParts<AppState> for UserLoggedIn {
Err(( Err((
StatusCode::TEMPORARY_REDIRECT, StatusCode::TEMPORARY_REDIRECT,
jar, 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 // basic handler that responds with a static string
async fn root(_user: UserLoggedIn) -> Html<&'static str> { async fn root(_user: UserLoggedIn) -> Html<&'static str> {
@ -313,7 +323,7 @@ async fn stop(_user: UserLoggedIn) -> Redirect {
async fn status() -> Result<String, StatusCode> { async fn status() -> Result<String, StatusCode> {
info!("Requested status"); info!("Requested status");
let mut output = tokio::process::Command::new("journalctl") let mut output = tokio::process::Command::new("journalctl")
.args(["-xeu", "botloc"]) .args(["--user", "-xeu", "botloc"])
.output() .output()
.await .await
// let mut output = child.wait_with_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("grant_type", "client_credentials");
form_data.insert("client_id", uid); form_data.insert("client_id", uid);
form_data.insert("client_secret", secret); form_data.insert("client_secret", secret);
let response = client let res = client
.post("https://api.intra.42.fr/oauth/token") .post("https://api.intra.42.fr/oauth/token")
.form(&form_data) .form(&form_data)
.send() .send()
.await .await
.wrap_err("Sending request to fetch 42 API token")?; .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) Ok(json)
} }
pub async fn new( pub async fn new(
@ -89,7 +91,7 @@ impl OauthClient {
)) ))
.build() .build()
.wrap_err("Failed to build URI")?; .wrap_err("Failed to build URI")?;
Ok(uri) Ok(dbg!(uri))
} }
pub async fn get_user_token( pub async fn get_user_token(
@ -107,14 +109,16 @@ impl OauthClient {
form_data.insert("client_secret", &self.client_secret); form_data.insert("client_secret", &self.client_secret);
form_data.insert("redirect_uri", &self.redirect_uri); form_data.insert("redirect_uri", &self.redirect_uri);
form_data.insert("grant_type", "authorization_code"); form_data.insert("grant_type", "authorization_code");
let response = self let res = self
.http .http
.post("https://api.intra.42.fr/oauth/token") .post("https://api.intra.42.fr/oauth/token")
.form(&form_data) .form(&form_data)
.send() .send()
.await .await
.wrap_err("Failed to get token for user")?; .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) Ok(json)
} }
@ -128,7 +132,7 @@ impl OauthClient {
let token = token let token = token
.map(IntoToken::get_token) .map(IntoToken::get_token)
.unwrap_or_else(|| self.token.get_token()); .unwrap_or_else(|| self.token.get_token());
let req = self let res = self
.http .http
.get(url) .get(url)
.query(qs) .query(qs)
@ -136,10 +140,9 @@ impl OauthClient {
.send() .send()
.await .await
.wrap_err("Failed to send request")?; .wrap_err("Failed to send request")?;
let json = req let text = res.text().await.wrap_err("API reponse to text")?;
.json() let json = serde_json::from_str(&text)
.await .wrap_err_with(|| format!("API response to json: {text}"))?;
.wrap_err("Failed to Deserialize response")?;
Ok(json) Ok(json)
} }
} }