Skip to content

Commit 1d68128

Browse files
🐛 Catch AuthTokenError in middleware (#13608)
1 parent fb49ecd commit 1d68128

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

dojo/middleware.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from django.shortcuts import redirect
1717
from django.urls import reverse
1818
from django.utils.functional import SimpleLazyObject
19-
from social_core.exceptions import AuthCanceled, AuthFailed, AuthForbidden
19+
from social_core.exceptions import AuthCanceled, AuthFailed, AuthForbidden, AuthTokenError
2020
from social_django.middleware import SocialAuthExceptionMiddleware
2121
from watson.middleware import SearchContextMiddleware
2222
from watson.search import search_context_manager
@@ -94,6 +94,9 @@ def process_exception(self, request, exception):
9494
if isinstance(exception, AuthForbidden):
9595
messages.error(request, "You are not authorized to log in via this method. Please contact support or use the standard login.")
9696
return redirect("/login?force_login_form")
97+
if isinstance(exception, AuthTokenError):
98+
messages.error(request, "Social login failed due to an invalid or expired token. Please try again or use the standard login.")
99+
return redirect("/login?force_login_form")
97100
if isinstance(exception, TypeError) and "'NoneType' object is not iterable" in str(exception):
98101
logger.warning("OIDC login error: NoneType is not iterable")
99102
messages.error(request, "An unexpected error occurred during social login. Please use the standard login.")

unittests/test_social_auth_failure_handling.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from django.http import HttpResponse
88
from django.test import RequestFactory, override_settings
99
from requests.exceptions import ConnectionError as RequestsConnectionError
10-
from social_core.exceptions import AuthCanceled, AuthFailed, AuthForbidden
10+
from social_core.exceptions import AuthCanceled, AuthFailed, AuthForbidden, AuthTokenError
1111

1212
from dojo.middleware import CustomSocialAuthExceptionMiddleware
1313

@@ -52,6 +52,7 @@ def test_social_auth_exception_redirects_to_login(self):
5252
(AuthCanceled("User canceled login"), "Social login was canceled. Please try again or use the standard login."),
5353
(AuthFailed("Token exchange failed"), "Social login failed. Please try again or use the standard login."),
5454
(AuthForbidden("User not allowed"), "You are not authorized to log in via this method. Please contact support or use the standard login."),
55+
(AuthTokenError("Invalid or expired token"), "Social login failed due to an invalid or expired token. Please try again or use the standard login."),
5556
]
5657
for path in login_paths:
5758
for exception, expected_message in exceptions:

0 commit comments

Comments
 (0)