-
Notifications
You must be signed in to change notification settings - Fork 1.9k
🐛 add middleware to handle social auth provider unavailability gracefully #13523
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Maffooch
merged 18 commits into
DefectDojo:bugfix
from
manuel-sommer:fix_auth_provider_500
Oct 30, 2025
Merged
Changes from 11 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
9a3c9b8
:tada: add middleware to handle social auth provider unavailability g…
manuel-sommer a37a3b1
-
manuel-sommer c6e1403
-
manuel-sommer bf5b515
update
manuel-sommer 894e86f
update according to recommendation
manuel-sommer 6c7768a
add unittest
manuel-sommer 660e370
Merge branch 'bugfix' into fix_auth_provider_500
manuel-sommer 11556e7
update
manuel-sommer 1674129
update on unittest
manuel-sommer ef6f0f2
add integrationtest
manuel-sommer 84b14ca
update unittest description
manuel-sommer d228d99
udpate
manuel-sommer 66022e7
udpate
manuel-sommer 496ee25
fix unittest
manuel-sommer acd29f5
Merge branch 'bugfix' into fix_auth_provider_500
manuel-sommer de8abc3
add authforbidden
manuel-sommer f7c9c3c
Merge branch 'bugfix' into fix_auth_provider_500
manuel-sommer baeaa57
Merge branch 'bugfix' into fix_auth_provider_500
manuel-sommer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| from unittest.mock import patch | ||
|
|
||
| from django.contrib import messages | ||
| from django.contrib.auth.models import AnonymousUser | ||
| from django.contrib.messages.storage.fallback import FallbackStorage | ||
| from django.contrib.sessions.middleware import SessionMiddleware | ||
| from django.http import HttpResponse | ||
| from django.test import RequestFactory, override_settings | ||
| from requests.exceptions import ConnectionError as RequestsConnectionError | ||
| from social_core.exceptions import AuthCanceled, AuthFailed | ||
|
|
||
| from dojo.middleware import CustomSocialAuthExceptionMiddleware | ||
|
|
||
| from .dojo_test_case import DojoTestCase | ||
|
|
||
|
|
||
| class TestSocialAuthMiddlewareUnit(DojoTestCase): | ||
|
|
||
| """ | ||
| Unit tests: | ||
| Directly test CustomSocialAuthExceptionMiddleware behavior | ||
| by simulating exceptions (ConnectionError, AuthCanceled, AuthFailed), | ||
| without relying on actual backend configuration or whether the | ||
| /complete/<backend>/ URLs are registered and accessible. | ||
| """ | ||
|
|
||
| def setUp(self): | ||
| self.factory = RequestFactory() | ||
| self.middleware = CustomSocialAuthExceptionMiddleware(lambda *_: HttpResponse("OK")) | ||
|
|
||
| def _prepare_request(self, path): | ||
| request = self.factory.get(path) | ||
| request.user = AnonymousUser() | ||
| SessionMiddleware(lambda *_: None).process_request(request) | ||
| request.session.save() | ||
| request._messages = FallbackStorage(request) | ||
| return request | ||
|
|
||
| def test_social_auth_exception_redirects_to_login(self): | ||
| login_paths = [ | ||
| "/login/oidc/", | ||
| "/login/auth0/", | ||
| "/login/google-oauth2/", | ||
| "/login/okta-oauth2/", | ||
| "/login/azuread-tenant-oauth2/", | ||
| "/login/gitlab/", | ||
| "/login/keycloak-oauth2/", | ||
| "/login/github/", | ||
| ] | ||
| exceptions = [ | ||
| (RequestsConnectionError("Host unreachable"), "Login via social authentication is temporarily unavailable. Please use the standard login below."), | ||
| (AuthCanceled("User canceled login"), "Social login was canceled. Please try again or use the standard login."), | ||
| (AuthFailed("Token exchange failed"), "Social login failed. Please try again or use the standard login."), | ||
| ] | ||
| for path in login_paths: | ||
| for exception, expected_message in exceptions: | ||
| with self.subTest(path=path, exception=type(exception).__name__): | ||
| request = self._prepare_request(path) | ||
| response = self.middleware.process_exception(request, exception) | ||
| self.assertEqual(response.status_code, 302) | ||
| self.assertEqual(response.url, "/login") | ||
| storage = list(messages.get_messages(request)) | ||
| self.assertTrue(any(expected_message in str(msg) for msg in storage)) | ||
|
|
||
| def test_non_social_auth_path_still_redirects_on_auth_exception(self): | ||
| """Ensure middleware handles AuthFailed even on unrelated paths.""" | ||
| request = self._prepare_request("/some/other/path/") | ||
| exception = AuthFailed("Should be handled globally") | ||
| response = self.middleware.process_exception(request, exception) | ||
| self.assertEqual(response.status_code, 302) | ||
| self.assertEqual(response.url, "/login") | ||
| storage = list(messages.get_messages(request)) | ||
| self.assertTrue(any("Social login failed. Please try again or use the standard login." in str(msg) for msg in storage)) | ||
|
|
||
|
|
||
| @override_settings( | ||
| AUTHENTICATION_BACKENDS=( | ||
| "social_core.backends.github.GithubOAuth2", | ||
| "social_core.backends.gitlab.GitLabOAuth2", | ||
| "social_core.backends.keycloak.KeycloakOAuth2", | ||
| "social_core.backends.azuread_tenant.AzureADTenantOAuth2", | ||
| "social_core.backends.auth0.Auth0OAuth2", | ||
| "social_core.backends.okta.OktaOAuth2", | ||
| "social_core.backends.open_id_connect.OpenIdConnectAuth", | ||
| "django.contrib.auth.backends.ModelBackend", | ||
| ), | ||
| ) | ||
| class TestSocialAuthIntegrationFailures(DojoTestCase): | ||
|
|
||
| """ | ||
| Integration tests: | ||
| Simulates social login failures by calling /complete/<backend>/ URLs | ||
| and mocking auth_complete() to raise AuthFailed and AuthCanceled. | ||
| Verifies that the middleware is correctly integrated and handles backend failures. | ||
| """ | ||
|
|
||
| BACKEND_CLASS_PATHS = { | ||
| "github": "social_core.backends.github.GithubOAuth2", | ||
| "gitlab": "social_core.backends.gitlab.GitLabOAuth2", | ||
| "keycloak": "social_core.backends.keycloak.KeycloakOAuth2", | ||
| "azuread-tenant-oauth2": "social_core.backends.azuread_tenant.AzureADTenantOAuth2", | ||
| "auth0": "social_core.backends.auth0.Auth0OAuth2", | ||
| "okta-oauth2": "social_core.backends.okta.OktaOAuth2", | ||
| "oidc": "social_core.backends.open_id_connect.OpenIdConnectAuth", | ||
| } | ||
|
|
||
| def _test_backend_exception(self, backend_slug, exception, expected_message): | ||
| backend_class_path = self.BACKEND_CLASS_PATHS[backend_slug] | ||
| with patch(f"{backend_class_path}.auth_complete", side_effect=exception): | ||
| response = self.client.get(f"/complete/{backend_slug}/", follow=True) | ||
| self.assertEqual(response.status_code, 200) | ||
| self.assertContains(response, expected_message) | ||
|
|
||
| def test_all_backends_auth_failed(self): | ||
| for backend in self.BACKEND_CLASS_PATHS: | ||
| with self.subTest(backend=backend): | ||
| self._test_backend_exception(backend, AuthFailed(backend=None), "Social login failed. Please try again or use the standard login.") | ||
|
|
||
| def test_all_backends_auth_canceled(self): | ||
| for backend in self.BACKEND_CLASS_PATHS: | ||
| with self.subTest(backend=backend): | ||
| self._test_backend_exception(backend, AuthCanceled(backend=None), "Social login was canceled. Please try again or use the standard login.") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.