41 lines
1.4 KiB
Python
41 lines
1.4 KiB
Python
from datetime import datetime, timedelta
|
|
from typing import Optional
|
|
|
|
import jwt
|
|
import bcrypt
|
|
|
|
from app.config.settings import settings
|
|
|
|
|
|
def hash_password(password: str) -> str:
|
|
return bcrypt.hashpw(password.encode(), bcrypt.gensalt()).decode()
|
|
|
|
|
|
def verify_password(password: str, hashed: str) -> bool:
|
|
return bcrypt.checkpw(password.encode(), hashed.encode())
|
|
|
|
|
|
def create_access_token(user_id: str) -> str:
|
|
expire = datetime.utcnow() + timedelta(minutes=settings.jwt_access_expire_minutes)
|
|
payload = {"sub": user_id, "exp": expire, "type": "access"}
|
|
return jwt.encode(payload, settings.jwt_secret, algorithm=settings.jwt_algorithm)
|
|
|
|
|
|
def create_refresh_token(user_id: str) -> str:
|
|
expire = datetime.utcnow() + timedelta(days=settings.jwt_refresh_expire_days)
|
|
payload = {"sub": user_id, "exp": expire, "type": "refresh"}
|
|
return jwt.encode(payload, settings.jwt_secret, algorithm=settings.jwt_algorithm)
|
|
|
|
|
|
def create_reset_token(user_id: str) -> str:
|
|
expire = datetime.utcnow() + timedelta(hours=1)
|
|
payload = {"sub": user_id, "exp": expire, "type": "reset"}
|
|
return jwt.encode(payload, settings.jwt_secret, algorithm=settings.jwt_algorithm)
|
|
|
|
|
|
def decode_token(token: str) -> Optional[dict]:
|
|
try:
|
|
return jwt.decode(token, settings.jwt_secret, algorithms=[settings.jwt_algorithm])
|
|
except jwt.PyJWTError:
|
|
return None
|