import datetime import os import re import secrets from typing import Dict, Tuple, Union import jwt from flask import ( Blueprint, Response, current_app, request, send_from_directory, ) from sqlalchemy import exc, func from werkzeug.exceptions import RequestEntityTooLarge from werkzeug.utils import secure_filename from fittrackee import appLog, db from fittrackee.emails.tasks import ( account_confirmation_email, email_updated_to_current_address, email_updated_to_new_address, password_change_email, reset_password_email, ) from fittrackee.files import get_absolute_file_path from fittrackee.oauth2.server import require_auth from fittrackee.responses import ( DataNotFoundErrorResponse, ForbiddenErrorResponse, HttpResponse, InvalidPayloadErrorResponse, NotFoundErrorResponse, PayloadTooLargeErrorResponse, UnauthorizedErrorResponse, get_error_response_if_file_is_invalid, handle_error_and_return_response, ) from fittrackee.utils import get_readable_duration from fittrackee.workouts.models import Sport from .exceptions import UserControlsException, UserCreationException from .models import BlacklistedToken, User, UserDataExport, UserSportPreference from .tasks import export_data from .utils.admin import UserManagerService from .utils.controls import check_password, is_valid_email from .utils.language import get_language from .utils.token import decode_user_token auth_blueprint = Blueprint('auth', __name__) HEX_COLOR_REGEX = regex = "^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$" NOT_FOUND_MESSAGE = 'the requested URL was not found on the server' def send_account_confirmation_email(user: User) -> None: if current_app.config['CAN_SEND_EMAILS']: ui_url = current_app.config['UI_URL'] email_data = { 'username': user.username, 'fittrackee_url': ui_url, 'operating_system': request.user_agent.platform, # type: ignore # noqa 'browser_name': request.user_agent.browser, # type: ignore 'account_confirmation_url': ( f'{ui_url}/account-confirmation' f'?token={user.confirmation_token}' ), } user_data = { 'language': get_language(user.language), 'email': user.email, } account_confirmation_email.send(user_data, email_data) @auth_blueprint.route('/auth/register', methods=['POST']) def register_user() -> Union[Tuple[Dict, int], HttpResponse]: """ Register a user and send confirmation email. The newly created account is inactive. The user must confirm his email to activate it. **Example request**: .. sourcecode:: http POST /api/auth/register HTTP/1.1 Content-Type: application/json **Example responses**: - success: .. sourcecode:: http HTTP/1.1 200 SUCCESS Content-Type: application/json { "status": "success" } - error on registration: .. sourcecode:: http HTTP/1.1 400 BAD REQUEST Content-Type: application/json { "message": "Errors: email: valid email must be provided\\n", "status": "error" } : Union[Dict, HttpResponse]: """ User login. Only user with an active account can log in. **Example request**: .. sourcecode:: http POST /api/auth/login HTTP/1.1 Content-Type: application/json **Example responses**: - successful login: .. sourcecode:: http HTTP/1.1 200 OK Content-Type: application/json { "auth_token": "JSON Web Token", "message": "successfully logged in", "status": "success" } - error on login .. sourcecode:: http HTTP/1.1 401 UNAUTHORIZED Content-Type: application/json { "message": "invalid credentials", "status": "error" } : Union[Dict, HttpResponse]: """ Get authenticated user info (profile, account, preferences). **Scope**: ``profile:read`` **Example request**: .. sourcecode:: http GET /api/auth/profile HTTP/1.1 Content-Type: application/json **Example response**: .. sourcecode:: http HTTP/1.1 200 OK Content-Type: application/json { "data": { "accepted_privacy_policy": true, "admin": false, "bio": null, "birth_date": null, "created_at": "Sun, 14 Jul 2019 14:09:58 GMT", "date_format": "dd/MM/yyyy", "display_ascent": true, "email": "sam@example.com", "email_to_confirm": null, "first_name": null, "imperial_units": false, "is_active": true, "language": "en", "last_name": null, "location": null, "nb_sports": 3, "nb_workouts": 6, "picture": false, "records": [ { "id": 9, "record_type": "AS", "sport_id": 1, "user": "sam", "value": 18, "workout_date": "Sun, 07 Jul 2019 08:00:00 GMT", "workout_id": "hvYBqYBRa7wwXpaStWR4V2" }, { "id": 10, "record_type": "FD", "sport_id": 1, "user": "sam", "value": 18, "workout_date": "Sun, 07 Jul 2019 08:00:00 GMT", "workout_id": "hvYBqYBRa7wwXpaStWR4V2" }, { "id": 13, "record_type": "HA", "sport_id": 1, "user": "Sam", "value": 43.97, "workout_date": "Sun, 07 Jul 2019 08:00:00 GMT", "workout_id": "hvYBqYBRa7wwXpaStWR4V2" }, { "id": 11, "record_type": "LD", "sport_id": 1, "user": "sam", "value": "1:01:00", "workout_date": "Sun, 07 Jul 2019 08:00:00 GMT", "workout_id": "hvYBqYBRa7wwXpaStWR4V2" }, { "id": 12, "record_type": "MS", "sport_id": 1, "user": "sam", "value": 18, "workout_date": "Sun, 07 Jul 2019 08:00:00 GMT", "workout_id": "hvYBqYBRa7wwXpaStWR4V2" } ], "sports_list": [ 1, 4, 6 ], "start_elevation_at_zero": false, "timezone": "Europe/Paris", "total_ascent": 720.35, "total_distance": 67.895, "total_duration": "6:50:27", "use_dark_mode": null, "use_raw_gpx_speed": false, "username": "sam", "weekm": false }, "status": "success" } :reqheader Authorization: OAuth 2.0 Bearer Token :statuscode 200: ``success`` :statuscode 401: - ``provide a valid auth token`` - ``signature expired, please log in again`` - ``invalid token, please log in again`` """ return {'status': 'success', 'data': auth_user.serialize(auth_user)} @auth_blueprint.route('/auth/profile/edit', methods=['POST']) @require_auth(scopes=['profile:write']) def edit_user(auth_user: User) -> Union[Dict, HttpResponse]: """ Edit authenticated user profile. **Scope**: ``profile:write`` **Example request**: .. sourcecode:: http POST /api/auth/profile/edit HTTP/1.1 Content-Type: application/json **Example response**: .. sourcecode:: http HTTP/1.1 200 OK Content-Type: application/json { "data": { "accepted_privacy_policy": true, "admin": false, "bio": null, "birth_date": null, "created_at": "Sun, 14 Jul 2019 14:09:58 GMT", "date_format": "dd/MM/yyyy", "display_ascent": true, "email": "sam@example.com", "email_to_confirm": null, "first_name": null, "imperial_units": false, "is_active": true, "language": "en", "last_name": null, "location": null, "nb_sports": 3, "nb_workouts": 6, "picture": false, "records": [ { "id": 9, "record_type": "AS", "sport_id": 1, "user": "sam", "value": 18, "workout_date": "Sun, 07 Jul 2019 08:00:00 GMT", "workout_id": "hvYBqYBRa7wwXpaStWR4V2" }, { "id": 10, "record_type": "FD", "sport_id": 1, "user": "sam", "value": 18, "workout_date": "Sun, 07 Jul 2019 08:00:00 GMT", "workout_id": "hvYBqYBRa7wwXpaStWR4V2" }, { "id": 13, "record_type": "HA", "sport_id": 1, "user": "Sam", "value": 43.97, "workout_date": "Sun, 07 Jul 2019 08:00:00 GMT", "workout_id": "hvYBqYBRa7wwXpaStWR4V2" }, { "id": 11, "record_type": "LD", "sport_id": 1, "user": "sam", "value": "1:01:00", "workout_date": "Sun, 07 Jul 2019 08:00:00 GMT", "workout_id": "hvYBqYBRa7wwXpaStWR4V2" }, { "id": 12, "record_type": "MS", "sport_id": 1, "user": "sam", "value": 18, "workout_date": "Sun, 07 Jul 2019 08:00:00 GMT", "workout_id": "hvYBqYBRa7wwXpaStWR4V2" } ], "sports_list": [ 1, 4, 6 ], "start_elevation_at_zero": false, "timezone": "Europe/Paris", "total_ascent": 720.35, "total_distance": 67.895, "total_duration": "6:50:27", "use_dark_mode": null, "use_raw_gpx_speed": false, "username": "sam" "weekm": true, }, "message": "user profile updated", "status": "success" } := user_mandatory_data: return InvalidPayloadErrorResponse() first_name = post_data.get('first_name') last_name = post_data.get('last_name') bio = post_data.get('bio') birth_date = post_data.get('birth_date') location = post_data.get('location') try: auth_user.first_name = first_name auth_user.last_name = last_name auth_user.bio = bio auth_user.location = location auth_user.birth_date = ( datetime.datetime.strptime(birth_date, '%Y-%m-%d') if birth_date else None ) db.session.commit() return { 'status': 'success', 'message': 'user profile updated', 'data': auth_user.serialize(auth_user), } # handler errors except (exc.IntegrityError, exc.OperationalError, ValueError) as e: return handle_error_and_return_response(e, db=db) @auth_blueprint.route('/auth/profile/edit/account', methods=['PATCH']) @require_auth(scopes=['profile:write']) def update_user_account(auth_user: User) -> Union[Dict, HttpResponse]: """ Update authenticated user email and password. It sends emails if sending is enabled: - Password change - Email change: - one to the current address to inform user - another one to the new address to confirm it. **Scope**: ``profile:write`` **Example request**: .. sourcecode:: http PATCH /api/auth/profile/edit/account HTTP/1.1 Content-Type: application/json **Example response**: .. sourcecode:: http HTTP/1.1 200 OK Content-Type: application/json { "data": { "accepted_privacy_policy": true, "admin": false, "bio": null, "birth_date": null, "created_at": "Sun, 14 Jul 2019 14:09:58 GMT", "date_format": "dd/MM/yyyy", "display_ascent": true, "email": "sam@example.com", "email_to_confirm": null, "first_name": null, "imperial_units": false, "is_active": true, "language": "en", "last_name": null, "location": null, "nb_sports": 3, "nb_workouts": 6, "picture": false, "records": [ { "id": 9, "record_type": "AS", "sport_id": 1, "user": "sam", "value": 18, "workout_date": "Sun, 07 Jul 2019 08:00:00 GMT", "workout_id": "hvYBqYBRa7wwXpaStWR4V2" }, { "id": 10, "record_type": "FD", "sport_id": 1, "user": "sam", "value": 18, "workout_date": "Sun, 07 Jul 2019 08:00:00 GMT", "workout_id": "hvYBqYBRa7wwXpaStWR4V2" }, { "id": 13, "record_type": "HA", "sport_id": 1, "user": "Sam", "value": 43.97, "workout_date": "Sun, 07 Jul 2019 08:00:00 GMT", "workout_id": "hvYBqYBRa7wwXpaStWR4V2" }, { "id": 11, "record_type": "LD", "sport_id": 1, "user": "sam", "value": "1:01:00", "workout_date": "Sun, 07 Jul 2019 08:00:00 GMT", "workout_id": "hvYBqYBRa7wwXpaStWR4V2" }, { "id": 12, "record_type": "MS", "sport_id": 1, "user": "sam", "value": 18, "workout_date": "Sun, 07 Jul 2019 08:00:00 GMT", "workout_id": "hvYBqYBRa7wwXpaStWR4V2" } ], "sports_list": [ 1, 4, 6 ], "start_elevation_at_zero": false, "timezone": "Europe/Paris", "total_ascent": 720.35, "total_distance": 67.895, "total_duration": "6:50:27", "use_dark_mode": null, "use_raw_gpx_speed": false, "username": "sam" "weekm": true, }, "message": "user account updated", "status": "success" } : Union[Dict, HttpResponse]: """ Edit authenticated user preferences. Supported date formats: - ``MM/dd/yyyy`` (default value) - ``dd/MM/yyyy`` - ``yyyy-MM-dd`` - ``date_string``, corresponding on client to: - ``MMM. do, yyyy`` for ``en`` locale - ``d MMM yyyy`` for ``es``, ``fr``, ``gl``, ``it`` and ``nl`` locales - ``do MMM yyyy`` for ``de`` and ``nb`` locales **Scope**: ``profile:write`` **Example request**: .. sourcecode:: http POST /api/auth/profile/edit/preferences HTTP/1.1 Content-Type: application/json **Example response**: .. sourcecode:: http HTTP/1.1 200 OK Content-Type: application/json { "data": { "accepted_privacy_policy": true, "admin": false, "bio": null, "birth_date": null, "created_at": "Sun, 14 Jul 2019 14:09:58 GMT", "date_format": "MM/dd/yyyy", "display_ascent": true, "email": "sam@example.com", "email_to_confirm": null, "first_name": null, "imperial_units": false, "is_active": true, "language": "en", "last_name": null, "location": null, "nb_sports": 3, "nb_workouts": 6, "picture": false, "records": [ { "id": 9, "record_type": "AS", "sport_id": 1, "user": "sam", "value": 18, "workout_date": "Sun, 07 Jul 2019 08:00:00 GMT", "workout_id": "hvYBqYBRa7wwXpaStWR4V2" }, { "id": 10, "record_type": "FD", "sport_id": 1, "user": "sam", "value": 18, "workout_date": "Sun, 07 Jul 2019 08:00:00 GMT", "workout_id": "hvYBqYBRa7wwXpaStWR4V2" }, { "id": 13, "record_type": "HA", "sport_id": 1, "user": "Sam", "value": 43.97, "workout_date": "Sun, 07 Jul 2019 08:00:00 GMT", "workout_id": "hvYBqYBRa7wwXpaStWR4V2" }, { "id": 11, "record_type": "LD", "sport_id": 1, "user": "sam", "value": "1:01:00", "workout_date": "Sun, 07 Jul 2019 08:00:00 GMT", "workout_id": "hvYBqYBRa7wwXpaStWR4V2" }, { "id": 12, "record_type": "MS", "sport_id": 1, "user": "sam", "value": 18, "workout_date": "Sun, 07 Jul 2019 08:00:00 GMT", "workout_id": "hvYBqYBRa7wwXpaStWR4V2" } ], "sports_list": [ 1, 4, 6 ], "start_elevation_at_zero": true, "timezone": "Europe/Paris", "total_ascent": 720.35, "total_distance": 67.895, "total_duration": "6:50:27", "use_dark_mode": null, "use_raw_gpx_speed": true, "username": "sam" "weekm": true, }, "message": "user preferences updated", "status": "success" } := user_mandatory_data: return InvalidPayloadErrorResponse() date_format = post_data.get('date_format') display_ascent = post_data.get('display_ascent') imperial_units = post_data.get('imperial_units') language = get_language(post_data.get('language')) start_elevation_at_zero = post_data.get('start_elevation_at_zero') use_raw_gpx_speed = post_data.get('use_raw_gpx_speed') use_dark_mode = post_data.get('use_dark_mode') timezone = post_data.get('timezone') weekm = post_data.get('weekm') try: auth_user.date_format = date_format auth_user.display_ascent = display_ascent auth_user.imperial_units = imperial_units auth_user.language = language auth_user.start_elevation_at_zero = start_elevation_at_zero auth_user.timezone = timezone auth_user.use_dark_mode = use_dark_mode auth_user.use_raw_gpx_speed = use_raw_gpx_speed auth_user.weekm = weekm db.session.commit() return { 'status': 'success', 'message': 'user preferences updated', 'data': auth_user.serialize(auth_user), } # handler errors except (exc.IntegrityError, exc.OperationalError, ValueError) as e: return handle_error_and_return_response(e, db=db) @auth_blueprint.route('/auth/profile/edit/sports', methods=['POST']) @require_auth(scopes=['profile:write']) def edit_user_sport_preferences( auth_user: User, ) -> Union[Dict, HttpResponse]: """ Edit authenticated user sport preferences. **Scope**: ``profile:write`` **Example request**: .. sourcecode:: http POST /api/auth/profile/edit/sports HTTP/1.1 Content-Type: application/json **Example response**: .. sourcecode:: http HTTP/1.1 200 OK Content-Type: application/json { "data": { "color": "#000000", "is_active": true, "sport_id": 1, "stopped_speed_threshold": 1, "user_id": 1 }, "message": "user sport preferences updated", "status": "success" } :', methods=['DELETE'] ) @require_auth(scopes=['profile:write']) def reset_user_sport_preferences( auth_user: User, sport_id: int ) -> Union[Tuple[Dict, int], HttpResponse]: """ Reset authenticated user preferences for a given sport. **Scope**: ``profile:write`` **Example request**: .. sourcecode:: http DELETE /api/auth/profile/reset/sports/1 HTTP/1.1 Content-Type: application/json **Example response**: .. sourcecode:: http HTTP/1.1 204 OK Content-Type: application/json :param string sport_id: sport id :reqheader Authorization: OAuth 2.0 Bearer Token :statuscode 204: user preferences deleted :statuscode 401: - ``provide a valid auth token`` - ``signature expired, please log in again`` - ``invalid token, please log in again`` :statuscode 404: ``sport does not exist`` :statuscode 500: ``error, please try again or contact the administrator`` """ sport = Sport.query.filter_by(id=sport_id).first() if not sport: return NotFoundErrorResponse('sport does not exist') try: user_sport = UserSportPreference.query.filter_by( user_id=auth_user.id, sport_id=sport_id, ).first() if user_sport: db.session.delete(user_sport) db.session.commit() return {'status': 'no content'}, 204 # handler errors except (exc.IntegrityError, exc.OperationalError) as e: return handle_error_and_return_response(e, db=db) @auth_blueprint.route('/auth/picture', methods=['POST']) @require_auth(scopes=['profile:write']) def edit_picture(auth_user: User) -> Union[Dict, HttpResponse]: """ Update authenticated user picture. **Scope**: ``profile:write`` **Example request**: .. sourcecode:: http POST /api/auth/picture HTTP/1.1 Content-Type: multipart/form-data **Example response**: .. sourcecode:: http HTTP/1.1 200 OK Content-Type: application/json { "message": "user picture updated", "status": "success" } :form file: image file (allowed extensions: .jpg, .png, .gif) :reqheader Authorization: OAuth 2.0 Bearer Token :statuscode 200: ``user picture updated`` :statuscode 400: - ``invalid payload`` - ``no file part`` - ``no selected file`` - ``file extension not allowed`` :statuscode 401: - ``provide a valid auth token`` - ``signature expired, please log in again`` - ``invalid token, please log in again`` :statuscode 413: ``error during picture update: file size exceeds 1.0MB`` :statuscode 500: ``error during picture update`` """ try: response_object = get_error_response_if_file_is_invalid( 'picture', request ) except RequestEntityTooLarge as e: appLog.error(e) return PayloadTooLargeErrorResponse( file_type='picture', file_size=request.content_length, max_size=current_app.config['MAX_CONTENT_LENGTH'], ) if response_object: return response_object file = request.files['file'] filename = secure_filename(file.filename) # type: ignore dirpath = os.path.join( current_app.config['UPLOAD_FOLDER'], 'pictures', str(auth_user.id) ) if not os.path.exists(dirpath): os.makedirs(dirpath) absolute_picture_path = os.path.join(dirpath, filename) relative_picture_path = os.path.join( 'pictures', str(auth_user.id), filename ) try: if auth_user.picture is not None: old_picture_path = get_absolute_file_path(auth_user.picture) if os.path.isfile(get_absolute_file_path(old_picture_path)): os.remove(old_picture_path) file.save(absolute_picture_path) auth_user.picture = relative_picture_path db.session.commit() return { 'status': 'success', 'message': 'user picture updated', } except (exc.IntegrityError, ValueError) as e: return handle_error_and_return_response( e, message='error during picture update', status='fail', db=db ) @auth_blueprint.route('/auth/picture', methods=['DELETE']) @require_auth(scopes=['profile:write']) def del_picture(auth_user: User) -> Union[Tuple[Dict, int], HttpResponse]: """ Delete authenticated user picture. **Scope**: ``profile:write`` **Example request**: .. sourcecode:: http DELETE /api/auth/picture HTTP/1.1 Content-Type: application/json **Example response**: .. sourcecode:: http HTTP/1.1 204 NO CONTENT Content-Type: application/json :reqheader Authorization: OAuth 2.0 Bearer Token :statuscode 204: picture deleted :statuscode 401: - ``provide a valid auth token`` - ``signature expired, please log in again`` - ``invalid token, please log in again`` :statuscode 500: ``error during picture deletion`` """ try: picture_path = get_absolute_file_path(auth_user.picture) if os.path.isfile(picture_path): os.remove(picture_path) auth_user.picture = None db.session.commit() return {'status': 'no content'}, 204 except (exc.IntegrityError, ValueError) as e: return handle_error_and_return_response( e, message='error during picture deletion', status='fail', db=db ) @auth_blueprint.route('/auth/password/reset-request', methods=['POST']) def request_password_reset() -> Union[Dict, HttpResponse]: """ Handle password reset request. If email sending is disabled, this endpoint is not available. **Example request**: .. sourcecode:: http POST /api/auth/password/reset-request HTTP/1.1 Content-Type: application/json **Example response**: .. sourcecode:: http HTTP/1.1 200 OK Content-Type: application/json { "message": "password reset request processed", "status": "success" } : Union[Dict, HttpResponse]: """ Update user password after password reset request. It sends emails if sending is enabled. **Example request**: .. sourcecode:: http POST /api/auth/password/update HTTP/1.1 Content-Type: application/json **Example response**: .. sourcecode:: http HTTP/1.1 200 OK Content-Type: application/json { "message": "password updated", "status": "success" } : Union[Dict, HttpResponse]: """ Update user email after confirmation. **Example request**: .. sourcecode:: http POST /api/auth/email/update HTTP/1.1 Content-Type: application/json **Example response**: .. sourcecode:: http HTTP/1.1 200 OK Content-Type: application/json { "message": "email updated", "status": "success" } : Union[Dict, HttpResponse]: """ Activate user account after registration. **Example request**: .. sourcecode:: http POST /api/auth/account/confirm HTTP/1.1 Content-Type: application/json **Example response**: .. sourcecode:: http HTTP/1.1 200 OK Content-Type: application/json { "auth_token": "JSON Web Token", "message": "account confirmation successful", "status": "success" } : Union[Dict, HttpResponse]: """ Resend email with instructions to confirm account. If email sending is disabled, this endpoint is not available. **Example request**: .. sourcecode:: http POST /api/auth/account/resend-confirmation HTTP/1.1 Content-Type: application/json **Example response**: .. sourcecode:: http HTTP/1.1 200 OK Content-Type: application/json { "message": "confirmation email resent", "status": "success" } : Union[Tuple[Dict, int], HttpResponse]: """ User logout. If a valid token is provided, it will be blacklisted. **Example request**: .. sourcecode:: http POST /api/auth/logout HTTP/1.1 Content-Type: application/json **Example responses**: - successful logout: .. sourcecode:: http HTTP/1.1 200 OK Content-Type: application/json { "message": "successfully logged out", "status": "success" } - error on logout: .. sourcecode:: http HTTP/1.1 401 UNAUTHORIZED Content-Type: application/json { "message": "provide a valid auth token", "status": "error" } :reqheader Authorization: OAuth 2.0 Bearer Token :statuscode 200: ``successfully logged out`` :statuscode 401: - ``provide a valid auth token`` - ``The access token provided is expired, revoked, malformed, or invalid for other reasons.`` :statuscode 500: ``error on token blacklist`` """ auth_token = request.headers.get('Authorization', '').split(' ')[1] try: db.session.add(BlacklistedToken(token=auth_token)) db.session.commit() except Exception: return { 'status': 'error', 'message': 'error on token blacklist', }, 500 return { 'status': 'success', 'message': 'successfully logged out', }, 200 @auth_blueprint.route('/auth/account/privacy-policy', methods=['POST']) @require_auth() def accept_privacy_policy(auth_user: User) -> Union[Dict, HttpResponse]: """ The authenticated user accepts the privacy policy. **Example request**: .. sourcecode:: http POST /auth/account/privacy-policy HTTP/1.1 Content-Type: application/json **Example response**: .. sourcecode:: http HTTP/1.1 200 OK Content-Type: application/json { "status": "success" } : Union[Dict, HttpResponse]: """ Request a data export for authenticated user. **Example request**: .. sourcecode:: http POST /auth/account/export/request HTTP/1.1 Content-Type: application/json **Example response**: .. sourcecode:: http HTTP/1.1 200 OK Content-Type: application/json { "status": "success", "request": { "created_at": "Wed, 01 Mar 2023 12:31:17 GMT", "status": "in_progress", "file_name": null, "file_size": null } } :reqheader Authorization: OAuth 2.0 Bearer Token :statuscode 200: ``success`` :statuscode 400: - ``ongoing request exists`` - ``completed request already exists`` :statuscode 401: - ``provide a valid auth token`` - ``signature expired, please log in again`` - ``invalid token, please log in again`` :statuscode 500: ``error, please try again or contact the administrator`` """ existing_export_request = UserDataExport.query.filter_by( user_id=auth_user.id ).first() if existing_export_request: if not existing_export_request.completed: return InvalidPayloadErrorResponse("ongoing request exists") export_expiration = current_app.config["DATA_EXPORT_EXPIRATION"] if existing_export_request.created_at > ( datetime.datetime.utcnow() - datetime.timedelta(hours=export_expiration) ): return InvalidPayloadErrorResponse( "completed request already exists" ) try: if existing_export_request: db.session.delete(existing_export_request) db.session.flush() export_request = UserDataExport(user_id=auth_user.id) db.session.add(export_request) db.session.commit() export_data.send(export_request_id=export_request.id) return {"status": "success", "request": export_request.serialize()} except (exc.IntegrityError, exc.OperationalError, ValueError) as e: return handle_error_and_return_response(e, db=db) @auth_blueprint.route('/auth/account/export', methods=['GET']) @require_auth() def get_user_data_export(auth_user: User) -> Union[Dict, HttpResponse]: """ Get a data export info for authenticated user if a request exists. It returns: - export creation date - export status (``in_progress``, ``successful`` and ``errored``) - file name and size (in bytes) when export is successful **Example request**: .. sourcecode:: http GET /auth/account/export HTTP/1.1 Content-Type: application/json **Example response**: - if a request exists: .. sourcecode:: http HTTP/1.1 200 OK Content-Type: application/json { "status": "success", "request": { "created_at": "Wed, 01 Mar 2023 12:31:17 GMT", "status": "successful", "file_name": "archive_rgjsR3fHt295ywNQr5Yp.zip", "file_size": 924 } } - if no request: .. sourcecode:: http HTTP/1.1 200 OK Content-Type: application/json { "status": "success", "request": null } :reqheader Authorization: OAuth 2.0 Bearer Token :statuscode 200: ``success`` :statuscode 401: - ``provide a valid auth token`` - ``signature expired, please log in again`` - ``invalid token, please log in again`` """ export_request = UserDataExport.query.filter_by( user_id=auth_user.id ).first() return { "status": "success", "request": export_request.serialize() if export_request else None, } @auth_blueprint.route( '/auth/account/export/', methods=['GET'] ) @require_auth() def download_data_export( auth_user: User, file_name: str ) -> Union[Response, HttpResponse]: """ Download a data export archive **Example request**: .. sourcecode:: http GET /auth/account/export/download/archive_rgjsR3fHr5Yp.zip HTTP/1.1 Content-Type: application/json **Example response**: .. sourcecode:: http HTTP/1.1 200 OK Content-Type: application/x-gzip :param string file_name: filename :reqheader Authorization: OAuth 2.0 Bearer Token :statuscode 200: ``success`` :statuscode 401: - ``provide a valid auth token`` - ``signature expired, please log in again`` - ``invalid token, please log in again`` :statuscode 404: ``file not found`` """ export_request = UserDataExport.query.filter_by( user_id=auth_user.id ).first() if ( not export_request or not export_request.completed or export_request.file_name != file_name ): return DataNotFoundErrorResponse( data_type="archive", message="file not found" ) return send_from_directory( f"{current_app.config['UPLOAD_FOLDER']}/exports/{auth_user.id}", export_request.file_name, mimetype='application/zip', as_attachment=True, )