def get_user(req: Request) -> Awaitable[OutUser]: tokens = get_tokens(req) with create_session() as session: user = (session.query(User).filter( User.access_token == tokens.access_token).first()) if user: return OutUser.from_orm(user) # type: ignore raise DALError(HTTPStatus.UNAUTHORIZED.value)
def register(self, params: SimpleRegistrationParams) -> Awaitable[OutUser]: with create_session() as session: user = self._add_user( session, username=params.username, password=params.password, db_obj=get_db_obj(params.type), ) return OutUser.from_orm(user) # type: ignore
def _authenticate_user(username: str, password: str) -> Awaitable[OutUser]: message = Message.INCORRECT_USERNAME_OR_PASSWORD.value with create_session() as session: user: Optional[UserDB] = session.query(UserDB).filter( UserDB.username == username ).first() if user is None: raise DALError(HTTPStatus.NOT_FOUND.value, message) if _is_password_correct(password, user.password_hash): return OutUser.from_orm(user) # type: ignore raise DALError(HTTPStatus.NOT_FOUND.value, message)
async def check_authorization(token: str) -> OutUser: ''' Обрабатывает jwt :raises HttpException со статусом 401 если произошла ошибка при обработке токена :return: user ''' user_id = _get_user_id(token) user = await _get_user_from_db(user_id) if _is_valid_token(token, user.access_token.decode()): return OutUser.parse_obj(user) raise AccessTokenOutdatedError( HTTPStatus.BAD_REQUEST.value, Message.ACCESS_TOKEN_OUTDATED.value )
def register(self, params: UniqueLinkRegistrationParams) -> Awaitable[OutUser]: with create_session() as session: user_to_register = self._get_user_to_register(session, params.uuid) if not user_to_register: raise DALError(HTTPStatus.BAD_REQUEST.value, Message.LINK_INVALID_OR_OUTDATED.value) user = self._add_user( session, username=params.username, password=params.password, db_obj=get_obj_from_obj_to_register(user_to_register), ) user = self._transfer_fields(user, user_to_register) session.delete(user_to_register) return OutUser.from_orm(user) # type: ignore
def contractor_representative_out(): return OutUser(id=1, type='contractor_representative', username='******')
def test_get_user_role(user_type, postfix, username): user = OutUser(id=1, type=user_type, username=username) assert get_url_postfix(user) == postfix
def out_user(username): return OutUser(id=1, username=username, type=UserRole.ADMIN.value)
def out_user(): return OutUser(id=2, type='contractor_representative', username='******')