2018-01-01 16:59:46 +01:00
|
|
|
import datetime
|
2018-01-01 21:54:03 +01:00
|
|
|
import os
|
2021-01-02 19:28:03 +01:00
|
|
|
from typing import Dict, Tuple, Union
|
2018-01-14 20:49:35 +01:00
|
|
|
|
2020-05-17 16:42:44 +02:00
|
|
|
import jwt
|
2021-01-20 16:47:00 +01:00
|
|
|
from flask import Blueprint, current_app, request
|
|
|
|
from sqlalchemy import exc, or_
|
|
|
|
from werkzeug.exceptions import RequestEntityTooLarge
|
|
|
|
from werkzeug.utils import secure_filename
|
|
|
|
|
2020-09-16 15:41:02 +02:00
|
|
|
from fittrackee import appLog, bcrypt, db
|
2021-01-01 16:39:25 +01:00
|
|
|
from fittrackee.responses import (
|
|
|
|
ForbiddenErrorResponse,
|
2021-01-02 19:28:03 +01:00
|
|
|
HttpResponse,
|
2021-01-01 16:39:25 +01:00
|
|
|
InvalidPayloadErrorResponse,
|
|
|
|
PayloadTooLargeErrorResponse,
|
|
|
|
UnauthorizedErrorResponse,
|
|
|
|
handle_error_and_return_response,
|
|
|
|
)
|
2020-09-16 15:41:02 +02:00
|
|
|
from fittrackee.tasks import reset_password_email
|
2021-02-20 21:37:31 +01:00
|
|
|
from fittrackee.utils import get_readable_duration, verify_extension_and_size
|
2021-01-20 16:47:00 +01:00
|
|
|
from fittrackee.workouts.utils_files import get_absolute_file_path
|
2017-12-16 21:00:46 +01:00
|
|
|
|
2021-01-20 16:24:01 +01:00
|
|
|
from .decorators import authenticate
|
2017-12-16 21:00:46 +01:00
|
|
|
from .models import User
|
2021-02-20 21:37:31 +01:00
|
|
|
from .utils import check_passwords, register_controls
|
2020-05-17 16:42:44 +02:00
|
|
|
from .utils_token import decode_user_token
|
2017-12-16 21:00:46 +01:00
|
|
|
|
|
|
|
auth_blueprint = Blueprint('auth', __name__)
|
|
|
|
|
|
|
|
|
|
|
|
@auth_blueprint.route('/auth/register', methods=['POST'])
|
2021-01-02 19:28:03 +01:00
|
|
|
def register_user() -> Union[Tuple[Dict, int], HttpResponse]:
|
2019-07-20 14:27:05 +02:00
|
|
|
"""
|
|
|
|
register a user
|
|
|
|
|
|
|
|
**Example request**:
|
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
2019-07-20 21:57:35 +02:00
|
|
|
POST /api/auth/register HTTP/1.1
|
2019-07-20 14:27:05 +02:00
|
|
|
Content-Type: application/json
|
|
|
|
|
|
|
|
**Example responses**:
|
|
|
|
|
|
|
|
- successful registration
|
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
|
|
|
HTTP/1.1 201 CREATED
|
|
|
|
Content-Type: application/json
|
|
|
|
|
|
|
|
{
|
|
|
|
"auth_token": "JSON Web Token",
|
2021-11-01 09:44:10 +01:00
|
|
|
"message": "successfully registered",
|
2019-07-20 14:27:05 +02:00
|
|
|
"status": "success"
|
|
|
|
}
|
|
|
|
|
|
|
|
- error on registration
|
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
|
|
|
HTTP/1.1 400 BAD REQUEST
|
|
|
|
Content-Type: application/json
|
|
|
|
|
|
|
|
{
|
2021-11-01 09:44:10 +01:00
|
|
|
"message": "Errors: email: valid email must be provided\\n",
|
2019-07-20 14:27:05 +02:00
|
|
|
"status": "error"
|
|
|
|
}
|
|
|
|
|
|
|
|
:<json string username: user name (3 to 12 characters required)
|
|
|
|
:<json string email: user email
|
|
|
|
:<json string password: password (8 characters required)
|
|
|
|
:<json string password_conf: password confirmation
|
|
|
|
|
2021-11-01 09:44:10 +01:00
|
|
|
:statuscode 201: successfully registered
|
2019-07-20 14:27:05 +02:00
|
|
|
:statuscode 400:
|
2021-11-01 09:44:10 +01:00
|
|
|
- invalid payload
|
|
|
|
- sorry, that user already exists
|
2019-07-20 14:27:05 +02:00
|
|
|
- Errors:
|
2021-11-01 09:44:10 +01:00
|
|
|
- username: 3 to 12 characters required
|
|
|
|
- email: valid email must be provided
|
|
|
|
- password: password and password confirmation don't match
|
|
|
|
- password: 8 characters required
|
2019-08-25 12:06:58 +02:00
|
|
|
:statuscode 403:
|
2021-11-01 09:44:10 +01:00
|
|
|
error, registration is disabled
|
2019-07-20 14:27:05 +02:00
|
|
|
:statuscode 500:
|
2021-11-01 09:44:10 +01:00
|
|
|
error, please try again or contact the administrator
|
2019-07-20 14:27:05 +02:00
|
|
|
|
|
|
|
"""
|
2019-11-13 18:40:01 +01:00
|
|
|
if not current_app.config.get('is_registration_enabled'):
|
2021-11-01 09:44:10 +01:00
|
|
|
return ForbiddenErrorResponse('error, registration is disabled')
|
2021-01-01 16:39:25 +01:00
|
|
|
|
2017-12-16 21:00:46 +01:00
|
|
|
# get post data
|
|
|
|
post_data = request.get_json()
|
2019-08-28 13:25:39 +02:00
|
|
|
if (
|
|
|
|
not post_data
|
|
|
|
or post_data.get('username') is None
|
|
|
|
or post_data.get('email') is None
|
|
|
|
or post_data.get('password') is None
|
|
|
|
or post_data.get('password_conf') is None
|
|
|
|
):
|
2021-01-01 16:39:25 +01:00
|
|
|
return InvalidPayloadErrorResponse()
|
2017-12-16 21:00:46 +01:00
|
|
|
username = post_data.get('username')
|
|
|
|
email = post_data.get('email')
|
|
|
|
password = post_data.get('password')
|
2018-01-01 11:10:39 +01:00
|
|
|
password_conf = post_data.get('password_conf')
|
|
|
|
|
2018-05-13 18:36:31 +02:00
|
|
|
try:
|
|
|
|
ret = register_controls(username, email, password, password_conf)
|
|
|
|
except TypeError as e:
|
2021-01-01 16:39:25 +01:00
|
|
|
return handle_error_and_return_response(e, db=db)
|
2018-05-13 18:36:31 +02:00
|
|
|
|
2018-01-01 11:10:39 +01:00
|
|
|
if ret != '':
|
2021-01-01 16:39:25 +01:00
|
|
|
return InvalidPayloadErrorResponse(ret)
|
2018-01-01 11:10:39 +01:00
|
|
|
|
2017-12-16 21:00:46 +01:00
|
|
|
try:
|
|
|
|
# check for existing user
|
|
|
|
user = User.query.filter(
|
2019-08-28 13:25:39 +02:00
|
|
|
or_(User.username == username, User.email == email)
|
|
|
|
).first()
|
2021-01-01 16:39:25 +01:00
|
|
|
if user:
|
|
|
|
return InvalidPayloadErrorResponse(
|
2021-11-01 09:44:10 +01:00
|
|
|
'sorry, that user already exists'
|
2021-01-01 16:39:25 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
# add new user to db
|
|
|
|
new_user = User(username=username, email=email, password=password)
|
|
|
|
new_user.timezone = 'Europe/Paris'
|
|
|
|
db.session.add(new_user)
|
|
|
|
db.session.commit()
|
|
|
|
# generate auth token
|
|
|
|
auth_token = new_user.encode_auth_token(new_user.id)
|
|
|
|
return {
|
|
|
|
'status': 'success',
|
2021-11-01 09:44:10 +01:00
|
|
|
'message': 'successfully registered',
|
2021-01-01 16:39:25 +01:00
|
|
|
'auth_token': auth_token,
|
|
|
|
}, 201
|
2017-12-16 21:00:46 +01:00
|
|
|
# handler errors
|
|
|
|
except (exc.IntegrityError, exc.OperationalError, ValueError) as e:
|
2021-01-01 16:39:25 +01:00
|
|
|
return handle_error_and_return_response(e, db=db)
|
2017-12-16 21:00:46 +01:00
|
|
|
|
|
|
|
|
|
|
|
@auth_blueprint.route('/auth/login', methods=['POST'])
|
2021-01-02 19:28:03 +01:00
|
|
|
def login_user() -> Union[Dict, HttpResponse]:
|
2019-07-20 14:27:05 +02:00
|
|
|
"""
|
|
|
|
user login
|
|
|
|
|
|
|
|
**Example request**:
|
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
2019-07-20 21:57:35 +02:00
|
|
|
POST /api/auth/login HTTP/1.1
|
2019-07-20 14:27:05 +02:00
|
|
|
Content-Type: application/json
|
|
|
|
|
|
|
|
**Example responses**:
|
|
|
|
|
|
|
|
- successful login
|
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
|
|
|
HTTP/1.1 200 OK
|
|
|
|
Content-Type: application/json
|
|
|
|
|
|
|
|
{
|
|
|
|
"auth_token": "JSON Web Token",
|
2021-11-01 09:44:10 +01:00
|
|
|
"message": "successfully logged in",
|
2019-07-20 14:27:05 +02:00
|
|
|
"status": "success"
|
|
|
|
}
|
|
|
|
|
|
|
|
- error on login
|
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
|
|
|
HTTP/1.1 404 NOT FOUND
|
|
|
|
Content-Type: application/json
|
|
|
|
|
|
|
|
{
|
2021-11-01 09:44:10 +01:00
|
|
|
"message": "invalid credentials",
|
2019-07-20 14:27:05 +02:00
|
|
|
"status": "error"
|
|
|
|
}
|
|
|
|
|
|
|
|
:<json string email: user email
|
|
|
|
:<json string password_conf: password confirmation
|
|
|
|
|
2021-11-01 09:44:10 +01:00
|
|
|
:statuscode 200: successfully logged in
|
|
|
|
:statuscode 400: invalid payload
|
|
|
|
:statuscode 401: invalid credentials
|
|
|
|
:statuscode 500: error, please try again or contact the administrator
|
2019-07-20 14:27:05 +02:00
|
|
|
|
|
|
|
"""
|
2017-12-16 21:00:46 +01:00
|
|
|
# get post data
|
|
|
|
post_data = request.get_json()
|
|
|
|
if not post_data:
|
2021-01-01 16:39:25 +01:00
|
|
|
return InvalidPayloadErrorResponse()
|
2017-12-16 21:00:46 +01:00
|
|
|
email = post_data.get('email')
|
|
|
|
password = post_data.get('password')
|
|
|
|
try:
|
|
|
|
# check for existing user
|
|
|
|
user = User.query.filter(User.email == email).first()
|
|
|
|
if user and bcrypt.check_password_hash(user.password, password):
|
|
|
|
# generate auth token
|
|
|
|
auth_token = user.encode_auth_token(user.id)
|
2021-01-01 16:39:25 +01:00
|
|
|
return {
|
2017-12-16 21:00:46 +01:00
|
|
|
'status': 'success',
|
2021-11-01 09:44:10 +01:00
|
|
|
'message': 'successfully logged in',
|
2020-12-25 19:35:15 +01:00
|
|
|
'auth_token': auth_token,
|
2017-12-16 21:00:46 +01:00
|
|
|
}
|
2021-11-01 09:44:10 +01:00
|
|
|
return UnauthorizedErrorResponse('invalid credentials')
|
2017-12-16 21:00:46 +01:00
|
|
|
# handler errors
|
|
|
|
except (exc.IntegrityError, exc.OperationalError, ValueError) as e:
|
2021-01-01 16:39:25 +01:00
|
|
|
return handle_error_and_return_response(e, db=db)
|
2017-12-16 21:00:46 +01:00
|
|
|
|
|
|
|
|
|
|
|
@auth_blueprint.route('/auth/logout', methods=['GET'])
|
2017-12-25 17:45:28 +01:00
|
|
|
@authenticate
|
2021-01-02 19:28:03 +01:00
|
|
|
def logout_user(auth_user_id: int) -> Union[Dict, HttpResponse]:
|
2019-07-20 14:27:05 +02:00
|
|
|
"""
|
|
|
|
user logout
|
|
|
|
|
|
|
|
**Example request**:
|
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
2019-07-20 21:57:35 +02:00
|
|
|
GET /api/auth/logout HTTP/1.1
|
2019-07-20 14:27:05 +02:00
|
|
|
Content-Type: application/json
|
|
|
|
|
|
|
|
**Example responses**:
|
|
|
|
|
|
|
|
- successful logout
|
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
|
|
|
HTTP/1.1 200 OK
|
|
|
|
Content-Type: application/json
|
|
|
|
|
|
|
|
{
|
2021-11-01 09:44:10 +01:00
|
|
|
"message": "successfully logged out",
|
2019-07-20 14:27:05 +02:00
|
|
|
"status": "success"
|
|
|
|
}
|
|
|
|
|
|
|
|
- error on login
|
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
|
|
|
HTTP/1.1 401 UNAUTHORIZED
|
|
|
|
Content-Type: application/json
|
|
|
|
|
|
|
|
{
|
2021-11-01 09:44:10 +01:00
|
|
|
"message": "provide a valid auth token",
|
2019-07-20 14:27:05 +02:00
|
|
|
"status": "error"
|
|
|
|
}
|
|
|
|
|
|
|
|
:reqheader Authorization: OAuth 2.0 Bearer Token
|
|
|
|
|
2021-11-01 09:44:10 +01:00
|
|
|
:statuscode 200: successfully logged out
|
|
|
|
:statuscode 401: provide a valid auth token
|
2019-07-20 14:27:05 +02:00
|
|
|
|
|
|
|
"""
|
2017-12-16 21:00:46 +01:00
|
|
|
# get auth token
|
|
|
|
auth_header = request.headers.get('Authorization')
|
2021-01-01 16:39:25 +01:00
|
|
|
if not auth_header:
|
2021-11-01 09:44:10 +01:00
|
|
|
return UnauthorizedErrorResponse('provide a valid auth token')
|
2021-01-01 16:39:25 +01:00
|
|
|
|
|
|
|
auth_token = auth_header.split(' ')[1]
|
|
|
|
resp = User.decode_auth_token(auth_token)
|
|
|
|
if isinstance(auth_user_id, str):
|
|
|
|
return UnauthorizedErrorResponse(resp)
|
|
|
|
|
|
|
|
return {
|
|
|
|
'status': 'success',
|
2021-11-01 09:44:10 +01:00
|
|
|
'message': 'successfully logged out',
|
2021-01-01 16:39:25 +01:00
|
|
|
}
|
2017-12-25 17:45:28 +01:00
|
|
|
|
|
|
|
|
|
|
|
@auth_blueprint.route('/auth/profile', methods=['GET'])
|
|
|
|
@authenticate
|
2021-01-02 19:28:03 +01:00
|
|
|
def get_authenticated_user_profile(
|
|
|
|
auth_user_id: int,
|
|
|
|
) -> Union[Dict, HttpResponse]:
|
2019-07-20 14:27:05 +02:00
|
|
|
"""
|
|
|
|
get authenticated user info
|
|
|
|
|
|
|
|
**Example request**:
|
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
2019-07-20 21:57:35 +02:00
|
|
|
GET /api/auth/profile HTTP/1.1
|
2019-07-20 14:27:05 +02:00
|
|
|
Content-Type: application/json
|
|
|
|
|
|
|
|
**Example response**:
|
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
|
|
|
HTTP/1.1 200 OK
|
|
|
|
Content-Type: application/json
|
|
|
|
|
|
|
|
{
|
|
|
|
"data": {
|
|
|
|
"admin": false,
|
|
|
|
"bio": null,
|
|
|
|
"birth_date": null,
|
|
|
|
"created_at": "Sun, 14 Jul 2019 14:09:58 GMT",
|
2019-09-01 11:40:39 +02:00
|
|
|
"email": "sam@example.com",
|
2019-07-20 14:27:05 +02:00
|
|
|
"first_name": null,
|
2019-09-16 14:19:21 +02:00
|
|
|
"language": "en",
|
2019-07-20 14:27:05 +02:00
|
|
|
"last_name": null,
|
|
|
|
"location": null,
|
|
|
|
"nb_sports": 3,
|
2021-01-10 11:16:43 +01:00
|
|
|
"nb_workouts": 6,
|
2019-07-20 14:27:05 +02:00
|
|
|
"picture": false,
|
2021-09-21 18:10:27 +02:00
|
|
|
"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": 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"
|
|
|
|
}
|
|
|
|
],
|
2019-09-23 20:01:11 +02:00
|
|
|
"sports_list": [
|
|
|
|
1,
|
|
|
|
4,
|
|
|
|
6
|
|
|
|
],
|
2019-07-20 14:27:05 +02:00
|
|
|
"timezone": "Europe/Paris",
|
|
|
|
"total_distance": 67.895,
|
|
|
|
"total_duration": "6:50:27",
|
2019-09-01 11:40:39 +02:00
|
|
|
"username": "sam",
|
|
|
|
"weekm": false
|
2019-07-20 14:27:05 +02:00
|
|
|
},
|
|
|
|
"status": "success"
|
|
|
|
}
|
|
|
|
|
|
|
|
:reqheader Authorization: OAuth 2.0 Bearer Token
|
|
|
|
|
|
|
|
:statuscode 200: success.
|
2019-07-20 21:57:35 +02:00
|
|
|
:statuscode 401:
|
2021-11-01 09:44:10 +01:00
|
|
|
- provide a valid auth token
|
|
|
|
- signature expired, please log in again
|
|
|
|
- invalid token, please log in again
|
2019-07-20 14:27:05 +02:00
|
|
|
|
|
|
|
"""
|
2020-02-08 14:49:37 +01:00
|
|
|
user = User.query.filter_by(id=auth_user_id).first()
|
2021-01-01 16:39:25 +01:00
|
|
|
return {'status': 'success', 'data': user.serialize()}
|
2018-01-01 16:59:46 +01:00
|
|
|
|
|
|
|
|
|
|
|
@auth_blueprint.route('/auth/profile/edit', methods=['POST'])
|
|
|
|
@authenticate
|
2021-01-02 19:28:03 +01:00
|
|
|
def edit_user(auth_user_id: int) -> Union[Dict, HttpResponse]:
|
2019-07-20 14:27:05 +02:00
|
|
|
"""
|
|
|
|
edit authenticated user
|
|
|
|
|
|
|
|
**Example request**:
|
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
2019-07-20 21:57:35 +02:00
|
|
|
POST /api/auth/profile/edit HTTP/1.1
|
2019-07-20 14:27:05 +02:00
|
|
|
Content-Type: application/json
|
|
|
|
|
|
|
|
**Example response**:
|
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
|
|
|
HTTP/1.1 200 OK
|
|
|
|
Content-Type: application/json
|
|
|
|
|
|
|
|
{
|
|
|
|
"data": {
|
|
|
|
"admin": false,
|
|
|
|
"bio": null,
|
|
|
|
"birth_date": null,
|
|
|
|
"created_at": "Sun, 14 Jul 2019 14:09:58 GMT",
|
2019-09-01 11:40:39 +02:00
|
|
|
"email": "sam@example.com",
|
2019-07-20 14:27:05 +02:00
|
|
|
"first_name": null,
|
2019-09-16 14:19:21 +02:00
|
|
|
"language": "en",
|
2019-07-20 14:27:05 +02:00
|
|
|
"last_name": null,
|
|
|
|
"location": null,
|
|
|
|
"nb_sports": 3,
|
2021-01-10 11:16:43 +01:00
|
|
|
"nb_workouts": 6,
|
2019-07-20 14:27:05 +02:00
|
|
|
"picture": false,
|
2021-09-21 18:10:27 +02:00
|
|
|
"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": 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"
|
|
|
|
}
|
|
|
|
],
|
2019-09-23 20:01:11 +02:00
|
|
|
"sports_list": [
|
|
|
|
1,
|
|
|
|
4,
|
|
|
|
6
|
|
|
|
],
|
2019-07-20 14:27:05 +02:00
|
|
|
"timezone": "Europe/Paris",
|
|
|
|
"total_distance": 67.895,
|
|
|
|
"total_duration": "6:50:27",
|
|
|
|
"username": "sam"
|
2019-09-01 11:40:39 +02:00
|
|
|
"weekm": true,
|
2019-07-20 14:27:05 +02:00
|
|
|
},
|
2021-11-01 09:44:10 +01:00
|
|
|
"message": "user profile updated",
|
2019-07-20 14:27:05 +02:00
|
|
|
"status": "success"
|
|
|
|
}
|
|
|
|
|
|
|
|
:<json string first_name: user first name
|
|
|
|
:<json string last_name: user last name
|
|
|
|
:<json string location: user location
|
|
|
|
:<json string bio: user biography
|
|
|
|
:<json string birth_date: user birth date (format: ``%Y-%m-%d``)
|
|
|
|
:<json string password: user password
|
|
|
|
:<json string password_conf: user password confirmation
|
|
|
|
|
|
|
|
:reqheader Authorization: OAuth 2.0 Bearer Token
|
|
|
|
|
2021-11-01 09:44:10 +01:00
|
|
|
:statuscode 200: user profile updated
|
2019-07-20 14:27:05 +02:00
|
|
|
:statuscode 400:
|
2021-11-01 09:44:10 +01:00
|
|
|
- invalid payload
|
|
|
|
- password: password and password confirmation don't match
|
2019-07-20 21:57:35 +02:00
|
|
|
:statuscode 401:
|
2021-11-01 09:44:10 +01:00
|
|
|
- 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
|
2019-07-20 14:27:05 +02:00
|
|
|
|
|
|
|
"""
|
2018-01-01 16:59:46 +01:00
|
|
|
# get post data
|
|
|
|
post_data = request.get_json()
|
2019-08-31 16:33:46 +02:00
|
|
|
user_mandatory_data = {
|
|
|
|
'first_name',
|
|
|
|
'last_name',
|
|
|
|
'bio',
|
|
|
|
'birth_date',
|
|
|
|
'location',
|
|
|
|
}
|
|
|
|
if not post_data or not post_data.keys() >= user_mandatory_data:
|
2021-01-01 16:39:25 +01:00
|
|
|
return InvalidPayloadErrorResponse()
|
|
|
|
|
2018-01-01 16:59:46 +01:00
|
|
|
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')
|
2018-01-01 17:50:12 +01:00
|
|
|
password = post_data.get('password')
|
|
|
|
password_conf = post_data.get('password_conf')
|
|
|
|
|
|
|
|
if password is not None and password != '':
|
2020-05-17 16:42:44 +02:00
|
|
|
message = check_passwords(password, password_conf)
|
|
|
|
if message != '':
|
2021-01-01 16:39:25 +01:00
|
|
|
return InvalidPayloadErrorResponse(message)
|
2020-05-17 16:42:44 +02:00
|
|
|
password = bcrypt.generate_password_hash(
|
|
|
|
password, current_app.config.get('BCRYPT_LOG_ROUNDS')
|
|
|
|
).decode()
|
2018-01-01 17:50:12 +01:00
|
|
|
|
2018-01-01 16:59:46 +01:00
|
|
|
try:
|
2020-02-08 14:49:37 +01:00
|
|
|
user = User.query.filter_by(id=auth_user_id).first()
|
2018-01-01 16:59:46 +01:00
|
|
|
user.first_name = first_name
|
|
|
|
user.last_name = last_name
|
|
|
|
user.bio = bio
|
|
|
|
user.location = location
|
|
|
|
user.birth_date = (
|
2018-05-08 18:26:42 +02:00
|
|
|
datetime.datetime.strptime(birth_date, '%Y-%m-%d')
|
2018-01-01 16:59:46 +01:00
|
|
|
if birth_date
|
|
|
|
else None
|
|
|
|
)
|
2018-01-01 17:50:12 +01:00
|
|
|
if password is not None and password != '':
|
|
|
|
user.password = password
|
2021-10-17 21:01:14 +02:00
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
return {
|
|
|
|
'status': 'success',
|
2021-11-01 09:44:10 +01:00
|
|
|
'message': 'user profile updated',
|
2021-10-17 21:01:14 +02:00
|
|
|
'data': user.serialize(),
|
|
|
|
}
|
|
|
|
|
|
|
|
# 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/preferences', methods=['POST'])
|
|
|
|
@authenticate
|
|
|
|
def edit_user_preferences(auth_user_id: int) -> Union[Dict, HttpResponse]:
|
|
|
|
"""
|
|
|
|
edit authenticated user preferences
|
|
|
|
|
|
|
|
**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": {
|
|
|
|
"admin": false,
|
|
|
|
"bio": null,
|
|
|
|
"birth_date": null,
|
|
|
|
"created_at": "Sun, 14 Jul 2019 14:09:58 GMT",
|
|
|
|
"email": "sam@example.com",
|
|
|
|
"first_name": null,
|
|
|
|
"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": 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
|
|
|
|
],
|
|
|
|
"timezone": "Europe/Paris",
|
|
|
|
"total_distance": 67.895,
|
|
|
|
"total_duration": "6:50:27",
|
|
|
|
"username": "sam"
|
|
|
|
"weekm": true,
|
|
|
|
},
|
2021-11-01 09:44:10 +01:00
|
|
|
"message": "user preferences updated",
|
2021-10-17 21:01:14 +02:00
|
|
|
"status": "success"
|
|
|
|
}
|
|
|
|
|
|
|
|
:<json string timezone: user time zone
|
|
|
|
:<json string weekm: does week start on Monday?
|
|
|
|
:<json string language: language preferences
|
|
|
|
|
|
|
|
:reqheader Authorization: OAuth 2.0 Bearer Token
|
|
|
|
|
2021-11-01 09:44:10 +01:00
|
|
|
:statuscode 200: user preferences updated
|
2021-10-17 21:01:14 +02:00
|
|
|
:statuscode 400:
|
2021-11-01 09:44:10 +01:00
|
|
|
- invalid payload
|
|
|
|
- password: password and password confirmation don't match
|
2021-10-17 21:01:14 +02:00
|
|
|
:statuscode 401:
|
2021-11-01 09:44:10 +01:00
|
|
|
- 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
|
2021-10-17 21:01:14 +02:00
|
|
|
|
|
|
|
"""
|
|
|
|
# get post data
|
|
|
|
post_data = request.get_json()
|
|
|
|
user_mandatory_data = {
|
|
|
|
'language',
|
|
|
|
'timezone',
|
|
|
|
'weekm',
|
|
|
|
}
|
|
|
|
if not post_data or not post_data.keys() >= user_mandatory_data:
|
|
|
|
return InvalidPayloadErrorResponse()
|
|
|
|
|
|
|
|
language = post_data.get('language')
|
|
|
|
timezone = post_data.get('timezone')
|
|
|
|
weekm = post_data.get('weekm')
|
|
|
|
|
|
|
|
try:
|
|
|
|
user = User.query.filter_by(id=auth_user_id).first()
|
|
|
|
user.language = language
|
2018-06-11 15:10:18 +02:00
|
|
|
user.timezone = timezone
|
2019-08-31 16:33:46 +02:00
|
|
|
user.weekm = weekm
|
2018-01-01 16:59:46 +01:00
|
|
|
db.session.commit()
|
|
|
|
|
2021-01-01 16:39:25 +01:00
|
|
|
return {
|
2018-01-01 16:59:46 +01:00
|
|
|
'status': 'success',
|
2021-11-01 09:44:10 +01:00
|
|
|
'message': 'user preferences updated',
|
2019-08-31 16:33:46 +02:00
|
|
|
'data': user.serialize(),
|
2018-01-01 16:59:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# handler errors
|
|
|
|
except (exc.IntegrityError, exc.OperationalError, ValueError) as e:
|
2021-01-01 16:39:25 +01:00
|
|
|
return handle_error_and_return_response(e, db=db)
|
2018-01-01 21:54:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
@auth_blueprint.route('/auth/picture', methods=['POST'])
|
|
|
|
@authenticate
|
2021-01-02 19:28:03 +01:00
|
|
|
def edit_picture(auth_user_id: int) -> Union[Dict, HttpResponse]:
|
2019-07-20 14:27:05 +02:00
|
|
|
"""
|
|
|
|
update authenticated user picture
|
|
|
|
|
|
|
|
**Example request**:
|
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
2019-07-20 21:57:35 +02:00
|
|
|
POST /api/auth/picture HTTP/1.1
|
2019-07-20 14:27:05 +02:00
|
|
|
Content-Type: multipart/form-data
|
|
|
|
|
|
|
|
**Example response**:
|
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
|
|
|
HTTP/1.1 200 OK
|
|
|
|
Content-Type: application/json
|
|
|
|
|
|
|
|
{
|
2021-11-01 09:44:10 +01:00
|
|
|
"message": "user picture updated",
|
2019-07-20 14:27:05 +02:00
|
|
|
"status": "success"
|
|
|
|
}
|
|
|
|
|
|
|
|
:form file: image file (allowed extensions: .jpg, .png, .gif)
|
|
|
|
|
|
|
|
:reqheader Authorization: OAuth 2.0 Bearer Token
|
|
|
|
|
2021-11-01 09:44:10 +01:00
|
|
|
:statuscode 200: user picture updated
|
2019-07-20 14:27:05 +02:00
|
|
|
:statuscode 400:
|
2021-11-01 09:44:10 +01:00
|
|
|
- invalid payload
|
|
|
|
- no file part
|
|
|
|
- no selected file
|
|
|
|
- file extension not allowed
|
2019-07-20 21:57:35 +02:00
|
|
|
:statuscode 401:
|
2021-11-01 09:44:10 +01:00
|
|
|
- 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
|
2019-07-20 14:27:05 +02:00
|
|
|
|
|
|
|
"""
|
2019-08-31 14:11:00 +02:00
|
|
|
try:
|
2021-01-01 16:39:25 +01:00
|
|
|
response_object = verify_extension_and_size('picture', request)
|
2019-08-31 14:11:00 +02:00
|
|
|
except RequestEntityTooLarge as e:
|
|
|
|
appLog.error(e)
|
2021-01-01 16:39:25 +01:00
|
|
|
return PayloadTooLargeErrorResponse(
|
2021-02-20 21:37:31 +01:00
|
|
|
file_type='picture',
|
|
|
|
file_size=request.content_length,
|
|
|
|
max_size=current_app.config['MAX_CONTENT_LENGTH'],
|
2021-01-01 16:39:25 +01:00
|
|
|
)
|
|
|
|
if response_object:
|
|
|
|
return response_object
|
2018-01-01 21:54:03 +01:00
|
|
|
|
2018-05-01 17:51:38 +02:00
|
|
|
file = request.files['file']
|
2021-05-22 17:14:24 +02:00
|
|
|
filename = secure_filename(file.filename) # type: ignore
|
2018-01-01 21:54:03 +01:00
|
|
|
dirpath = os.path.join(
|
2020-02-08 14:49:37 +01:00
|
|
|
current_app.config['UPLOAD_FOLDER'], 'pictures', str(auth_user_id)
|
2018-01-01 21:54:03 +01:00
|
|
|
)
|
|
|
|
if not os.path.exists(dirpath):
|
|
|
|
os.makedirs(dirpath)
|
2018-07-04 14:13:19 +02:00
|
|
|
absolute_picture_path = os.path.join(dirpath, filename)
|
2020-02-08 14:49:37 +01:00
|
|
|
relative_picture_path = os.path.join(
|
|
|
|
'pictures', str(auth_user_id), filename
|
|
|
|
)
|
2018-01-01 21:54:03 +01:00
|
|
|
|
|
|
|
try:
|
2020-02-08 14:49:37 +01:00
|
|
|
user = User.query.filter_by(id=auth_user_id).first()
|
2018-07-04 14:13:19 +02:00
|
|
|
if user.picture is not None:
|
|
|
|
old_picture_path = get_absolute_file_path(user.picture)
|
|
|
|
if os.path.isfile(get_absolute_file_path(old_picture_path)):
|
|
|
|
os.remove(old_picture_path)
|
|
|
|
file.save(absolute_picture_path)
|
|
|
|
user.picture = relative_picture_path
|
2018-01-01 21:54:03 +01:00
|
|
|
db.session.commit()
|
2021-01-01 16:39:25 +01:00
|
|
|
return {
|
2018-01-01 21:54:03 +01:00
|
|
|
'status': 'success',
|
2021-11-01 09:44:10 +01:00
|
|
|
'message': 'user picture updated',
|
2018-01-01 21:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
except (exc.IntegrityError, ValueError) as e:
|
2021-01-01 16:39:25 +01:00
|
|
|
return handle_error_and_return_response(
|
2021-11-01 09:44:10 +01:00
|
|
|
e, message='error during picture update', status='fail', db=db
|
2021-01-01 16:39:25 +01:00
|
|
|
)
|
2018-01-01 21:54:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
@auth_blueprint.route('/auth/picture', methods=['DELETE'])
|
|
|
|
@authenticate
|
2021-01-02 19:28:03 +01:00
|
|
|
def del_picture(auth_user_id: int) -> Union[Tuple[Dict, int], HttpResponse]:
|
2019-07-20 14:27:05 +02:00
|
|
|
"""
|
|
|
|
delete authenticated user picture
|
|
|
|
|
|
|
|
**Example request**:
|
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
2019-07-20 21:57:35 +02:00
|
|
|
DELETE /api/auth/picture HTTP/1.1
|
2019-07-20 14:27:05 +02:00
|
|
|
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
|
2019-07-20 21:57:35 +02:00
|
|
|
:statuscode 401:
|
2021-11-01 09:44:10 +01:00
|
|
|
- provide a valid auth token
|
|
|
|
- signature expired, please log in again
|
|
|
|
- invalid token, please log in again
|
|
|
|
:statuscode 500: error during picture deletion
|
2019-07-20 14:27:05 +02:00
|
|
|
|
|
|
|
"""
|
2018-01-01 21:54:03 +01:00
|
|
|
try:
|
2020-02-08 14:49:37 +01:00
|
|
|
user = User.query.filter_by(id=auth_user_id).first()
|
2018-07-04 14:13:19 +02:00
|
|
|
picture_path = get_absolute_file_path(user.picture)
|
|
|
|
if os.path.isfile(picture_path):
|
|
|
|
os.remove(picture_path)
|
2018-01-01 21:54:03 +01:00
|
|
|
user.picture = None
|
|
|
|
db.session.commit()
|
2021-01-01 16:39:25 +01:00
|
|
|
return {'status': 'no content'}, 204
|
2018-01-01 21:54:03 +01:00
|
|
|
except (exc.IntegrityError, ValueError) as e:
|
2021-01-01 16:39:25 +01:00
|
|
|
return handle_error_and_return_response(
|
2021-11-01 09:44:10 +01:00
|
|
|
e, message='error during picture deletion', status='fail', db=db
|
2021-01-01 16:39:25 +01:00
|
|
|
)
|
2020-05-10 17:08:18 +02:00
|
|
|
|
|
|
|
|
2020-05-17 16:42:44 +02:00
|
|
|
@auth_blueprint.route('/auth/password/reset-request', methods=['POST'])
|
2021-01-02 19:28:03 +01:00
|
|
|
def request_password_reset() -> Union[Dict, HttpResponse]:
|
2020-05-10 17:08:18 +02:00
|
|
|
"""
|
|
|
|
handle password reset request
|
|
|
|
|
|
|
|
**Example request**:
|
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
2020-05-17 16:42:44 +02:00
|
|
|
POST /api/auth/password/reset-request HTTP/1.1
|
2020-05-10 17:08:18 +02:00
|
|
|
Content-Type: application/json
|
|
|
|
|
|
|
|
**Example response**:
|
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
|
|
|
HTTP/1.1 200 OK
|
|
|
|
Content-Type: application/json
|
|
|
|
|
|
|
|
{
|
2021-11-01 09:44:10 +01:00
|
|
|
"message": "password reset request processed",
|
2020-05-10 17:08:18 +02:00
|
|
|
"status": "success"
|
|
|
|
}
|
|
|
|
|
|
|
|
:<json string email: user email
|
|
|
|
|
2021-11-01 09:44:10 +01:00
|
|
|
:statuscode 200: password reset request processed
|
|
|
|
:statuscode 400: invalid payload
|
2020-05-10 17:08:18 +02:00
|
|
|
|
|
|
|
"""
|
|
|
|
post_data = request.get_json()
|
|
|
|
if not post_data or post_data.get('email') is None:
|
2021-01-01 16:39:25 +01:00
|
|
|
return InvalidPayloadErrorResponse()
|
2020-05-10 17:08:18 +02:00
|
|
|
email = post_data.get('email')
|
|
|
|
|
|
|
|
user = User.query.filter(User.email == email).first()
|
|
|
|
if user:
|
2020-05-17 16:42:44 +02:00
|
|
|
password_reset_token = user.encode_password_reset_token(user.id)
|
|
|
|
ui_url = current_app.config['UI_URL']
|
2021-01-31 10:21:40 +01:00
|
|
|
user_language = 'en' if user.language is None else user.language
|
2020-05-17 16:42:44 +02:00
|
|
|
email_data = {
|
2020-07-11 19:35:20 +02:00
|
|
|
'expiration_delay': get_readable_duration(
|
2021-01-02 19:28:03 +01:00
|
|
|
current_app.config['PASSWORD_TOKEN_EXPIRATION_SECONDS'],
|
2021-01-31 10:21:40 +01:00
|
|
|
user_language,
|
2020-07-11 19:35:20 +02:00
|
|
|
),
|
2020-05-17 16:42:44 +02:00
|
|
|
'username': user.username,
|
|
|
|
'password_reset_url': (
|
2020-12-25 19:35:15 +01:00
|
|
|
f'{ui_url}/password-reset?token={password_reset_token}' # noqa
|
2020-05-17 16:42:44 +02:00
|
|
|
),
|
2021-01-02 19:28:03 +01:00
|
|
|
'operating_system': request.user_agent.platform, # type: ignore
|
|
|
|
'browser_name': request.user_agent.browser, # type: ignore
|
2020-05-17 16:42:44 +02:00
|
|
|
}
|
2020-07-14 22:03:56 +02:00
|
|
|
user_data = {
|
2021-01-31 10:21:40 +01:00
|
|
|
'language': user_language,
|
2020-07-14 22:03:56 +02:00
|
|
|
'email': user.email,
|
|
|
|
}
|
|
|
|
reset_password_email.send(user_data, email_data)
|
2021-01-01 16:39:25 +01:00
|
|
|
return {
|
2020-05-10 17:08:18 +02:00
|
|
|
'status': 'success',
|
2021-11-01 09:44:10 +01:00
|
|
|
'message': 'password reset request processed',
|
2020-05-10 17:08:18 +02:00
|
|
|
}
|
2020-05-17 16:42:44 +02:00
|
|
|
|
|
|
|
|
|
|
|
@auth_blueprint.route('/auth/password/update', methods=['POST'])
|
2021-01-02 19:28:03 +01:00
|
|
|
def update_password() -> Union[Dict, HttpResponse]:
|
2020-05-17 16:42:44 +02:00
|
|
|
"""
|
|
|
|
update user password
|
|
|
|
|
|
|
|
**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
|
|
|
|
|
|
|
|
{
|
2021-11-01 09:44:10 +01:00
|
|
|
"message": "password updated",
|
2020-05-17 16:42:44 +02:00
|
|
|
"status": "success"
|
|
|
|
}
|
|
|
|
|
|
|
|
:<json string password: password (8 characters required)
|
|
|
|
:<json string password_conf: password confirmation
|
|
|
|
:<json string token: password reset token
|
|
|
|
|
2021-11-01 09:44:10 +01:00
|
|
|
:statuscode 200: password updated
|
|
|
|
:statuscode 400: invalid payload
|
|
|
|
:statuscode 401: invalid token, please request a new token
|
|
|
|
:statuscode 500: error, please try again or contact the administrator
|
2020-05-17 16:42:44 +02:00
|
|
|
|
|
|
|
"""
|
|
|
|
post_data = request.get_json()
|
|
|
|
if (
|
|
|
|
not post_data
|
|
|
|
or post_data.get('password') is None
|
|
|
|
or post_data.get('password_conf') is None
|
|
|
|
or post_data.get('token') is None
|
|
|
|
):
|
2021-01-01 16:39:25 +01:00
|
|
|
return InvalidPayloadErrorResponse()
|
2020-05-17 16:42:44 +02:00
|
|
|
password = post_data.get('password')
|
|
|
|
password_conf = post_data.get('password_conf')
|
|
|
|
token = post_data.get('token')
|
|
|
|
|
|
|
|
try:
|
|
|
|
user_id = decode_user_token(token)
|
|
|
|
except (jwt.ExpiredSignatureError, jwt.InvalidTokenError):
|
2021-01-01 16:39:25 +01:00
|
|
|
return UnauthorizedErrorResponse()
|
2020-05-17 16:42:44 +02:00
|
|
|
|
|
|
|
message = check_passwords(password, password_conf)
|
|
|
|
if message != '':
|
2021-01-01 16:39:25 +01:00
|
|
|
return InvalidPayloadErrorResponse(message)
|
2020-05-17 16:42:44 +02:00
|
|
|
|
|
|
|
user = User.query.filter(User.id == user_id).first()
|
|
|
|
if not user:
|
2021-01-01 16:39:25 +01:00
|
|
|
return UnauthorizedErrorResponse()
|
2020-05-17 16:42:44 +02:00
|
|
|
try:
|
|
|
|
user.password = bcrypt.generate_password_hash(
|
|
|
|
password, current_app.config.get('BCRYPT_LOG_ROUNDS')
|
|
|
|
).decode()
|
|
|
|
db.session.commit()
|
2021-01-01 16:39:25 +01:00
|
|
|
return {
|
2020-05-17 16:42:44 +02:00
|
|
|
'status': 'success',
|
2021-11-01 09:44:10 +01:00
|
|
|
'message': 'password updated',
|
2020-05-17 16:42:44 +02:00
|
|
|
}
|
|
|
|
except (exc.OperationalError, ValueError) as e:
|
2021-01-01 16:39:25 +01:00
|
|
|
return handle_error_and_return_response(e, db=db)
|