backend_application.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 __future__ import absolute_import, unicode_literals
  9. from .base import Client
  10. from ..parameters import prepare_token_request
  11. from ..parameters import parse_token_response
  12. class BackendApplicationClient(Client):
  13. """A public client utilizing the client credentials grant workflow.
  14. The client can request an access token using only its client
  15. credentials (or other supported means of authentication) when the
  16. client is requesting access to the protected resources under its
  17. control, or those of another resource owner which has been previously
  18. arranged with the authorization server (the method of which is beyond
  19. the scope of this specification).
  20. The client credentials grant type MUST only be used by confidential
  21. clients.
  22. Since the client authentication is used as the authorization grant,
  23. no additional authorization request is needed.
  24. """
  25. def prepare_request_body(self, body='', scope=None, **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 scope: The scope of the access request as described by
  31. `Section 3.3`_.
  32. :param kwargs: Extra credentials to include in the token request.
  33. The client MUST authenticate with the authorization server as
  34. described in `Section 3.2.1`_.
  35. The prepared body will include all provided credentials as well as
  36. the ``grant_type`` parameter set to ``client_credentials``::
  37. >>> from oauthlib.oauth2 import BackendApplicationClient
  38. >>> client = BackendApplicationClient('your_id')
  39. >>> client.prepare_request_body(scope=['hello', 'world'])
  40. 'grant_type=client_credentials&scope=hello+world'
  41. .. _`Appendix B`: http://tools.ietf.org/html/rfc6749#appendix-B
  42. .. _`Section 3.3`: http://tools.ietf.org/html/rfc6749#section-3.3
  43. .. _`Section 3.2.1`: http://tools.ietf.org/html/rfc6749#section-3.2.1
  44. """
  45. return prepare_token_request('client_credentials', body=body,
  46. scope=scope, **kwargs)