2016-05-11 10:56:24 +01:00
|
|
|
import json
|
2017-02-17 14:06:16 +00:00
|
|
|
import uuid
|
2016-01-21 17:29:24 +00:00
|
|
|
from datetime import datetime
|
2017-11-03 16:00:22 +00:00
|
|
|
from urllib.parse import urlencode
|
2017-02-17 14:06:16 +00:00
|
|
|
|
2021-03-10 13:55:06 +00:00
|
|
|
from flask import Blueprint, abort, current_app, jsonify, request
|
|
|
|
|
from notifications_utils.recipients import (
|
|
|
|
|
is_uk_phone_number,
|
|
|
|
|
use_numeric_sender,
|
|
|
|
|
)
|
2017-11-09 14:18:47 +00:00
|
|
|
from sqlalchemy.exc import IntegrityError
|
2017-02-17 14:06:16 +00:00
|
|
|
|
2017-07-19 13:50:29 +01:00
|
|
|
from app.config import QueueNames
|
2021-03-10 13:55:06 +00:00
|
|
|
from app.dao.permissions_dao import permission_dao
|
|
|
|
|
from app.dao.service_user_dao import (
|
|
|
|
|
dao_get_service_user,
|
|
|
|
|
dao_update_service_user,
|
|
|
|
|
)
|
|
|
|
|
from app.dao.services_dao import dao_fetch_service_by_id
|
|
|
|
|
from app.dao.template_folder_dao import (
|
|
|
|
|
dao_get_template_folder_by_id_and_service_id,
|
|
|
|
|
)
|
|
|
|
|
from app.dao.templates_dao import dao_get_template_by_id
|
2016-01-12 10:39:49 +00:00
|
|
|
from app.dao.users_dao import (
|
2021-03-10 13:55:06 +00:00
|
|
|
count_user_verify_codes,
|
|
|
|
|
create_secret_code,
|
2016-01-28 11:32:46 +00:00
|
|
|
create_user_code,
|
2021-03-10 13:55:06 +00:00
|
|
|
dao_archive_user,
|
|
|
|
|
get_user_and_accounts,
|
|
|
|
|
get_user_by_email,
|
|
|
|
|
get_user_by_id,
|
2016-01-28 11:32:46 +00:00
|
|
|
get_user_code,
|
2021-03-10 13:55:06 +00:00
|
|
|
get_users_by_partial_email,
|
2016-01-28 11:32:46 +00:00
|
|
|
increment_failed_login_count,
|
2016-02-23 11:03:59 +00:00
|
|
|
reset_failed_login_count,
|
2021-03-10 13:55:06 +00:00
|
|
|
save_model_user,
|
2017-02-07 11:27:13 +00:00
|
|
|
save_user_attribute,
|
2017-02-15 16:18:05 +00:00
|
|
|
update_user_password,
|
2021-03-10 13:55:06 +00:00
|
|
|
use_user_code,
|
|
|
|
|
)
|
|
|
|
|
from app.errors import InvalidRequest, register_errors
|
|
|
|
|
from app.models import (
|
|
|
|
|
EMAIL_TYPE,
|
|
|
|
|
KEY_TYPE_NORMAL,
|
|
|
|
|
SMS_TYPE,
|
|
|
|
|
Permission,
|
|
|
|
|
Service,
|
2016-01-28 11:32:46 +00:00
|
|
|
)
|
2016-12-09 14:55:24 +00:00
|
|
|
from app.notifications.process_notifications import (
|
|
|
|
|
persist_notification,
|
2021-03-10 13:55:06 +00:00
|
|
|
send_notification_to_queue,
|
2016-12-09 14:55:24 +00:00
|
|
|
)
|
2021-03-10 13:55:06 +00:00
|
|
|
from app.schema_validation import validate
|
2016-01-11 15:07:13 +00:00
|
|
|
from app.schemas import (
|
2021-03-10 13:55:06 +00:00
|
|
|
create_user_schema,
|
2016-03-07 15:21:05 +00:00
|
|
|
email_data_request_schema,
|
2018-07-09 17:25:13 +01:00
|
|
|
partial_email_data_request_schema,
|
2021-03-10 13:55:06 +00:00
|
|
|
user_update_password_schema_load_json,
|
2017-02-07 11:27:13 +00:00
|
|
|
user_update_schema_load_json,
|
2016-06-14 15:07:23 +01:00
|
|
|
)
|
2017-11-03 09:51:50 +00:00
|
|
|
from app.user.users_schema import (
|
|
|
|
|
post_send_user_email_code_schema,
|
2021-03-10 13:55:06 +00:00
|
|
|
post_send_user_sms_code_schema,
|
2019-02-22 11:26:44 +00:00
|
|
|
post_set_permissions_schema,
|
2021-03-10 13:55:06 +00:00
|
|
|
post_verify_code_schema,
|
2021-05-17 20:32:01 +01:00
|
|
|
post_verify_webauthn_schema,
|
2017-11-03 09:51:50 +00:00
|
|
|
)
|
2021-03-10 13:55:06 +00:00
|
|
|
from app.utils import url_with_token
|
2016-01-08 17:51:46 +00:00
|
|
|
|
2017-03-16 18:15:49 +00:00
|
|
|
user_blueprint = Blueprint('user', __name__)
|
|
|
|
|
register_errors(user_blueprint)
|
2016-02-17 17:04:50 +00:00
|
|
|
|
2016-01-14 16:13:27 +00:00
|
|
|
|
2017-11-09 14:18:47 +00:00
|
|
|
@user_blueprint.errorhandler(IntegrityError)
|
|
|
|
|
def handle_integrity_error(exc):
|
|
|
|
|
"""
|
|
|
|
|
Handle integrity errors caused by the auth type/mobile number check constraint
|
|
|
|
|
"""
|
2021-05-12 15:58:13 +01:00
|
|
|
if 'ck_user_has_mobile_or_other_auth' in str(exc):
|
2017-11-09 14:18:47 +00:00
|
|
|
# we don't expect this to trip, so still log error
|
2021-05-12 15:58:13 +01:00
|
|
|
current_app.logger.exception('Check constraint ck_user_has_mobile_or_other_auth triggered')
|
2017-11-09 14:18:47 +00:00
|
|
|
return jsonify(result='error', message='Mobile number must be set if auth_type is set to sms_auth'), 400
|
|
|
|
|
|
2019-06-25 16:18:48 +01:00
|
|
|
raise exc
|
2017-11-09 14:18:47 +00:00
|
|
|
|
|
|
|
|
|
2017-03-16 18:15:49 +00:00
|
|
|
@user_blueprint.route('', methods=['POST'])
|
2016-01-11 15:07:13 +00:00
|
|
|
def create_user():
|
2016-01-19 11:38:29 +00:00
|
|
|
req_json = request.get_json()
|
2022-05-06 15:25:14 +01:00
|
|
|
user_to_create = create_user_schema.load(req_json)
|
|
|
|
|
|
2020-01-24 15:18:39 +00:00
|
|
|
save_model_user(user_to_create, password=req_json.get('password'), validated_email_access=True)
|
2018-03-06 17:47:29 +00:00
|
|
|
result = user_to_create.serialize()
|
|
|
|
|
return jsonify(data=result), 201
|
2016-01-11 15:07:13 +00:00
|
|
|
|
|
|
|
|
|
2017-03-16 18:15:49 +00:00
|
|
|
@user_blueprint.route('/<uuid:user_id>', methods=['POST'])
|
2016-11-07 17:42:23 +00:00
|
|
|
def update_user_attribute(user_id):
|
|
|
|
|
user_to_update = get_user_by_id(user_id=user_id)
|
|
|
|
|
req_json = request.get_json()
|
2019-02-26 16:25:04 +00:00
|
|
|
if 'updated_by' in req_json:
|
|
|
|
|
updated_by = get_user_by_id(user_id=req_json.pop('updated_by'))
|
|
|
|
|
else:
|
|
|
|
|
updated_by = None
|
|
|
|
|
|
2022-05-06 15:25:14 +01:00
|
|
|
update_dct = user_update_schema_load_json.load(req_json)
|
|
|
|
|
|
2016-11-07 17:42:23 +00:00
|
|
|
save_user_attribute(user_to_update, update_dict=update_dct)
|
2019-02-26 16:25:04 +00:00
|
|
|
if updated_by:
|
|
|
|
|
if 'email_address' in update_dct:
|
|
|
|
|
template = dao_get_template_by_id(current_app.config['TEAM_MEMBER_EDIT_EMAIL_TEMPLATE_ID'])
|
|
|
|
|
recipient = user_to_update.email_address
|
|
|
|
|
reply_to = template.service.get_default_reply_to_email_address()
|
|
|
|
|
elif 'mobile_number' in update_dct:
|
|
|
|
|
template = dao_get_template_by_id(current_app.config['TEAM_MEMBER_EDIT_MOBILE_TEMPLATE_ID'])
|
|
|
|
|
recipient = user_to_update.mobile_number
|
2021-02-25 08:10:52 +00:00
|
|
|
reply_to = get_sms_reply_to_for_notify_service(recipient, template)
|
2019-02-26 16:25:04 +00:00
|
|
|
else:
|
|
|
|
|
return jsonify(data=user_to_update.serialize()), 200
|
|
|
|
|
service = Service.query.get(current_app.config['NOTIFY_SERVICE_ID'])
|
|
|
|
|
|
|
|
|
|
saved_notification = persist_notification(
|
|
|
|
|
template_id=template.id,
|
|
|
|
|
template_version=template.version,
|
|
|
|
|
recipient=recipient,
|
|
|
|
|
service=service,
|
|
|
|
|
personalisation={
|
|
|
|
|
'name': user_to_update.name,
|
|
|
|
|
'servicemanagername': updated_by.name,
|
|
|
|
|
'email address': user_to_update.email_address
|
|
|
|
|
},
|
|
|
|
|
notification_type=template.template_type,
|
|
|
|
|
api_key_id=None,
|
|
|
|
|
key_type=KEY_TYPE_NORMAL,
|
|
|
|
|
reply_to_text=reply_to
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
send_notification_to_queue(saved_notification, False, queue=QueueNames.NOTIFY)
|
2018-03-06 17:47:29 +00:00
|
|
|
return jsonify(data=user_to_update.serialize()), 200
|
2016-11-07 17:42:23 +00:00
|
|
|
|
|
|
|
|
|
2021-02-25 08:10:52 +00:00
|
|
|
def get_sms_reply_to_for_notify_service(recipient, template):
|
2021-02-24 14:38:06 +00:00
|
|
|
if not is_uk_phone_number(recipient) and use_numeric_sender(recipient):
|
|
|
|
|
reply_to = current_app.config['NOTIFY_INTERNATIONAL_SMS_SENDER']
|
|
|
|
|
else:
|
|
|
|
|
reply_to = template.service.get_default_sms_sender()
|
|
|
|
|
return reply_to
|
|
|
|
|
|
|
|
|
|
|
2019-05-21 15:59:23 +01:00
|
|
|
@user_blueprint.route('/<uuid:user_id>/archive', methods=['POST'])
|
|
|
|
|
def archive_user(user_id):
|
|
|
|
|
user = get_user_by_id(user_id)
|
|
|
|
|
dao_archive_user(user)
|
|
|
|
|
|
|
|
|
|
return '', 204
|
|
|
|
|
|
|
|
|
|
|
2017-11-09 14:27:24 +00:00
|
|
|
@user_blueprint.route('/<uuid:user_id>/activate', methods=['POST'])
|
|
|
|
|
def activate_user(user_id):
|
|
|
|
|
user = get_user_by_id(user_id=user_id)
|
|
|
|
|
if user.state == 'active':
|
|
|
|
|
raise InvalidRequest('User already active', status_code=400)
|
|
|
|
|
|
|
|
|
|
user.state = 'active'
|
|
|
|
|
save_model_user(user)
|
2018-03-06 17:47:29 +00:00
|
|
|
return jsonify(data=user.serialize()), 200
|
2017-11-09 14:27:24 +00:00
|
|
|
|
|
|
|
|
|
2017-03-16 18:15:49 +00:00
|
|
|
@user_blueprint.route('/<uuid:user_id>/reset-failed-login-count', methods=['POST'])
|
2017-02-28 14:28:33 +00:00
|
|
|
def user_reset_failed_login_count(user_id):
|
|
|
|
|
user_to_update = get_user_by_id(user_id=user_id)
|
|
|
|
|
reset_failed_login_count(user_to_update)
|
2018-03-06 17:47:29 +00:00
|
|
|
return jsonify(data=user_to_update.serialize()), 200
|
2017-02-28 14:28:33 +00:00
|
|
|
|
|
|
|
|
|
2017-03-16 18:15:49 +00:00
|
|
|
@user_blueprint.route('/<uuid:user_id>/verify/password', methods=['POST'])
|
2016-01-20 16:25:18 +00:00
|
|
|
def verify_user_password(user_id):
|
2016-11-07 17:42:23 +00:00
|
|
|
user_to_verify = get_user_by_id(user_id=user_id)
|
2016-02-19 11:37:35 +00:00
|
|
|
|
2016-01-20 16:25:18 +00:00
|
|
|
try:
|
2016-01-21 17:29:24 +00:00
|
|
|
txt_pwd = request.get_json()['password']
|
2016-01-20 16:25:18 +00:00
|
|
|
except KeyError:
|
2016-06-14 15:07:23 +01:00
|
|
|
message = 'Required field missing data'
|
|
|
|
|
errors = {'password': [message]}
|
|
|
|
|
raise InvalidRequest(errors, status_code=400)
|
|
|
|
|
|
2016-02-19 11:37:35 +00:00
|
|
|
if user_to_verify.check_password(txt_pwd):
|
|
|
|
|
reset_failed_login_count(user_to_verify)
|
2016-01-28 11:41:21 +00:00
|
|
|
return jsonify({}), 204
|
2016-01-20 16:25:18 +00:00
|
|
|
else:
|
2016-02-19 11:37:35 +00:00
|
|
|
increment_failed_login_count(user_to_verify)
|
2016-06-14 15:07:23 +01:00
|
|
|
message = 'Incorrect password'
|
|
|
|
|
errors = {'password': [message]}
|
|
|
|
|
raise InvalidRequest(errors, status_code=400)
|
2016-01-20 16:25:18 +00:00
|
|
|
|
|
|
|
|
|
2017-03-16 18:15:49 +00:00
|
|
|
@user_blueprint.route('/<uuid:user_id>/verify/code', methods=['POST'])
|
2016-01-21 17:29:24 +00:00
|
|
|
def verify_user_code(user_id):
|
2017-11-03 09:51:50 +00:00
|
|
|
data = request.get_json()
|
|
|
|
|
validate(data, post_verify_code_schema)
|
2016-02-19 11:37:35 +00:00
|
|
|
|
2017-11-03 09:51:50 +00:00
|
|
|
user_to_verify = get_user_by_id(user_id=user_id)
|
2016-06-14 15:07:23 +01:00
|
|
|
|
2017-11-03 09:51:50 +00:00
|
|
|
code = get_user_code(user_to_verify, data['code'], data['code_type'])
|
2021-10-04 10:18:58 +01:00
|
|
|
if user_to_verify.failed_login_count >= current_app.config.get('MAX_FAILED_LOGIN_COUNT'):
|
2017-02-28 14:28:33 +00:00
|
|
|
raise InvalidRequest("Code not found", status_code=404)
|
2016-01-21 17:29:24 +00:00
|
|
|
if not code:
|
2017-11-07 16:05:43 +00:00
|
|
|
# only relevant from sms
|
2017-02-14 14:04:11 +00:00
|
|
|
increment_failed_login_count(user_to_verify)
|
2016-06-14 15:07:23 +01:00
|
|
|
raise InvalidRequest("Code not found", status_code=404)
|
2020-05-05 12:00:36 +01:00
|
|
|
if datetime.utcnow() > code.expiry_datetime or code.code_used:
|
2017-11-07 16:05:43 +00:00
|
|
|
# sms and email
|
2017-02-14 14:04:11 +00:00
|
|
|
increment_failed_login_count(user_to_verify)
|
2016-06-14 15:07:23 +01:00
|
|
|
raise InvalidRequest("Code has expired", status_code=400)
|
2017-02-17 14:06:16 +00:00
|
|
|
|
2017-11-03 09:51:50 +00:00
|
|
|
user_to_verify.current_session_id = str(uuid.uuid4())
|
|
|
|
|
user_to_verify.logged_in_at = datetime.utcnow()
|
2020-01-31 17:19:24 +00:00
|
|
|
if data['code_type'] == 'email':
|
2020-01-24 15:18:39 +00:00
|
|
|
user_to_verify.email_access_validated_at = datetime.utcnow()
|
2017-11-03 09:51:50 +00:00
|
|
|
user_to_verify.failed_login_count = 0
|
|
|
|
|
save_model_user(user_to_verify)
|
2017-02-17 14:06:16 +00:00
|
|
|
|
2016-01-21 17:29:24 +00:00
|
|
|
use_user_code(code.id)
|
2016-01-28 11:41:21 +00:00
|
|
|
return jsonify({}), 204
|
2016-01-21 17:29:24 +00:00
|
|
|
|
|
|
|
|
|
2021-06-02 11:13:53 +01:00
|
|
|
# TODO: Remove the "verify" endpoint once admin no longer points at it
|
|
|
|
|
@user_blueprint.route('/<uuid:user_id>/complete/webauthn-login', methods=['POST'])
|
2021-05-17 20:32:01 +01:00
|
|
|
@user_blueprint.route('/<uuid:user_id>/verify/webauthn-login', methods=['POST'])
|
2021-06-02 11:13:53 +01:00
|
|
|
def complete_login_after_webauthn_authentication_attempt(user_id):
|
2021-05-17 20:32:01 +01:00
|
|
|
"""
|
2021-06-02 11:13:53 +01:00
|
|
|
complete login after a webauthn authentication. There's nothing webauthn specific in this code
|
|
|
|
|
but the sms/email flows do this as part of `verify_user_code` above and this is the equivalent spot in the
|
|
|
|
|
webauthn flow.
|
|
|
|
|
|
|
|
|
|
If the authentication was successful, we've already confirmed the user holds the right security key,
|
|
|
|
|
but we still need to check the max login count and set up a current_session_id and last_logged_in_at here.
|
|
|
|
|
|
|
|
|
|
If the authentication was unsuccessful then we just bump the failed_login_count in the db.
|
2021-05-17 20:32:01 +01:00
|
|
|
"""
|
|
|
|
|
data = request.get_json()
|
|
|
|
|
validate(data, post_verify_webauthn_schema)
|
|
|
|
|
|
|
|
|
|
user = get_user_by_id(user_id=user_id)
|
|
|
|
|
successful = data['successful']
|
|
|
|
|
|
|
|
|
|
if user.failed_login_count >= current_app.config.get('MAX_VERIFY_CODE_COUNT'):
|
|
|
|
|
raise InvalidRequest("Maximum login count exceeded", status_code=403)
|
|
|
|
|
|
|
|
|
|
if successful:
|
|
|
|
|
user.current_session_id = str(uuid.uuid4())
|
|
|
|
|
user.logged_in_at = datetime.utcnow()
|
|
|
|
|
user.failed_login_count = 0
|
|
|
|
|
save_model_user(user)
|
|
|
|
|
else:
|
|
|
|
|
increment_failed_login_count(user)
|
|
|
|
|
|
|
|
|
|
return jsonify({}), 204
|
|
|
|
|
|
|
|
|
|
|
2017-11-07 16:15:49 +00:00
|
|
|
@user_blueprint.route('/<uuid:user_id>/<code_type>-code', methods=['POST'])
|
|
|
|
|
def send_user_2fa_code(user_id, code_type):
|
|
|
|
|
user_to_send_to = get_user_by_id(user_id=user_id)
|
2017-11-03 09:51:50 +00:00
|
|
|
|
2017-11-07 16:15:49 +00:00
|
|
|
if count_user_verify_codes(user_to_send_to) >= current_app.config.get('MAX_VERIFY_CODE_COUNT'):
|
|
|
|
|
# Prevent more than `MAX_VERIFY_CODE_COUNT` active verify codes at a time
|
2018-09-19 10:49:11 +01:00
|
|
|
current_app.logger.warning('Too many verify codes created for user {}'.format(user_to_send_to.id))
|
2017-11-07 16:15:49 +00:00
|
|
|
else:
|
|
|
|
|
data = request.get_json()
|
|
|
|
|
if code_type == SMS_TYPE:
|
|
|
|
|
validate(data, post_send_user_sms_code_schema)
|
|
|
|
|
send_user_sms_code(user_to_send_to, data)
|
|
|
|
|
elif code_type == EMAIL_TYPE:
|
|
|
|
|
validate(data, post_send_user_email_code_schema)
|
|
|
|
|
send_user_email_code(user_to_send_to, data)
|
|
|
|
|
else:
|
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
|
|
return '{}', 204
|
2017-11-03 09:51:50 +00:00
|
|
|
|
|
|
|
|
|
2017-11-07 16:15:49 +00:00
|
|
|
def send_user_sms_code(user_to_send_to, data):
|
|
|
|
|
recipient = data.get('to') or user_to_send_to.mobile_number
|
|
|
|
|
|
|
|
|
|
secret_code = create_secret_code()
|
2017-11-03 16:00:22 +00:00
|
|
|
personalisation = {'verify_code': secret_code}
|
2017-11-03 09:51:50 +00:00
|
|
|
|
2017-11-07 16:15:49 +00:00
|
|
|
create_2fa_code(
|
|
|
|
|
current_app.config['SMS_CODE_TEMPLATE_ID'],
|
|
|
|
|
user_to_send_to,
|
|
|
|
|
secret_code,
|
|
|
|
|
recipient,
|
|
|
|
|
personalisation
|
|
|
|
|
)
|
2017-11-03 09:51:50 +00:00
|
|
|
|
|
|
|
|
|
2017-11-07 16:15:49 +00:00
|
|
|
def send_user_email_code(user_to_send_to, data):
|
|
|
|
|
recipient = user_to_send_to.email_address
|
2017-11-03 09:51:50 +00:00
|
|
|
|
2017-11-03 16:44:22 +00:00
|
|
|
secret_code = str(uuid.uuid4())
|
|
|
|
|
personalisation = {
|
|
|
|
|
'name': user_to_send_to.name,
|
2018-02-09 14:16:10 +00:00
|
|
|
'url': _create_2fa_url(user_to_send_to, secret_code, data.get('next'), data.get('email_auth_link_host'))
|
2017-11-03 16:44:22 +00:00
|
|
|
}
|
2017-11-03 09:51:50 +00:00
|
|
|
|
2017-11-07 16:15:49 +00:00
|
|
|
create_2fa_code(
|
|
|
|
|
current_app.config['EMAIL_2FA_TEMPLATE_ID'],
|
|
|
|
|
user_to_send_to,
|
|
|
|
|
secret_code,
|
|
|
|
|
recipient,
|
|
|
|
|
personalisation
|
|
|
|
|
)
|
2016-02-19 11:37:35 +00:00
|
|
|
|
2017-02-15 16:18:05 +00:00
|
|
|
|
2017-11-07 16:15:49 +00:00
|
|
|
def create_2fa_code(template_id, user_to_send_to, secret_code, recipient, personalisation):
|
|
|
|
|
template = dao_get_template_by_id(template_id)
|
2016-02-19 11:37:35 +00:00
|
|
|
|
2017-11-07 16:15:49 +00:00
|
|
|
# save the code in the VerifyCode table
|
|
|
|
|
create_user_code(user_to_send_to, secret_code, template.template_type)
|
2017-11-27 16:52:52 +00:00
|
|
|
reply_to = None
|
|
|
|
|
if template.template_type == SMS_TYPE:
|
2021-02-25 08:10:52 +00:00
|
|
|
reply_to = get_sms_reply_to_for_notify_service(recipient, template)
|
2017-11-27 16:52:52 +00:00
|
|
|
elif template.template_type == EMAIL_TYPE:
|
|
|
|
|
reply_to = template.service.get_default_reply_to_email_address()
|
2016-12-09 14:55:24 +00:00
|
|
|
saved_notification = persist_notification(
|
2017-11-03 09:51:50 +00:00
|
|
|
template_id=template.id,
|
|
|
|
|
template_version=template.version,
|
|
|
|
|
recipient=recipient,
|
|
|
|
|
service=template.service,
|
|
|
|
|
personalisation=personalisation,
|
|
|
|
|
notification_type=template.template_type,
|
2016-12-09 14:55:24 +00:00
|
|
|
api_key_id=None,
|
2017-11-27 14:36:54 +00:00
|
|
|
key_type=KEY_TYPE_NORMAL,
|
2017-11-27 16:52:52 +00:00
|
|
|
reply_to_text=reply_to
|
2016-12-09 14:55:24 +00:00
|
|
|
)
|
|
|
|
|
# Assume that we never want to observe the Notify service's research mode
|
|
|
|
|
# setting for this notification - we still need to be able to log into the
|
|
|
|
|
# admin even if we're doing user research using this service:
|
2017-05-25 10:51:49 +01:00
|
|
|
send_notification_to_queue(saved_notification, False, queue=QueueNames.NOTIFY)
|
2016-02-19 11:37:35 +00:00
|
|
|
|
|
|
|
|
|
2017-03-16 18:15:49 +00:00
|
|
|
@user_blueprint.route('/<uuid:user_id>/change-email-verification', methods=['POST'])
|
2016-10-12 13:06:39 +01:00
|
|
|
def send_user_confirm_new_email(user_id):
|
2016-11-07 17:42:23 +00:00
|
|
|
user_to_send_to = get_user_by_id(user_id=user_id)
|
2022-05-06 15:25:14 +01:00
|
|
|
|
|
|
|
|
email = email_data_request_schema.load(request.get_json())
|
2016-10-12 13:06:39 +01:00
|
|
|
|
|
|
|
|
template = dao_get_template_by_id(current_app.config['CHANGE_EMAIL_CONFIRMATION_TEMPLATE_ID'])
|
2016-12-19 16:51:57 +00:00
|
|
|
service = Service.query.get(current_app.config['NOTIFY_SERVICE_ID'])
|
2016-12-19 15:19:05 +00:00
|
|
|
|
|
|
|
|
saved_notification = persist_notification(
|
|
|
|
|
template_id=template.id,
|
|
|
|
|
template_version=template.version,
|
|
|
|
|
recipient=email['email'],
|
2016-12-19 16:51:57 +00:00
|
|
|
service=service,
|
2016-12-19 15:19:05 +00:00
|
|
|
personalisation={
|
2016-10-12 13:06:39 +01:00
|
|
|
'name': user_to_send_to.name,
|
2016-10-13 11:59:47 +01:00
|
|
|
'url': _create_confirmation_url(user=user_to_send_to, email_address=email['email']),
|
2017-06-07 11:58:10 +01:00
|
|
|
'feedback_url': current_app.config['ADMIN_BASE_URL'] + '/support'
|
2016-12-19 15:19:05 +00:00
|
|
|
},
|
2017-11-07 16:05:43 +00:00
|
|
|
notification_type=template.template_type,
|
2016-12-19 15:19:05 +00:00
|
|
|
api_key_id=None,
|
2017-11-27 14:36:54 +00:00
|
|
|
key_type=KEY_TYPE_NORMAL,
|
|
|
|
|
reply_to_text=service.get_default_reply_to_email_address()
|
2016-12-19 15:19:05 +00:00
|
|
|
)
|
2016-10-12 13:06:39 +01:00
|
|
|
|
2017-05-25 10:51:49 +01:00
|
|
|
send_notification_to_queue(saved_notification, False, queue=QueueNames.NOTIFY)
|
2016-10-12 13:06:39 +01:00
|
|
|
return jsonify({}), 204
|
|
|
|
|
|
|
|
|
|
|
2017-03-16 18:15:49 +00:00
|
|
|
@user_blueprint.route('/<uuid:user_id>/email-verification', methods=['POST'])
|
2017-11-03 14:38:30 +00:00
|
|
|
def send_new_user_email_verification(user_id):
|
2022-06-25 18:36:39 -07:00
|
|
|
current_app.logger.info('Sending email verification for user {}'.format(user_id))
|
2022-03-07 15:03:46 +00:00
|
|
|
request_json = request.get_json()
|
|
|
|
|
|
2017-11-03 09:51:50 +00:00
|
|
|
# when registering, we verify all users' email addresses using this function
|
2016-11-07 17:42:23 +00:00
|
|
|
user_to_send_to = get_user_by_id(user_id=user_id)
|
2022-06-25 18:36:39 -07:00
|
|
|
current_app.logger.info('user_to_send_to is {}'.format(user_to_send_to))
|
|
|
|
|
current_app.logger.info('user_to_send_to.email_address is {}'.format(user_to_send_to.email_address))
|
2016-03-17 13:06:17 +00:00
|
|
|
|
2017-11-03 14:38:30 +00:00
|
|
|
template = dao_get_template_by_id(current_app.config['NEW_USER_EMAIL_VERIFICATION_TEMPLATE_ID'])
|
2016-12-19 16:51:57 +00:00
|
|
|
service = Service.query.get(current_app.config['NOTIFY_SERVICE_ID'])
|
2022-10-14 14:45:27 +00:00
|
|
|
|
2022-06-25 18:36:39 -07:00
|
|
|
current_app.logger.info('template.id is {}'.format(template.id))
|
|
|
|
|
current_app.logger.info('service.id is {}'.format(service.id))
|
2016-12-19 15:31:54 +00:00
|
|
|
|
|
|
|
|
saved_notification = persist_notification(
|
|
|
|
|
template_id=template.id,
|
|
|
|
|
template_version=template.version,
|
|
|
|
|
recipient=user_to_send_to.email_address,
|
2016-12-19 16:51:57 +00:00
|
|
|
service=service,
|
2016-12-19 15:31:54 +00:00
|
|
|
personalisation={
|
2016-06-13 11:29:07 +01:00
|
|
|
'name': user_to_send_to.name,
|
2022-03-07 15:03:46 +00:00
|
|
|
'url': _create_verification_url(
|
|
|
|
|
user_to_send_to,
|
|
|
|
|
base_url=request_json.get('admin_base_url'),
|
|
|
|
|
),
|
2016-12-19 15:31:54 +00:00
|
|
|
},
|
2017-11-07 16:05:43 +00:00
|
|
|
notification_type=template.template_type,
|
2016-12-19 15:31:54 +00:00
|
|
|
api_key_id=None,
|
2017-11-27 14:36:54 +00:00
|
|
|
key_type=KEY_TYPE_NORMAL,
|
|
|
|
|
reply_to_text=service.get_default_reply_to_email_address()
|
2016-12-19 15:31:54 +00:00
|
|
|
)
|
2022-06-25 18:36:39 -07:00
|
|
|
current_app.logger.info('Sending notification to queue')
|
2016-12-19 15:31:54 +00:00
|
|
|
|
2017-05-25 10:51:49 +01:00
|
|
|
send_notification_to_queue(saved_notification, False, queue=QueueNames.NOTIFY)
|
2022-10-14 14:45:27 +00:00
|
|
|
|
2022-06-25 18:36:39 -07:00
|
|
|
current_app.logger.info('Sent notification to queue')
|
2016-03-17 13:06:17 +00:00
|
|
|
|
|
|
|
|
return jsonify({}), 204
|
|
|
|
|
|
|
|
|
|
|
2017-03-16 18:15:49 +00:00
|
|
|
@user_blueprint.route('/<uuid:user_id>/email-already-registered', methods=['POST'])
|
2016-07-07 17:23:07 +01:00
|
|
|
def send_already_registered_email(user_id):
|
2022-06-25 18:36:39 -07:00
|
|
|
current_app.logger.info('Email already registered for user {}'.format(user_id))
|
2022-05-06 15:25:14 +01:00
|
|
|
to = email_data_request_schema.load(request.get_json())
|
2022-10-14 14:45:27 +00:00
|
|
|
|
2022-06-25 18:36:39 -07:00
|
|
|
current_app.logger.info('To email is {}'.format(to['email']))
|
2022-05-06 15:25:14 +01:00
|
|
|
|
2016-07-07 17:23:07 +01:00
|
|
|
template = dao_get_template_by_id(current_app.config['ALREADY_REGISTERED_EMAIL_TEMPLATE_ID'])
|
2017-01-10 13:41:16 +00:00
|
|
|
service = Service.query.get(current_app.config['NOTIFY_SERVICE_ID'])
|
2022-10-14 14:45:27 +00:00
|
|
|
|
2022-06-25 18:36:39 -07:00
|
|
|
current_app.logger.info('template.id is {}'.format(template.id))
|
|
|
|
|
current_app.logger.info('service.id is {}'.format(service.id))
|
2016-07-07 17:23:07 +01:00
|
|
|
|
2016-12-19 17:35:13 +00:00
|
|
|
saved_notification = persist_notification(
|
|
|
|
|
template_id=template.id,
|
|
|
|
|
template_version=template.version,
|
|
|
|
|
recipient=to['email'],
|
2017-01-10 13:41:16 +00:00
|
|
|
service=service,
|
2016-12-19 17:35:13 +00:00
|
|
|
personalisation={
|
2016-07-07 17:23:07 +01:00
|
|
|
'signin_url': current_app.config['ADMIN_BASE_URL'] + '/sign-in',
|
|
|
|
|
'forgot_password_url': current_app.config['ADMIN_BASE_URL'] + '/forgot-password',
|
2017-06-07 11:58:10 +01:00
|
|
|
'feedback_url': current_app.config['ADMIN_BASE_URL'] + '/support'
|
2016-12-19 17:35:13 +00:00
|
|
|
},
|
2017-11-07 16:05:43 +00:00
|
|
|
notification_type=template.template_type,
|
2016-12-19 17:35:13 +00:00
|
|
|
api_key_id=None,
|
2017-11-27 14:36:54 +00:00
|
|
|
key_type=KEY_TYPE_NORMAL,
|
|
|
|
|
reply_to_text=service.get_default_reply_to_email_address()
|
2016-12-19 17:35:13 +00:00
|
|
|
)
|
2022-10-14 14:45:27 +00:00
|
|
|
|
2022-06-25 18:36:39 -07:00
|
|
|
current_app.logger.info('Sending notification to queue')
|
2016-12-19 17:35:13 +00:00
|
|
|
|
2017-05-25 10:51:49 +01:00
|
|
|
send_notification_to_queue(saved_notification, False, queue=QueueNames.NOTIFY)
|
2022-10-14 14:45:27 +00:00
|
|
|
|
2022-06-25 18:36:39 -07:00
|
|
|
current_app.logger.info('Sent notification to queue')
|
2016-07-07 17:23:07 +01:00
|
|
|
|
|
|
|
|
return jsonify({}), 204
|
|
|
|
|
|
|
|
|
|
|
2017-03-16 18:15:49 +00:00
|
|
|
@user_blueprint.route('/<uuid:user_id>', methods=['GET'])
|
|
|
|
|
@user_blueprint.route('', methods=['GET'])
|
2016-01-11 15:07:13 +00:00
|
|
|
def get_user(user_id=None):
|
2016-11-07 17:42:23 +00:00
|
|
|
users = get_user_by_id(user_id=user_id)
|
2018-03-06 17:47:29 +00:00
|
|
|
result = [x.serialize() for x in users] if isinstance(users, list) else users.serialize()
|
|
|
|
|
return jsonify(data=result)
|
2016-02-23 11:03:59 +00:00
|
|
|
|
|
|
|
|
|
2017-03-16 18:15:49 +00:00
|
|
|
@user_blueprint.route('/<uuid:user_id>/service/<uuid:service_id>/permission', methods=['POST'])
|
2016-03-01 14:21:28 +00:00
|
|
|
def set_permissions(user_id, service_id):
|
|
|
|
|
# TODO fix security hole, how do we verify that the user
|
|
|
|
|
# who is making this request has permission to make the request.
|
2019-02-22 11:26:44 +00:00
|
|
|
service_user = dao_get_service_user(user_id, service_id)
|
2021-04-26 11:50:17 +01:00
|
|
|
user = get_user_by_id(user_id)
|
2016-03-01 14:21:28 +00:00
|
|
|
service = dao_fetch_service_by_id(service_id=service_id)
|
2019-02-21 14:48:24 +00:00
|
|
|
|
|
|
|
|
data = request.get_json()
|
2019-02-22 11:26:44 +00:00
|
|
|
validate(data, post_set_permissions_schema)
|
|
|
|
|
|
|
|
|
|
permission_list = [
|
|
|
|
|
Permission(service_id=service_id, user_id=user_id, permission=p['permission'])
|
|
|
|
|
for p in data['permissions']
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
permission_dao.set_user_service_permission(user, service, permission_list, _commit=True, replace=True)
|
|
|
|
|
|
|
|
|
|
if 'folder_permissions' in data:
|
|
|
|
|
folders = [
|
|
|
|
|
dao_get_template_folder_by_id_and_service_id(folder_id, service_id)
|
|
|
|
|
for folder_id in data['folder_permissions']
|
|
|
|
|
]
|
2019-02-21 14:48:24 +00:00
|
|
|
|
2019-02-22 11:26:44 +00:00
|
|
|
service_user.folders = folders
|
|
|
|
|
dao_update_service_user(service_user)
|
2016-06-14 15:07:23 +01:00
|
|
|
|
2016-03-01 14:21:28 +00:00
|
|
|
return jsonify({}), 204
|
|
|
|
|
|
|
|
|
|
|
2021-03-05 12:38:14 +00:00
|
|
|
@user_blueprint.route('/email', methods=['POST'])
|
|
|
|
|
def fetch_user_by_email():
|
2022-05-06 15:25:14 +01:00
|
|
|
email = email_data_request_schema.load(request.get_json())
|
2021-03-05 12:38:14 +00:00
|
|
|
|
|
|
|
|
fetched_user = get_user_by_email(email['email'])
|
|
|
|
|
result = fetched_user.serialize()
|
|
|
|
|
return jsonify(data=result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# TODO: Deprecate this GET endpoint
|
2017-03-16 18:15:49 +00:00
|
|
|
@user_blueprint.route('/email', methods=['GET'])
|
2016-02-23 11:03:59 +00:00
|
|
|
def get_by_email():
|
|
|
|
|
email = request.args.get('email')
|
|
|
|
|
if not email:
|
2016-06-14 15:07:23 +01:00
|
|
|
error = 'Invalid request. Email query string param required'
|
|
|
|
|
raise InvalidRequest(error, status_code=400)
|
2016-03-07 15:21:05 +00:00
|
|
|
fetched_user = get_user_by_email(email)
|
2018-03-06 17:47:29 +00:00
|
|
|
result = fetched_user.serialize()
|
|
|
|
|
return jsonify(data=result)
|
2016-03-07 14:34:53 +00:00
|
|
|
|
|
|
|
|
|
2018-07-09 17:25:13 +01:00
|
|
|
@user_blueprint.route('/find-users-by-email', methods=['POST'])
|
|
|
|
|
def find_users_by_email():
|
2022-05-06 15:25:14 +01:00
|
|
|
email = partial_email_data_request_schema.load(request.get_json())
|
|
|
|
|
|
2018-07-09 17:25:13 +01:00
|
|
|
fetched_users = get_users_by_partial_email(email['email'])
|
2019-08-19 13:31:29 +01:00
|
|
|
result = [user.serialize_for_users_list() for user in fetched_users]
|
2018-07-09 17:25:13 +01:00
|
|
|
return jsonify(data=result), 200
|
|
|
|
|
|
|
|
|
|
|
2017-03-16 18:15:49 +00:00
|
|
|
@user_blueprint.route('/reset-password', methods=['POST'])
|
2016-03-07 18:20:20 +00:00
|
|
|
def send_user_reset_password():
|
2022-02-02 16:42:55 +00:00
|
|
|
request_json = request.get_json()
|
2022-05-06 15:25:14 +01:00
|
|
|
email = email_data_request_schema.load(request_json)
|
|
|
|
|
|
2016-03-07 15:21:05 +00:00
|
|
|
user_to_send_to = get_user_by_email(email['email'])
|
2016-06-16 10:43:41 +01:00
|
|
|
template = dao_get_template_by_id(current_app.config['PASSWORD_RESET_TEMPLATE_ID'])
|
2017-01-10 13:41:16 +00:00
|
|
|
service = Service.query.get(current_app.config['NOTIFY_SERVICE_ID'])
|
2016-12-20 11:55:26 +00:00
|
|
|
saved_notification = persist_notification(
|
|
|
|
|
template_id=template.id,
|
|
|
|
|
template_version=template.version,
|
|
|
|
|
recipient=email['email'],
|
2017-01-10 13:41:16 +00:00
|
|
|
service=service,
|
2016-12-20 11:55:26 +00:00
|
|
|
personalisation={
|
2016-06-16 10:43:41 +01:00
|
|
|
'user_name': user_to_send_to.name,
|
2020-10-05 16:49:50 +01:00
|
|
|
'url': _create_reset_password_url(
|
|
|
|
|
user_to_send_to.email_address,
|
2022-02-02 16:42:55 +00:00
|
|
|
base_url=request_json.get('admin_base_url'),
|
|
|
|
|
next_redirect=request_json.get('next')
|
2020-10-05 16:49:50 +01:00
|
|
|
)
|
2016-12-20 11:55:26 +00:00
|
|
|
},
|
2017-11-07 16:05:43 +00:00
|
|
|
notification_type=template.template_type,
|
2016-12-20 11:55:26 +00:00
|
|
|
api_key_id=None,
|
2017-11-27 14:36:54 +00:00
|
|
|
key_type=KEY_TYPE_NORMAL,
|
|
|
|
|
reply_to_text=service.get_default_reply_to_email_address()
|
2016-12-20 11:55:26 +00:00
|
|
|
)
|
|
|
|
|
|
2017-05-25 10:51:49 +01:00
|
|
|
send_notification_to_queue(saved_notification, False, queue=QueueNames.NOTIFY)
|
2016-03-07 18:20:20 +00:00
|
|
|
|
2016-03-07 14:34:53 +00:00
|
|
|
return jsonify({}), 204
|
|
|
|
|
|
|
|
|
|
|
2017-03-16 18:15:49 +00:00
|
|
|
@user_blueprint.route('/<uuid:user_id>/update-password', methods=['POST'])
|
2017-02-07 11:27:13 +00:00
|
|
|
def update_password(user_id):
|
|
|
|
|
user = get_user_by_id(user_id=user_id)
|
|
|
|
|
req_json = request.get_json()
|
2020-01-24 15:18:39 +00:00
|
|
|
password = req_json.get('_password')
|
2022-05-06 15:25:14 +01:00
|
|
|
|
|
|
|
|
user_update_password_schema_load_json.load(req_json)
|
|
|
|
|
|
2021-08-17 16:59:51 +01:00
|
|
|
update_user_password(user, password)
|
2018-03-06 17:47:29 +00:00
|
|
|
return jsonify(data=user.serialize()), 200
|
2017-02-07 11:27:13 +00:00
|
|
|
|
|
|
|
|
|
2018-03-07 18:31:18 +00:00
|
|
|
@user_blueprint.route('/<uuid:user_id>/organisations-and-services', methods=['GET'])
|
|
|
|
|
def get_organisations_and_services_for_user(user_id):
|
2018-03-13 13:07:02 +00:00
|
|
|
user = get_user_and_accounts(user_id)
|
2018-03-15 12:21:04 +00:00
|
|
|
data = get_orgs_and_services(user)
|
|
|
|
|
return jsonify(data)
|
|
|
|
|
|
|
|
|
|
|
2022-02-02 16:42:55 +00:00
|
|
|
def _create_reset_password_url(email, next_redirect, base_url=None):
|
2018-03-15 12:21:04 +00:00
|
|
|
data = json.dumps({'email': email, 'created_at': str(datetime.utcnow())})
|
2020-10-05 16:49:50 +01:00
|
|
|
static_url_part = '/new-password/'
|
2022-02-02 16:42:55 +00:00
|
|
|
full_url = url_with_token(data, static_url_part, current_app.config, base_url=base_url)
|
2020-10-05 16:49:50 +01:00
|
|
|
if next_redirect:
|
|
|
|
|
full_url += '?{}'.format(urlencode({'next': next_redirect}))
|
|
|
|
|
return full_url
|
2018-03-15 12:21:04 +00:00
|
|
|
|
|
|
|
|
|
2022-03-07 15:03:46 +00:00
|
|
|
def _create_verification_url(user, base_url):
|
2018-03-15 12:21:04 +00:00
|
|
|
data = json.dumps({'user_id': str(user.id), 'email': user.email_address})
|
|
|
|
|
url = '/verify-email/'
|
2022-03-07 15:03:46 +00:00
|
|
|
return url_with_token(data, url, current_app.config, base_url=base_url)
|
2018-03-15 12:21:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def _create_confirmation_url(user, email_address):
|
|
|
|
|
data = json.dumps({'user_id': str(user.id), 'email': email_address})
|
|
|
|
|
url = '/user-profile/email/confirm/'
|
|
|
|
|
return url_with_token(data, url, current_app.config)
|
|
|
|
|
|
|
|
|
|
|
2020-10-09 17:47:26 +01:00
|
|
|
def _create_2fa_url(user, secret_code, next_redirect, email_auth_link_host):
|
2018-03-15 12:21:04 +00:00
|
|
|
data = json.dumps({'user_id': str(user.id), 'secret_code': secret_code})
|
|
|
|
|
url = '/email-auth/'
|
2020-10-09 17:47:26 +01:00
|
|
|
full_url = url_with_token(data, url, current_app.config, base_url=email_auth_link_host)
|
|
|
|
|
if next_redirect:
|
|
|
|
|
full_url += '?{}'.format(urlencode({'next': next_redirect}))
|
|
|
|
|
return full_url
|
2018-03-15 12:21:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_orgs_and_services(user):
|
|
|
|
|
return {
|
2018-03-07 18:31:18 +00:00
|
|
|
'organisations': [
|
|
|
|
|
{
|
|
|
|
|
'name': org.name,
|
|
|
|
|
'id': org.id,
|
2019-06-12 13:15:25 +01:00
|
|
|
'count_of_live_services': len(org.live_services),
|
2018-03-07 18:31:18 +00:00
|
|
|
}
|
2018-03-13 13:07:02 +00:00
|
|
|
for org in user.organisations if org.active
|
2018-03-07 18:31:18 +00:00
|
|
|
],
|
2019-06-12 10:34:15 +01:00
|
|
|
'services': [
|
|
|
|
|
{
|
|
|
|
|
'id': service.id,
|
|
|
|
|
'name': service.name,
|
|
|
|
|
'restricted': service.restricted,
|
|
|
|
|
'organisation': service.organisation.id if service.organisation else None,
|
|
|
|
|
}
|
|
|
|
|
for service in user.services if service.active
|
2018-03-07 18:31:18 +00:00
|
|
|
]
|
|
|
|
|
}
|