@@ -5,6 +5,12 @@ use actix_web::HttpRequest;
55use crate :: errors:: ServiceError ;
66use crate :: models:: user:: { UserClaims , UserCompact , UserId } ;
77use crate :: services:: authentication:: JsonWebToken ;
8+ use crate :: web:: api:: v1:: extractors:: bearer_token:: BearerToken ;
9+
10+ // todo: refactor this after finishing migration to Axum.
11+ // - Extract service to handle Json Web Tokens: `new`, `sign_jwt`, `verify_jwt`.
12+ // - Move the rest to `src/web/api/v1/auth.rs`. It's a helper for Axum handlers
13+ // to get user id from request.
814
915pub struct Authentication {
1016 json_web_token : Arc < JsonWebToken > ,
@@ -30,13 +36,25 @@ impl Authentication {
3036 self . json_web_token . verify ( token) . await
3137 }
3238
33- /// Get Claims from Request
39+ // Begin ActixWeb
40+
41+ /// Get User id from `ActixWeb` Request
42+ ///
43+ /// # Errors
44+ ///
45+ /// This function will return an error if it can get claims from the request
46+ pub async fn get_user_id_from_actix_web_request ( & self , req : & HttpRequest ) -> Result < UserId , ServiceError > {
47+ let claims = self . get_claims_from_actix_web_request ( req) . await ?;
48+ Ok ( claims. user . user_id )
49+ }
50+
51+ /// Get Claims from `ActixWeb` Request
3452 ///
3553 /// # Errors
3654 ///
37- /// This function will return an `ServiceError::TokenNotFound` if `HeaderValue` is `None`
38- /// This function will pass through the `ServiceError::TokenInvalid` if unable to verify the JWT.
39- pub async fn get_claims_from_request ( & self , req : & HttpRequest ) -> Result < UserClaims , ServiceError > {
55+ /// - Return an `ServiceError::TokenNotFound` if `HeaderValue` is `None`.
56+ /// - Pass through the `ServiceError::TokenInvalid` if unable to verify the JWT.
57+ async fn get_claims_from_actix_web_request ( & self , req : & HttpRequest ) -> Result < UserClaims , ServiceError > {
4058 match req. headers ( ) . get ( "Authorization" ) {
4159 Some ( auth) => {
4260 let split: Vec < & str > = auth
@@ -55,13 +73,37 @@ impl Authentication {
5573 }
5674 }
5775
58- /// Get User id from Request
76+ // End ActixWeb
77+
78+ // Begin Axum
79+
80+ /// Get User id from bearer token
5981 ///
6082 /// # Errors
6183 ///
6284 /// This function will return an error if it can get claims from the request
63- pub async fn get_user_id_from_request ( & self , req : & HttpRequest ) -> Result < UserId , ServiceError > {
64- let claims = self . get_claims_from_request ( req ) . await ?;
85+ pub async fn get_user_id_from_bearer_token ( & self , maybe_token : & Option < BearerToken > ) -> Result < UserId , ServiceError > {
86+ let claims = self . get_claims_from_bearer_token ( maybe_token ) . await ?;
6587 Ok ( claims. user . user_id )
6688 }
89+
90+ /// Get Claims from bearer token
91+ ///
92+ /// # Errors
93+ ///
94+ /// This function will:
95+ ///
96+ /// - Return an `ServiceError::TokenNotFound` if `HeaderValue` is `None`.
97+ /// - Pass through the `ServiceError::TokenInvalid` if unable to verify the JWT.
98+ async fn get_claims_from_bearer_token ( & self , maybe_token : & Option < BearerToken > ) -> Result < UserClaims , ServiceError > {
99+ match maybe_token {
100+ Some ( token) => match self . verify_jwt ( & token. value ( ) ) . await {
101+ Ok ( claims) => Ok ( claims) ,
102+ Err ( e) => Err ( e) ,
103+ } ,
104+ None => Err ( ServiceError :: TokenNotFound ) ,
105+ }
106+ }
107+
108+ // End Axum
67109}
0 commit comments