/
tinypot
/
integr
Обзор
Документация
Войти
/
tinypot
/
integr
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
task_management_api/app/core/security.py
50 строк
1 KB
Tim Polus
lab1
21 янв 2026, 23:15
21 янв 2026, 23:15
dd1578c
Код
Авторство
О чём код?
""" Security utilities for authentication and authorization """ from datetime import datetime, timedelta from typing import Any, Union from jose import jwt from passlib.context import CryptContext from app.core.config import settings pwd_context = CryptContext(schemes=["argon2"], deprecated="auto") def create_access_token( subject: Union[str, Any], expires_delta: timedelta = None ) -> str: """Create JWT access token""" if expires_delta: expire = datetime.utcnow() + expires_delta else: expire = datetime.utcnow() + timedelta( minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES ) to_encode = {"exp": expire, "sub": str(subject)} encoded_jwt = jwt.encode(to_encode, settings.SECRET_KEY, algorithm=settings.ALGORITHM) return encoded_jwt def verify_password(plain_password: str, hashed_password: str) -> bool: """Verify password against hash""" return pwd_context.verify(plain_password, hashed_password) def get_password_hash(password: str) -> str: """Hash password""" return pwd_context.hash(password) def verify_token(token: str) -> Union[str, None]: """Verify JWT token and return subject""" try: payload = jwt.decode(token, settings.SECRET_KEY, algorithms=[settings.ALGORITHM]) username: str = payload.get("sub") if username is None: return None return username except jwt.JWTError: return None