Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /olaflaitinen_Promethium /src /promethium /api /deps /auth.py
| """ | |
| Authentication dependencies. | |
| """ | |
| from typing import Optional | |
| from fastapi import Depends, HTTPException, status | |
| from fastapi.security import OAuth2PasswordBearer | |
| from jose import JWTError, jwt | |
| from sqlalchemy.ext.asyncio import AsyncSession | |
| from sqlalchemy.future import select | |
| from promethium.core.config import get_settings | |
| from promethium.core.database import get_db | |
| from promethium.api.models.user import User | |
| from promethium.api.schemas.auth import TokenPayload | |
| settings = get_settings() | |
| oauth2_scheme = OAuth2PasswordBearer(tokenUrl=f"{settings.API_PREFIX}/auth/login") | |
| async def get_current_user( | |
| token: str = Depends(oauth2_scheme), | |
| db: AsyncSession = Depends(get_db) | |
| ) -> User: | |
| """ | |
| Validate access token and return current user. | |
| """ | |
| credentials_exception = HTTPException( | |
| status_code=status.HTTP_401_UNAUTHORIZED, | |
| detail="Could not validate credentials", | |
| headers={"WWW-Authenticate": "Bearer"}, | |
| ) | |
| try: | |
| # Use a default secret key if not set in settings (SAFEGUARD for dev) | |
| secret_key = getattr(settings, "SECRET_KEY", "dev_secret_key_change_me") | |
| algorithm = getattr(settings, "ALGORITHM", "HS256") | |
| payload = jwt.decode(token, secret_key, algorithms=[algorithm]) | |
| user_id: str = payload.get("sub") | |
| if user_id is None: | |
| raise credentials_exception | |
| token_data = TokenPayload(sub=int(user_id)) | |
| except (JWTError, ValueError): | |
| raise credentials_exception | |
| user = await db.get(User, token_data.sub) | |
| if user is None: | |
| raise credentials_exception | |
| return user | |
| async def get_current_active_user( | |
| current_user: User = Depends(get_current_user) | |
| ) -> User: | |
| """ | |
| Ensure current user is active. | |
| """ | |
| if not current_user.is_active: | |
| raise HTTPException(status_code=400, detail="Inactive user") | |
| return current_user | |
| async def get_current_admin_user( | |
| current_user: User = Depends(get_current_active_user) | |
| ) -> User: | |
| """ | |
| Ensure current user is an admin. | |
| """ | |
| if current_user.role != "admin": | |
| raise HTTPException( | |
| status_code=status.HTTP_403_FORBIDDEN, | |
| detail="The user does not have enough privileges" | |
| ) | |
| return current_user | |
Xet Storage Details
- Size:
- 2.3 kB
- Xet hash:
- 9f5b75896ead7625e2ab0f99db253e2508f042c1a0167089c6e6a3350690ebc6
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.