Skip to content

Commit 7e2480c

Browse files
Auth Improvements (#10752) (#10761)
* Return more detail in MFA failure response * Reject auth requests for users who are inactive * Move markdown config out of settings.py (cherry picked from commit 9018462) Co-authored-by: Oliver <oliver.henry.walters@gmail.com>
1 parent fd43753 commit 7e2480c

File tree

4 files changed

+58
-41
lines changed

4 files changed

+58
-41
lines changed

src/backend/InvenTree/InvenTree/AllUserRequire2FAMiddleware.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ class AllUserRequire2FAMiddleware(MiddlewareMixin):
4444

4545
def on_require_2fa(self, request: HttpRequest) -> HttpResponse:
4646
"""Force user to mfa activation."""
47-
return JsonResponse({'id': 'mfa_register'}, status=401)
47+
return JsonResponse(
48+
{'id': 'mfa_register', 'error': self.require_2fa_message}, status=401
49+
)
4850

4951
def is_allowed_page(self, request: HttpRequest) -> bool:
5052
"""Check if the current page can be accessed without mfa."""
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"""Configuration options for django-markdownify.
2+
3+
Ref: https://django-markdownify.readthedocs.io/en/latest/settings.html
4+
"""
5+
6+
7+
def markdownify_config():
8+
"""Return configuration dictionary for django-markdownify."""
9+
return {
10+
'default': {
11+
'BLEACH': True,
12+
'WHITELIST_ATTRS': ['href', 'src', 'alt'],
13+
'MARKDOWN_EXTENSIONS': ['markdown.extensions.extra'],
14+
'WHITELIST_TAGS': [
15+
'a',
16+
'abbr',
17+
'b',
18+
'blockquote',
19+
'code',
20+
'em',
21+
'h1',
22+
'h2',
23+
'h3',
24+
'h4',
25+
'h5',
26+
'hr',
27+
'i',
28+
'img',
29+
'li',
30+
'ol',
31+
'p',
32+
'pre',
33+
's',
34+
'strong',
35+
'table',
36+
'thead',
37+
'tbody',
38+
'th',
39+
'tr',
40+
'td',
41+
'ul',
42+
],
43+
}
44+
}

src/backend/InvenTree/InvenTree/settings.py

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
from users.oauth2_scopes import oauth2_scopes
4343

4444
from . import config
45-
from .setting import locales, storages
45+
from .setting import locales, markdown, storages
4646

4747
try:
4848
import django_stubs_ext
@@ -1399,42 +1399,7 @@
13991399
# Markdownify configuration
14001400
# Ref: https://django-markdownify.readthedocs.io/en/latest/settings.html
14011401

1402-
MARKDOWNIFY = {
1403-
'default': {
1404-
'BLEACH': True,
1405-
'WHITELIST_ATTRS': ['href', 'src', 'alt'],
1406-
'MARKDOWN_EXTENSIONS': ['markdown.extensions.extra'],
1407-
'WHITELIST_TAGS': [
1408-
'a',
1409-
'abbr',
1410-
'b',
1411-
'blockquote',
1412-
'code',
1413-
'em',
1414-
'h1',
1415-
'h2',
1416-
'h3',
1417-
'h4',
1418-
'h5',
1419-
'hr',
1420-
'i',
1421-
'img',
1422-
'li',
1423-
'ol',
1424-
'p',
1425-
'pre',
1426-
's',
1427-
'strong',
1428-
'table',
1429-
'thead',
1430-
'tbody',
1431-
'th',
1432-
'tr',
1433-
'td',
1434-
'ul',
1435-
],
1436-
}
1437-
}
1402+
MARKDOWNIFY = markdown.markdownify_config()
14381403

14391404
# Ignore these error types for in-database error logging
14401405
IGNORED_ERRORS = [Http404, HttpResponseGone, django.core.exceptions.PermissionDenied]

src/backend/InvenTree/InvenTree/views.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ def auth_request(request):
1212
1313
Useful for (for example) redirecting authentication requests through django's permission framework.
1414
"""
15-
if request.user and request.user.is_authenticated:
16-
return HttpResponse(status=200)
17-
return HttpResponse(status=403)
15+
if not request.user or not request.user.is_authenticated:
16+
return HttpResponse(status=403)
17+
18+
if not request.user.is_active:
19+
# Reject requests from inactive users
20+
return HttpResponse(status=403)
21+
22+
# User is authenticated and active
23+
return HttpResponse(status=200)

0 commit comments

Comments
 (0)