@@ -12,10 +12,10 @@ use crate::schema::users;
1212#[ derive( Debug , Clone , Copy ) ]
1313pub struct CurrentUser ;
1414
15- #[ derive( Debug , Copy , Clone , Eq , PartialEq ) ]
15+ #[ derive( Debug , Clone , Eq , PartialEq ) ]
1616pub enum AuthenticationSource {
1717 SessionCookie ,
18- ApiToken ,
18+ ApiToken { auth_header : String } ,
1919}
2020
2121impl Middleware for CurrentUser {
@@ -42,18 +42,19 @@ impl Middleware for CurrentUser {
4242 } else {
4343 // Otherwise, look for an `Authorization` header on the request
4444 // and try to find a user in the database with a matching API token
45- let user = if let Some ( headers ) = req. headers ( ) . find ( "Authorization" ) {
46- User :: find_by_api_token ( & conn , headers[ 0 ] )
47- . optional ( )
48- . map_err ( |e| Box :: new ( e ) as Box < dyn Error + Send > ) ?
49- } else {
50- None
51- } ;
45+ let user_auth = req. headers ( ) . find ( "Authorization" ) . and_then ( |headers| {
46+ let auth_header = headers[ 0 ] . to_string ( ) ;
47+
48+ User :: find_by_api_token ( & conn , & auth_header )
49+ . ok ( )
50+ . map ( |user| ( AuthenticationSource :: ApiToken { auth_header } , user ) )
51+ } ) ;
5252 drop ( conn) ;
53- if let Some ( user) = user {
53+
54+ if let Some ( ( api_token, user) ) = user_auth {
5455 // Attach the `User` model from the database to the request
5556 req. mut_extensions ( ) . insert ( user) ;
56- req. mut_extensions ( ) . insert ( AuthenticationSource :: ApiToken ) ;
57+ req. mut_extensions ( ) . insert ( api_token ) ;
5758 }
5859 }
5960
0 commit comments