36 lines
983 B
Python
36 lines
983 B
Python
from __future__ import annotations
|
|
|
|
from fastapi import Depends, HTTPException, Request, status
|
|
from passlib.context import CryptContext
|
|
|
|
from config import get_settings
|
|
|
|
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
|
|
|
|
|
def verify_password(plain: str, hashed: str) -> bool:
|
|
return pwd_context.verify(plain, hashed)
|
|
|
|
|
|
def hash_password(password: str) -> str:
|
|
return pwd_context.hash(password)
|
|
|
|
|
|
def check_admin_credentials(username: str, password: str) -> bool:
|
|
settings = get_settings()
|
|
return username == settings.admin_username and password == settings.admin_password
|
|
|
|
|
|
def get_current_admin(request: Request) -> str:
|
|
user = request.session.get("admin")
|
|
if not user:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
detail="Требуется авторизация",
|
|
)
|
|
return user
|
|
|
|
|
|
def require_admin_page(request: Request) -> str | None:
|
|
return request.session.get("admin")
|