From 679f15b5ea948a345e959fc3eef1465d2a3b8b48 Mon Sep 17 00:00:00 2001 From: niliara-edu Date: Sat, 25 Jan 2025 13:31:09 +0100 Subject: . --- src/extractors/auth_token.rs | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) (limited to 'src/extractors') 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::>().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::>() + .unwrap() + .secret + .to_string(); // decode token with secret let decode: Result, JwtError> = decode::( @@ -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!"))), } } -- cgit v1.2.3