crappy
This commit is contained in:
		
							
								
								
									
										0
									
								
								services/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								services/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
								
								
									
										56
									
								
								services/auth.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										56
									
								
								services/auth.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,56 @@
 | 
			
		||||
from typing import Union, Annotated
 | 
			
		||||
 | 
			
		||||
from fastapi import Depends, HTTPException
 | 
			
		||||
from fastapi.security import OAuth2PasswordBearer, HTTPBearer, HTTPAuthorizationCredentials
 | 
			
		||||
from jose import jwt, JWTError
 | 
			
		||||
from sqlalchemy import select
 | 
			
		||||
from sqlalchemy.ext.asyncio import AsyncSession
 | 
			
		||||
from starlette import status
 | 
			
		||||
 | 
			
		||||
import backend.config
 | 
			
		||||
from backend.session import get_session
 | 
			
		||||
from models import User
 | 
			
		||||
from services.base import BaseService
 | 
			
		||||
from schemas.auth import *
 | 
			
		||||
 | 
			
		||||
oauth2_schema = HTTPBearer()
 | 
			
		||||
algorithm = 'HS256'
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
async def get_current_user(session: Annotated[AsyncSession, Depends(get_session)],
 | 
			
		||||
                           token: Annotated[HTTPAuthorizationCredentials, Depends(oauth2_schema)]) -> User | None:
 | 
			
		||||
    if not token.credentials:
 | 
			
		||||
        raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail='Invalid token')
 | 
			
		||||
    try:
 | 
			
		||||
        payload = jwt.decode(token.credentials, backend.config.SECRET_KEY, algorithms=[algorithm])
 | 
			
		||||
        user_id = payload.get('sub')
 | 
			
		||||
        if not user_id:
 | 
			
		||||
            raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail='Invalid credentials')
 | 
			
		||||
        user_id = int(user_id)
 | 
			
		||||
 | 
			
		||||
        user = await session.get(User, user_id)
 | 
			
		||||
        if not user:
 | 
			
		||||
            raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail='Invalid credentials')
 | 
			
		||||
        return user
 | 
			
		||||
    except JWTError as e:
 | 
			
		||||
        print(e)
 | 
			
		||||
        raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail='Invalid token')
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class AuthService(BaseService):
 | 
			
		||||
    @staticmethod
 | 
			
		||||
    def _generate_jwt_token(user: User) -> str:
 | 
			
		||||
        payload = {
 | 
			
		||||
            'sub': str(user.id)
 | 
			
		||||
        }
 | 
			
		||||
        return jwt.encode(payload, backend.config.SECRET_KEY, algorithm=algorithm)
 | 
			
		||||
 | 
			
		||||
    async def authenticate(self, request: AuthLoginRequest):
 | 
			
		||||
        user: Union[User, None] = await self.session.scalar(select(User).where(User.telegram_id == request.id))
 | 
			
		||||
        if not user:
 | 
			
		||||
            user = User(telegram_id=request.id,
 | 
			
		||||
                        is_admin=False)
 | 
			
		||||
            self.session.add(user)
 | 
			
		||||
            await self.session.commit()
 | 
			
		||||
        access_token = self._generate_jwt_token(user)
 | 
			
		||||
        return AuthLoginResponse(access_token=access_token)
 | 
			
		||||
							
								
								
									
										8
									
								
								services/base.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								services/base.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,8 @@
 | 
			
		||||
from sqlalchemy.ext.asyncio import AsyncSession
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class BaseService:
 | 
			
		||||
    session: AsyncSession
 | 
			
		||||
 | 
			
		||||
    def __init__(self, session: AsyncSession):
 | 
			
		||||
        self.session = session
 | 
			
		||||
							
								
								
									
										42
									
								
								services/deal.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								services/deal.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,42 @@
 | 
			
		||||
import datetime
 | 
			
		||||
 | 
			
		||||
from sqlalchemy import select
 | 
			
		||||
 | 
			
		||||
from models import User
 | 
			
		||||
from models.deal import *
 | 
			
		||||
from schemas.deal import *
 | 
			
		||||
from services.base import BaseService
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class DealService(BaseService):
 | 
			
		||||
 | 
			
		||||
    async def _get_deal_by_id(self, deal_id) -> Deal:
 | 
			
		||||
        return await self.session.get(Deal, deal_id)
 | 
			
		||||
 | 
			
		||||
    async def create(self, request: DealCreateRequest) -> DealCreateResponse:
 | 
			
		||||
        deal = Deal(
 | 
			
		||||
            name=request.name,
 | 
			
		||||
            created_at=datetime.datetime.now(),
 | 
			
		||||
            current_status=DealStatus.AWAITING_ACCEPTANCE
 | 
			
		||||
        )
 | 
			
		||||
        self.session.add(deal)
 | 
			
		||||
        await self.session.commit()
 | 
			
		||||
        return DealCreateResponse(ok=True)
 | 
			
		||||
 | 
			
		||||
    async def change_status(self, request: DealChangeStatusRequest, user: User) -> DealChangeStatusResponse:
 | 
			
		||||
        # Changing current status
 | 
			
		||||
        deal = await self._get_deal_by_id(request.deal_id)
 | 
			
		||||
        from_status = deal.current_status
 | 
			
		||||
        deal.current_status = request.new_status
 | 
			
		||||
 | 
			
		||||
        # Append status history
 | 
			
		||||
        status_change = DealStatusHistory(
 | 
			
		||||
            deal_id=request.deal_id,
 | 
			
		||||
            user_id=user.id,
 | 
			
		||||
            changed_at=datetime.datetime.now(),
 | 
			
		||||
            from_status=from_status,
 | 
			
		||||
            to_status=request.new_status
 | 
			
		||||
        )
 | 
			
		||||
        self.session.add(status_change)
 | 
			
		||||
        await self.session.commit()
 | 
			
		||||
        return DealChangeStatusResponse(ok=True)
 | 
			
		||||
		Reference in New Issue
	
	Block a user