49 lines
1.9 KiB
Python
49 lines
1.9 KiB
Python
from oauth2_provider.settings import OAuth2ProviderSettings, USER_SETTINGS, DEFAULTS, IMPORT_STRINGS, MANDATORY, \
|
|
_PhonyHttpRequest
|
|
|
|
from django.http import HttpRequest
|
|
from django.urls import reverse
|
|
from oauthlib.common import Request
|
|
|
|
|
|
class GooyalOAuth2Settings(OAuth2ProviderSettings):
|
|
def oidc_issuer(self, request):
|
|
"""
|
|
Helper function to get the OIDC issuer URL, either from the settings
|
|
or constructing it from the passed request.
|
|
|
|
If only an oauthlib request is available, a dummy django request is
|
|
built from that and used to generate the URL.
|
|
"""
|
|
if self.OIDC_ISS_ENDPOINT:
|
|
return self.OIDC_ISS_ENDPOINT
|
|
if isinstance(request, HttpRequest):
|
|
django_request = request
|
|
elif isinstance(request, Request):
|
|
django_request = _PhonyHttpRequest()
|
|
django_request.META = request.headers
|
|
if request.headers.get("X_DJANGO_OAUTH_TOOLKIT_SECURE", False):
|
|
django_request._scheme = "https"
|
|
else:
|
|
raise TypeError("request must be a django or oauthlib request: got %r" % request)
|
|
abs_url = django_request.build_absolute_uri(reverse("gooyal_oauth2:oidc-connect-discovery-info"))
|
|
return abs_url[: -len("/.well-known/openid-configuration")]
|
|
|
|
GOOYAL_DEFAULTS = {
|
|
# Resource Server with Token Introspection additions
|
|
"RESOURCE_SERVER_CLIENT_ID": None,
|
|
"RESOURCE_SERVER_CLIENT_SECRET": None,
|
|
|
|
# set default
|
|
"INTROSPECTION_USER_CREATE_METHOD": None,
|
|
}
|
|
|
|
GOOYAL_IMPORT_STRINGS = ("INTROSPECTION_USER_CREATE_METHOD",)
|
|
|
|
GOOYAL_MANDATORY = ("INTROSPECTION_USER_CREATE_METHOD",)
|
|
|
|
DEFAULTS.update(GOOYAL_DEFAULTS)
|
|
IMPORT_STRINGS = IMPORT_STRINGS + GOOYAL_IMPORT_STRINGS
|
|
MANDATORY = MANDATORY + GOOYAL_MANDATORY
|
|
|
|
oauth2_settings = GooyalOAuth2Settings(USER_SETTINGS, DEFAULTS, IMPORT_STRINGS, MANDATORY)
|