Spaces:
Sleeping
Sleeping
Download Backend/fastapi/security/credentials.py from nktforsure04org/private: direct link, hf CLI and curl.
- Browser
- Download file 981 Bytes
-
https://huggingface.co/spaces/nktforsure04org/private/resolve/main/Backend/fastapi/security/credentials.py
- Command line
-
hf download hf://spaces/nktforsure04org/private/Backend/fastapi/security/credentials.py
-
curl -L -o credentials.py https://huggingface.co/spaces/nktforsure04org/private/resolve/main/Backend/fastapi/security/credentials.py
981 Bytes
| from fastapi import HTTPException, Request | |
| from fastapi.security import HTTPBearer | |
| from Backend.config import Telegram | |
| from typing import Optional | |
| import hashlib | |
| ADMIN_PASSWORD_HASH = hashlib.sha256(Telegram.ADMIN_PASSWORD.encode()).hexdigest() | |
| security = HTTPBearer(auto_error=False) | |
| def verify_password(password: str) -> bool: | |
| return hashlib.sha256(password.encode()).hexdigest() == ADMIN_PASSWORD_HASH | |
| def verify_credentials(username: str, password: str) -> bool: | |
| return username == Telegram.ADMIN_USERNAME and verify_password(password) | |
| def is_authenticated(request: Request) -> bool: | |
| return request.session.get("authenticated", False) | |
| def require_auth(request: Request): | |
| if not is_authenticated(request): | |
| raise HTTPException(status_code=401, detail="Authentication required") | |
| return True | |
| def get_current_user(request: Request) -> Optional[str]: | |
| if is_authenticated(request): | |
| return request.session.get("username") | |
| return None | |