Turn on redirects two_factor_email

This is part of the work to make sure user is redirected
to the page they initially were meant to visit after
they sign in.
This commit is contained in:
Pea Tyczynska
2020-10-09 11:41:47 +01:00
parent 5dd010ece8
commit a531c888ba
3 changed files with 27 additions and 7 deletions

View File

@@ -36,6 +36,7 @@ def two_factor_email_interstitial(token):
@main.route('/email-auth/<token>', methods=['POST'])
def two_factor_email(token):
redirect_url = request.args.get('next')
if current_user.is_authenticated:
return redirect_when_logged_in(platform_admin=current_user.platform_admin)
@@ -48,14 +49,14 @@ def two_factor_email(token):
current_app.config['EMAIL_2FA_EXPIRY_SECONDS']
))
except SignatureExpired:
return render_template('views/email-link-invalid.html')
return render_template('views/email-link-invalid.html', redirect_url=redirect_url)
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:
return render_template('views/email-link-invalid.html')
return render_template('views/email-link-invalid.html', redirect_url=redirect_url)
return log_in_user(user_id)

View File

@@ -11,7 +11,11 @@
<div class="govuk-grid-column-two-thirds">
<h1 class="heading-large">The link has expired</h1>
<p class="govuk-body"><a class="govuk-link govuk-link--no-visited-state" href="{{ url_for('main.sign_in') }}">Sign in again</a> to get a new link.</p>
<p class="govuk-body">
<a class="govuk-link govuk-link--no-visited-state" href="{{ url_for('main.sign_in', next=redirect_url) }}">
Sign in again
</a> to get a new link.
</p>
</div>
</div>

View File

@@ -339,17 +339,22 @@ def test_valid_two_factor_email_link_logs_in_user(
assert response.location == url_for('main.show_accounts_or_dashboard', _external=True)
@pytest.mark.parametrize('redirect_url', [
None,
'blob',
])
def test_two_factor_email_link_has_expired(
app_,
valid_token,
client,
mock_send_verify_code,
fake_uuid
fake_uuid,
redirect_url
):
with set_config(app_, 'EMAIL_2FA_EXPIRY_SECONDS', -1):
response = client.post(
url_for_endpoint_with_token('main.two_factor_email', token=valid_token),
url_for_endpoint_with_token('main.two_factor_email', token=valid_token, next=redirect_url),
follow_redirects=True,
)
@@ -357,6 +362,8 @@ def test_two_factor_email_link_has_expired(
page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
assert page.h1.text.strip() == 'The link has expired'
assert page.select_one('a:contains("Sign in again")')['href'] == url_for('main.sign_in', next=redirect_url)
assert mock_send_verify_code.called is False
@@ -372,20 +379,26 @@ def test_two_factor_email_link_is_invalid(
assert normalize_spaces(
page.select_one('.banner-dangerous').text
) == "Theres something wrong with the link youve used."
assert response.status_code == 404
@pytest.mark.parametrize('redirect_url', [
None,
'blob',
])
def test_two_factor_email_link_is_already_used(
client,
valid_token,
mocker,
mock_send_verify_code
mock_send_verify_code,
redirect_url
):
mocker.patch('app.user_api_client.check_verify_code', return_value=(False, 'Code has expired'))
response = client.post(
url_for_endpoint_with_token('main.two_factor_email', token=valid_token),
url_for_endpoint_with_token('main.two_factor_email', token=valid_token, next=redirect_url),
follow_redirects=True
)
@@ -393,6 +406,8 @@ def test_two_factor_email_link_is_already_used(
assert response.status_code == 200
assert page.h1.text.strip() == 'The link has expired'
assert page.select_one('a:contains("Sign in again")')['href'] == url_for('main.sign_in', next=redirect_url)
assert mock_send_verify_code.called is False