def login_endpoint() -> Tuple[Response, int]: try: if not request.json: raise APIError('Missing request body', 400) uow = UnitOfWork() email = request.json['email'] password = request.json['password'] access_token = get_access_token(uow, email, password) token_store = TokenStore() user = Authentication().get_jwt_identity(access_token) user_id = int(user['user_id']) username = user['username'] refresh_token = set_refresh_token(token_store, user_id, username) res = jsonify({'access_token': access_token}) res.set_cookie('refresh_token', refresh_token, httponly=True, domain='.talel.io', secure=not current_app.config['DEBUG']) return res, 200 except KeyError as error: raise APIError(f'Expected {error} key', 400) from error except AccountError as error: raise APIError(str(error), 401) from error
def verify_account_endpoint(token: str) -> Tuple[Response, int]: try: uow = UnitOfWork() verified_account = verify_account(uow, token) res_body = AccountSchema().dump(verified_account) return res_body, 200 except InvalidSignatureError as error: raise APIError(str(error), 400) from error except AccountError as error: raise APIError(str(error), 400) from error except AccountVerificationError as error: raise APIError(str(error), 400) from error
def create_article_endpoint() -> Tuple[Response, int]: authorization_header = request.headers.get('Authorization') @authorization_required(authorization_header) def protected_create_article_endpoint() -> Tuple[Response, int]: try: if not request.json: raise APIError('Missing request body', 400) access_token = extract_access_token_from_authorization_header( cast(str, authorization_header)) uow = UnitOfWork() user = Authentication().get_jwt_identity(access_token) user_id = int(user['user_id']) title = request.json['title'] body = request.json['body'] created_article = create_article(uow, user_id, title, body) res_body = ArticleSchema().dump(created_article) return res_body, 201 except KeyError as error: raise APIError(f'Expected {error} key', 400) from error try: return protected_create_article_endpoint() except AuthorizationError as error: raise APIError(str(error), 403) from error
def upload_images_endpoint() -> Tuple[Response, int]: authorization_header = request.headers.get('Authorization') @authorization_required(authorization_header) def protected_upload_images_endpoint() -> Tuple[Response, int]: try: access_token = extract_access_token_from_authorization_header( cast(str, authorization_header)) user = Authentication().get_jwt_identity(access_token) asset_store = AssetStore() image_streams = get_streams_from_request_files(request.files) user_id = int(user['user_id']) bucket = current_app.config['S3_BUCKET'] image_objects_urls = upload_images(asset_store, image_streams, user_id, bucket) return jsonify(image_objects_urls), 200 except ImageError as error: raise APIError(str(error), 400) from error except client('s3').exceptions.ClientError as error: raise APIError(str(error), 400) from error try: return protected_upload_images_endpoint() except AuthorizationError as error: raise APIError(str(error), 403) from error
def new_access_token_endpoint() -> Tuple[Response, int]: try: refresh_token = request.cookies.get('refresh_token') or '' token_store = TokenStore() user = Authentication().get_jwt_identity(refresh_token) user_id = int(user['user_id']) username = user['username'] assert verify_refresh_token(token_store, user_id, refresh_token) access_token = generate_access_token(user_id, username) return jsonify({'access_token': access_token}), 200 except InvalidSignatureError as error: raise APIError(str(error), 400) from error except DecodeError as error: raise APIError(str(error), 400) from error except TokenError as error: raise APIError(str(error), 401) from error
def register_account_endpoint() -> Tuple[Response, int]: try: if not request.json: raise APIError('Missing request body', 400) uow = UnitOfWork() email = request.json['email'] password = request.json['password'] username = request.json['username'] registered_account = register_account(uow, email, password, username) res_body = AccountSchema().dump(registered_account) return res_body, 201 except KeyError as error: raise APIError(f'Expected {error} key', 400) from error except AccountRegistrationError as error: raise APIError(str(error), 400) from error
def get_user_projects_endpoint(username: str) -> Tuple[Response, int]: try: uow = UnitOfWork() user_projects = get_user_projects(uow, username) res_body = ProjectSchema(many=True).dump(user_projects) return jsonify(res_body), 200 except UserError as error: raise APIError(str(error), 400) from error
def protected_create_project_endpoint() -> Tuple[Response, int]: try: if not request.json: raise APIError('Missing request body', 400) access_token = extract_access_token_from_authorization_header( cast(str, authorization_header)) uow = UnitOfWork() user = Authentication().get_jwt_identity(access_token) user_id = int(user['user_id']) title = request.json['title'] body = request.json['body'] created_project = create_project(uow, user_id, title, body) res_body = ProjectSchema().dump(created_project) return res_body, 201 except KeyError as error: raise APIError(f'Expected {error} key', 400) from error
def protected_logout_endpoint() -> Tuple[Response, int]: try: access_token = extract_access_token_from_authorization_header( cast(str, authorization_header)) token_store = TokenStore() user = Authentication().get_jwt_identity(access_token) user_id = int(user['user_id']) assert delete_refresh_token(token_store, user_id) return jsonify({'message': 'Successfully logged out'}), 200 except TokenError as error: raise APIError(str(error), 409) from error