legacy_application.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 LegacyApplicationClient(Client):
  11. """A public client using the resource owner password and username directly.
  12. The resource owner password credentials grant type is suitable in
  13. cases where the resource owner has a trust relationship with the
  14. client, such as the device operating system or a highly privileged
  15. application. The authorization server should take special care when
  16. enabling this grant type, and only allow it when other flows are not
  17. viable.
  18. The grant type is suitable for clients capable of obtaining the
  19. resource owner's credentials (username and password, typically using
  20. an interactive form). It is also used to migrate existing clients
  21. using direct authentication schemes such as HTTP Basic or Digest
  22. authentication to OAuth by converting the stored credentials to an
  23. access token.
  24. The method through which the client obtains the resource owner
  25. credentials is beyond the scope of this specification. The client
  26. MUST discard the credentials once an access token has been obtained.
  27. """
  28. grant_type = 'password'
  29. def __init__(self, client_id, **kwargs):
  30. super().__init__(client_id, **kwargs)
  31. def prepare_request_body(self, username, password, body='', scope=None,
  32. include_client_id=False, **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 body: Existing request body (URL encoded string) to embed parameters
  40. into. This may contain extra parameters. Default ''.
  41. :param scope: The scope of the access request as described by
  42. `Section 3.3`_.
  43. :param include_client_id: `True` to send the `client_id` in the
  44. body of the upstream request. This is required
  45. if the client is not authenticating with the
  46. authorization server as described in
  47. `Section 3.2.1`_. False otherwise (default).
  48. :type include_client_id: Boolean
  49. :param kwargs: Extra credentials to include in the token request.
  50. If the client type is confidential or the client was issued client
  51. credentials (or assigned other authentication requirements), the
  52. client MUST authenticate with the authorization server as described
  53. in `Section 3.2.1`_.
  54. The prepared body will include all provided credentials as well as
  55. the ``grant_type`` parameter set to ``password``::
  56. >>> from oauthlib.oauth2 import LegacyApplicationClient
  57. >>> client = LegacyApplicationClient('your_id')
  58. >>> client.prepare_request_body(username='foo', password='bar', scope=['hello', 'world'])
  59. 'grant_type=password&username=foo&scope=hello+world&password=bar'
  60. .. _`Appendix B`: https://tools.ietf.org/html/rfc6749#appendix-B
  61. .. _`Section 3.3`: https://tools.ietf.org/html/rfc6749#section-3.3
  62. .. _`Section 3.2.1`: https://tools.ietf.org/html/rfc6749#section-3.2.1
  63. """
  64. kwargs['client_id'] = self.client_id
  65. kwargs['include_client_id'] = include_client_id
  66. scope = self.scope if scope is None else scope
  67. return prepare_token_request(self.grant_type, body=body, username=username,
  68. password=password, scope=scope, **kwargs)