@@ -35,6 +35,7 @@ use hmac::Hmac;
35
35
use jwt:: SignWithKey ;
36
36
use sha2:: Sha256 ;
37
37
use std:: io:: Result ;
38
+ use std:: time:: { SystemTime , UNIX_EPOCH } ;
38
39
39
40
#[ actix_web:: main]
40
41
async fn main ( ) -> Result < ( ) > {
@@ -222,8 +223,14 @@ async fn login(payload: Json<LoginPayload>) -> impl Responder {
222
223
. unwrap ( ) ;
223
224
224
225
if is_valid {
226
+ let expiry_date = SystemTime :: now ( )
227
+ . duration_since ( UNIX_EPOCH )
228
+ . unwrap ( )
229
+ . as_millis ( )
230
+ + 24 * 60 * 60 * 1000 ;
225
231
let claims = TokenClaims {
226
232
id : database_user. id ,
233
+ expiry_date,
227
234
} ;
228
235
let token_str = claims. sign_with_key ( & jwt_secret) . unwrap ( ) ;
229
236
@@ -816,27 +823,89 @@ mod tests {
816
823
use crate :: database:: DataBase ;
817
824
use crate :: get_tickets;
818
825
use crate :: middleware:: validator;
819
- use crate :: models:: NewSession ;
826
+ use crate :: models:: { NewSession , TokenClaims } ;
820
827
use crate :: schema:: sessions:: dsl:: sessions;
821
828
use actix_web:: http:: StatusCode ;
822
829
use actix_web:: { test, web} ;
823
830
use actix_web_httpauth:: middleware:: HttpAuthentication ;
824
831
use diesel:: RunQueryDsl ;
832
+ use hmac:: { Hmac , Mac } ;
833
+ use jwt:: SignWithKey ;
825
834
use serial_test:: serial;
835
+ use sha2:: Sha256 ;
836
+ use std:: time:: { SystemTime , UNIX_EPOCH } ;
837
+
838
+ #[ actix_web:: test]
839
+ #[ serial]
840
+ async fn test_expiry_date ( ) {
841
+ setup_database ( ) ;
842
+
843
+ let jwt_secret: Hmac < Sha256 > = Hmac :: new_from_slice (
844
+ std:: env:: var ( "JWT_SECRET" )
845
+ . expect ( "JWT_SECRET must be set!" )
846
+ . as_bytes ( ) ,
847
+ )
848
+ . unwrap ( ) ;
849
+
850
+ let expiry_date = SystemTime :: now ( )
851
+ . duration_since ( UNIX_EPOCH )
852
+ . unwrap ( )
853
+ . as_millis ( )
854
+ // 24 hours in present
855
+ - 24 * 60 * 60 * 1000 ;
856
+ let claims = TokenClaims { id : 1 , expiry_date } ;
857
+ let token_str = claims. sign_with_key ( & jwt_secret) . unwrap ( ) ;
858
+
859
+ let mut db = DataBase :: new ( ) ;
860
+ diesel:: insert_into ( sessions)
861
+ . values ( NewSession {
862
+ token : token_str. clone ( ) ,
863
+ } )
864
+ . execute ( & mut db. connection )
865
+ . unwrap ( ) ;
866
+
867
+ let app = test:: init_service (
868
+ App :: new ( ) . service (
869
+ web:: scope ( "" )
870
+ . wrap ( HttpAuthentication :: bearer ( validator) )
871
+ . service ( get_tickets) ,
872
+ ) ,
873
+ )
874
+ . await ;
875
+ let req = TestRequest :: get ( )
876
+ . uri ( "/tickets" )
877
+ . insert_header ( ( "Authorization" , format ! ( "Bearer {token_str}" ) ) )
878
+ . to_request ( ) ;
879
+
880
+ let response = test:: call_service ( & app, req) . await ;
881
+
882
+ assert_eq ! ( response. status( ) . as_u16( ) , StatusCode :: UNAUTHORIZED ) ;
883
+ }
826
884
827
885
#[ actix_web:: test]
828
886
#[ serial]
829
887
async fn test_middleware ( ) {
830
888
setup_database ( ) ;
831
889
832
- // bearer for "123"
833
- let bearer_token =
834
- "eyJhbGciOiJIUzI1NiJ9.eyJpZCI6MX0.oi92tHHWj5HdQO8Hd9vIYD6suTWosoiBnpdRBIcNGpM" ;
890
+ let jwt_secret: Hmac < Sha256 > = Hmac :: new_from_slice (
891
+ std:: env:: var ( "JWT_SECRET" )
892
+ . expect ( "JWT_SECRET must be set!" )
893
+ . as_bytes ( ) ,
894
+ )
895
+ . unwrap ( ) ;
896
+
897
+ let expiry_date = SystemTime :: now ( )
898
+ . duration_since ( UNIX_EPOCH )
899
+ . unwrap ( )
900
+ . as_millis ( )
901
+ + 24 * 60 * 60 * 60 * 1000 ;
902
+ let claims = TokenClaims { id : 1 , expiry_date } ;
903
+ let token_str = claims. sign_with_key ( & jwt_secret) . unwrap ( ) ;
835
904
836
905
let mut db = DataBase :: new ( ) ;
837
906
diesel:: insert_into ( sessions)
838
907
. values ( NewSession {
839
- token : bearer_token . to_string ( ) ,
908
+ token : token_str . clone ( ) ,
840
909
} )
841
910
. execute ( & mut db. connection )
842
911
. unwrap ( ) ;
@@ -851,7 +920,7 @@ mod tests {
851
920
. await ;
852
921
let req = TestRequest :: get ( )
853
922
. uri ( "/tickets" )
854
- . insert_header ( ( "Authorization" , format ! ( "Bearer {bearer_token }" ) ) )
923
+ . insert_header ( ( "Authorization" , format ! ( "Bearer {token_str }" ) ) )
855
924
. to_request ( ) ;
856
925
857
926
let response = test:: call_service ( & app, req) . await ;
0 commit comments