class Login(Resource): def __init__(self): self.user_controller = UserController() self.response_creator = ResponseCreator() @staticmethod def get_tokens_dict(username): tokens_dict = { 'accessToken': flask_jwt_extended.create_access_token(identity=username), 'refreshToken': flask_jwt_extended.create_refresh_token(identity=username) } return tokens_dict @errorhandler.invalid_recaptcha @errorhandler.internal_server_error @errorhandler.bad_login_or_register_request @errorhandler.unauthorized def post(self): request_body_dict = request.json username, password = request_body_dict['username'], request_body_dict[ 'password'] if self.user_controller.is_username_password_valid(username, password): tokens_json = json.dumps(self.get_tokens_dict(username)) self.response_creator.create_response(tokens_json) return self.response_creator.response_obj return self.response_creator.unauthorized()
class ApiKey(Resource): decorators = [flask_jwt_extended.jwt_required] def __init__(self): self.user_controller = UserController() self.response_creator = ResponseCreator() @errorhandler.internal_server_error def get(self): current_user = flask_jwt_extended.get_jwt_identity() api_key = self.user_controller.get_api_key_by_username(current_user) return self.response_creator.create_response( json.dumps({"apiKey": api_key}))
class RefreshToken(Resource): decorators = [flask_jwt_extended.jwt_refresh_token_required] def __init__(self): self.response_creator = ResponseCreator() def get(self): current_user = flask_jwt_extended.get_jwt_identity() ret = { 'accessToken': flask_jwt_extended.create_access_token(identity=current_user) } return self.response_creator.create_response(json.dumps(ret))
class LogoutAccess(Resource): decorators = [flask_jwt_extended.jwt_required] def __init__(self): self.response_creator = ResponseCreator() def delete(self): from application import blacklist jti = flask_jwt_extended.get_raw_jwt()['jti'] blacklist.add(jti) message_dict = {"msg": "Successfully logged out access"} message_json = json.dumps(message_dict) return self.response_creator.create_response(message_json)
class Videos(Resource): def __init__(self): self.video_controller = VideoController() self.response_creator = ResponseCreator() self.user_controller = UserController() @errorhandler.internal_server_error @errorhandler.invalid_api_key def post(self): api_key = request.headers.get('x-api-key') user = self.user_controller.get_user_by_api_key(api_key) self.video_controller.create_video(request.json, user) return self.response_creator.ok() @flask_jwt_extended.jwt_required @errorhandler.internal_server_error def get(self): username = flask_jwt_extended.get_jwt_identity() videos_dict = self.video_controller.get_videos_by_username(username) videos_json = json.dumps(videos_dict) return self.response_creator.create_response(videos_json)