Download src/utils/jwt_utils.py from asmaakbar/phase-iii: direct link, hf CLI and curl.
- Browser
- Download file 1.64 kB
-
https://huggingface.co/spaces/asmaakbar/phase-iii/resolve/main/src/utils/jwt_utils.py
- Command line
-
hf download hf://spaces/asmaakbar/phase-iii/src/utils/jwt_utils.py
-
curl -L -o jwt_utils.py https://huggingface.co/spaces/asmaakbar/phase-iii/resolve/main/src/utils/jwt_utils.py
1.64 kB
| from datetime import datetime, timedelta | |
| from typing import Optional | |
| from jose import JWTError, jwt | |
| from passlib.context import CryptContext | |
| from ..config import settings | |
| pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto") | |
| def verify_password(plain_password: str, hashed_password: str) -> bool: | |
| """Verify a plaintext password against a hashed password.""" | |
| return pwd_context.verify(plain_password, hashed_password) | |
| def get_password_hash(password: str) -> str: | |
| """Hash a plaintext password.""" | |
| return pwd_context.hash(password) | |
| def create_access_token(data: dict, expires_delta: Optional[timedelta] = None) -> str: | |
| """ | |
| Create a JWT access token. | |
| Args: | |
| data: Dictionary containing the data to encode in the token | |
| expires_delta: Optional timedelta for token expiration | |
| Returns: | |
| Encoded JWT token as string | |
| """ | |
| to_encode = data.copy() | |
| if expires_delta: | |
| expire = datetime.utcnow() + expires_delta | |
| else: | |
| expire = datetime.utcnow() + timedelta(minutes=settings.access_token_expire_minutes) | |
| to_encode.update({"exp": expire}) | |
| encoded_jwt = jwt.encode(to_encode, settings.secret_key, algorithm=settings.algorithm) | |
| return encoded_jwt | |
| def decode_access_token(token: str) -> Optional[dict]: | |
| """ | |
| Decode a JWT access token. | |
| Args: | |
| token: JWT token to decode | |
| Returns: | |
| Decoded token payload as dictionary or None if invalid | |
| """ | |
| try: | |
| payload = jwt.decode(token, settings.secret_key, algorithms=[settings.algorithm]) | |
| return payload | |
| except JWTError: | |
| return None |