|
| 1 | +from __future__ import absolute_import, unicode_literals |
| 2 | + |
| 3 | +import json |
| 4 | + |
| 5 | +from django.http import JsonResponse |
| 6 | +from django.urls import reverse_lazy |
| 7 | +from django.views.generic import View |
| 8 | +from jwcrypto import jwk |
| 9 | + |
| 10 | +from ..settings import oauth2_settings |
| 11 | + |
| 12 | + |
| 13 | +class ConnectDiscoveryInfoView(View): |
| 14 | + """ |
| 15 | + View used to show oidc provider configuration information |
| 16 | + """ |
| 17 | + def get(self, request, *args, **kwargs): |
| 18 | + issuer_url = oauth2_settings.OIDC_ISS_ENDPOINT |
| 19 | + data = { |
| 20 | + "issuer": issuer_url, |
| 21 | + "authorization_endpoint": "{}{}".format(issuer_url, reverse_lazy("oauth2_provider:authorize")), |
| 22 | + "token_endpoint": "{}{}".format(issuer_url, reverse_lazy("oauth2_provider:token")), |
| 23 | + "userinfo_endpoint": oauth2_settings.OIDC_USERINFO_ENDPOINT, |
| 24 | + "jwks_uri": "{}{}".format(issuer_url, reverse_lazy("oauth2_provider:jwks-info")), |
| 25 | + "response_types_supported": oauth2_settings.OIDC_RESPONSE_TYPES_SUPPORTED, |
| 26 | + "subject_types_supported": oauth2_settings.OIDC_SUBJECT_TYPES_SUPPORTED, |
| 27 | + "id_token_signing_alg_values_supported": oauth2_settings.OIDC_ID_TOKEN_SIGNING_ALG_VALUES_SUPPORTED, |
| 28 | + "token_endpoint_auth_methods_supported": oauth2_settings.OIDC_TOKEN_ENDPOINT_AUTH_METHODS_SUPPORTED, |
| 29 | + } |
| 30 | + response = JsonResponse(data) |
| 31 | + response["Access-Control-Allow-Origin"] = "*" |
| 32 | + return response |
| 33 | + |
| 34 | + |
| 35 | +class JwksInfoView(View): |
| 36 | + """ |
| 37 | + View used to show oidc json web key set document |
| 38 | + """ |
| 39 | + def get(self, request, *args, **kwargs): |
| 40 | + key = jwk.JWK.from_pem(oauth2_settings.OIDC_RSA_PRIVATE_KEY.encode("utf8")) |
| 41 | + data = { |
| 42 | + "keys": [{ |
| 43 | + "alg": "RS256", |
| 44 | + "use": "sig", |
| 45 | + "kid": key.thumbprint() |
| 46 | + }] |
| 47 | + } |
| 48 | + data["keys"][0].update(json.loads(key.export_public())) |
| 49 | + response = JsonResponse(data) |
| 50 | + response["Access-Control-Allow-Origin"] = "*" |
| 51 | + return response |
0 commit comments