40 lines
1.3 KiB
Python
40 lines
1.3 KiB
Python
from datetime import datetime, timedelta, timezone
|
|
|
|
from fastapi import Depends, HTTPException, status
|
|
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
|
|
from jose import JWTError, jwt
|
|
|
|
from app.config import get_settings
|
|
|
|
bearer_scheme = HTTPBearer(auto_error=False)
|
|
|
|
|
|
def create_access_token(subject: str) -> str:
|
|
settings = get_settings()
|
|
expire = datetime.now(timezone.utc) + timedelta(minutes=settings.jwt_expire_minutes)
|
|
payload = {"sub": subject, "exp": expire}
|
|
return jwt.encode(payload, settings.jwt_secret, algorithm=settings.jwt_algorithm)
|
|
|
|
|
|
def get_current_user(
|
|
credentials: HTTPAuthorizationCredentials | None = Depends(bearer_scheme),
|
|
) -> str:
|
|
if credentials is None or credentials.scheme.lower() != "bearer":
|
|
raise HTTPException(
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
detail="Not authenticated",
|
|
)
|
|
settings = get_settings()
|
|
try:
|
|
payload = jwt.decode(
|
|
credentials.credentials,
|
|
settings.jwt_secret,
|
|
algorithms=[settings.jwt_algorithm],
|
|
)
|
|
username = payload.get("sub")
|
|
if not username:
|
|
raise HTTPException(status_code=401, detail="Invalid token")
|
|
return str(username)
|
|
except JWTError as exc:
|
|
raise HTTPException(status_code=401, detail="Invalid token") from exc
|