Beispiel #1
0
def send_user_sms_code(user_id):
    user_to_send_to = get_user_by_id(user_id=user_id)
    verify_code, errors = request_verify_code_schema.load(request.get_json())

    secret_code = create_secret_code()
    create_user_code(user_to_send_to, secret_code, SMS_TYPE)

    mobile = user_to_send_to.mobile_number if verify_code.get('to', None) is None else verify_code.get('to')
    sms_code_template_id = current_app.config['SMS_CODE_TEMPLATE_ID']
    sms_code_template = dao_get_template_by_id(sms_code_template_id)
    notify_service_id = current_app.config['NOTIFY_SERVICE_ID']

    saved_notification = persist_notification(
        template_id=sms_code_template_id,
        template_version=sms_code_template.version,
        recipient=mobile,
        service_id=notify_service_id,
        personalisation={'verify_code': secret_code},
        notification_type=SMS_TYPE,
        api_key_id=None,
        key_type=KEY_TYPE_NORMAL
    )
    # 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:
    send_notification_to_queue(saved_notification, False, queue='notify')

    return jsonify({}), 204
Beispiel #2
0
def send_user_code(user_id):
    try:
        user = get_model_users(user_id=user_id)
    except DataError:
        return jsonify(result="error", message="Invalid user id"), 400
    except NoResultFound:
        return jsonify(result="error", message="User not found"), 404
    text_pwd = None
    verify_code, errors = verify_code_schema.load(request.get_json())
    if errors:
        return jsonify(result="error", message=errors), 400
    code = create_user_code(
        user, create_secret_code(), verify_code.code_type)
    # TODO this will need to fixed up when we stop using
    # notify_alpha_client
    if verify_code.code_type == 'sms':
        notify_alpha_client.send_sms(
            mobile_number=user.mobile_number,
            message=code.code)
    elif verify_code.code_type == 'email':
        notify_alpha_client.send_email(
            user.email_address,
            code.code,
            '*****@*****.**',
            'Verification code')
    else:
        abort(500)
    return jsonify(''), 204
Beispiel #3
0
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()
    personalisation = {'verify_code': secret_code}

    create_2fa_code(current_app.config['SMS_CODE_TEMPLATE_ID'],
                    user_to_send_to, secret_code, recipient, personalisation)
Beispiel #4
0
def send_user_email_code(user_to_send_to, data):
    recipient = user_to_send_to.email_address

    secret_code = create_secret_code()

    personalisation = {
        'name': user_to_send_to.name,
        'verify_code': secret_code
    }

    create_2fa_code(current_app.config['EMAIL_2FA_TEMPLATE_ID'],
                    user_to_send_to, secret_code, recipient, personalisation)
Beispiel #5
0
def send_user_email_verification(user_id):
    user_to_send_to = get_user_by_id(user_id=user_id)
    secret_code = create_secret_code()
    create_user_code(user_to_send_to, secret_code, 'email')

    template = dao_get_template_by_id(current_app.config['EMAIL_VERIFY_CODE_TEMPLATE_ID'])

    saved_notification = persist_notification(
        template_id=template.id,
        template_version=template.version,
        recipient=user_to_send_to.email_address,
        service_id=current_app.config['NOTIFY_SERVICE_ID'],
        personalisation={
            'name': user_to_send_to.name,
            'url': _create_verification_url(user_to_send_to, secret_code)
        },
        notification_type=EMAIL_TYPE,
        api_key_id=None,
        key_type=KEY_TYPE_NORMAL
    )

    send_notification_to_queue(saved_notification, False, queue="notify")

    return jsonify({}), 204
Beispiel #6
0
def send_user_code(user_id):
    try:
        user = get_model_users(user_id=user_id)
    except DataError:
        return jsonify(result="error", message="Invalid user id"), 400
    except NoResultFound:
        return jsonify(result="error", message="User not found"), 404
    text_pwd = None
    verify_code, errors = verify_code_schema.load(request.get_json())
    if errors:
        return jsonify(result="error", message=errors), 400
    code = create_user_code(user, create_secret_code(), verify_code.code_type)
    # TODO this will need to fixed up when we stop using
    # notify_alpha_client
    if verify_code.code_type == 'sms':
        notify_alpha_client.send_sms(mobile_number=user.mobile_number,
                                     message=code.code)
    elif verify_code.code_type == 'email':
        notify_alpha_client.send_email(user.email_address, code.code,
                                       '*****@*****.**',
                                       'Verification code')
    else:
        abort(500)
    return jsonify(''), 204
Beispiel #7
0
def create_code(notify_db_session, code_type):
    code = create_secret_code()
    usr = create_user()
    return create_user_code(usr, code, code_type), code
Beispiel #8
0
def create_code(notify_db, notify_db_session, code_type, usr=None, code=None):
    if code is None:
        code = create_secret_code()
    if usr is None:
        usr = create_user()
    return create_user_code(usr, code, code_type), code
def test_create_secret_code_returns_5_digits():
    code = create_secret_code()
    assert len(str(code)) == 5
def test_create_secret_code_different_subsequent_codes():
    code1 = create_secret_code()
    code2 = create_secret_code()
    assert code1 != code2
def create_code(notify_db, notify_db_session, code_type, usr=None, code=None):
    if code is None:
        code = create_secret_code()
    if usr is None:
        usr = sample_user(notify_db, notify_db_session)
    return create_user_code(usr, code, code_type), code