@@ -10,11 +10,12 @@ use axum::{
10
10
use error_set:: ErrContext ;
11
11
use std:: sync:: Arc ;
12
12
13
+ const COMPONENT : & str = "JWT_MIDDLEWARE" ;
13
14
const AUTHORIZATION : & str = "authorization" ;
14
15
const BEARER : & str = "Bearer " ;
15
16
const UNAUTHORIZED : StatusCode = StatusCode :: UNAUTHORIZED ;
16
17
17
- const UNAUTHORIZED_PATHS : & [ & str ] = & [
18
+ const PUBLIC_PATHS : & [ & str ] = & [
18
19
"/" ,
19
20
"/metrics" ,
20
21
"/ping" ,
@@ -29,17 +30,19 @@ pub async fn jwt_auth(
29
30
mut request : Request < Body > ,
30
31
next : Next ,
31
32
) -> Result < Response , StatusCode > {
32
- if UNAUTHORIZED_PATHS . contains ( & request. uri ( ) . path ( ) ) {
33
+ if PUBLIC_PATHS . contains ( & request. uri ( ) . path ( ) ) {
33
34
return Ok ( next. run ( request) . await ) ;
34
35
}
35
36
36
37
let bearer = request
37
38
. headers ( )
38
39
. get ( AUTHORIZATION )
39
40
. ok_or ( UNAUTHORIZED )
40
- . with_error_context ( |_| "{COMPONENT} - missing or inaccessible Authorization header" ) ?
41
+ . with_error_context ( |_| {
42
+ format ! ( "{COMPONENT} - missing or inaccessible Authorization header" )
43
+ } ) ?
41
44
. to_str ( )
42
- . with_error_context ( |_| "{COMPONENT} - invalid authorization header format" )
45
+ . with_error_context ( |_| format ! ( "{COMPONENT} - invalid authorization header format" ) )
43
46
. map_err ( |_| UNAUTHORIZED ) ?;
44
47
45
48
if !bearer. starts_with ( BEARER ) {
@@ -48,12 +51,14 @@ pub async fn jwt_auth(
48
51
49
52
let jwt_token = & bearer[ BEARER . len ( ) ..] ;
50
53
let token_header = jsonwebtoken:: decode_header ( jwt_token)
51
- . with_error_context ( |_| "{COMPONENT} - failed to decode JWT header" )
54
+ . with_error_context ( |_| format ! ( "{COMPONENT} - failed to decode JWT header" ) )
52
55
. map_err ( |_| UNAUTHORIZED ) ?;
53
56
let jwt_claims = state
54
57
. jwt_manager
55
58
. decode ( jwt_token, token_header. alg )
56
- . with_error_context ( |_| "{COMPONENT} - failed to decode JWT with provided algorithm" )
59
+ . with_error_context ( |_| {
60
+ format ! ( "{COMPONENT} - failed to decode JWT with provided algorithm" )
61
+ } )
57
62
. map_err ( |_| UNAUTHORIZED ) ?;
58
63
if state
59
64
. jwt_manager
0 commit comments