2016-03-17 13:07:52 +00:00
|
|
|
import json
|
|
|
|
|
|
2016-01-05 17:08:50 +00:00
|
|
|
from flask import (
|
2016-01-19 22:47:42 +00:00
|
|
|
render_template,
|
|
|
|
|
redirect,
|
|
|
|
|
session,
|
2016-03-17 13:07:52 +00:00
|
|
|
url_for,
|
|
|
|
|
current_app,
|
2016-03-29 12:13:36 +01:00
|
|
|
flash,
|
|
|
|
|
abort
|
2016-01-19 22:47:42 +00:00
|
|
|
)
|
2016-01-05 13:13:06 +00:00
|
|
|
|
2016-03-17 13:07:52 +00:00
|
|
|
from itsdangerous import SignatureExpired
|
|
|
|
|
|
2015-12-07 16:08:30 +00:00
|
|
|
from flask_login import login_user
|
2015-12-04 16:21:01 +00:00
|
|
|
|
2016-03-17 13:07:52 +00:00
|
|
|
from notifications_python_client.errors import HTTPError
|
|
|
|
|
|
2015-12-07 16:08:30 +00:00
|
|
|
from app.main import main
|
2016-03-17 13:07:52 +00:00
|
|
|
from app.main.forms import TwoFactorForm
|
|
|
|
|
|
|
|
|
|
from app import user_api_client
|
2015-12-04 16:21:01 +00:00
|
|
|
|
|
|
|
|
|
2016-01-05 17:08:50 +00:00
|
|
|
@main.route('/verify', methods=['GET', 'POST'])
|
|
|
|
|
def verify():
|
|
|
|
|
# TODO there needs to be a way to regenerate a session id
|
2016-01-05 17:24:13 +00:00
|
|
|
# or handle gracefully.
|
2016-01-19 22:47:42 +00:00
|
|
|
user_id = session['user_details']['id']
|
|
|
|
|
|
2016-03-17 13:07:52 +00:00
|
|
|
def _check_code(code):
|
|
|
|
|
return user_api_client.check_verify_code(user_id, code, 'sms')
|
2016-03-10 14:48:33 +00:00
|
|
|
|
2016-03-17 13:07:52 +00:00
|
|
|
form = TwoFactorForm(_check_code)
|
2016-03-11 16:36:15 +00:00
|
|
|
|
2016-01-27 12:22:32 +00:00
|
|
|
if form.validate_on_submit():
|
2016-01-20 15:13:15 +00:00
|
|
|
try:
|
2016-03-17 13:07:52 +00:00
|
|
|
user = user_api_client.get_user(user_id)
|
|
|
|
|
activated_user = user_api_client.activate_user(user)
|
2016-01-20 15:13:15 +00:00
|
|
|
login_user(activated_user)
|
|
|
|
|
return redirect(url_for('main.add_service', first='first'))
|
2016-01-28 11:34:15 +00:00
|
|
|
finally:
|
2016-03-10 14:48:33 +00:00
|
|
|
session.pop('user_details', None)
|
2016-01-19 22:47:42 +00:00
|
|
|
|
2016-03-17 13:07:52 +00:00
|
|
|
return render_template('views/two-factor.html', form=form)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@main.route('/verify-email/<token>')
|
|
|
|
|
def verify_email(token):
|
|
|
|
|
from utils.url_safe_token import check_token
|
|
|
|
|
try:
|
|
|
|
|
token_data = check_token(token,
|
|
|
|
|
current_app.config['SECRET_KEY'],
|
|
|
|
|
current_app.config['DANGEROUS_SALT'],
|
|
|
|
|
current_app.config['EMAIL_EXPIRY_SECONDS'])
|
|
|
|
|
|
|
|
|
|
token_data = json.loads(token_data)
|
|
|
|
|
verified = user_api_client.check_verify_code(token_data['user_id'], token_data['secret_code'], 'email')
|
2016-03-29 12:13:36 +01:00
|
|
|
user = user_api_client.get_user(token_data['user_id'])
|
|
|
|
|
if not user:
|
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
|
|
if user.is_active():
|
2016-03-29 13:21:51 +01:00
|
|
|
flash("That verification link has expired.")
|
2016-03-29 12:13:36 +01:00
|
|
|
return redirect(url_for('main.sign_in'))
|
|
|
|
|
|
|
|
|
|
session['user_details'] = {"email": user.email_address, "id": user.id}
|
2016-03-17 13:07:52 +00:00
|
|
|
if verified[0]:
|
|
|
|
|
user_api_client.send_verify_code(user.id, 'sms', user.mobile_number)
|
|
|
|
|
return redirect('verify')
|
|
|
|
|
else:
|
2016-03-22 13:38:35 +00:00
|
|
|
if verified[1] == 'Code has expired':
|
|
|
|
|
flash("The link in the email we sent you has expired. We've sent you a new one.")
|
|
|
|
|
return redirect(url_for('main.resend_email_verification'))
|
|
|
|
|
else:
|
|
|
|
|
message = "There was a problem verifying your account. Error message: '{}'".format(verified[1])
|
|
|
|
|
flash(message)
|
|
|
|
|
return redirect(url_for('main.index'))
|
2016-03-17 13:07:52 +00:00
|
|
|
|
|
|
|
|
except SignatureExpired:
|
|
|
|
|
flash('The link in the email we sent you has expired')
|
|
|
|
|
return redirect(url_for('main.resend_email_verification'))
|