backend_application.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # -*- coding: utf-8 -*-
  2. """
  3. oauthlib.oauth2.rfc6749
  4. ~~~~~~~~~~~~~~~~~~~~~~~
  5. This module is an implementation of various logic needed
  6. for consuming and providing OAuth 2.0 RFC6749.
  7. """
  8. from ..parameters import prepare_token_request
  9. from .base import Client
  10. class BackendApplicationClient(Client):
  11. """A public client utilizing the client credentials grant workflow.
  12. The client can request an access token using only its client
  13. credentials (or other supported means of authentication) when the
  14. client is requesting access to the protected resources under its
  15. control, or those of another resource owner which has been previously
  16. arranged with the authorization server (the method of which is beyond
  17. the scope of this specification).
  18. The client credentials grant type MUST only be used by confidential
  19. clients.
  20. Since the client authentication is used as the authorization grant,
  21. no additional authorization request is needed.
  22. """
  23. grant_type = 'client_credentials'
  24. def prepare_request_body(self, body='', scope=None,
  25. include_client_id=False, **kwargs):
  26. """Add the client credentials to the request body.
  27. The client makes a request to the token endpoint by adding the
  28. following parameters using the "application/x-www-form-urlencoded"
  29. format per `Appendix B`_ in the HTTP request entity-body:
  30. :param body: Existing request body (URL encoded string) to embed parameters
  31. into. This may contain extra parameters. Default ''.
  32. :param scope: The scope of the access request as described by
  33. `Section 3.3`_.
  34. :param include_client_id: `True` to send the `client_id` in the
  35. body of the upstream request. This is required
  36. if the client is not authenticating with the
  37. authorization server as described in
  38. `Section 3.2.1`_. False otherwise (default).
  39. :type include_client_id: Boolean
  40. :param kwargs: Extra credentials to include in the token request.
  41. The client MUST authenticate with the authorization server as
  42. described in `Section 3.2.1`_.
  43. The prepared body will include all provided credentials as well as
  44. the ``grant_type`` parameter set to ``client_credentials``::
  45. >>> from oauthlib.oauth2 import BackendApplicationClient
  46. >>> client = BackendApplicationClient('your_id')
  47. >>> client.prepare_request_body(scope=['hello', 'world'])
  48. 'grant_type=client_credentials&scope=hello+world'
  49. .. _`Appendix B`: https://tools.ietf.org/html/rfc6749#appendix-B
  50. .. _`Section 3.3`: https://tools.ietf.org/html/rfc6749#section-3.3
  51. .. _`Section 3.2.1`: https://tools.ietf.org/html/rfc6749#section-3.2.1
  52. """
  53. kwargs['client_id'] = self.client_id
  54. kwargs['include_client_id'] = include_client_id
  55. scope = self.scope if scope is None else scope
  56. return prepare_token_request(self.grant_type, body=body,
  57. scope=scope, **kwargs)