|
7 | 7 |
|
8 | 8 | import pghistory.middleware |
9 | 9 | import requests |
| 10 | +import urllib3 |
10 | 11 | from auditlog.context import set_actor |
11 | 12 | from auditlog.middleware import AuditlogMiddleware as _AuditlogMiddleware |
12 | 13 | from django.conf import settings |
|
16 | 17 | from django.shortcuts import redirect |
17 | 18 | from django.urls import reverse |
18 | 19 | from django.utils.functional import SimpleLazyObject |
| 20 | +from social_core.exceptions import AuthCanceled, AuthFailed |
| 21 | +from social_django.middleware import SocialAuthExceptionMiddleware |
19 | 22 | from watson.middleware import SearchContextMiddleware |
20 | 23 | from watson.search import search_context_manager |
21 | 24 |
|
@@ -78,58 +81,22 @@ def __call__(self, request): |
78 | 81 | return self.get_response(request) |
79 | 82 |
|
80 | 83 |
|
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 | | - } |
| 84 | +class CustomSocialAuthExceptionMiddleware(SocialAuthExceptionMiddleware): |
| 85 | + def process_exception(self, request, exception): |
| 86 | + if isinstance(exception, ( |
| 87 | + requests.exceptions.RequestException, |
| 88 | + requests.exceptions.SSLError, |
| 89 | + urllib3.exceptions.MaxRetryError, |
| 90 | + AuthCanceled, |
| 91 | + AuthFailed, |
| 92 | + )): |
| 93 | + messages.error( |
| 94 | + request, |
| 95 | + "Login via social authentication is temporarily unavailable. Please use the standard login below.", |
| 96 | + ) |
| 97 | + return redirect("/login") |
118 | 98 |
|
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) |
| 99 | + return super().process_exception(request, exception) |
133 | 100 |
|
134 | 101 |
|
135 | 102 | class DojoSytemSettingsMiddleware: |
|
0 commit comments