Spaces:
Sleeping
Sleeping
Download app/core/security.py from thabsheeeeeer/DocBrain-API: direct link, hf CLI and curl.
- Browser
- Download file 1.03 kB
-
https://huggingface.co/spaces/thabsheeeeeer/DocBrain-API/resolve/main/app/core/security.py
- Command line
-
hf download hf://spaces/thabsheeeeeer/DocBrain-API/app/core/security.py
-
curl -L -o security.py https://huggingface.co/spaces/thabsheeeeeer/DocBrain-API/resolve/main/app/core/security.py
1.03 kB
| import os | |
| from datetime import datetime, timedelta, timezone | |
| from dotenv import load_dotenv | |
| from passlib.context import CryptContext | |
| from jose import jwt | |
| load_dotenv() | |
| # ENV VARIABLES | |
| SECRET_KEY = os.getenv("SECRET_KEY") | |
| if not SECRET_KEY: | |
| raise ValueError("SECRET_KEY is missing in .env file") | |
| ALGORITHM = os.getenv("ALGORITHM", "HS256") | |
| ACCESS_TOKEN_EXPIRE_MINUTES = int(os.getenv("ACCESS_TOKEN_EXPIRE_MINUTES", 60)) | |
| # PASSWORD HASHING | |
| pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto") | |
| def hash_password(password: str): | |
| return pwd_context.hash(password) | |
| def verify_password(plain_password: str, hashed_password: str): | |
| return pwd_context.verify(plain_password, hashed_password) | |
| # JWT TOKEN | |
| def create_access_token(data: dict): | |
| to_encode = data.copy() | |
| expire = datetime.now(timezone.utc) + timedelta( | |
| minutes=ACCESS_TOKEN_EXPIRE_MINUTES | |
| ) | |
| to_encode.update({"exp": expire}) | |
| encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM) | |
| return encoded_jwt |