1
0
mirror of https://gitlab.com/MoonTestUse1/AdministrationItDepartmens.git synced 2025-08-14 00:25:46 +02:00

Добавления тестов бекенда

This commit is contained in:
MoonTestUse1
2025-01-02 04:30:51 +06:00
parent 7729acaa09
commit 01677cf5df
16 changed files with 903 additions and 328 deletions

View File

@@ -1,22 +1,69 @@
"""Authentication utilities"""
import bcrypt
from fastapi import Depends, HTTPException, status
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
from passlib.context import CryptContext
import re
def verify_password(plain_password: str, hashed_password: str) -> bool:
"""Verify a password against its hash"""
try:
return bcrypt.checkpw(
plain_password.encode('utf-8'),
hashed_password.encode('utf-8')
)
except Exception as e:
print(f"Password verification error: {e}")
return False
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
security = HTTPBearer(auto_error=False)
def get_password_hash(password: str) -> str:
"""Generate password hash"""
"""Hash password"""
return pwd_context.hash(password)
def verify_password(plain_password: str, hashed_password: str) -> bool:
"""Verify password"""
return pwd_context.verify(plain_password, hashed_password)
def get_current_admin(credentials: HTTPAuthorizationCredentials = Depends(security)) -> dict:
"""Get current admin from token"""
if not credentials:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Not authenticated",
headers={"WWW-Authenticate": "Bearer"},
)
try:
salt = bcrypt.gensalt()
return bcrypt.hashpw(password.encode('utf-8'), salt).decode('utf-8')
except Exception as e:
print(f"Password hashing error: {e}")
raise
token = credentials.credentials
if token != "admin_token":
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid authentication credentials",
headers={"WWW-Authenticate": "Bearer"},
)
return {"is_admin": True}
except Exception:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid authentication credentials",
headers={"WWW-Authenticate": "Bearer"},
)
def get_current_employee(credentials: HTTPAuthorizationCredentials = Depends(security)) -> dict:
"""Get current employee from token"""
if not credentials:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Not authenticated",
headers={"WWW-Authenticate": "Bearer"},
)
try:
token = credentials.credentials
# Проверяем формат токена employee_token_{id}
match = re.match(r"employee_token_(\d+)", token)
if not match:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid authentication credentials",
headers={"WWW-Authenticate": "Bearer"},
)
employee_id = int(match.group(1))
return {"id": employee_id}
except Exception:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid authentication credentials",
headers={"WWW-Authenticate": "Bearer"},
)