mirror of
https://gitlab.com/MoonTestUse1/AdministrationItDepartmens.git
synced 2025-08-14 00:25:46 +02:00
57 lines
2.0 KiB
Python
57 lines
2.0 KiB
Python
from fastapi import Depends, HTTPException, status, WebSocket
|
|
from fastapi.security import OAuth2PasswordBearer
|
|
from jose import JWTError, jwt
|
|
from sqlalchemy.orm import Session
|
|
from typing import Optional
|
|
|
|
from app.core.config import settings
|
|
from app.database import get_db
|
|
from app.models.user import User
|
|
|
|
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
|
|
|
|
def get_current_user(token: str = Depends(oauth2_scheme), db: Session = Depends(get_db)) -> User:
|
|
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])
|
|
email: str = payload.get("sub")
|
|
if email is None:
|
|
raise credentials_exception
|
|
except JWTError:
|
|
raise credentials_exception
|
|
|
|
user = db.query(User).filter(User.email == email).first()
|
|
if user is None:
|
|
raise credentials_exception
|
|
return user
|
|
|
|
async def get_current_user_ws(websocket: WebSocket, db: Session = Depends(get_db)) -> Optional[User]:
|
|
try:
|
|
# Получаем токен из параметров запроса
|
|
token = websocket.query_params.get("token")
|
|
if not token:
|
|
await websocket.close(code=status.WS_1008_POLICY_VIOLATION)
|
|
return None
|
|
|
|
# Проверяем токен
|
|
payload = jwt.decode(token, settings.secret_key, algorithms=[settings.algorithm])
|
|
email: str = payload.get("sub")
|
|
if email is None:
|
|
await websocket.close(code=status.WS_1008_POLICY_VIOLATION)
|
|
return None
|
|
|
|
# Получаем пользователя
|
|
user = db.query(User).filter(User.email == email).first()
|
|
if user is None:
|
|
await websocket.close(code=status.WS_1008_POLICY_VIOLATION)
|
|
return None
|
|
|
|
return user
|
|
|
|
except JWTError:
|
|
await websocket.close(code=status.WS_1008_POLICY_VIOLATION)
|
|
return None |