@@ -245,23 +245,46 @@ required claims, eg ``iss``, ``aud``, ``exp``, ``iat``, ``auth_time`` etc),
245
245
and the ``sub `` claim will use the primary key of the user as the value.
246
246
You'll probably want to customize this and add additional claims or change
247
247
what is sent for the ``sub `` claim. To do so, you will need to add a method to
248
- our custom validator. It should return a dictionary mapping a claim name to
249
- either the claim data, or a callable that will be called with the request to
250
- produce the claim data.
251
- Standard claim `` sub `` is included by default, to remove it override `` get_claim_list `` ::
248
+ our custom validator. It takes one of two forms:
249
+
250
+ The first form gets passed a request object, and should return a dictionary
251
+ mapping a claim name to claim data ::
252
252
class CustomOAuth2Validator(OAuth2Validator):
253
253
def get_additional_claims(self, request):
254
+ claims = {}
255
+ claims["email"] = request.user.get_user_email()
256
+ claims["username"] = request.user.get_full_name()
257
+
258
+ return claims
259
+
260
+ The second form gets no request object, and should return a dictionary
261
+ mapping a claim name to a callable, accepting a request and producing
262
+ the claim data::
263
+ class CustomOAuth2Validator(OAuth2Validator):
264
+ def get_additional_claims(self):
254
265
def get_user_email(request):
255
266
return request.user.get_user_email()
256
267
257
268
claims = {}
258
- # Element name, callback to obtain data
259
269
claims["email"] = get_user_email
260
- # Element name, plain data returned
261
- claims["username"] = request.user.get_full_name()
270
+ claims["username"] = lambda r: r.user.get_full_name()
262
271
263
272
return claims
264
273
274
+ Standard claim ``sub `` is included by default, to remove it override ``get_claim_dict ``.
275
+
276
+ In some cases, it might be desirable to not list all claims in discovery info. To customize
277
+ which claims are advertised, you can override the ``get_discovery_claims `` method to return
278
+ a list of claim names to advertise. If your ``get_additional_claims `` uses the first form
279
+ and you still want to advertise claims, you can also override ``get_discovery_claims ``.
280
+
281
+ In order to help lcients discover claims early, they can be advertised in the discovery
282
+ info, under the ``claims_supported `` key. In order for the discovery info view to automatically
283
+ add all claims your validator returns, you need to use the second form (producing callables),
284
+ because the discovery info views are requested with an unauthenticated request, so directly
285
+ producing claim data would fail. If you use the first form, producing claim data directly,
286
+ your claims will not be added to discovery info.
287
+
265
288
.. note ::
266
289
This ``request `` object is not a ``django.http.Request `` object, but an
267
290
``oauthlib.common.Request `` object. This has a number of attributes that
0 commit comments