@@ -160,6 +160,8 @@ def claim_user_email(request):
160160@pytest .mark .django_db
161161def test_userinfo_endpoint_custom_claims_callable (oidc_tokens , client , oauth2_settings ):
162162 class CustomValidator (OAuth2Validator ):
163+ oidc_claim_scope = None
164+
163165 def get_additional_claims (self ):
164166 return {
165167 "username" : claim_user_email ,
@@ -183,9 +185,38 @@ def get_additional_claims(self):
183185 assert data ["email" ] == EXAMPLE_EMAIL
184186
185187
188+ @pytest .mark .django_db
189+ def test_userinfo_endpoint_custom_claims_email_scope_callable (
190+ oidc_email_scope_tokens , client , oauth2_settings
191+ ):
192+ class CustomValidator (OAuth2Validator ):
193+ def get_additional_claims (self ):
194+ return {
195+ "username" : claim_user_email ,
196+ "email" : claim_user_email ,
197+ }
198+
199+ oidc_email_scope_tokens .oauth2_settings .OAUTH2_VALIDATOR_CLASS = CustomValidator
200+ auth_header = "Bearer %s" % oidc_email_scope_tokens .access_token
201+ rsp = client .get (
202+ reverse ("oauth2_provider:user-info" ),
203+ HTTP_AUTHORIZATION = auth_header ,
204+ )
205+ data = rsp .json ()
206+ assert "sub" in data
207+ assert data ["sub" ] == str (oidc_email_scope_tokens .user .pk )
208+
209+ assert "username" not in data
210+
211+ assert "email" in data
212+ assert data ["email" ] == EXAMPLE_EMAIL
213+
214+
186215@pytest .mark .django_db
187216def test_userinfo_endpoint_custom_claims_plain (oidc_tokens , client , oauth2_settings ):
188217 class CustomValidator (OAuth2Validator ):
218+ oidc_claim_scope = None
219+
189220 def get_additional_claims (self , request ):
190221 return {
191222 "username" : EXAMPLE_EMAIL ,
@@ -207,3 +238,28 @@ def get_additional_claims(self, request):
207238
208239 assert "email" in data
209240 assert data ["email" ] == EXAMPLE_EMAIL
241+
242+
243+ @pytest .mark .django_db
244+ def test_userinfo_endpoint_custom_claims_email_scopeplain (oidc_email_scope_tokens , client , oauth2_settings ):
245+ class CustomValidator (OAuth2Validator ):
246+ def get_additional_claims (self , request ):
247+ return {
248+ "username" : EXAMPLE_EMAIL ,
249+ "email" : EXAMPLE_EMAIL ,
250+ }
251+
252+ oidc_email_scope_tokens .oauth2_settings .OAUTH2_VALIDATOR_CLASS = CustomValidator
253+ auth_header = "Bearer %s" % oidc_email_scope_tokens .access_token
254+ rsp = client .get (
255+ reverse ("oauth2_provider:user-info" ),
256+ HTTP_AUTHORIZATION = auth_header ,
257+ )
258+ data = rsp .json ()
259+ assert "sub" in data
260+ assert data ["sub" ] == str (oidc_email_scope_tokens .user .pk )
261+
262+ assert "username" not in data
263+
264+ assert "email" in data
265+ assert data ["email" ] == EXAMPLE_EMAIL
0 commit comments