async def method_post(self, request: Request, body: dict, session, *args,
                          **kwargs) -> BaseHTTPResponse:

        request_model = RequestAuthEmployeeDto(body)

        try:
            db_employee = employee_queries.get_employee(
                session, login=request_model.login)
        except DBEmployeeNotExistsException:
            raise SanicEmployeeNotFound('Employee not found')

        try:
            check_hash(request_model.password, db_employee.password)
        except CheckPasswordHashException:
            raise SanicPasswordHashException('Wrong password')

        payload = {
            'eid': db_employee.id,
        }

        token = create_token(payload)
        response = AuthResponseObject(token)

        response_model = ResponseAuthEmployeeDto(response)

        return await self.make_response_json(
            body=response_model.dump(),
            status=200,
        )
Exemple #2
0
    async def method_post(self, request: Request, body: dict, session, *args,
                          **kwargs) -> BaseHTTPResponse:

        request_model = RequestAuthUserDto(body)

        try:
            db_user = get_user_by_login(session, login=request_model.login)
        except DBUserNotExists:
            raise SanicUserNotFound("User not found")

        try:
            check_hash(request_model.password, db_user.password)
        except CheckPasswordHashException:
            raise SanicPasswordHashException("Wrong password")

        payload = {
            "sub": db_user.id,
        }
        token = create_token(payload)
        response = AuthResponseObject(token)

        response_model = ResponseAuthUserDto(response)

        return await self.make_response_json(
            body=response_model.dump(),
            status=200,
        )
Exemple #3
0
    async def method_post(self, request: Request, body: dict, session, *args,
                          **kwargs) -> BaseHTTPResponse:

        request_model = RequestCreateEmployeeDto(body)

        try:
            hashed_password = generate_hash(request_model.password)
        except GeneratePasswordHashException as e:
            raise SanicPasswordHashException(str(e))

        try:
            db_employee = employee_queries.create_employee(
                session, request_model, hashed_password)
        except DBEmployeeExistsException:
            raise SanicEmployeeConflictException('Login is busy')

        try:
            session.commit_session()
        except (DBDataException, DBIntegrityException) as e:
            raise SanicDBException(str(e))

        response_model = ResponseEmployeeDto(db_employee)

        return await self.make_response_json(body=response_model.dump(),
                                             status=201)
    async def method_patch(self, request: Request, body: dict,
                           session: DBSession, user_id: int, token: dict,
                           *args, **kwargs) -> BaseHTTPResponse:

        # проверяем, что пользователь посылает запрос от своего имени
        if token['id'] != user_id:
            return await self.make_response_json(status=403)

        request_model = RequestPatchUserPasswordDto(body)

        try:
            hashed_password = generate_hash(request_model.password)
        except GeneratePasswordHashException as error:
            raise SanicPasswordHashException(str(error))

        # проверка, что пользователь не удален
        try:
            db_user = user_queries.change_password(session, hashed_password,
                                                   user_id)
        except DBUserDeletedException:
            raise SanicUserDeletedException('User deleted')

        # проверяем, что secret_word валидный и совпадает с тем, который находится в БД
        try:
            check_hash(request_model.secret_word, db_user.secret_word)
        except CheckPasswordHashException:
            raise SanicSecretWordHashException('Error')

        try:
            session.commit_session()
        except (DBIntegrityException, DBDataException) as error:
            raise SanicDBException(str(error))

        return await self.make_response_json(status=200)
Exemple #5
0
    async def method_post(
            self, request: Request, body: dict, session: DBSession, *args, **kwargs
    ) -> BaseHTTPResponse:

        # DTO объект
        request_model = RequestCreateUserDto(body)

        try:
            hashed_password = generate_hash(request_model.password)
            secret_word = generate_hash(request_model.secret_word)
        except GeneratePasswordHashException as error:
            raise SanicPasswordHashException(str(error))

        try:
            # экземпляр базы данных
            db_user = user_queries.create_user(session, request_model, hashed_password, secret_word)
            session.commit_session()
        # ошибка уникальности, то есть подразумевается, что такой пользователь
        # уже существует в базе
        except DBUserAlreadyExistsException:
            return await self.make_response_json(status=409, message='User already exists')
        except (DBIntegrityException, DBDataException) as error:
            raise SanicDBException(str(error))

        response_model = ResponseGetUserDto(db_user)

        return await self.make_response_json(
            body=response_model.dump(),
            status=201
        )
Exemple #6
0
    async def method_post(self, request: Request, body: dict,
                          session: DBSession, *args,
                          **kwargs) -> BaseHTTPResponse:

        request_model = RequestAuthUserDto(body)

        try:
            # процесс идентификации пользователя
            db_user = user_queries.get_user(session, login=request_model.login)
        except DBUserNotFoundException:
            raise SanicUserNotFoundException('User not found')
        except DBUserDeletedException:
            raise SanicUserDeletedException('User deleted')

        try:
            check_hash(request_model.password, db_user.password)
        except CheckPasswordHashException:
            raise SanicPasswordHashException('Wrong password')

        payload = {
            'id': db_user.id,
        }

        token = create_token(payload)
        response = AuthResponseObject(token)

        response_model = ResponseAuthUserDto(response)

        return await self.make_response_json(body=response_model.dump(),
                                             status=200)
Exemple #7
0
    async def method_post(self, request: Request, body: dict, session, *args,
                          **kwargs) -> BaseHTTPResponse:

        request_model = RequestCreateUserDto(body)

        try:
            hashed_password = generate_hash(request_model.password)
        except GeneratePasswordHashException as e:
            raise SanicPasswordHashException(str(e))

        try:
            db_user = create_user(session,
                                  user=request_model,
                                  hashed_password=hashed_password)
        except DBUserExists:
            raise SanicUserConflictException("Login is busy")

        try:
            session.commit_session()
        except (DBDataException, DBIntegrityException) as e:
            raise SanicDBException(str(e))

        response_model = ResponseUserDto(db_user)

        return await self.make_response_json(body=response_model.dump(),
                                             status=201)
Exemple #8
0
    async def method_post(self, request: Request, body: dict,
                          session: DBSession, *args,
                          **kwargs) -> BaseHTTPResponse:

        request_model = RequestCreateUserDto(body)

        try:
            hashed_password = generate_hash(request_model.password)
            user_queries.create_user(session=session,
                                     user=request_model,
                                     hashed_password=hashed_password)
            session.commit_session()
        except GeneratePasswordHashException as error:
            raise SanicPasswordHashException(str(error))
        except DBLoginExistsException:
            raise SanicLoginExistsException('login exits')
        except (DBDataError, DBIntegrityError) as error:
            raise SanicDBException(str(error))

        return await self.make_response_json(status=201)
Exemple #9
0
    async def method_post(self, request: Request, body: dict,
                          session: DBSession, *args,
                          **kwargs) -> BaseHTTPResponse:
        request_model = RequestAuthUserDto(body)

        try:
            db_user = user_queries.get_user(session=session,
                                            login=request_model.login)
            check_hash(request_model.password, db_user.password)
        except DBUserNotExistsException:
            raise SanicDBLoginNotFound('Login not found')
        except CheckPasswordHashException:
            raise SanicPasswordHashException("password is wrong")

        data = {'id_auth': db_user.id}

        token = create_token(data)

        response_body = {'Authorization': token}

        return await self.make_response_json(
            body=response_body,
            status=200,
        )