1# -*- coding: utf-8 -*-
2"""
3oauthlib.oauth2.rfc6749
4~~~~~~~~~~~~~~~~~~~~~~~
5
6This module is an implementation of various logic needed
7for consuming and providing OAuth 2.0 RFC6749.
8"""
9from ..parameters import prepare_token_request
10from .base import Client
11
12
13class LegacyApplicationClient(Client):
14
15 """A public client using the resource owner password and username directly.
16
17 The resource owner password credentials grant type is suitable in
18 cases where the resource owner has a trust relationship with the
19 client, such as the device operating system or a highly privileged
20 application. The authorization server should take special care when
21 enabling this grant type, and only allow it when other flows are not
22 viable.
23
24 The grant type is suitable for clients capable of obtaining the
25 resource owner's credentials (username and password, typically using
26 an interactive form). It is also used to migrate existing clients
27 using direct authentication schemes such as HTTP Basic or Digest
28 authentication to OAuth by converting the stored credentials to an
29 access token.
30
31 The method through which the client obtains the resource owner
32 credentials is beyond the scope of this specification. The client
33 MUST discard the credentials once an access token has been obtained.
34 """
35
36 grant_type = 'password'
37
38 def __init__(self, client_id, **kwargs):
39 super().__init__(client_id, **kwargs)
40
41 def prepare_request_body(self, username, password, body='', scope=None,
42 include_client_id=False, **kwargs):
43 """Add the resource owner password and username to the request body.
44
45 The client makes a request to the token endpoint by adding the
46 following parameters using the "application/x-www-form-urlencoded"
47 format per `Appendix B`_ in the HTTP request entity-body:
48
49 :param username: The resource owner username.
50 :param password: The resource owner password.
51 :param body: Existing request body (URL encoded string) to embed parameters
52 into. This may contain extra parameters. Default ''.
53 :param scope: The scope of the access request as described by
54 `Section 3.3`_.
55 :param include_client_id: `True` to send the `client_id` in the
56 body of the upstream request. This is required
57 if the client is not authenticating with the
58 authorization server as described in
59 `Section 3.2.1`_. False otherwise (default).
60 :type include_client_id: Boolean
61 :param kwargs: Extra credentials to include in the token request.
62
63 If the client type is confidential or the client was issued client
64 credentials (or assigned other authentication requirements), the
65 client MUST authenticate with the authorization server as described
66 in `Section 3.2.1`_.
67
68 The prepared body will include all provided credentials as well as
69 the ``grant_type`` parameter set to ``password``::
70
71 >>> from oauthlib.oauth2 import LegacyApplicationClient
72 >>> client = LegacyApplicationClient('your_id')
73 >>> client.prepare_request_body(username='foo', password='bar', scope=['hello', 'world'])
74 'grant_type=password&username=foo&scope=hello+world&password=bar'
75
76 .. _`Appendix B`: https://tools.ietf.org/html/rfc6749#appendix-B
77 .. _`Section 3.3`: https://tools.ietf.org/html/rfc6749#section-3.3
78 .. _`Section 3.2.1`: https://tools.ietf.org/html/rfc6749#section-3.2.1
79 """
80 kwargs['client_id'] = self.client_id
81 kwargs['include_client_id'] = include_client_id
82 scope = self.scope if scope is None else scope
83 return prepare_token_request(self.grant_type, body=body, username=username,
84 password=password, scope=scope, **kwargs)