codekingpro/portable-devtools
114k
1# -*- coding: utf-8 -*-2"""3oauthlib.oauth2.rfc67494~~~~~~~~~~~~~~~~~~~~~~~5 6This module is an implementation of various logic needed7for consuming and providing OAuth 2.0 RFC6749.8"""9from ..parameters import prepare_token_request10from .base import Client11 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 in18 cases where the resource owner has a trust relationship with the19 client, such as the device operating system or a highly privileged20 application. The authorization server should take special care when21 enabling this grant type, and only allow it when other flows are not22 viable.23 24 The grant type is suitable for clients capable of obtaining the25 resource owner's credentials (username and password, typically using26 an interactive form). It is also used to migrate existing clients27 using direct authentication schemes such as HTTP Basic or Digest28 authentication to OAuth by converting the stored credentials to an29 access token.30 31 The method through which the client obtains the resource owner32 credentials is beyond the scope of this specification. The client33 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 the46 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 parameters52 into. This may contain extra parameters. Default ''.53 :param scope: The scope of the access request as described by54 `Section 3.3`_.55 :param include_client_id: `True` to send the `client_id` in the56 body of the upstream request. This is required57 if the client is not authenticating with the58 authorization server as described in59 `Section 3.2.1`_. False otherwise (default).60 :type include_client_id: Boolean61 :param kwargs: Extra credentials to include in the token request.62 63 If the client type is confidential or the client was issued client64 credentials (or assigned other authentication requirements), the65 client MUST authenticate with the authorization server as described66 in `Section 3.2.1`_.67 68 The prepared body will include all provided credentials as well as69 the ``grant_type`` parameter set to ``password``::70 71 >>> from oauthlib.oauth2 import LegacyApplicationClient72 >>> 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-B77 .. _`Section 3.3`: https://tools.ietf.org/html/rfc6749#section-3.378 .. _`Section 3.2.1`: https://tools.ietf.org/html/rfc6749#section-3.2.179 """80 kwargs['client_id'] = self.client_id81 kwargs['include_client_id'] = include_client_id82 scope = self.scope if scope is None else scope83 return prepare_token_request(self.grant_type, body=body, username=username,84 password=password, scope=scope, **kwargs)85 