Skip to content

Commit 9b0e8c1

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 16de4dc commit 9b0e8c1

File tree

5 files changed

+162
-12
lines changed

5 files changed

+162
-12
lines changed

src/open_inwoner/accounts/backends.py

+14-8
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33

44
from django.conf import settings
55
from django.contrib.auth import get_user_model
6-
from django.contrib.auth.backends import ModelBackend
6+
from django.contrib.auth.backends import BaseBackend, ModelBackend
77
from django.contrib.auth.hashers import check_password
88
from django.urls import reverse, reverse_lazy
99

1010
from axes.backends import AxesBackend
11-
from digid_eherkenning.oidc.backends import BaseBackend
1211
from mozilla_django_oidc_db.backends import OIDCAuthenticationBackend
1312
from mozilla_django_oidc_db.config import dynamic_setting
1413
from oath import accept_totp
@@ -25,14 +24,14 @@
2524
class UserModelEmailBackend(ModelBackend):
2625
"""
2726
Authentication backend for login with email address.
27+
28+
Based on the default ModelBackend, but with support for case insensitive
29+
email lookups, scoped to the correct login type.
2830
"""
2931

30-
def authenticate(
31-
self, request, username=None, password=None, user=None, token=None, **kwargs
32-
):
33-
config = SiteConfiguration.get_solo()
32+
def authenticate(self, request, username=None, password=None):
3433
User = get_user_model()
35-
if username and password and not config.login_2fa_sms:
34+
if username and password:
3635
try:
3736
user = User.objects.get(
3837
email__iexact=username,
@@ -55,8 +54,15 @@ def authenticate(
5554
User().set_password(password)
5655
return None
5756

57+
58+
class Verify2FATokenBackend(BaseBackend):
59+
"""
60+
Verify a TOTP token for a user.
61+
"""
62+
63+
def authenticate(self, request, *, user=None, token=None):
5864
# 2FA with sms verification
59-
if config.login_2fa_sms and user and token:
65+
if user and token:
6066
accepted, drift = accept_totp(
6167
key=user.seed,
6268
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

@@ -1709,6 +1711,38 @@ def test_login(self):
17091711
# Verify that the user has been authenticated
17101712
self.assertIn("_auth_user_id", self.app.session)
17111713

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

src/open_inwoner/accounts/tests/test_backends.py

+111-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
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
710
from furl import furl
811
from mozilla_django_oidc_db.config import store_config
12+
from oath import totp
913

10-
from .factories import UserFactory
14+
from open_inwoner.accounts.choices import LoginTypeChoices
15+
from open_inwoner.accounts.tests.factories import UserFactory
1116

1217

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

src/open_inwoner/conf/base.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -492,11 +492,10 @@
492492
]
493493

494494

495-
# Allow logging in with email+password
496495
AUTHENTICATION_BACKENDS = [
497496
"open_inwoner.accounts.backends.CustomAxesBackend",
498497
"open_inwoner.accounts.backends.UserModelEmailBackend",
499-
"django.contrib.auth.backends.ModelBackend",
498+
"open_inwoner.accounts.backends.Verify2FATokenBackend",
500499
"digid_eherkenning.backends.DigiDBackend",
501500
"eherkenning.backends.eHerkenningBackend",
502501
"open_inwoner.accounts.backends.DigiDEHerkenningOIDCBackend",

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)