Spaces:
Sleeping
Sleeping
Download backend/app/core/security.py from tech-ties/filezkit-backend: direct link, hf CLI and curl.
- Browser
- Download file 2.91 kB
-
https://huggingface.co/spaces/tech-ties/filezkit-backend/resolve/main/backend/app/core/security.py
- Command line
-
hf download hf://spaces/tech-ties/filezkit-backend/backend/app/core/security.py
-
curl -L -o security.py https://huggingface.co/spaces/tech-ties/filezkit-backend/resolve/main/backend/app/core/security.py
2.91 kB
| from passlib.context import CryptContext | |
| from datetime import datetime, timedelta, UTC | |
| from jose import jwt, JWTError | |
| from fastapi import Depends, HTTPException | |
| from sqlalchemy.orm import Session | |
| from fastapi.security import OAuth2PasswordBearer | |
| from app.core.config import settings | |
| from app.db.dependencies import get_db | |
| from app.models.user import User | |
| pwd_context = CryptContext( | |
| schemes=["bcrypt"], | |
| deprecated="auto" | |
| ) | |
| def hash_password(password:str)-> str: | |
| return pwd_context.hash(password) | |
| def verify_password( | |
| plain_password:str, | |
| hashed_password:str | |
| ) -> bool: | |
| return pwd_context.verify( | |
| plain_password, | |
| hashed_password | |
| ) | |
| def create_access_token(data: dict): | |
| to_encode = data.copy() | |
| expire = datetime.now(UTC) + timedelta( | |
| minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES | |
| ) | |
| to_encode.update({"exp": expire}) | |
| return jwt.encode( | |
| to_encode, | |
| settings.SECRET_KEY, | |
| algorithm=settings.ALGORITHM | |
| ) | |
| oauth2_scheme = OAuth2PasswordBearer( | |
| tokenUrl="/auth/login" | |
| ) | |
| # Same scheme but does NOT auto-raise 401 when the Authorization header is | |
| # absent — used by the public, no-login tool flow so anonymous requests are | |
| # allowed through with current_user == None. | |
| oauth2_scheme_optional = OAuth2PasswordBearer( | |
| tokenUrl="/auth/login", | |
| auto_error=False | |
| ) | |
| def get_current_user( | |
| token: str = Depends(oauth2_scheme), | |
| db: Session = Depends(get_db), | |
| ): | |
| try: | |
| payload = jwt.decode( | |
| token, | |
| settings.SECRET_KEY, | |
| algorithms=[settings.ALGORITHM] | |
| ) | |
| email = payload.get("sub") | |
| if email is None: | |
| raise HTTPException( | |
| status_code=401, | |
| detail="Invalid token" | |
| ) | |
| except JWTError: | |
| raise HTTPException( | |
| status_code=401, | |
| detail="Invalid token" | |
| ) | |
| user = ( | |
| db.query(User) | |
| .filter(User.email == email) | |
| .first() | |
| ) | |
| if not user: | |
| raise HTTPException( | |
| status_code=404, | |
| detail="User not found" | |
| ) | |
| return user | |
| def get_current_user_optional( | |
| token: str = Depends(oauth2_scheme_optional), | |
| db: Session = Depends(get_db), | |
| ): | |
| """Return the authenticated User, or None for anonymous requests. | |
| Never raises: a missing/invalid/expired token simply yields None so the | |
| public tool endpoints keep working without a login. | |
| """ | |
| if not token: | |
| return None | |
| try: | |
| payload = jwt.decode( | |
| token, | |
| settings.SECRET_KEY, | |
| algorithms=[settings.ALGORITHM] | |
| ) | |
| email = payload.get("sub") | |
| if email is None: | |
| return None | |
| except JWTError: | |
| return None | |
| return ( | |
| db.query(User) | |
| .filter(User.email == email) | |
| .first() | |
| ) |