legacy_application.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 LegacyApplicationClient(Client):
  13. """A public client using the resource owner password and username directly.
  14. The resource owner password credentials grant type is suitable in
  15. cases where the resource owner has a trust relationship with the
  16. client, such as the device operating system or a highly privileged
  17. application. The authorization server should take special care when
  18. enabling this grant type, and only allow it when other flows are not
  19. viable.
  20. The grant type is suitable for clients capable of obtaining the
  21. resource owner's credentials (username and password, typically using
  22. an interactive form). It is also used to migrate existing clients
  23. using direct authentication schemes such as HTTP Basic or Digest
  24. authentication to OAuth by converting the stored credentials to an
  25. access token.
  26. The method through which the client obtains the resource owner
  27. credentials is beyond the scope of this specification. The client
  28. MUST discard the credentials once an access token has been obtained.
  29. """
  30. def __init__(self, client_id, **kwargs):
  31. super(LegacyApplicationClient, self).__init__(client_id, **kwargs)
  32. def prepare_request_body(self, username, password, body='', scope=None, **kwargs):
  33. """Add the resource owner password and username to the request body.
  34. The client makes a request to the token endpoint by adding the
  35. following parameters using the "application/x-www-form-urlencoded"
  36. format per `Appendix B`_ in the HTTP request entity-body:
  37. :param username: The resource owner username.
  38. :param password: The resource owner password.
  39. :param scope: The scope of the access request as described by
  40. `Section 3.3`_.
  41. :param kwargs: Extra credentials to include in the token request.
  42. If the client type is confidential or the client was issued client
  43. credentials (or assigned other authentication requirements), the
  44. client MUST authenticate with the authorization server as described
  45. in `Section 3.2.1`_.
  46. The prepared body will include all provided credentials as well as
  47. the ``grant_type`` parameter set to ``password``::
  48. >>> from oauthlib.oauth2 import LegacyApplicationClient
  49. >>> client = LegacyApplicationClient('your_id')
  50. >>> client.prepare_request_body(username='foo', password='bar', scope=['hello', 'world'])
  51. 'grant_type=password&username=foo&scope=hello+world&password=bar'
  52. .. _`Appendix B`: http://tools.ietf.org/html/rfc6749#appendix-B
  53. .. _`Section 3.3`: http://tools.ietf.org/html/rfc6749#section-3.3
  54. .. _`Section 3.2.1`: http://tools.ietf.org/html/rfc6749#section-3.2.1
  55. """
  56. return prepare_token_request('password', body=body, username=username,
  57. password=password, scope=scope, **kwargs)