diff options
author | niliara-edu <nil.jimeno@estudiant.fjaverianas.com> | 2025-01-26 19:11:24 +0100 |
---|---|---|
committer | niliara-edu <nil.jimeno@estudiant.fjaverianas.com> | 2025-01-26 19:11:24 +0100 |
commit | c622eab39ac7dd9f794b5d60eb937e29c9b3bd6e (patch) | |
tree | a75067a661250a4ed74096929083948f65c01fdb /src/extractors/auth_token.rs | |
parent | 5891af7e8c1411029fe1ad9c6d3182f88bcf3dfd (diff) |
add api documentation
Diffstat (limited to 'src/extractors/auth_token.rs')
-rw-r--r-- | src/extractors/auth_token.rs | 21 |
1 files changed, 16 insertions, 5 deletions
diff --git a/src/extractors/auth_token.rs b/src/extractors/auth_token.rs index c3f781c..9728146 100644 --- a/src/extractors/auth_token.rs +++ b/src/extractors/auth_token.rs @@ -26,26 +26,37 @@ impl FromRequest for AuthenticationToken { 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() { + /* Get value as &str */ + let processed_header: &str = auth_header.to_str().unwrap_or(""); + if processed_header.is_empty() { return ready(Err(ErrorUnauthorized("Invalid auth token!"))); } + + /* Accept both bearer tokens and raw tokens */ + let splitted_header: Vec<&str> = processed_header.split_whitespace().collect(); + let auth_token: String = match splitted_header[0] { + "Bearer" => splitted_header[1].to_string(), + _ => processed_header.to_string(), + }; + + /* Get application secret */ let secret: String = req .app_data::<web::Data<AppState>>() .unwrap() .secret .to_string(); - // decode token with secret + /* Decode token with secret */ let decode: Result<TokenData<Claims>, JwtError> = decode::<Claims>( &auth_token, &DecodingKey::from_secret(secret.as_str().as_ref()), &Validation::new(Algorithm::HS256), ); + // for testing purposes println!("{}", auth_token); - // return authenticationtoken + + /* Return authenticationtoken */ match decode { Ok(token) => ready(Ok(AuthenticationToken { id: token.claims.id, |