Skip to content

Commit 15ef532

Browse files
dekkersammar92underdarknl
authored
Fix broken token auth when 2FA is enabled (1.16) (#3327)
Co-authored-by: ammar92 <ammar.abdulamir@gmail.com> Co-authored-by: Jan Klopper <janklopper+underdark@gmail.com>
1 parent f76eee6 commit 15ef532

3 files changed

Lines changed: 17 additions & 3 deletions

File tree

docs/source/manual/usermanual.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,8 @@ After the CSV file has been uploaded the users receive a welcome email on their
246246
The OpenKAT team
247247

248248

249-
Token authentication
250-
--------------------
249+
API token authentication
250+
------------------------
251251

252252
Authentication tokens can be created in the admin interface (/admin). The token is created for an user account and will have the same permissions as the user. After creating a token it will display the newly created token once. You need to copy the token immediately, because the token are stored hashed in the database and won't be visible anymore.
253253

rocky/rocky/middleware/auth_required.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,15 @@ def middleware(request):
5757
# When 2fa is enabled, check if user is verified, otherwise redirect to 2fa setup page
5858
if (
5959
settings.TWOFACTOR_ENABLED
60-
and not request.user.is_verified()
6160
and not (
6261
# check if path is not in excluded list
6362
request.path in excluded
6463
or request.path in excluded_2fa
6564
# check if path starts with anything in excluded_prefix
6665
or any([request.path.startswith(prefix) for prefix in excluded_prefix])
6766
)
67+
# This check should be after excluding /api because API users won't have `is_verified`
68+
and not request.user.is_verified()
6869
):
6970
return redirect(two_factor_setup_path)
7071

rocky/tests/test_api.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from account.models import AuthToken
2+
3+
4+
# Regression test for https://github.com/minvws/nl-kat-coordination/issues/2872
5+
def test_api_2fa_enabled(client, settings, admin_user):
6+
settings.TWOFACTOR_ENABLED = True
7+
8+
token_object = AuthToken(name="Test", user=admin_user)
9+
token = token_object.generate_new_token()
10+
token_object.save()
11+
12+
response = client.get("/api/v1/organization/", headers={"Authorization": f"Token {token}"})
13+
assert response.status_code == 200

0 commit comments

Comments
 (0)