Spaces:
Sleeping
Sleeping
File size: 1,260 Bytes
9953dc0 ae4fc51 9953dc0 432f137 9953dc0 d59647e 9953dc0 ae4fc51 9953dc0 ae4fc51 9953dc0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | from datetime import datetime, timedelta, timezone
from jose import JWTError, jwt, ExpiredSignatureError
from passlib.context import CryptContext
from app.config import *
settings = Settings()
SECRET_KEY = settings.jwt_secret
ALGORITHM="HS256"
ACCESS_TOKEN_EXPIRE_MINUTES = 10080 # 7 days
pwd_context = CryptContext(schemes=['argon2'], deprecated="auto")
def hash_password(password: str):
return pwd_context.hash(password)
def verify_password(plain_password, hashed_password):
return pwd_context.verify(plain_password, hashed_password)
def create_access_token(data: dict, expires_delta: timedelta | None = None):
to_encode = data.copy()
expire = datetime.now(timezone.utc) + (expires_delta or timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES))
to_encode.update({"exp": expire})
return jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
def decode_access_token(token: str):
try:
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
return payload
except ExpiredSignatureError:
# Raise explicitly so backend knows token expired
raise ExpiredSignatureError("Token Expired")
except JWTError:
# Any other JWT error (invalid signature, tampered, etc.)
return None |