11use crate :: { config:: db:: Pool , constants, models:: response:: ResponseBody , utils:: token_utils} ;
22use actix_service:: { Service , Transform } ;
33use actix_web:: {
4+ http:: { Method } ,
45 dev:: { ServiceRequest , ServiceResponse } ,
56 Error , HttpResponse ,
67} ;
@@ -12,6 +13,7 @@ use std::{
1213 pin:: Pin ,
1314 task:: { Context , Poll } ,
1415} ;
16+ use actix_web:: http:: header:: { HeaderName , HeaderValue } ;
1517
1618pub struct Authentication ;
1719
@@ -55,35 +57,42 @@ where
5557 let mut authenticate_pass: bool = false ;
5658
5759 // Bypass some account routes
58- for ignore_route in constants:: IGNORE_ROUTES . iter ( ) {
59- if req. path ( ) . starts_with ( ignore_route) {
60- authenticate_pass = true ;
60+ debug ! ( "{:?}" , req. head_mut( ) . headers( ) ) ;
61+ let headers = req. headers_mut ( ) ;
62+ headers. append ( HeaderName :: from_static ( "content-length" ) , HeaderValue :: from_static ( "true" ) ) ;
63+ if Method :: OPTIONS == * req. method ( ) {
64+ authenticate_pass = true ;
65+ } else {
66+ for ignore_route in constants:: IGNORE_ROUTES . iter ( ) {
67+ debug ! ( "route:{}" , ignore_route) ;
68+ if req. path ( ) . starts_with ( ignore_route) {
69+ authenticate_pass = true ;
70+ }
6171 }
62- }
63-
64- if let Some ( pool) = req. app_data :: < Pool > ( ) {
65- info ! ( "Connecting to database..." ) ;
66- if let Some ( authen_header) = req. headers_mut ( ) . get ( constants:: AUTHORIZATION ) {
67- info ! ( "Parsing authorization header..." ) ;
68- if let Ok ( authen_str) = authen_header. to_str ( ) {
69- if authen_str. starts_with ( "bearer" ) || authen_str. starts_with ( "Bearer" ) {
70- info ! ( "Parsing token..." ) ;
71- let token = authen_str[ 6 ..authen_str. len ( ) ] . trim ( ) ;
72- if let Ok ( token_data) = token_utils:: decode_token ( token. to_string ( ) ) {
73- info ! ( "Decoding token..." ) ;
74- if token_utils:: verify_token ( & token_data, & pool) . is_ok ( ) {
75- info ! ( "Valid token" ) ;
76- authenticate_pass = true ;
77- } else {
78- error ! ( "Invalid token" ) ;
72+ if !authenticate_pass {
73+ if let Some ( pool) = req. app_data :: < Pool > ( ) {
74+ info ! ( "Connecting to database..." ) ;
75+ if let Some ( authen_header) = req. headers_mut ( ) . get ( constants:: AUTHORIZATION ) {
76+ info ! ( "Parsing authorization header..." ) ;
77+ if let Ok ( authen_str) = authen_header. to_str ( ) {
78+ if authen_str. starts_with ( "bearer" ) || authen_str. starts_with ( "Bearer" ) {
79+ info ! ( "Parsing token..." ) ;
80+ let token = authen_str[ 6 ..authen_str. len ( ) ] . trim ( ) ;
81+ if let Ok ( token_data) = token_utils:: decode_token ( token. to_string ( ) ) {
82+ info ! ( "Decoding token..." ) ;
83+ if token_utils:: verify_token ( & token_data, & pool) . is_ok ( ) {
84+ info ! ( "Valid token" ) ;
85+ authenticate_pass = true ;
86+ } else {
87+ error ! ( "Invalid token" ) ;
88+ }
89+ }
7990 }
8091 }
8192 }
8293 }
8394 }
8495 }
85-
86- error ! ( "{}" , constants:: MESSAGE_PROCESS_TOKEN_ERROR ) ;
8796 if authenticate_pass {
8897 let fut = self . service . call ( req) ;
8998 Box :: pin ( async move {
0 commit comments