Spaces:
Running on Zero
Running on Zero
Download backend/auth.py from Lvvbaaal/legalgpt-api: direct link, hf CLI and curl.
- Browser
- Download file 1.27 kB
-
https://huggingface.co/spaces/Lvvbaaal/legalgpt-api/resolve/main/backend/auth.py
- Command line
-
hf download hf://spaces/Lvvbaaal/legalgpt-api/backend/auth.py
-
curl -L -o auth.py https://huggingface.co/spaces/Lvvbaaal/legalgpt-api/resolve/main/backend/auth.py
1.27 kB
| """ | |
| SaulGPT — Authentication Layer | |
| =============================== | |
| JWT token creation/verification + bcrypt password hashing. | |
| """ | |
| from datetime import datetime, timedelta, timezone | |
| from typing import Optional | |
| from jose import JWTError, jwt | |
| import bcrypt | |
| # Secret key — in production, move to .env | |
| SECRET_KEY = "saulgpt-dev-secret-change-in-production-2024" | |
| ALGORITHM = "HS256" | |
| ACCESS_TOKEN_EXPIRE_DAYS = 30 | |
| def hash_password(password: str) -> str: | |
| salt = bcrypt.gensalt() | |
| return bcrypt.hashpw(password.encode("utf-8"), salt).decode("utf-8") | |
| def verify_password(plain: str, hashed: str) -> bool: | |
| try: | |
| return bcrypt.checkpw(plain.encode("utf-8"), hashed.encode("utf-8")) | |
| except Exception: | |
| return False | |
| def create_token(user_id: int) -> str: | |
| expire = datetime.now(timezone.utc) + timedelta(days=ACCESS_TOKEN_EXPIRE_DAYS) | |
| payload = {"sub": str(user_id), "exp": expire} | |
| return jwt.encode(payload, SECRET_KEY, algorithm=ALGORITHM) | |
| def decode_token(token: str) -> Optional[int]: | |
| try: | |
| payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM]) | |
| user_id = payload.get("sub") | |
| if user_id is None: | |
| return None | |
| return int(user_id) | |
| except JWTError: | |
| return None | |