fix auth middleware bug

This commit is contained in:
Xander Bil 2024-11-12 17:51:55 +01:00
parent 62ff6e321e
commit 4209e4b23c
No known key found for this signature in database
GPG key ID: EC9706B54A278598
2 changed files with 22 additions and 9 deletions

View file

@ -15,7 +15,7 @@ use axum::{
use config::Config; use config::Config;
use migration::{Migrator, MigratorTrait}; use migration::{Migrator, MigratorTrait};
use models::user::UserSession; use models::user::UserSession;
use routes::{auth, middelware::auth_guard, user::update_password}; use routes::{auth, middelware::auth, user::update_password};
use axum::response::IntoResponse; use axum::response::IntoResponse;
use sea_orm::Database; use sea_orm::Database;
@ -33,8 +33,8 @@ async fn index(Extension(user): Extension<UserSession>) -> impl IntoResponse {
<body> <body>
<h1> Good day {} </h1> <h1> Good day {} </h1>
<form action="{}/update_password" method="POST"> <form action="{}/update_password" method="POST">
<input type="text" name="password" placeholder="Enter some text" required /> <input name="password" type="password" placeholder="Enter password" required />
<button type="submit">Send Data</button> <button type="submit">Set password</button>
</form> </form>
</body> </body>
</html> </html>
@ -62,7 +62,7 @@ async fn main() {
.route("/", get(index)) .route("/", get(index))
.route("/index", get(index)) .route("/index", get(index))
.route("/update_password", post(update_password)) .route("/update_password", post(update_password))
.route_layer(axum::middleware::from_fn(auth_guard)) .route_layer(axum::middleware::from_fn(auth))
.route("/login", get(login)) .route("/login", get(login))
.route("/oauth/callback", get(callback)) .route("/oauth/callback", get(callback))
.with_state(state); .with_state(state);

View file

@ -2,7 +2,7 @@ use axum::{
extract::Request, extract::Request,
http::{HeaderMap, StatusCode}, http::{HeaderMap, StatusCode},
middleware::Next, middleware::Next,
response::{IntoResponse, Redirect}, response::{IntoResponse, Redirect, Response},
}; };
use axum_extra::extract::SignedCookieJar; use axum_extra::extract::SignedCookieJar;
@ -21,12 +21,22 @@ pub struct JwkSet {
pub keys: Vec<Jwk>, pub keys: Vec<Jwk>,
} }
pub async fn auth_guard( pub async fn auth(headers: HeaderMap, req: Request, next: Next) -> impl IntoResponse {
let redirect = req.uri().clone();
match auth_guard(headers, req, next).await {
Ok(r) => r,
Err(e) => {
eprintln!("{}", e);
Redirect::to(&format!("/login?redirect={}", redirect)).into_response()
}
}
}
async fn auth_guard(
headers: HeaderMap, headers: HeaderMap,
mut req: Request, mut req: Request,
next: Next, next: Next,
) -> Result<impl IntoResponse, ThisError> { ) -> Result<Response, ThisError> {
let redirect = req.uri().clone();
let jar = SignedCookieJar::from_headers(&headers, Config::get().cookies_key.clone()); let jar = SignedCookieJar::from_headers(&headers, Config::get().cookies_key.clone());
let token = jar let token = jar
@ -68,5 +78,8 @@ pub async fn auth_guard(
} }
} }
Ok(Redirect::to(&format!("/login?redirect={}", redirect)).into_response()) Err(ThisError::Generic {
code: StatusCode::UNAUTHORIZED,
message: "unauthorized".to_string(),
})
} }