2016-01-05 17:08:50 +00:00
|
|
|
from flask import (
|
2018-02-20 11:22:17 +00:00
|
|
|
Markup,
|
2016-04-26 12:14:06 +01:00
|
|
|
abort,
|
2018-02-20 11:22:17 +00:00
|
|
|
flash,
|
|
|
|
|
redirect,
|
|
|
|
|
render_template,
|
|
|
|
|
request,
|
|
|
|
|
session,
|
|
|
|
|
url_for,
|
2016-01-21 11:33:53 +00:00
|
|
|
)
|
2018-02-20 11:22:17 +00:00
|
|
|
from flask_login import current_user
|
2016-01-21 11:33:53 +00:00
|
|
|
|
2019-05-23 15:27:35 +01:00
|
|
|
from app import login_manager
|
2015-11-27 09:47:29 +00:00
|
|
|
from app.main import main
|
2015-12-08 15:30:55 +00:00
|
|
|
from app.main.forms import LoginForm
|
2019-05-23 15:27:35 +01:00
|
|
|
from app.models.user import InvitedUser, User
|
2020-05-26 17:39:25 +01:00
|
|
|
from app.utils import hide_from_search_engines
|
2021-07-14 23:10:49 +01:00
|
|
|
from app.utils.login import is_safe_redirect_url
|
2015-11-27 09:47:29 +00:00
|
|
|
|
|
|
|
|
|
2016-01-05 14:30:06 +00:00
|
|
|
@main.route('/sign-in', methods=(['GET', 'POST']))
|
2020-05-26 17:39:25 +01:00
|
|
|
@hide_from_search_engines
|
2016-01-05 14:30:06 +00:00
|
|
|
def sign_in():
|
2021-07-14 23:10:49 +01:00
|
|
|
redirect_url = request.args.get('next')
|
2016-05-04 13:01:55 +01:00
|
|
|
if current_user and current_user.is_authenticated:
|
2021-07-14 23:10:49 +01:00
|
|
|
if redirect_url and is_safe_redirect_url(redirect_url):
|
|
|
|
|
return redirect(redirect_url)
|
2018-11-15 15:38:43 +00:00
|
|
|
return redirect(url_for('main.show_accounts_or_dashboard'))
|
2016-02-23 15:45:19 +00:00
|
|
|
|
2016-01-27 12:22:32 +00:00
|
|
|
form = LoginForm()
|
2020-10-05 15:38:34 +01:00
|
|
|
password_reset_url = url_for('.forgot_password', next=request.args.get('next'))
|
2017-12-06 20:24:25 +00:00
|
|
|
|
2016-01-27 12:22:32 +00:00
|
|
|
if form.validate_on_submit():
|
2016-03-30 16:16:34 +01:00
|
|
|
|
2019-05-23 15:27:35 +01:00
|
|
|
user = User.from_email_address_and_password_or_none(
|
|
|
|
|
form.email_address.data, form.password.data
|
|
|
|
|
)
|
|
|
|
|
|
2021-06-10 19:07:35 +01:00
|
|
|
if user:
|
|
|
|
|
# add user to session to mark us as in the process of signing the user in
|
|
|
|
|
session['user_details'] = {"email": user.email_address, "id": user.id}
|
2016-03-30 16:16:34 +01:00
|
|
|
|
2021-06-10 19:07:35 +01:00
|
|
|
if user.state == 'pending':
|
|
|
|
|
return redirect(url_for('main.resend_email_verification', next=redirect_url))
|
|
|
|
|
|
|
|
|
|
if user.is_active:
|
|
|
|
|
if session.get('invited_user_id'):
|
|
|
|
|
invited_user = InvitedUser.from_session()
|
|
|
|
|
if user.email_address.lower() != invited_user.email_address.lower():
|
|
|
|
|
flash("You cannot accept an invite for another person.")
|
|
|
|
|
session.pop('invited_user_id', None)
|
|
|
|
|
abort(403)
|
|
|
|
|
else:
|
|
|
|
|
invited_user.accept_invite()
|
|
|
|
|
|
|
|
|
|
user.send_login_code()
|
|
|
|
|
|
|
|
|
|
if user.sms_auth:
|
|
|
|
|
return redirect(url_for('.two_factor_sms', next=redirect_url))
|
|
|
|
|
if user.email_auth:
|
|
|
|
|
return redirect(url_for('.two_factor_email_sent', next=redirect_url))
|
|
|
|
|
if user.webauthn_auth:
|
|
|
|
|
return redirect(url_for('.two_factor_webauthn', next=redirect_url))
|
2017-11-07 16:11:31 +00:00
|
|
|
|
2016-01-28 16:36:36 +00:00
|
|
|
# Vague error message for login in case of user not known, locked, inactive or password not verified
|
2017-10-18 14:51:26 +01:00
|
|
|
flash(Markup(
|
|
|
|
|
(
|
2020-10-05 15:38:34 +01:00
|
|
|
f"The email address or password you entered is incorrect."
|
2021-07-14 09:42:56 +01:00
|
|
|
f" <a href={password_reset_url} class='govuk-link'>Forgotten your password?</a>"
|
2020-10-05 15:38:34 +01:00
|
|
|
)
|
2016-04-26 12:14:06 +01:00
|
|
|
))
|
2016-01-05 14:30:06 +00:00
|
|
|
|
2017-02-17 14:06:09 +00:00
|
|
|
other_device = current_user.logged_in_elsewhere()
|
|
|
|
|
return render_template(
|
|
|
|
|
'views/signin.html',
|
|
|
|
|
form=form,
|
2020-10-09 11:41:06 +01:00
|
|
|
again=bool(redirect_url),
|
2020-10-05 15:38:34 +01:00
|
|
|
other_device=other_device,
|
|
|
|
|
password_reset_url=password_reset_url
|
2017-02-17 14:06:09 +00:00
|
|
|
)
|
2017-02-16 13:33:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@login_manager.unauthorized_handler
|
|
|
|
|
def sign_in_again():
|
|
|
|
|
return redirect(
|
|
|
|
|
url_for('main.sign_in', next=request.path)
|
|
|
|
|
)
|