Skip to content

Commit 9e85d5b

Browse files
committed
[#2816] Split user/password auth from token verification
Previously, the UserModelEmailBackend was overloaded to support both username/password authentication, as well as TOTP validation. It did this, in part, by verifying the 2FA flag on the site configuration. This was both confusing (an authentication backend should do one thing only, multiple logics means multiple backends), as well as mixing concerns (the _view_ should decide which arguments to pass to to authentication backend based on the site configuration, authentication backends should only authenticate). This commit separates both concerns into independent backends, and adds some tests to ensure that they are properly invoked.
1 parent 224f34b commit 9e85d5b

File tree

5 files changed

+162
-8
lines changed

5 files changed

+162
-8
lines changed

src/open_inwoner/accounts/backends.py

+14-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from django.conf import settings
44
from django.contrib.auth import get_user_model
5-
from django.contrib.auth.backends import ModelBackend
5+
from django.contrib.auth.backends import BaseBackend, ModelBackend
66
from django.contrib.auth.hashers import check_password
77
from django.urls import reverse, reverse_lazy
88

@@ -21,14 +21,14 @@
2121
class UserModelEmailBackend(ModelBackend):
2222
"""
2323
Authentication backend for login with email address.
24+
25+
Based on the default ModelBackend, but with support for case insensitive
26+
email lookups, scoped to the correct login type.
2427
"""
2528

26-
def authenticate(
27-
self, request, username=None, password=None, user=None, token=None, **kwargs
28-
):
29-
config = SiteConfiguration.get_solo()
29+
def authenticate(self, request, username=None, password=None):
3030
User = get_user_model()
31-
if username and password and not config.login_2fa_sms:
31+
if username and password:
3232
try:
3333
user = User.objects.get(
3434
email__iexact=username,
@@ -51,8 +51,15 @@ def authenticate(
5151
User().set_password(password)
5252
return None
5353

54+
55+
class Verify2FATokenBackend(BaseBackend):
56+
"""
57+
Verify a TOTP token for a user.
58+
"""
59+
60+
def authenticate(self, request, *, user=None, token=None):
5461
# 2FA with sms verification
55-
if config.login_2fa_sms and user and token:
62+
if user and token:
5663
accepted, drift = accept_totp(
5764
key=user.seed,
5865
response=token,

src/open_inwoner/accounts/tests/test_auth.py

+35-1
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
from unittest.mock import patch
1+
from unittest.mock import ANY, patch
22
from urllib.parse import urlencode
33

44
from django.contrib.auth.signals import user_logged_in
55
from django.contrib.sites.models import Site
66
from django.core import mail
7+
from django.core.signing import TimestampSigner
78
from django.test import RequestFactory, TestCase, override_settings
89
from django.urls import reverse, reverse_lazy
910
from django.utils.translation import gettext as _
1011

1112
import requests_mock
1213
from django_webtest import WebTest
14+
from freezegun import freeze_time
1315
from furl import furl
1416
from pyquery import PyQuery as PQ
1517

@@ -1715,6 +1717,38 @@ def test_login(self):
17151717
# Verify that the user has been authenticated
17161718
self.assertIn("_auth_user_id", self.app.session)
17171719

1720+
@patch("open_inwoner.accounts.gateways.gateway.send")
1721+
@freeze_time("2023-05-22 12:05:01")
1722+
def test_regular_login_with_valid_credentials_triggers_the_2fa_flow(
1723+
self, mock_gateway_send
1724+
):
1725+
config = SiteConfiguration.get_solo()
1726+
config.login_2fa_sms = True
1727+
config.save()
1728+
1729+
response = self.app.get(reverse("login"))
1730+
mock_gateway_send.assert_not_called()
1731+
1732+
login_form = response.forms["login-form"]
1733+
login_form["username"] = self.user.email
1734+
login_form["password"] = "test"
1735+
login_form_response = login_form.submit()
1736+
login_form_response.follow()
1737+
1738+
mock_gateway_send.assert_called_once_with(to=self.user.phonenumber, token=ANY)
1739+
1740+
params = {
1741+
"next": "",
1742+
"user": TimestampSigner().sign(self.user.pk),
1743+
}
1744+
verify_token_url = furl(reverse("verify_token")).add(params).url
1745+
self.assertRedirects(login_form_response, verify_token_url)
1746+
self.assertNotIn(
1747+
"_auth_user_id",
1748+
self.app.session,
1749+
msg="Valid credentials alone should not authenticate a user, second factor required",
1750+
)
1751+
17181752
def test_login_page_shows_correct_digid_login_url(self):
17191753
config = OpenIDConnectDigiDConfig.get_solo()
17201754

src/open_inwoner/accounts/tests/test_backends.py

+111
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1+
import datetime
12
from unittest.mock import patch
23

4+
from django.conf import settings
35
from django.contrib import auth
46
from django.test import RequestFactory, TestCase, override_settings
57
from django.urls import reverse
68

9+
from freezegun import freeze_time
10+
from oath import totp
11+
12+
from open_inwoner.accounts.choices import LoginTypeChoices
713
from open_inwoner.accounts.tests.factories import UserFactory
814

915

@@ -69,3 +75,108 @@ def test_admin_oidc_use_correct_backend(
6975
result = auth.authenticate(request)
7076

7177
self.assertEqual(result, self.user)
78+
79+
80+
@override_settings(
81+
AUTHENTICATION_BACKENDS=[
82+
"open_inwoner.accounts.backends.UserModelEmailBackend",
83+
]
84+
)
85+
class UserModelEmailBackendTestCase(TestCase):
86+
@classmethod
87+
def setUpTestData(cls):
88+
super().setUpTestData()
89+
90+
cls.password = "keepitsecert"
91+
cls.user = UserFactory(
92+
login_type=LoginTypeChoices.default, password=cls.password
93+
)
94+
for login_type in LoginTypeChoices:
95+
UserFactory(login_type=login_type)
96+
97+
def test_duplicate_emails_on_case_results_in_no_match(self):
98+
request = RequestFactory().post(reverse("login"))
99+
100+
UserFactory(email=self.user.email.upper(), login_type=self.user.login_type)
101+
result = auth.authenticate(
102+
request, username=self.user.email, password=self.password
103+
)
104+
self.assertEqual(result, None)
105+
106+
def test_correct_username_password_return_user(self):
107+
request = RequestFactory().post(reverse("login"))
108+
109+
result = auth.authenticate(
110+
request, username=self.user.email, password=self.password
111+
)
112+
self.assertEqual(result, self.user)
113+
114+
def test_incorrect_username_password_return_none(self):
115+
request = RequestFactory().post(reverse("login"))
116+
117+
for username, password in (
118+
(self.user.email, "incorrect"),
119+
("incorrect", self.password),
120+
):
121+
result = auth.authenticate(request, username=username, password=password)
122+
self.assertEqual(result, None)
123+
124+
def test_missing_username_and_or_password_returns_none(self):
125+
for username in (self.user.email, "", None):
126+
for password in (self.password, "", None):
127+
if username and password:
128+
# This is the successful case, exclude it, but ensure we
129+
# also have permutations with one valid/one invalid value.
130+
continue
131+
132+
with self.subTest(f"{username=} {password=}"):
133+
request = RequestFactory().post(reverse("login"))
134+
result = auth.authenticate(
135+
request, username=username, password=password
136+
)
137+
self.assertEqual(result, None)
138+
139+
140+
@override_settings(
141+
AUTHENTICATION_BACKENDS=[
142+
"open_inwoner.accounts.backends.Verify2FATokenBackend",
143+
]
144+
)
145+
class Verify2FATokenBackendTestCase(TestCase):
146+
@classmethod
147+
def setUpTestData(cls):
148+
super().setUpTestData()
149+
150+
cls.user = UserFactory(login_type=LoginTypeChoices.default)
151+
cls.expires_in = getattr(settings, "ACCOUNTS_USER_TOKEN_EXPIRE_TIME", 300)
152+
cls.make_token = lambda: totp(cls.user.seed, period=cls.expires_in)
153+
154+
@freeze_time("2023-05-22 12:05:01")
155+
def test_valid_token_and_user_returns_user(self):
156+
request = RequestFactory().get(reverse("verify_token"))
157+
158+
result = auth.authenticate(request, user=self.user, token=self.make_token())
159+
self.assertEqual(result, self.user)
160+
161+
def test_expired_token_and_valid_user_returns_none(self):
162+
request = RequestFactory().get(reverse("verify_token"))
163+
164+
with freeze_time("2023-05-22 12:05:01") as ft:
165+
token = self.make_token()
166+
167+
ft.tick(delta=datetime.timedelta(seconds=self.expires_in * 2))
168+
result = auth.authenticate(request, user=self.user, token=token)
169+
self.assertEqual(result, None)
170+
171+
def test_missing_user_and_or_token_returns_none(self):
172+
for user in (self.user.email, "", None):
173+
for token in (self.make_token(), "", None):
174+
if user and token:
175+
# This is the successful case, exclude it, but ensure we
176+
# also have permutations with one valid/one invalid value.
177+
continue
178+
179+
with self.subTest(f"{user=} {token=}"):
180+
request = RequestFactory().get(reverse("verify_token"))
181+
result = auth.authenticate(request, user=user, token=token)
182+
self.assertEqual(result, None)

src/open_inwoner/conf/base.py

+1
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,7 @@
496496
AUTHENTICATION_BACKENDS = [
497497
"open_inwoner.accounts.backends.CustomAxesBackend",
498498
"open_inwoner.accounts.backends.UserModelEmailBackend",
499+
"open_inwoner.accounts.backends.Verify2FATokenBackend",
499500
"digid_eherkenning.backends.DigiDBackend",
500501
"eherkenning.backends.eHerkenningBackend",
501502
"digid_eherkenning_oidc_generics.backends.OIDCAuthenticationDigiDBackend",

src/open_inwoner/conf/ci.py

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
AUTHENTICATION_BACKENDS = [
3333
"open_inwoner.accounts.backends.CustomAxesBackend",
3434
"open_inwoner.accounts.backends.UserModelEmailBackend",
35+
"open_inwoner.accounts.backends.Verify2FATokenBackend",
3536
"django.contrib.auth.backends.ModelBackend",
3637
# mock login like dev.py
3738
"digid_eherkenning.mock.backends.DigiDBackend",

0 commit comments

Comments
 (0)