/
mkiiis
/
task_tracker
Обзор
Документация
Войти
/
mkiiis
/
task_tracker
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
main
app/auth.py
67 строк
2 KB
mkiiis
Initial commit
15 ноя 2025, 20:48
15 ноя 2025, 20:48
666c5fe
Код
Авторство
О чём код?
import os from datetime import datetime, timedelta from fastapi import Depends, HTTPException from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials from jose import jwt, JWTError from sqlmodel import Session from app.db import engine from app.models import User import bcrypt SECRET_KEY = os.getenv("SECRET_KEY", "dev-secret-change-me") ALGORITHM = "HS256" ACCESS_TOKEN_EXPIRE_MINUTES = int(os.getenv("ACCESS_TOKEN_EXPIRE_MINUTES", "15")) REFRESH_TOKEN_EXPIRE_DAYS = int(os.getenv("REFRESH_TOKEN_EXPIRE_DAYS", "7")) security = HTTPBearer() def get_password_hash(password: str) -> str: password_bytes = password.encode("utf-8") salt = bcrypt.gensalt() hashed = bcrypt.hashpw(password_bytes, salt) return hashed.decode("utf-8") def verify_password(plain_password: str, hashed_password: str) -> bool: plain_bytes = plain_password.encode("utf-8") hashed_bytes = hashed_password.encode("utf-8") return bcrypt.checkpw(plain_bytes, hashed_bytes) def create_access_token(subject: str, expires_minutes: int = ACCESS_TOKEN_EXPIRE_MINUTES) -> str: expire = datetime.utcnow() + timedelta(minutes=expires_minutes) to_encode = {"exp": expire, "sub": str(subject), "type": "access"} return jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM) def create_refresh_token(subject: str, expires_days: int = REFRESH_TOKEN_EXPIRE_DAYS) -> str: expire = datetime.utcnow() + timedelta(days=expires_days) to_encode = {"exp": expire, "sub": str(subject), "type": "refresh"} return jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM) def get_current_user(credentials: HTTPAuthorizationCredentials = Depends(security)) -> User: token = credentials.credentials credentials_exception = HTTPException( status_code=401, detail="Could not validate credentials", ) try: payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM]) if payload.get("type") != "access": raise credentials_exception user_id = int(payload.get("sub")) with Session(engine) as session: user = session.get(User, user_id) if not user: raise credentials_exception return user except JWTError: raise credentials_exception