Init
This commit is contained in:
+23
-15
@@ -91,30 +91,38 @@ pub async fn auth_middleware(
|
||||
mut req: Request<Body>,
|
||||
next: Next,
|
||||
) -> Response {
|
||||
// Extraction du JWT depuis le header Authorization
|
||||
let user: Option<CurrentUser> = match req
|
||||
// Extraction du JWT : d'abord via le header Authorization, sinon via la query string "token"
|
||||
let token = req
|
||||
.headers()
|
||||
.get(header::AUTHORIZATION)
|
||||
.and_then(|v| v.to_str().ok())
|
||||
.and_then(|auth_header| {
|
||||
if auth_header.starts_with("Bearer ") {
|
||||
Some(&auth_header[7..])
|
||||
Some(auth_header[7..].to_string())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.and_then(|token| verify_jwt(token, &app_state.config.jwt.secret).ok())
|
||||
{
|
||||
Some(claims) => app_state
|
||||
.repositories
|
||||
.user
|
||||
.get_by_id(claims.user_id)
|
||||
.await
|
||||
.ok()
|
||||
.flatten()
|
||||
.map(CurrentUser),
|
||||
None => None,
|
||||
};
|
||||
.or_else(|| {
|
||||
req.uri().query().and_then(|q| {
|
||||
form_urlencoded::parse(q.as_bytes())
|
||||
.find(|(key, _)| key == "token")
|
||||
.map(|(_, value)| value.into_owned())
|
||||
})
|
||||
});
|
||||
|
||||
let user: Option<CurrentUser> =
|
||||
match token.and_then(|t| verify_jwt(&t, &app_state.config.jwt.secret).ok()) {
|
||||
Some(claims) => app_state
|
||||
.repositories
|
||||
.user
|
||||
.get_by_id(claims.user_id)
|
||||
.await
|
||||
.ok()
|
||||
.flatten()
|
||||
.map(CurrentUser),
|
||||
None => None,
|
||||
};
|
||||
|
||||
// Mise à jour du RequestContext existant
|
||||
if let Some(user) = &user {
|
||||
|
||||
Reference in New Issue
Block a user