/
Odisseywolf
/
BioSphere
Обзор
Документация
Войти
/
Odisseywolf
/
BioSphere
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
server/auth.py
54 строки
2 KB
Odisseywolf
create: LICENSE, install.bat , install.sh , main.py, README.md, requirements.txt, main.py, auth.py, simulation.py, actions.py, database.py, models.py, config.py
27 май 2026, 16:28
Верифицирован
27 май 2026, 16:28
d6b5588
Код
Авторство
О чём код?
# FILE: server/auth.py from fastapi import Depends, HTTPException, status from fastapi.security import OAuth2PasswordBearer from jose import JWTError, jwt from passlib.context import CryptContext from models import User from database import SessionLocal from typing import Generator pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto") oauth2_scheme = OAuth2PasswordBearer(tokenUrl="auth/login") def verify_password(plain_password, hashed_password): return pwd_context.verify(plain_password, hashed_password) def get_password_hash(password): return pwd_context.hash(password) def get_user(username: str, db): return db.query(User).filter(User.username == username).first() def authenticate_user(username: str, password: str, db): user = get_user(username, db) if not user or not verify_password(password, user.hashed_password): return False return user def create_access_token(data: dict): from datetime import timedelta from config import settings from jose import jwt to_encode = data.copy() expire = timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES) to_encode.update({"exp": datetime.utcnow() + expire}) return jwt.encode(to_encode, settings.SECRET_KEY, algorithm=settings.ALGORITHM) async def get_current_user(token: str = Depends(oauth2_scheme), db: SessionLocal = None): from config import settings credentials_exception = HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="Could not validate credentials", headers={"WWW-Authenticate": "Bearer"}, ) try: payload = jwt.decode(token, settings.SECRET_KEY, algorithms=[settings.ALGORITHM]) username: str = payload.get("sub") if username is None: raise credentials_exception except JWTError: raise credentials_exception user = get_user(username, db) if user is None: raise credentials_exception return user