|
16 | 16 | from django.shortcuts import redirect |
17 | 17 | from django.urls import reverse |
18 | 18 | from django.utils.functional import SimpleLazyObject |
| 19 | +from social_core.exceptions import AuthCanceled, AuthFailed |
| 20 | +from social_django.middleware import SocialAuthExceptionMiddleware |
19 | 21 | from watson.middleware import SearchContextMiddleware |
20 | 22 | from watson.search import search_context_manager |
21 | 23 |
|
@@ -78,58 +80,12 @@ def __call__(self, request): |
78 | 80 | return self.get_response(request) |
79 | 81 |
|
80 | 82 |
|
81 | | -class AuthProviderHealthCheckMiddleware: |
82 | | - def __init__(self, get_response): |
83 | | - self.get_response = get_response |
84 | | - self.providers = { |
85 | | - "/login/oidc/": { |
86 | | - "name": "OIDC", |
87 | | - "endpoint": getattr(settings, "SOCIAL_AUTH_OIDC_OIDC_ENDPOINT", None), |
88 | | - }, |
89 | | - "/login/google-oauth2/": { |
90 | | - "name": "Google", |
91 | | - "endpoint": "https://accounts.google.com/.well-known/openid-configuration", |
92 | | - }, |
93 | | - "/login/okta-oauth2/": { |
94 | | - "name": "Okta", |
95 | | - "endpoint": getattr(settings, "SOCIAL_AUTH_OKTA_OAUTH2_API_URL", None), |
96 | | - }, |
97 | | - "/login/azuread-tenant-oauth2/": { |
98 | | - "name": "Azure AD", |
99 | | - "endpoint": f"https://login.microsoftonline.com/{getattr(settings, 'SOCIAL_AUTH_AZUREAD_TENANT_OAUTH2_TENANT_ID', '')}/v2.0/.well-known/openid-configuration", |
100 | | - }, |
101 | | - "/login/keycloak-oauth2/": { |
102 | | - "name": "Keycloak", |
103 | | - "endpoint": getattr(settings, "SOCIAL_AUTH_KEYCLOAK_OAUTH2_API_URL", None), |
104 | | - }, |
105 | | - "/login/auth0/": { |
106 | | - "name": "Auth0", |
107 | | - "endpoint": getattr(settings, "SOCIAL_AUTH_AUTH0_DOMAIN", None), |
108 | | - }, |
109 | | - "/login/gitlab/": { |
110 | | - "name": "GitLab", |
111 | | - "endpoint": getattr(settings, "SOCIAL_AUTH_GITLAB_API_URL", None), |
112 | | - }, |
113 | | - "/login/github/": { |
114 | | - "name": "GitHub Enterprise", |
115 | | - "endpoint": getattr(settings, "SOCIAL_AUTH_GITHUB_ENTERPRISE_URL", None), |
116 | | - }, |
117 | | - } |
118 | | - |
119 | | - def __call__(self, request): |
120 | | - for path, config in self.providers.items(): |
121 | | - if request.path.startswith(path) and config["endpoint"]: |
122 | | - try: |
123 | | - response = requests.get(config["endpoint"], timeout=3, allow_redirects=False) |
124 | | - if response.status_code >= 500: |
125 | | - raise requests.exceptions.RequestException(config["name"] + " returned " + str(response.status_code)) |
126 | | - except requests.exceptions.RequestException: |
127 | | - messages.error( |
128 | | - request, |
129 | | - f"Login via {config['name']} is temporarily unavailable. Please use the standard login below. ", |
130 | | - ) |
131 | | - return redirect("/login") |
132 | | - return self.get_response(request) |
| 83 | +class CustomSocialAuthExceptionMiddleware(SocialAuthExceptionMiddleware): |
| 84 | + def process_exception(self, request, exception): |
| 85 | + if isinstance(exception, (requests.exceptions.RequestException, AuthCanceled, AuthFailed)): |
| 86 | + messages.error(request, "Login via social authentication is temporarily unavailable. Please use the standard login below.") |
| 87 | + return redirect("/login") |
| 88 | + return super().process_exception(request, exception) |
133 | 89 |
|
134 | 90 |
|
135 | 91 | class DojoSytemSettingsMiddleware: |
|
0 commit comments