diff options
-rw-r--r-- | src/extractors/auth_token.rs | 31 |
1 files changed, 22 insertions, 9 deletions
diff --git a/src/extractors/auth_token.rs b/src/extractors/auth_token.rs index 6ad2a45..c3f781c 100644 --- a/src/extractors/auth_token.rs +++ b/src/extractors/auth_token.rs @@ -1,9 +1,14 @@ -use std::future::{ Ready, ready }; -use actix_web::{web, FromRequest, Error as ActixWebError, HttpRequest, dev::Payload, http, http::header::HeaderValue, error::ErrorUnauthorized}; -use serde::{Serialize, Deserialize}; -use jsonwebtoken:: {decode, DecodingKey, errors::Error as JwtError, Algorithm, Validation, TokenData}; use crate::auth::Claims; use crate::AppState; +use actix_web::{ + dev::Payload, error::ErrorUnauthorized, http, http::header::HeaderValue, web, + Error as ActixWebError, FromRequest, HttpRequest, +}; +use jsonwebtoken::{ + decode, errors::Error as JwtError, Algorithm, DecodingKey, TokenData, Validation, +}; +use serde::{Deserialize, Serialize}; +use std::future::{ready, Ready}; #[derive(Serialize, Deserialize, Debug)] pub struct AuthenticationToken { @@ -18,13 +23,19 @@ impl FromRequest for AuthenticationToken { // get auth token from the authorization header let auth_header: &HeaderValue = match req.headers().get(http::header::AUTHORIZATION) { Some(res) => res, - None => { return ready(Err(ErrorUnauthorized("No authorization token given")))}, + None => return ready(Err(ErrorUnauthorized("No authorization token given"))), }; let auth_token: String = auth_header.to_str().unwrap_or("").to_string(); // check errors later - // stop empty and weird (ascii, chinese...) auth_token strings: - if auth_token.is_empty() { return ready(Err(ErrorUnauthorized("Invalid auth token!")))} - let secret: String = req.app_data::<web::Data<AppState>>().unwrap().secret.to_string(); + // stop empty and weird (ascii, chinese...) auth_token strings: + if auth_token.is_empty() { + return ready(Err(ErrorUnauthorized("Invalid auth token!"))); + } + let secret: String = req + .app_data::<web::Data<AppState>>() + .unwrap() + .secret + .to_string(); // decode token with secret let decode: Result<TokenData<Claims>, JwtError> = decode::<Claims>( @@ -36,7 +47,9 @@ impl FromRequest for AuthenticationToken { println!("{}", auth_token); // return authenticationtoken match decode { - Ok(token) => ready(Ok(AuthenticationToken { id: token.claims.id })), + Ok(token) => ready(Ok(AuthenticationToken { + id: token.claims.id, + })), Err(_) => ready(Err(ErrorUnauthorized("Unauthorized!"))), } } |