Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /Aquiles-ai_Aquiles-Image /aquilesimage /auth /jwt_lo.py
| from aquilesimage.configs import load_config_app as load_aquiles_config | |
| from fastapi import Depends, HTTPException, Cookie | |
| from datetime import datetime, timedelta | |
| from pydantic import Field | |
| from pydantic_settings import BaseSettings, SettingsConfigDict | |
| import jwt | |
| import secrets | |
| class Settings(BaseSettings): | |
| model_config = SettingsConfigDict(extra="ignore") | |
| JWT_SECRET: str = Field( | |
| default_factory=lambda: secrets.token_urlsafe(32), | |
| description="Secret key to sign JWT" | |
| ) | |
| ALGORITHM: str = Field("HS256", description="JWT signature algorithm") | |
| settings = Settings(ALGORITHM="HS256") | |
| SECRET_KEY = settings.JWT_SECRET | |
| ALGORITHM = settings.ALGORITHM | |
| ACCESS_TOKEN_EXPIRE_MINUTES = 30 | |
| async def authenticate_user(username: str, password: str) -> bool: | |
| cfg = await load_aquiles_config() | |
| users_db = {u["username"]: u["password"] for u in cfg.get("allows_users", [])} | |
| pwd = users_db.get(username) | |
| return bool(pwd and pwd == password) | |
| async def create_access_token(username: str, expires_delta: timedelta) -> str: | |
| to_encode = {"sub": username} | |
| expire = datetime.utcnow() + expires_delta | |
| to_encode.update({"exp": expire}) | |
| return jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM) | |
| async def get_token_from_cookie(access_token: str = Cookie(None)) -> str: | |
| if not access_token: | |
| raise HTTPException(status_code=401, detail="No token cookie") | |
| scheme, _, token = access_token.partition(" ") | |
| if scheme.lower() != "bearer" or not token: | |
| raise HTTPException(status_code=401, detail="Esquema de autenticación inválido") | |
| return token | |
| async def get_current_user(token: str = Depends(get_token_from_cookie)) -> str: | |
| try: | |
| cfg = await load_aquiles_config() | |
| users_db = {u["username"]: u["password"] for u in cfg.get("allows_users", [])} | |
| payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM]) | |
| username: str = payload.get("sub") | |
| except jwt.PyJWTError: | |
| raise HTTPException(status_code=401, detail="Token inválido o expirado") | |
| if not username or username not in users_db: | |
| raise HTTPException(status_code=401, detail="Usuario no autenticado") | |
| return username | |
| async def require_user(user: str = Depends(get_current_user)): | |
| return user |
Xet Storage Details
- Size:
- 2.29 kB
- Xet hash:
- b09046710da92fe03b36d4af8c35568122233a80c14e390d870b6a2cd2d5dabd
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.