2017-11-07 16:11:31 +00:00
|
|
|
import json
|
2018-02-20 11:22:17 +00:00
|
|
|
|
2016-01-05 17:08:50 +00:00
|
|
|
from flask import (
|
2021-06-10 19:27:17 +01:00
|
|
|
abort,
|
2018-02-20 11:22:17 +00:00
|
|
|
current_app,
|
2016-03-07 14:39:20 +00:00
|
|
|
redirect,
|
2018-02-20 11:22:17 +00:00
|
|
|
render_template,
|
|
|
|
|
request,
|
2016-03-07 14:39:20 +00:00
|
|
|
session,
|
2016-03-14 16:30:48 +00:00
|
|
|
url_for,
|
2016-03-07 14:39:20 +00:00
|
|
|
)
|
2019-05-23 15:27:35 +01:00
|
|
|
from flask_login import current_user
|
2018-02-20 11:22:17 +00:00
|
|
|
from itsdangerous import SignatureExpired
|
|
|
|
|
from notifications_utils.url_safe_token import check_token
|
|
|
|
|
|
2018-03-19 16:38:57 +00:00
|
|
|
from app import user_api_client
|
2015-12-07 16:56:11 +00:00
|
|
|
from app.main import main
|
|
|
|
|
from app.main.forms import TwoFactorForm
|
2019-05-23 15:27:35 +01:00
|
|
|
from app.models.user import User
|
2021-06-14 11:15:57 +01:00
|
|
|
from app.utils.login import (
|
2021-06-14 12:40:12 +01:00
|
|
|
email_needs_revalidating,
|
2021-06-14 11:15:57 +01:00
|
|
|
log_in_user,
|
|
|
|
|
redirect_to_sign_in,
|
|
|
|
|
redirect_when_logged_in,
|
|
|
|
|
)
|
2017-11-07 16:11:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@main.route('/two-factor-email-sent', methods=['GET'])
|
|
|
|
|
def two_factor_email_sent():
|
|
|
|
|
title = 'Email resent' if request.args.get('email_resent') else 'Check your email'
|
|
|
|
|
return render_template(
|
|
|
|
|
'views/two-factor-email.html',
|
2020-10-09 11:41:24 +01:00
|
|
|
title=title,
|
|
|
|
|
redirect_url=request.args.get('next')
|
2017-11-07 16:11:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2020-05-04 12:27:51 +01:00
|
|
|
@main.route('/email-auth/<token>', methods=['GET'])
|
|
|
|
|
def two_factor_email_interstitial(token):
|
|
|
|
|
return render_template('views/email-link-interstitial.html')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@main.route('/email-auth/<token>', methods=['POST'])
|
2017-11-07 16:11:31 +00:00
|
|
|
def two_factor_email(token):
|
2020-10-09 11:41:47 +01:00
|
|
|
redirect_url = request.args.get('next')
|
2017-11-07 16:11:31 +00:00
|
|
|
if current_user.is_authenticated:
|
2019-05-23 15:27:35 +01:00
|
|
|
return redirect_when_logged_in(platform_admin=current_user.platform_admin)
|
2017-11-07 16:11:31 +00:00
|
|
|
|
|
|
|
|
# checks url is valid, and hasn't timed out
|
|
|
|
|
try:
|
|
|
|
|
token_data = json.loads(check_token(
|
|
|
|
|
token,
|
|
|
|
|
current_app.config['SECRET_KEY'],
|
|
|
|
|
current_app.config['DANGEROUS_SALT'],
|
|
|
|
|
current_app.config['EMAIL_2FA_EXPIRY_SECONDS']
|
|
|
|
|
))
|
2018-11-13 10:49:35 +00:00
|
|
|
except SignatureExpired:
|
2020-10-09 11:41:47 +01:00
|
|
|
return render_template('views/email-link-invalid.html', redirect_url=redirect_url)
|
2017-11-07 16:11:31 +00:00
|
|
|
|
|
|
|
|
user_id = token_data['user_id']
|
|
|
|
|
# checks if code was already used
|
|
|
|
|
logged_in, msg = user_api_client.check_verify_code(user_id, token_data['secret_code'], "email")
|
|
|
|
|
|
|
|
|
|
if not logged_in:
|
2020-10-09 11:41:47 +01:00
|
|
|
return render_template('views/email-link-invalid.html', redirect_url=redirect_url)
|
2017-11-07 16:11:31 +00:00
|
|
|
return log_in_user(user_id)
|
2015-12-07 16:56:11 +00:00
|
|
|
|
|
|
|
|
|
2021-06-03 12:43:53 +01:00
|
|
|
@main.route('/two-factor-sms', methods=['GET', 'POST'])
|
2016-06-17 11:36:30 +01:00
|
|
|
@redirect_to_sign_in
|
2021-05-14 19:15:12 +01:00
|
|
|
def two_factor_sms():
|
2016-06-17 11:36:30 +01:00
|
|
|
user_id = session['user_details']['id']
|
2020-01-27 18:10:45 +00:00
|
|
|
user = User.from_id(user_id)
|
2016-01-27 12:22:32 +00:00
|
|
|
|
|
|
|
|
def _check_code(code):
|
2016-03-30 09:58:10 +01:00
|
|
|
return user_api_client.check_verify_code(user_id, code, "sms")
|
2016-01-27 12:22:32 +00:00
|
|
|
|
|
|
|
|
form = TwoFactorForm(_check_code)
|
2020-10-09 11:42:21 +01:00
|
|
|
redirect_url = request.args.get('next')
|
2015-12-07 16:56:11 +00:00
|
|
|
|
|
|
|
|
if form.validate_on_submit():
|
2021-06-14 12:40:12 +01:00
|
|
|
if email_needs_revalidating(user):
|
2020-10-09 11:42:21 +01:00
|
|
|
user_api_client.send_verify_code(user.id, 'email', None, redirect_url)
|
|
|
|
|
return redirect(url_for('.revalidate_email_sent', next=redirect_url))
|
2021-06-14 12:40:12 +01:00
|
|
|
else:
|
|
|
|
|
return log_in_user(user_id)
|
2016-01-05 17:08:50 +00:00
|
|
|
|
2021-05-14 19:15:12 +01:00
|
|
|
return render_template('views/two-factor-sms.html', form=form, redirect_url=redirect_url)
|
2016-03-14 16:30:48 +00:00
|
|
|
|
|
|
|
|
|
2021-05-14 11:20:56 +01:00
|
|
|
@main.route('/two-factor-webauthn', methods=['GET'])
|
|
|
|
|
@redirect_to_sign_in
|
|
|
|
|
def two_factor_webauthn():
|
2021-06-10 19:27:17 +01:00
|
|
|
user_id = session['user_details']['id']
|
|
|
|
|
user = User.from_id(user_id)
|
2021-06-30 14:54:20 +01:00
|
|
|
|
2021-06-10 19:27:17 +01:00
|
|
|
if not user.webauthn_auth:
|
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
|
|
return render_template('views/two-factor-webauthn.html')
|
2021-05-14 11:20:56 +01:00
|
|
|
|
|
|
|
|
|
2020-01-27 18:10:45 +00:00
|
|
|
@main.route('/re-validate-email', methods=['GET'])
|
|
|
|
|
def revalidate_email_sent():
|
|
|
|
|
title = 'Email resent' if request.args.get('email_resent') else 'Check your email'
|
2020-10-09 11:42:46 +01:00
|
|
|
redirect_url = request.args.get('next')
|
|
|
|
|
return render_template('views/re-validate-email-sent.html', title=title, redirect_url=redirect_url)
|