Exemplo n.º 1
0
 def delete_user(user: user_schema.UserId,
                 db: Session = Depends(deps.get_db)):
     db_user = usecase.read(db, user_id=user.id)
     if db_user is None:
         raise HTTPException(status_code=status.HTTP_404_NOT_FOUND,
                             detail="User not found")
     return usecase.delete(db=db, user_id=user.id)
Exemplo n.º 2
0
 def create_user(user: user_schema.UserCreate,
                 db: Session = Depends(deps.get_db),
                 current_user: user_schema.User = Depends(
                     auth.get_current_active_user)):
     db_user = usecase.read_by_email(db, email=user.email)
     if db_user:
         raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST,
                             detail="Email already registered")
     return usecase.create(db=db, user=user)
Exemplo n.º 3
0
async def get_current_user(
        token: Optional[str] = Security(api_key_header),
        db: Session = Depends(get_db),
        commons: dict = Depends(di.common_parameters),
        active: bool = True,
):
    credentials_exception = HTTPException(
        status_code=status.HTTP_401_UNAUTHORIZED,
        detail="Could not validate credentials.",
        headers={"WWW-Authenticate": "Bearer"},
    )

    if token is None:
        raise credentials_exception
        pass
    else:
        split_token = token.rsplit(' ')

        if split_token[0] != 'Bearer':
            user = UserService.read(db, user_id=split_token[0])
            if user is not None:
                if user.id == token:
                    return user
                else:
                    raise credentials_exception
                    pass
                pass
            else:
                raise credentials_exception
                pass
        else:
            if len(split_token) > 1:
                try:
                    payload = jwt.decode(split_token[1],
                                         SECRET_KEY,
                                         algorithms=[ALGORITHM])
                    email: str = payload.get("sub")
                    if email is None:
                        raise credentials_exception
                    token_data = TokenData(email=email)
                except JWTError:
                    raise credentials_exception
                user = UserService.read_by_email(db, email=token_data.email)
                if user is None:
                    raise credentials_exception
                else:
                    if user.email == token_data.email:
                        return user
                    else:
                        raise credentials_exception
                        pass
                pass
            else:
                raise credentials_exception
                pass
    pass
Exemplo n.º 4
0
def authenticate_user(db, email: str, password: str):
    user = UserService.read_by_email(db, email)
    if not user:
        return False
    if not verify_hassing(password, user.hashed_password):
        return False
    return user
Exemplo n.º 5
0
 def read_user(user_id: str,
               db: Session = Depends(deps.get_db),
               current_user: user_schema.User = Depends(
                   auth.get_current_active_user)):
     db_user = usecase.read(db, user_id=user_id)
     if db_user is None:
         raise HTTPException(status_code=status.HTTP_404_NOT_FOUND,
                             detail="User not found")
     return db_user
Exemplo n.º 6
0
 def read_users(commons: dict = Depends(di.common_parameters),
                active: bool = True,
                db: Session = Depends(deps.get_db),
                current_user: user_schema.User = Depends(
                    auth.get_current_active_user)):
     users = usecase.reads(db,
                           skip=commons['skip'],
                           limit=commons['limit'],
                           active=active)
     return users
Exemplo n.º 7
0
async def get_current_user(token: str = Depends(oauth2_scheme),
                           db: Session = Depends(get_db)):
    credentials_exception = HTTPException(
        status_code=status.HTTP_401_UNAUTHORIZED,
        detail="Could not validate credentials",
        headers={"WWW-Authenticate": "Bearer"},
    )
    try:
        payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
        email: str = payload.get("sub")
        if email is None:
            raise credentials_exception
        token_data = TokenData(email=email)
    except JWTError:
        raise credentials_exception
    user = UserService.read_by_email(db, email=token_data.email)
    if user is None:
        raise credentials_exception
    return user
Exemplo n.º 8
0
from app.interfaces.api_interfaces import ServiceInterface
from app.usecases.item_service import ItemService
from app.usecases.user_service import UserService

ItemService()
UserService()


def test_item_repository():
    assert issubclass(ItemService, ServiceInterface)


def test_user_repository():
    assert issubclass(UserService, ServiceInterface)