from urllib.parse import urlparse from django.views.generic import View from django.http import JsonResponse from django.urls import reverse_lazy from oauth2_provider.settings import oauth2_settings from oauth2_provider.models import ( AbstractGrant, Application, ) class DiscoveryEndpoint(View): # Ref: https://openid.net/specs/openid-connect-discovery-1_0.html # The response is a set of Claims about the OpenID Provider's configuration, # including all necessary endpoints and public key location information. def get(self, request, *args, **kwargs): issuer_url = oauth2_settings.OIDC_ISS_ENDPOINT parsed_url = urlparse(oauth2_settings.OIDC_ISS_ENDPOINT) host = parsed_url.scheme + "://" + parsed_url.netloc scopes_class = oauth2_settings.SCOPES_BACKEND_CLASS scopes = scopes_class() scopes_supported = [scope for scope in scopes.get_available_scopes()] validator_class = oauth2_settings.OAUTH2_VALIDATOR_CLASS validator = validator_class() oidc_claims = list(set(validator.get_discovery_claims(request))) signing_algorithms = [Application.RS256_ALGORITHM, Application.HS256_ALGORITHM] return JsonResponse({ 'issuer': issuer_url, 'authorization_endpoint': '{}{}'.format(host, reverse_lazy('oidc-auth')), 'token_endpoint': '{}{}'.format(host, reverse_lazy('oidc-token')), 'userinfo_endpoint': '{}{}'.format(host, reverse_lazy('oidc-userinfo')), 'jwks_uri': '{}{}'.format(host, reverse_lazy('oidc-jwks')), 'response_types_supported': oauth2_settings.OIDC_RESPONSE_TYPES_SUPPORTED, 'subject_types_supported': oauth2_settings.OIDC_SUBJECT_TYPES_SUPPORTED, 'id_token_signing_alg_values_supported': signing_algorithms, 'token_endpoint_auth_methods_supported': oauth2_settings.OIDC_TOKEN_ENDPOINT_AUTH_METHODS_SUPPORTED, 'scopes_supported': scopes_supported, 'claims_supported': oidc_claims, 'code_challenge_methods_supported': [key for key, _ in AbstractGrant.CODE_CHALLENGE_METHODS] })