Merge commit from fork

* remove mfa disableing on the token flow

* also check for mfa in token issuance
This commit is contained in:
Matthias Mair authored and GitHub committed 2026-11-03 20:26:16 +11:00
1 parent 4121d9a8a5
commit 315b5bd134
3 files changed
+33 -2

No files matched your search

@@ -96,7 +96,6 @@ pages_mfa_bypass = [
'api-user-me',
'api-user-roles',
'api-inventree-info',
'api-token',
# web platform urls
'password_reset_confirm',
'index',
@@ -89,6 +89,21 @@ class MiddlewareTests(InvenTreeTestCase):
self.assertEqual(rsp3.status_code, 200)
self.check_path(url)
def test_Check2FAMiddleware_token(self):
"""Test that no API token can be generated before MFA is set up, if MFA is enforced."""
url = reverse('api-token')
with patch.dict('os.environ', {'INVENTREE_LOGIN_ENFORCE_MFA': 'True'}):
response = self.check_path(url, 401)
self.assertContains(
response,
'You must enable two-factor authentication before doing anything else.',
status_code=401,
)
# Not enforced - token can be generated
self.check_path(url)
def test_token_auth(self):
"""Test auth with token auth."""
target = reverse('api-license')
+18 -1
View File
@@ -6,14 +6,20 @@ from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ValidationError
from django.db.models import Q
from django.urls import include, path
from django.utils.translation import gettext_lazy as _
from django.views.decorators.csrf import ensure_csrf_cookie
from django.views.generic.base import RedirectView
import django_filters.rest_framework.filters as rest_filters
import structlog
from allauth.account.internal.flows.reauthentication import (
did_recently_authenticate,
get_reauthentication_flows,
)
from allauth.mfa.utils import is_mfa_enabled
from django_filters.rest_framework.filterset import FilterSet
from drf_spectacular.utils import OpenApiParameter, OpenApiResponse, extend_schema
from rest_framework import exceptions
from rest_framework import exceptions, status
from rest_framework.generics import DestroyAPIView, GenericAPIView
from rest_framework.response import Response
@@ -404,6 +410,17 @@ class GetAuthToken(GenericAPIView):
if not user.is_authenticated:
raise exceptions.NotAuthenticated() # pragma: no cover
# does this user have MFA set up? if so: was he recently verified?
if is_mfa_enabled(user) and not did_recently_authenticate(request):
return Response(
{
'id': 'reauthentication_required',
'detail': _('Reauthentication is required to generate a token'),
'data': {'flows': get_reauthentication_flows(user)},
},
status=status.HTTP_401_UNAUTHORIZED,
)
name = ApiToken.sanitize_name(name)
today = InvenTree.helpers.current_date()