@@ -12,10 +12,10 @@ use crate::schema::users;
12
12
#[ derive( Debug , Clone , Copy ) ]
13
13
pub struct CurrentUser ;
14
14
15
- #[ derive( Debug , Copy , Clone , Eq , PartialEq ) ]
15
+ #[ derive( Debug , Clone , Eq , PartialEq ) ]
16
16
pub enum AuthenticationSource {
17
17
SessionCookie ,
18
- ApiToken ,
18
+ ApiToken { auth_header : String } ,
19
19
}
20
20
21
21
impl Middleware for CurrentUser {
@@ -41,15 +41,18 @@ impl Middleware for CurrentUser {
41
41
} else {
42
42
// Otherwise, look for an `Authorization` header on the request
43
43
// and try to find a user in the database with a matching API token
44
- let user = if let Some ( headers) = req. headers ( ) . find ( "Authorization" ) {
45
- User :: find_by_api_token ( & conn, headers[ 0 ] ) . ok ( )
46
- } else {
47
- None
48
- } ;
49
- if let Some ( user) = user {
44
+ let user_auth = req. headers ( ) . find ( "Authorization" ) . and_then ( |headers| {
45
+ let auth_header = headers[ 0 ] . to_string ( ) ;
46
+
47
+ User :: find_by_api_token ( & conn, & auth_header)
48
+ . ok ( )
49
+ . map ( |user| ( AuthenticationSource :: ApiToken { auth_header } , user) )
50
+ } ) ;
51
+
52
+ if let Some ( ( api_token, user) ) = user_auth {
50
53
// Attach the `User` model from the database to the request
51
54
req. mut_extensions ( ) . insert ( user) ;
52
- req. mut_extensions ( ) . insert ( AuthenticationSource :: ApiToken ) ;
55
+ req. mut_extensions ( ) . insert ( api_token ) ;
53
56
}
54
57
}
55
58
0 commit comments