Init.
This commit is contained in:
0
backend/app/utils/__init__.py
Normal file
0
backend/app/utils/__init__.py
Normal file
50
backend/app/utils/exceptions.py
Normal file
50
backend/app/utils/exceptions.py
Normal file
@ -0,0 +1,50 @@
|
||||
from fastapi import HTTPException, status
|
||||
|
||||
|
||||
class BetNotFoundError(HTTPException):
|
||||
def __init__(self):
|
||||
super().__init__(status_code=status.HTTP_404_NOT_FOUND, detail="Bet not found")
|
||||
|
||||
|
||||
class InsufficientFundsError(HTTPException):
|
||||
def __init__(self):
|
||||
super().__init__(status_code=status.HTTP_400_BAD_REQUEST, detail="Insufficient funds")
|
||||
|
||||
|
||||
class BetNotAvailableError(HTTPException):
|
||||
def __init__(self):
|
||||
super().__init__(status_code=status.HTTP_400_BAD_REQUEST, detail="Bet is no longer available")
|
||||
|
||||
|
||||
class CannotAcceptOwnBetError(HTTPException):
|
||||
def __init__(self):
|
||||
super().__init__(status_code=status.HTTP_400_BAD_REQUEST, detail="Cannot accept your own bet")
|
||||
|
||||
|
||||
class UnauthorizedError(HTTPException):
|
||||
def __init__(self, detail: str = "Not authorized"):
|
||||
super().__init__(status_code=status.HTTP_401_UNAUTHORIZED, detail=detail)
|
||||
|
||||
|
||||
class UserNotFoundError(HTTPException):
|
||||
def __init__(self):
|
||||
super().__init__(status_code=status.HTTP_404_NOT_FOUND, detail="User not found")
|
||||
|
||||
|
||||
class InvalidCredentialsError(HTTPException):
|
||||
def __init__(self):
|
||||
super().__init__(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="Incorrect email or password",
|
||||
headers={"WWW-Authenticate": "Bearer"},
|
||||
)
|
||||
|
||||
|
||||
class UserAlreadyExistsError(HTTPException):
|
||||
def __init__(self, detail: str = "User already exists"):
|
||||
super().__init__(status_code=status.HTTP_400_BAD_REQUEST, detail=detail)
|
||||
|
||||
|
||||
class NotBetParticipantError(HTTPException):
|
||||
def __init__(self):
|
||||
super().__init__(status_code=status.HTTP_403_FORBIDDEN, detail="You are not a participant in this bet")
|
||||
38
backend/app/utils/security.py
Normal file
38
backend/app/utils/security.py
Normal file
@ -0,0 +1,38 @@
|
||||
from datetime import datetime, timedelta
|
||||
from typing import Any
|
||||
from jose import JWTError, jwt
|
||||
from passlib.context import CryptContext
|
||||
from app.config import settings
|
||||
|
||||
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
||||
|
||||
|
||||
def verify_password(plain_password: str, hashed_password: str) -> bool:
|
||||
return pwd_context.verify(plain_password, hashed_password)
|
||||
|
||||
|
||||
def get_password_hash(password: str) -> str:
|
||||
return pwd_context.hash(password)
|
||||
|
||||
|
||||
def create_access_token(data: dict[str, Any], expires_delta: timedelta | None = None) -> str:
|
||||
to_encode = data.copy()
|
||||
if expires_delta:
|
||||
expire = datetime.utcnow() + expires_delta
|
||||
else:
|
||||
expire = datetime.utcnow() + timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES)
|
||||
to_encode.update({"exp": expire})
|
||||
encoded_jwt = jwt.encode(to_encode, settings.JWT_SECRET, algorithm=settings.JWT_ALGORITHM)
|
||||
return encoded_jwt
|
||||
|
||||
|
||||
def create_refresh_token(data: dict[str, Any]) -> str:
|
||||
to_encode = data.copy()
|
||||
expire = datetime.utcnow() + timedelta(days=settings.REFRESH_TOKEN_EXPIRE_DAYS)
|
||||
to_encode.update({"exp": expire})
|
||||
encoded_jwt = jwt.encode(to_encode, settings.JWT_SECRET, algorithm=settings.JWT_ALGORITHM)
|
||||
return encoded_jwt
|
||||
|
||||
|
||||
def decode_token(token: str) -> dict[str, Any]:
|
||||
return jwt.decode(token, settings.JWT_SECRET, algorithms=[settings.JWT_ALGORITHM])
|
||||
Reference in New Issue
Block a user