55import pyotp
66from app .core .config import settings
77from app .core .exceptions import AppException
8+ from app .core .logger import logger
9+
810
911pwd_context = CryptContext (schemes = ["bcrypt" ], deprecated = "auto" )
1012_BCRYPT_MAX_LEN = 72
1113
1214
13- def _normalize_password (password : str ) -> str :
14- return password [:_BCRYPT_MAX_LEN ]
15+ def _normalize_password (password : str ) -> bytes :
16+ return password . encode ( "utf-8" ) [:_BCRYPT_MAX_LEN ]
1517
1618
1719def hash_password (password : str ) -> str :
1820 normalized = _normalize_password (password )
21+ logger .debug ("hashing password (normalized %s bytes)" , len (normalized ))
1922 return pwd_context .hash (normalized )
2023
2124
2225def verify_password (password : str , hashed : str ) -> bool :
2326 normalized = _normalize_password (password )
24- return pwd_context .verify (normalized , hashed )
27+ result = pwd_context .verify (normalized , hashed )
28+ logger .debug ("password verification result: %s" , result )
29+ return result
2530
26- def Get_expiry_time ()-> int :
27- if settings .environment == "dev" :
28- expirty = 60 * 60 * 24 * 7
29- else :
30- expirty = 60 * 60 * 24
31- return expirty
3231
32+ def Get_expiry_time () -> int :
33+ if settings .environment == "dev" :
34+ expiry = 60 * 60 * 24 * 7
35+ else :
36+ expiry = 60 * 60 * 24
37+ return expiry
3338
3439
35- def create_acces_mobile_token (session_id :str ) -> str :
36- payload : dict [str ,Any ]= {
40+ def create_acces_mobile_token (session_id : str ) -> str :
41+ payload : dict [str , Any ] = {
3742 "session_id" : session_id ,
38- "exp" : int ((datetime .now (timezone .utc ) + timedelta (seconds = Get_expiry_time ())).timestamp ())
43+ "exp" : int (
44+ (datetime .now (timezone .utc ) + timedelta (seconds = Get_expiry_time ())).timestamp ()
45+ ),
3946 }
4047 return jwt .encode (payload , key = settings .jwt_secret , algorithm = settings .jwt_algorithm )
4148
49+
4250def decode_access_mobile_token (token : str ) -> dict [str , Any ]:
4351 try :
4452 payload = jwt .decode (token , key = settings .jwt_secret , algorithms = [settings .jwt_algorithm ])
@@ -48,13 +56,17 @@ def decode_access_mobile_token(token: str) -> dict[str, Any]:
4856 except jwt .InvalidTokenError :
4957 raise AppException .unauthorized ("Invalid token" )
5058
51- def create_refresh_mobile_token (session_id :str ) -> str :
52- payload :dict [str ,Any ]= {
59+
60+ def create_refresh_mobile_token (session_id : str ) -> str :
61+ payload : dict [str , Any ] = {
5362 "session_id" : session_id ,
54- "exp" : int ((datetime .now (timezone .utc ) + timedelta (seconds = Get_expiry_time ()* 4 )).timestamp ())
63+ "exp" : int (
64+ (datetime .now (timezone .utc ) + timedelta (seconds = Get_expiry_time () * 4 )).timestamp ()
65+ ),
5566 }
5667 return jwt .encode (payload , key = settings .jwt_secret , algorithm = settings .jwt_algorithm )
5768
69+
5870def decode_refresh_mobile_token (token : str ) -> dict [str , Any ]:
5971 try :
6072 payload = jwt .decode (token , key = settings .jwt_secret , algorithms = [settings .jwt_algorithm ])
@@ -69,19 +81,22 @@ def create_totp_secret() -> str:
6981 return pyotp .random_base32 ()
7082
7183
72-
7384def get_totp_uri (secret : str , email : str ) -> str :
7485 totp = pyotp .TOTP (secret )
7586 return totp .provisioning_uri (name = email , issuer_name = settings .totp_issuer )
7687
88+
7789def verify_totp_token_with_window (secret : str , token : str , valid_window : int = 8 ) -> bool :
7890 totp = pyotp .TOTP (secret )
7991 return totp .verify (token , valid_window = valid_window )
8092
81- def generate_Acces_token_stuff (user_id :str ,role :str ) -> str :
82- payload :dict [str ,Any ]= {
93+
94+ def generate_Acces_token_stuff (user_id : str , role : str ) -> str :
95+ payload : dict [str , Any ] = {
8396 "user_id" : user_id ,
8497 "role" : role ,
85- "exp" : int ((datetime .now (timezone .utc ) + timedelta (seconds = Get_expiry_time ())).timestamp ())
98+ "exp" : int (
99+ (datetime .now (timezone .utc ) + timedelta (seconds = Get_expiry_time ())).timestamp ()
100+ ),
86101 }
87102 return jwt .encode (payload , key = settings .jwt_secret , algorithm = settings .jwt_algorithm )
0 commit comments