Skip to content

Commit 36dc8b2

Browse files
committed
* #1066: Revert #967 which incorrectly breaks API.
1 parent 6911c81 commit 36dc8b2

File tree

6 files changed

+23
-55
lines changed

6 files changed

+23
-55
lines changed

AUTHORS

+1
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,6 @@ pySilver
6363
Łukasz Skarżyński
6464
Shaheed Haque
6565
Andrea Greco
66+
Peter Karman
6667
Vinay Karanam
6768

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2121
### Fixed
2222

2323
* #1056 Add missing migration triggered by [Django 4.0 changes to the migrations autodetector](https://docs.djangoproject.com/en/4.0/releases/4.0/#migrations-autodetector-changes).
24+
* #1068 Revert #967 which incorrectly changed an API. See #1066.
2425

2526
## [1.6.1] 2021-12-23
2627

docs/oidc.rst

+9-10
Original file line numberDiff line numberDiff line change
@@ -245,17 +245,16 @@ required claims, eg ``iss``, ``aud``, ``exp``, ``iat``, ``auth_time`` etc),
245245
and the ``sub`` claim will use the primary key of the user as the value.
246246
You'll probably want to customize this and add additional claims or change
247247
what is sent for the ``sub`` claim. To do so, you will need to add a method to
248-
our custom validator.
249-
Standard claim ``sub`` is included by default, for remove it override ``get_claim_list``::
248+
our custom validator::
249+
250250
class CustomOAuth2Validator(OAuth2Validator):
251-
def get_additional_claims(self):
252-
def get_user_email(request):
253-
return request.user.get_full_name()
254-
255-
# Element name, callback to obtain data
256-
claims_list = [ ("email", get_sub_cod),
257-
("username", get_user_email) ]
258-
return claims_list
251+
252+
def get_additional_claims(self, request):
253+
return {
254+
"sub": request.user.email,
255+
"first_name": request.user.first_name,
256+
"last_name": request.user.last_name,
257+
}
259258

260259
.. note::
261260
This ``request`` object is not a ``django.http.Request`` object, but an

oauth2_provider/oauth2_validators.py

+8-17
Original file line numberDiff line numberDiff line change
@@ -728,24 +728,15 @@ def _save_id_token(self, jti, request, expires, *args, **kwargs):
728728
def get_jwt_bearer_token(self, token, token_handler, request):
729729
return self.get_id_token(token, token_handler, request)
730730

731-
def get_claim_list(self):
732-
def get_sub_code(request):
733-
return str(request.user.id)
734-
735-
list = [("sub", get_sub_code)]
731+
def get_oidc_claims(self, token, token_handler, request):
732+
# Required OIDC claims
733+
claims = {
734+
"sub": str(request.user.id),
735+
}
736736

737737
# https://openid.net/specs/openid-connect-core-1_0.html#StandardClaims
738-
add = self.get_additional_claims()
739-
list.extend(add)
740-
741-
return list
738+
claims.update(**self.get_additional_claims(request))
742739

743-
def get_oidc_claims(self, token, token_handler, request):
744-
data = self.get_claim_list()
745-
claims = {}
746-
747-
for k, call in data:
748-
claims[k] = call(request)
749740
return claims
750741

751742
def get_id_token_dictionary(self, token, token_handler, request):
@@ -898,5 +889,5 @@ def get_userinfo_claims(self, request):
898889
"""
899890
return self.get_oidc_claims(None, None, request)
900891

901-
def get_additional_claims(self):
902-
return []
892+
def get_additional_claims(self, request):
893+
return {}

oauth2_provider/views/oidc.py

-8
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,6 @@ def get(self, request, *args, **kwargs):
4545
signing_algorithms = [Application.HS256_ALGORITHM]
4646
if oauth2_settings.OIDC_RSA_PRIVATE_KEY:
4747
signing_algorithms = [Application.RS256_ALGORITHM, Application.HS256_ALGORITHM]
48-
49-
validator_class = oauth2_settings.OAUTH2_VALIDATOR_CLASS
50-
validator = validator_class()
51-
oidc_claims = []
52-
for el, _ in validator.get_claim_list():
53-
oidc_claims.append(el)
54-
5548
data = {
5649
"issuer": issuer_url,
5750
"authorization_endpoint": authorization_endpoint,
@@ -64,7 +57,6 @@ def get(self, request, *args, **kwargs):
6457
"token_endpoint_auth_methods_supported": (
6558
oauth2_settings.OIDC_TOKEN_ENDPOINT_AUTH_METHODS_SUPPORTED
6659
),
67-
"claims_supported": oidc_claims,
6860
}
6961
response = JsonResponse(data)
7062
response["Access-Control-Allow-Origin"] = "*"

tests/test_oidc_views.py

+4-20
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ def test_get_connect_discovery_info(self):
2929
"subject_types_supported": ["public"],
3030
"id_token_signing_alg_values_supported": ["RS256", "HS256"],
3131
"token_endpoint_auth_methods_supported": ["client_secret_post", "client_secret_basic"],
32-
"claims_supported": ["sub"],
3332
}
3433
response = self.client.get(reverse("oauth2_provider:oidc-connect-discovery-info"))
3534
self.assertEqual(response.status_code, 200)
@@ -56,7 +55,6 @@ def test_get_connect_discovery_info_without_issuer_url(self):
5655
"subject_types_supported": ["public"],
5756
"id_token_signing_alg_values_supported": ["RS256", "HS256"],
5857
"token_endpoint_auth_methods_supported": ["client_secret_post", "client_secret_basic"],
59-
"claims_supported": ["sub"],
6058
}
6159
response = self.client.get(reverse("oauth2_provider:oidc-connect-discovery-info"))
6260
self.assertEqual(response.status_code, 200)
@@ -148,21 +146,11 @@ def test_userinfo_endpoint_bad_token(oidc_tokens, client):
148146
assert rsp.status_code == 401
149147

150148

151-
EXAMPLE_EMAIL = "[email protected]"
152-
153-
154-
def claim_user_email(request):
155-
return EXAMPLE_EMAIL
156-
157-
158149
@pytest.mark.django_db
159150
def test_userinfo_endpoint_custom_claims(oidc_tokens, client, oauth2_settings):
160151
class CustomValidator(OAuth2Validator):
161-
def get_additional_claims(self):
162-
return [
163-
("username", claim_user_email),
164-
("email", claim_user_email),
165-
]
152+
def get_additional_claims(self, request):
153+
return {"state": "very nice"}
166154

167155
oidc_tokens.oauth2_settings.OAUTH2_VALIDATOR_CLASS = CustomValidator
168156
auth_header = "Bearer %s" % oidc_tokens.access_token
@@ -173,9 +161,5 @@ def get_additional_claims(self):
173161
data = rsp.json()
174162
assert "sub" in data
175163
assert data["sub"] == str(oidc_tokens.user.pk)
176-
177-
assert "username" in data
178-
assert data["username"] == EXAMPLE_EMAIL
179-
180-
assert "email" in data
181-
assert data["email"] == EXAMPLE_EMAIL
164+
assert "state" in data
165+
assert data["state"] == "very nice"

0 commit comments

Comments
 (0)