Download Backend/fastapi/security/credentials.py from dhyeye/streamio: direct link, hf CLI and curl.
- Browser
- Download file 1.13 kB
-
https://huggingface.co/spaces/dhyeye/streamio/resolve/main/Backend/fastapi/security/credentials.py
- Command line
-
hf download hf://spaces/dhyeye/streamio/Backend/fastapi/security/credentials.py
-
curl -L -o credentials.py https://huggingface.co/spaces/dhyeye/streamio/resolve/main/Backend/fastapi/security/credentials.py
1.13 kB
| from fastapi import HTTPException, Request | |
| from starlette.status import HTTP_401_UNAUTHORIZED | |
| from Backend.helper.passwords import verify_password | |
| from Backend.helper.settings_manager import SettingsManager | |
| #----- Match a username/password pair against the stored admin credentials | |
| def verify_credentials(username: str, password: str) -> bool: | |
| s = SettingsManager.current() | |
| return username == s.admin_username and verify_password(password, s.admin_password) | |
| #----- Whether the session carries a valid authentication flag | |
| def is_authenticated(request: Request) -> bool: | |
| return bool(request.session.get("authenticated")) | |
| #----- Logged-in username from the session, or None | |
| def get_current_user(request: Request) -> str | None: | |
| if is_authenticated(request): | |
| return request.session.get("username", "admin") | |
| return None | |
| #----- FastAPI dependency: raise 401 (redirected to /login) when unauthenticated | |
| async def require_auth(request: Request) -> bool: | |
| if not is_authenticated(request): | |
| raise HTTPException(status_code=HTTP_401_UNAUTHORIZED, detail="Not authenticated") | |
| return True | |