mirror of
https://gitlab.com/MoonTestUse1/AdministrationItDepartmens.git
synced 2025-08-14 00:25:46 +02:00
Создание чата9testt
This commit is contained in:
@@ -62,31 +62,4 @@ async def get_current_user(
|
||||
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, SECRET_KEY, algorithms=[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
|
||||
return user
|
||||
@@ -5,15 +5,29 @@ from typing import Optional
|
||||
class Settings(BaseSettings):
|
||||
"""Application settings"""
|
||||
PROJECT_NAME: str = "Support System"
|
||||
VERSION: str = "1.0.0"
|
||||
API_V1_STR: str = "/api"
|
||||
SECRET_KEY: str = "your-secret-key-for-jwt" # В продакшене использовать безопасный ключ
|
||||
|
||||
# Security
|
||||
SECRET_KEY: str = "your-secret-key-for-jwt"
|
||||
ACCESS_TOKEN_EXPIRE_MINUTES: int = 60 * 24 * 8 # 8 дней
|
||||
|
||||
# Database
|
||||
POSTGRES_SERVER: str = "db"
|
||||
POSTGRES_USER: str = "postgres"
|
||||
POSTGRES_PASSWORD: str = "postgres"
|
||||
POSTGRES_DB: str = "support_db"
|
||||
SQLALCHEMY_DATABASE_URI: Optional[str] = None
|
||||
DATABASE_URL: Optional[str] = None
|
||||
|
||||
# Redis
|
||||
REDIS_HOST: str = "redis"
|
||||
REDIS_PORT: int = 6379
|
||||
REDIS_DB: int = 0
|
||||
REDIS_PASSWORD: Optional[str] = None
|
||||
|
||||
# Telegram
|
||||
TELEGRAM_BOT_TOKEN: Optional[str] = None
|
||||
TELEGRAM_CHAT_ID: Optional[str] = None
|
||||
|
||||
@property
|
||||
def get_database_url(self) -> str:
|
||||
@@ -23,6 +37,8 @@ class Settings(BaseSettings):
|
||||
class Config:
|
||||
case_sensitive = True
|
||||
env_file = ".env"
|
||||
extra = "allow" # Разрешаем дополнительные поля
|
||||
|
||||
settings = Settings()
|
||||
settings.SQLALCHEMY_DATABASE_URI = settings.get_database_url
|
||||
if not settings.DATABASE_URL:
|
||||
settings.DATABASE_URL = settings.get_database_url
|
||||
10
backend/app/core/redis.py
Normal file
10
backend/app/core/redis.py
Normal file
@@ -0,0 +1,10 @@
|
||||
import redis
|
||||
from app.core.config import settings
|
||||
|
||||
redis_client = redis.Redis(
|
||||
host=settings.REDIS_HOST,
|
||||
port=settings.REDIS_PORT,
|
||||
db=settings.REDIS_DB,
|
||||
password=settings.REDIS_PASSWORD,
|
||||
decode_responses=True
|
||||
)
|
||||
34
backend/app/core/ws_auth.py
Normal file
34
backend/app/core/ws_auth.py
Normal file
@@ -0,0 +1,34 @@
|
||||
from typing import Optional
|
||||
from fastapi import WebSocket, status
|
||||
from jose import JWTError, jwt
|
||||
from sqlalchemy.orm import Session
|
||||
from app.core.config import settings
|
||||
from app.models.user import User
|
||||
|
||||
async def get_current_user_ws(websocket: WebSocket, db: Session) -> Optional[User]:
|
||||
"""Get current user from WebSocket connection."""
|
||||
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=["HS256"])
|
||||
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
|
||||
Reference in New Issue
Block a user