|
6 | 6 | from urllib.parse import quote |
7 | 7 |
|
8 | 8 | import pghistory.middleware |
| 9 | +import requests |
9 | 10 | from auditlog.context import set_actor |
10 | 11 | from auditlog.middleware import AuditlogMiddleware as _AuditlogMiddleware |
11 | 12 | from django.conf import settings |
| 13 | +from django.contrib import messages |
12 | 14 | from django.db import models |
13 | 15 | from django.http import HttpResponseRedirect |
| 16 | +from django.shortcuts import redirect |
14 | 17 | from django.urls import reverse |
15 | 18 | from django.utils.functional import SimpleLazyObject |
16 | 19 | from watson.middleware import SearchContextMiddleware |
@@ -75,6 +78,87 @@ def __call__(self, request): |
75 | 78 | return self.get_response(request) |
76 | 79 |
|
77 | 80 |
|
| 81 | +class AuthProviderHealthCheckMiddleware: |
| 82 | + def __init__(self, get_response): |
| 83 | + self.get_response = get_response |
| 84 | + self.providers = [ |
| 85 | + { |
| 86 | + "name": "OIDC", |
| 87 | + "enabled": getattr(settings, "OIDC_AUTH_ENABLED", False), |
| 88 | + "endpoint": getattr(settings, "SOCIAL_AUTH_OIDC_OIDC_ENDPOINT", None), |
| 89 | + "path_prefix": "/login/oidc/", |
| 90 | + "check_path": "/.well-known/openid-configuration", |
| 91 | + }, |
| 92 | + { |
| 93 | + "name": "GitLab", |
| 94 | + "enabled": getattr(settings, "GITLAB_OAUTH2_ENABLED", False), |
| 95 | + "endpoint": getattr(settings, "SOCIAL_AUTH_GITLAB_API_URL", None), |
| 96 | + "path_prefix": "/login/gitlab/", |
| 97 | + "check_path": "", |
| 98 | + }, |
| 99 | + { |
| 100 | + "name": "GitHub Enterprise", |
| 101 | + "enabled": getattr(settings, "GITHUB_ENTERPRISE_OAUTH2_ENABLED", False), |
| 102 | + "endpoint": getattr(settings, "SOCIAL_AUTH_GITHUB_ENTERPRISE_URL", None), |
| 103 | + "path_prefix": "/login/github/", |
| 104 | + "check_path": "", |
| 105 | + }, |
| 106 | + { |
| 107 | + "name": "Google", |
| 108 | + "enabled": getattr(settings, "GOOGLE_OAUTH_ENABLED", False), |
| 109 | + "endpoint": "https://accounts.google.com", |
| 110 | + "path_prefix": "/login/google-oauth2/", |
| 111 | + "check_path": "/.well-known/openid-configuration", |
| 112 | + }, |
| 113 | + { |
| 114 | + "name": "Azure AD", |
| 115 | + "enabled": getattr(settings, "AZUREAD_TENANT_OAUTH2_ENABLED", False), |
| 116 | + "endpoint": f"https://login.microsoftonline.com/{getattr(settings, 'SOCIAL_AUTH_AZUREAD_TENANT_OAUTH2_TENANT_ID', '')}", |
| 117 | + "path_prefix": "/login/azuread-tenant-oauth2/", |
| 118 | + "check_path": "/v2.0/.well-known/openid-configuration", |
| 119 | + }, |
| 120 | + { |
| 121 | + "name": "Okta", |
| 122 | + "enabled": getattr(settings, "OKTA_OAUTH_ENABLED", False), |
| 123 | + "endpoint": getattr(settings, "SOCIAL_AUTH_OKTA_OAUTH2_API_URL", None), |
| 124 | + "path_prefix": "/login/okta-oauth2/", |
| 125 | + "check_path": "/.well-known/openid-configuration", |
| 126 | + }, |
| 127 | + { |
| 128 | + "name": "Keycloak", |
| 129 | + "enabled": getattr(settings, "KEYCLOAK_OAUTH2_ENABLED", False), |
| 130 | + "endpoint": getattr(settings, "SOCIAL_AUTH_KEYCLOAK_OAUTH2_API_URL", None), |
| 131 | + "path_prefix": "/login/keycloak-oauth2/", |
| 132 | + "check_path": "/.well-known/openid-configuration", |
| 133 | + }, |
| 134 | + { |
| 135 | + "name": "Auth0", |
| 136 | + "enabled": getattr(settings, "AUTH0_OAUTH2_ENABLED", False), |
| 137 | + "endpoint": getattr(settings, "SOCIAL_AUTH_AUTH0_DOMAIN", None), |
| 138 | + "path_prefix": "/login/auth0/", |
| 139 | + "check_path": "/.well-known/openid-configuration", |
| 140 | + }, |
| 141 | + ] |
| 142 | + |
| 143 | + def __call__(self, request): |
| 144 | + for provider in self.providers: |
| 145 | + if provider["enabled"] and provider["endpoint"] and request.path.startswith(provider["path_prefix"]): |
| 146 | + check_url = provider["endpoint"] + provider["check_path"] |
| 147 | + try: |
| 148 | + response = requests.get(check_url, timeout=3, allow_redirects=False) |
| 149 | + if response.status_code >= 500: |
| 150 | + raise requests.exceptions.RequestException(provider["name"] + " returned " + str(response.status_code)) |
| 151 | + except requests.exceptions.RequestException as e: |
| 152 | + logger.warning(f"{provider['name']} provider unavailable: {e!s}") |
| 153 | + messages.error( |
| 154 | + request, |
| 155 | + f"{provider['name']} login is temporarily unavailable. Please use the standard login below.", |
| 156 | + ) |
| 157 | + return redirect("/login") |
| 158 | + |
| 159 | + return self.get_response(request) |
| 160 | + |
| 161 | + |
78 | 162 | class DojoSytemSettingsMiddleware: |
79 | 163 | _thread_local = local() |
80 | 164 |
|
|
0 commit comments