def note_create(): try: token_id = Utils.get_bearer_token(request.headers) bo = NoteBo() note = bo.create_note(token_id, request.json) except InvalidTokenError as e: return Utils.response(message=Message.MESSAGE_INVALID_TOKEN, errors=e.errors, status=e.status) except InvalidParametersError as e: return Utils.response(message=Message.MESSAGE_INVALID_PARAMETERS, errors=e.errors, status=e.status) except UserNotFoundError as e: return Utils.response(message=Message.MESSAGE_USER_NOT_FOUND, errors=e.errors, status=e.status) return Utils.response(message=Message.MESSAGE_SUCCESSFULL, data=note, status=201)
def user_logout(): try: token_id = Utils.get_bearer_token(request.headers) bo = UserBo() bo.user_logout(token_id) except InvalidTokenError as e: return Utils.response(message=Message.MESSAGE_INVALID_TOKEN, errors=e.errors, status=e.status) return Utils.response(message=Message.MESSAGE_SUCCESSFULL)
def login_user(self, credentials: dict) -> str: try: UserLoginSchema().load(credentials) user = User.get(User.email == credentials['email']) if not Utils.verify_password(user.password, credentials['password']): raise AuthenticationError( credentials, "Invalid password or email", "Error occurred when trying to login user") tokens = SessionBo().generate_session(user) except ValidationError as e: raise InvalidParametersError( e.data, e.messages, "Error occurred when trying to loggin user", e) except User.DoesNotExist as e: raise AuthenticationError( credentials, "Invalid password or email", "Error occurred when trying to login user", e) return tokens["token_id"]
def user_signup(): bo = UserBo() try: bo.create_user(request.json) except UserExistsError as e: return Utils.response(message=Message.MESSAGE_INVALID_PARAMETERS, errors=e.errors, status=e.status) except InvalidParametersError as e: return Utils.response(message=Message.MESSAGE_INVALID_PARAMETERS, errors=e.errors, status=e.status) return Utils.response(message=Message.MESSAGE_SUCCESSFULL, status=201)
def wrapper(*args, **kwars): try: token = Utils.get_bearer_token(request.headers) token_decode = Utils.token_decode(token) token_session = token_decode["token"] exists = SessionBo().session_exists(token_session) if not exists: raise InvalidTokenError(token, "User does not have an active session", "Invalid token") except InvalidTokenError as e: return Utils.response(message=Message.MESSAGE_INVALID_TOKEN, errors=e.errors, status=e.status) return f(*args, **kwars)
def user_info(): bo = UserBo() try: token_id = Utils.get_bearer_token(request.headers) user_info = bo.user_info(token_id) except InvalidTokenError as e: return Utils.response(message=Message.MESSAGE_INVALID_TOKEN, errors=e.errors, status=e.status) except UserNotFoundError as e: return Utils.response(message=Message.MESSAGE_USER_NOT_FOUND, errors=e.errors, status=e.status) return Utils.response(message=Message.MESSAGE_SUCCESSFULL, data=user_info)
def user_login(): bo = UserBo() try: token_jwt = bo.login_user(request.json) except InvalidParametersError as e: return Utils.response(message=Message.MESSAGE_INVALID_PARAMETERS, errors=e.errors, status=e.status) except AuthenticationError as e: return Utils.response(message=Message.MESSAGE_INVALID_CREDENTIALS, errors=e.errors, status=e.status) return Utils.response(message=Message.MESSAGE_SUCCESSFULL, data=dict(token_id=token_jwt))
def note_index(): try: token_id = Utils.get_bearer_token(request.headers) bo = NoteBo() notes = bo.get_notes_by_user(token_id) except InvalidTokenError as e: return Utils.response(message=Message.MESSAGE_INVALID_TOKEN, errors=e.errors, status=e.status) except UserNotFoundError as e: return Utils.response(message=Message.MESSAGE_USER_NOT_FOUND, errors=e.errors, status=e.status) return Utils.response(message=Message.MESSAGE_SUCCESSFULL, data=notes)
def generate_tokens(self, data: dict) -> dict: data["exp"] = datetime.utcnow() + timedelta( minutes=Constants.JWT_EXPIRATION_TIME) data["iat"] = datetime.utcnow() data["token"] = self.generate_session_token() token_jwt = Utils.token_encode(data) return dict(token_id=token_jwt, token_session=data['token'])
def get_user_by_token_id(self, token_id: str) -> User: try: token_decode = Utils.token_decode(token_id) user = User.get_by_id(token_decode['id']) except User.DoesNotExist as e: raise UserNotFoundError( token_decode, "User not found", "Error occurred when trying to get user by token id") return user
def user_logout(self, token_id: str) -> None: token_session = Utils.token_decode(token_id)['token'] SessionBo().delete_session_by_token(token_session)