diff options
Diffstat (limited to 'src/extract/auth.rs')
| -rw-r--r-- | src/extract/auth.rs | 10 |
1 files changed, 5 insertions, 5 deletions
diff --git a/src/extract/auth.rs b/src/extract/auth.rs index 1feb985..c603ee7 100644 --- a/src/extract/auth.rs +++ b/src/extract/auth.rs | |||
| @@ -4,12 +4,12 @@ use entity::users; | |||
| 4 | use headers::authorization::{Authorization, Bearer}; | 4 | use headers::authorization::{Authorization, Bearer}; |
| 5 | use sea_orm::EntityTrait; | 5 | use sea_orm::EntityTrait; |
| 6 | 6 | ||
| 7 | use crate::{ApiError, AppState, validate_jwt}; | 7 | use crate::{AppState, ClientError, ErrorResponse, validate_jwt}; |
| 8 | 8 | ||
| 9 | pub struct Auth(pub users::Model); | 9 | pub struct Auth(pub users::Model); |
| 10 | 10 | ||
| 11 | impl FromRequestParts<AppState> for Auth { | 11 | impl FromRequestParts<AppState> for Auth { |
| 12 | type Rejection = ApiError; | 12 | type Rejection = ErrorResponse; |
| 13 | 13 | ||
| 14 | async fn from_request_parts( | 14 | async fn from_request_parts( |
| 15 | parts: &mut Parts, | 15 | parts: &mut Parts, |
| @@ -19,15 +19,15 @@ impl FromRequestParts<AppState> for Auth { | |||
| 19 | TypedHeader::<Authorization<Bearer>>::from_request_parts(parts, state).await?; | 19 | TypedHeader::<Authorization<Bearer>>::from_request_parts(parts, state).await?; |
| 20 | 20 | ||
| 21 | let jwt_claims = validate_jwt(token_header.token(), &state.secret) | 21 | let jwt_claims = validate_jwt(token_header.token(), &state.secret) |
| 22 | .map_err(|_| ApiError::NotAuthorized)?; | 22 | .map_err(|_| ClientError::NotAuthorized)?; |
| 23 | 23 | ||
| 24 | let user = users::Entity::find_by_id(jwt_claims.sub) | 24 | let user = users::Entity::find_by_id(jwt_claims.sub) |
| 25 | .one(&state.db) | 25 | .one(&state.db) |
| 26 | .await? | 26 | .await? |
| 27 | .ok_or(ApiError::NotAuthorized)?; | 27 | .ok_or(ClientError::NotAuthorized)?; |
| 28 | 28 | ||
| 29 | if jwt_claims.iat < user.password_issue_date.and_utc().timestamp() { | 29 | if jwt_claims.iat < user.password_issue_date.and_utc().timestamp() { |
| 30 | return Err(ApiError::NotAuthorized); | 30 | return Err(ClientError::NotAuthorized.into()); |
| 31 | } | 31 | } |
| 32 | 32 | ||
| 33 | Ok(Auth(user)) | 33 | Ok(Auth(user)) |
