2016-01-05 12:35:36 +00:00
|
|
|
|
2016-01-05 17:08:50 +00:00
|
|
|
from flask import (
|
2016-03-07 14:39:20 +00:00
|
|
|
render_template,
|
|
|
|
|
redirect,
|
|
|
|
|
session,
|
2016-03-14 16:30:48 +00:00
|
|
|
url_for,
|
|
|
|
|
request
|
2016-03-07 14:39:20 +00:00
|
|
|
)
|
2016-01-05 17:24:13 +00:00
|
|
|
|
2016-03-18 10:49:22 +00:00
|
|
|
from flask_login import login_user, current_user
|
2015-12-07 16:56:11 +00:00
|
|
|
from app.main import main
|
|
|
|
|
from app.main.forms import TwoFactorForm
|
2016-03-29 22:50:40 +01:00
|
|
|
from app import service_api_client
|
2016-03-30 09:58:10 +01:00
|
|
|
from app import user_api_client
|
2015-12-07 16:56:11 +00:00
|
|
|
|
|
|
|
|
|
2016-01-05 17:08:50 +00:00
|
|
|
@main.route('/two-factor', methods=['GET', 'POST'])
|
|
|
|
|
def two_factor():
|
2016-01-07 12:43:10 +00:00
|
|
|
# TODO handle user_email not in session
|
2016-02-23 15:45:19 +00:00
|
|
|
try:
|
|
|
|
|
user_id = session['user_details']['id']
|
|
|
|
|
except KeyError:
|
|
|
|
|
return redirect('main.sign_in')
|
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)
|
2015-12-07 16:56:11 +00:00
|
|
|
|
|
|
|
|
if form.validate_on_submit():
|
2016-01-28 11:34:15 +00:00
|
|
|
try:
|
2016-03-30 09:58:10 +01:00
|
|
|
user = user_api_client.get_user(user_id)
|
2016-03-29 22:50:40 +01:00
|
|
|
services = service_api_client.get_services({'user_id': str(user_id)}).get('data', [])
|
2016-01-28 11:34:15 +00:00
|
|
|
# Check if coming from new password page
|
|
|
|
|
if 'password' in session['user_details']:
|
|
|
|
|
user.set_password(session['user_details']['password'])
|
2016-03-30 09:58:10 +01:00
|
|
|
user_api_client.update_user(user)
|
2016-03-07 14:39:20 +00:00
|
|
|
login_user(user, remember=True)
|
2016-01-28 11:34:15 +00:00
|
|
|
finally:
|
|
|
|
|
del session['user_details']
|
2016-03-14 16:30:48 +00:00
|
|
|
|
|
|
|
|
next_url = request.args.get('next')
|
|
|
|
|
if next_url and _is_safe_redirect_url(next_url):
|
|
|
|
|
return redirect(next_url)
|
|
|
|
|
|
2016-03-18 10:49:22 +00:00
|
|
|
if current_user.platform_admin:
|
|
|
|
|
return redirect(url_for('main.show_all_services'))
|
2016-03-08 14:58:29 +00:00
|
|
|
if len(services) == 1:
|
2016-02-05 14:25:48 +00:00
|
|
|
return redirect(url_for('main.service_dashboard', service_id=services[0]['id']))
|
|
|
|
|
else:
|
|
|
|
|
return redirect(url_for('main.choose_service'))
|
2016-01-05 17:08:50 +00:00
|
|
|
|
|
|
|
|
return render_template('views/two-factor.html', form=form)
|
2016-03-14 16:30:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
# see http://flask.pocoo.org/snippets/62/
|
|
|
|
|
def _is_safe_redirect_url(target):
|
|
|
|
|
from urllib.parse import urlparse, urljoin
|
|
|
|
|
host_url = urlparse(request.host_url)
|
|
|
|
|
redirect_url = urlparse(urljoin(request.host_url, target))
|
|
|
|
|
return redirect_url.scheme in ('http', 'https') and \
|
|
|
|
|
host_url.netloc == redirect_url.netloc
|