/
Zero1l
/
aaa
Обзор
Документация
Войти
/
Zero1l
/
aaa
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
auth.py
42 строки
1 KB
Zero1l
create auth.py
31 янв 2026, 07:10
Верифицирован
31 янв 2026, 07:10
159d065
Код
Авторство
О чём код?
#Auth.py from datetime import datetime, timedelta from jose import jwt, JWTError from passlib.context import CryptContext from fastapi import HTTPException, Depends from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials SECRET_KEY = "super-secret-key" ALGORITHM = "HS256" TOKEN_EXPIRE_HOURS = 48 pwd_context = CryptContext(schemes=["bcrypt"]) security = HTTPBearer(auto_error=False) def hash_password(password: str) -> str: return pwd_context.hash(password) def verify_password(password: str, hash_: str) -> bool: return pwd_context.verify(password, hash_) def create_token(user_id, group): payload = { "sub": str(user_id), "group": group, "exp": datetime.utcnow() + timedelta(hours=TOKEN_EXPIRE_HOURS), } return jwt.encode(payload, SECRET_KEY, algorithm=ALGORITHM) def get_current_user(credentials: HTTPAuthorizationCredentials = Depends(security)): if credentials is None: return None try: payload = jwt.decode(credentials.credentials, SECRET_KEY, algorithms=[ALGORITHM]) return payload except JWTError: raise HTTPException(status_code=401, detail="Invalid token")