Spaces:
Sleeping
Sleeping
Download backend/auth.py from Rindhuja/intelligent-doc-api: direct link, hf CLI and curl.
- Browser
- Download file 833 Bytes
-
https://huggingface.co/spaces/Rindhuja/intelligent-doc-api/resolve/main/backend/auth.py
- Command line
-
hf download hf://spaces/Rindhuja/intelligent-doc-api/backend/auth.py
-
curl -L -o auth.py https://huggingface.co/spaces/Rindhuja/intelligent-doc-api/resolve/main/backend/auth.py
833 Bytes
| from passlib.context import CryptContext | |
| from jose import jwt,JWTError | |
| from datetime import datetime, timedelta, timezone | |
| import os | |
| SECRET_KEY = os.getenv("JWT_SECRET_KEY", "supersecretkey") | |
| ALGORITHM = "HS256" | |
| pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto") | |
| def hash_password(password: str): | |
| return pwd_context.hash(password) | |
| def verify_password(password: str, hashed: str): | |
| return pwd_context.verify(password, hashed) | |
| def create_token(data: dict): | |
| payload = data.copy() | |
| payload["exp"] = datetime.now(timezone.utc) + timedelta(hours=8) | |
| token = jwt.encode(payload, SECRET_KEY, algorithm=ALGORITHM) | |
| return token | |
| def decode_token(token: str): | |
| try: | |
| payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM]) | |
| return payload | |
| except: | |
| return None |