Skip to content

Commit d76311c

Browse files
committed
feat: remove monkey patching
1 parent ee91b53 commit d76311c

File tree

6 files changed

+28
-63
lines changed

6 files changed

+28
-63
lines changed

Diff for: CHANGELOG.md

+26
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,31 @@
99

1010
### Changed
1111

12+
### Removed
13+
14+
- Admin Monkey Patching
15+
16+
The Admin UI will not longer be automatically patched. The `TwoFactorSiteAdmin` will need to be explicitly
17+
configured in urls.py.
18+
19+
```py
20+
# urls.py
21+
from django.urls import path
22+
from two_factor.admin import TwoFactorAdminSite
23+
url_patterns = [
24+
path('admin/', TwoFactorAdminSite().urls),
25+
]
26+
```
27+
28+
Custom admin sites can extend `TwoFactorSiteAdmin` or `TwoFactorSideAdminMixin` to inherit the behavior.
29+
30+
```py
31+
# admin.py
32+
class MyCustomAdminSite(TwoFactorSiteAdminMixin, AdminSite):
33+
# implement your customizations here.
34+
pass
35+
```
36+
1237

1338
## 1.14.0
1439

@@ -38,6 +63,7 @@
3863
- The QR code now always uses a white background to support pages displayed
3964
with a dark theme.
4065

66+
4167
### Removed
4268

4369
- Python 3.5 and 3.6 support

Diff for: docs/configuration.rst

+1-8
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,6 @@ Configuration
44
General Settings
55
----------------
66

7-
``TWO_FACTOR_PATCH_ADMIN`` (default: ``True``)
8-
Whether the Django admin is patched to use the default login view.
9-
10-
.. warning::
11-
The admin currently does not enforce one-time passwords being set for
12-
admin users.
13-
147
``LOGIN_URL``
158
Should point to the login view provided by this application as described in
169
setup. This login view handles password authentication followed by a one-time
@@ -123,7 +116,7 @@ Next, add additional urls to your config:
123116
124117
# urls.py
125118
from two_factor.gateways.twilio.urls import urlpatterns as tf_twilio_urls
126-
119+
127120
urlpatterns = [
128121
path('', include(tf_twilio_urls)),
129122
...

Diff for: tests/test_admin.py

-18
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,11 @@
55
from django.test import TestCase
66
from django.test.utils import override_settings
77

8-
from two_factor.admin import patch_admin, unpatch_admin
9-
108
from .utils import UserMixin
119

1210

1311
@override_settings(ROOT_URLCONF='tests.urls_admin')
1412
class TwoFactorAdminSiteTest(UserMixin, TestCase):
15-
16-
def setUp(self):
17-
patch_admin()
18-
19-
def tearDown(self):
20-
unpatch_admin()
21-
22-
def test(self):
23-
response = self.client.get('/admin/', follow=True)
24-
redirect_to = '%s?next=/admin/' % reverse('admin:login')
25-
self.assertRedirects(response, redirect_to)
26-
27-
28-
@override_settings(ROOT_URLCONF='tests.urls_admin')
29-
class AdminPatchTest(TestCase):
3013
"""
3114
otp_admin is admin console that needs OTP for access.
3215
Only admin users (is_staff and is_active)
@@ -55,7 +38,6 @@ def test_anonymous_get_admin_login(self):
5538
response = self.client.get(login_url, follow=True)
5639
self.assertEqual(response.status_code, 200)
5740

58-
5941
def test_is_staff_not_verified_not_setup_get_admin_index_redirects_to_setup(self):
6042
"""
6143
admins without MFA setup should be redirected to the setup page.

Diff for: two_factor/admin.py

-30
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ def redirect_to_mfa_setup(self, request):
4141
# have MFA enabled on their account. We're going to redirect them
4242
# to the MFA setup.
4343

44-
# TODO: Add redirect_to functionality to MFA setup.
4544
# TODO: Add message indicating why the user was directed or setup and MFA required
4645
# interstitial page to explain to the user they need to setup MFA.
4746
setup_url = reverse('two_factor:setup')
@@ -159,32 +158,3 @@ class AdminSiteOTPRequired(TwoFactorAdminSite):
159158
warnings.warn('AdminSiteOTPRequired is deprecated by TwoFactorAdminSite, please update.',
160159
category=DeprecationWarning)
161160
pass
162-
163-
164-
def patch_admin():
165-
warnings.warn('two-factor admin patching will be removed, use TwoFactorAdminSite or TwoFactorAdminSiteMixin.',
166-
category=DeprecationWarning)
167-
# overrides
168-
setattr(AdminSite, 'login', TwoFactorAdminSiteMixin.login)
169-
setattr(AdminSite, 'admin_view', TwoFactorAdminSiteMixin.admin_view)
170-
setattr(AdminSite, 'has_permission', TwoFactorAdminSiteMixin.has_permission)
171-
# additions
172-
setattr(AdminSite, 'has_admin_permission', original_has_permission)
173-
setattr(AdminSite, 'has_mfa_setup', TwoFactorAdminSiteMixin.has_mfa_setup)
174-
setattr(AdminSite, 'redirect_to_mfa_setup', TwoFactorAdminSiteMixin.redirect_to_mfa_setup)
175-
176-
177-
def unpatch_admin():
178-
warnings.warn('django-two-factor admin patching is deprecated, use TwoFactorAdminSite or TwoFactorAdminSiteMixin.',
179-
category=DeprecationWarning)
180-
# we really only need unpatching in our tests so this can be a noop.
181-
# overrides
182-
setattr(AdminSite, 'login', original_login)
183-
setattr(AdminSite, 'admin_view', original_admin_view)
184-
setattr(AdminSite, 'has_permission', original_has_permission)
185-
# NOTE: this unpatching doesn't really work, but becuase it just patches in our mixin it isn't harmful.
186-
187-
188-
original_login = AdminSite.login
189-
original_admin_view = AdminSite.admin_view
190-
original_has_permission = AdminSite.has_permission

Diff for: two_factor/apps.py

-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
from django.apps import AppConfig
2-
from django.conf import settings
32

43

54
class TwoFactorConfig(AppConfig):
65
name = 'two_factor'
76
verbose_name = "Django Two Factor Authentication"
8-
9-
def ready(self):
10-
if getattr(settings, 'TWO_FACTOR_PATCH_ADMIN', True):
11-
from .admin import patch_admin
12-
patch_admin()

Diff for: two_factor/views/core.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ def dispatch(self, request, *args, **kwargs):
394394

395395
@class_view_decorator(never_cache)
396396
@class_view_decorator(login_required)
397-
class SetupView(SuccessURLAllowedHostsMixin, IdempotentSessionWizardView):
397+
class SetupView(RedirectURLMixin, IdempotentSessionWizardView):
398398
"""
399399
View for handling OTP setup using a wizard.
400400

0 commit comments

Comments
 (0)