From f68ec220d9ff5ec8e710d3e916318dad0de3e2d2 Mon Sep 17 00:00:00 2001 From: Jonathan Huot Date: Fri, 25 Oct 2019 11:28:51 +0200 Subject: Add custom grant example --- docs/oauth2/grants/custom_grant.rst | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) (limited to 'docs') diff --git a/docs/oauth2/grants/custom_grant.rst b/docs/oauth2/grants/custom_grant.rst index ba5446c..7408cf6 100644 --- a/docs/oauth2/grants/custom_grant.rst +++ b/docs/oauth2/grants/custom_grant.rst @@ -1,5 +1,6 @@ +================= Custom Grant type ------------------ +================= Writing a custom grant type can be useful to implement a specification which is in an early draft, or implement a grant provided by a @@ -38,12 +39,25 @@ for examples. 3. Example ---------- -To be completed. +Sample below shows the creation of a new custom `grant_type` parameter +and declare it in the `/token` endpoint of your `Server`. Note that +you can reuse `pre_configured.Server` or use your own class inheriting +of the `Endpoint` classes you have decided. .. code-block:: python - class XXXZZZGrant(GrantTypeBase): + + class MyCustomGrant(GrantTypeBase): def create_token_response(self, request, token_handler): - if not request.grant_type == 'xxx_zzz': + if not request.grant_type == 'urn:ietf:params:oauth:grant-type:my-custom-grant': raise errors.UnsupportedGrantTypeError(request=request) - ... - + # implement your custom validation checks + # .. + + token = token_handler.create_token(request, + refresh_token=self.issue_new_refresh_tokens) + return self._get_default_headers(), json.dumps(token), 200 + + def setup_oauthlib(): + my_custom_grant = MyCustomGrant() + server = Server(request_validator) + server.grant_types["urn:ietf:params:oauth:grant-type:my-custom-grant"] = my_custom_grant -- cgit v1.2.1