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 BackendApplicationClient(Client):
14
15 """A public client utilizing the client credentials grant workflow.
16
17 The client can request an access token using only its client
18 credentials (or other supported means of authentication) when the
19 client is requesting access to the protected resources under its
20 control, or those of another resource owner which has been previously
21 arranged with the authorization server (the method of which is beyond
22 the scope of this specification).
23
24 The client credentials grant type MUST only be used by confidential
25 clients.
26
27 Since the client authentication is used as the authorization grant,
28 no additional authorization request is needed.
29 """
30
31 grant_type = 'client_credentials'
32
33 def prepare_request_body(self, body='', scope=None,
34 include_client_id=False, **kwargs):
35 """Add the client credentials to the request body.
36
37 The client makes a request to the token endpoint by adding the
38 following parameters using the "application/x-www-form-urlencoded"
39 format per `Appendix B`_ in the HTTP request entity-body:
40
41 :param body: Existing request body (URL encoded string) to embed parameters
42 into. This may contain extra parameters. Default ''.
43 :param scope: The scope of the access request as described by
44 `Section 3.3`_.
45
46 :param include_client_id: `True` to send the `client_id` in the
47 body of the upstream request. This is required
48 if the client is not authenticating with the
49 authorization server as described in
50 `Section 3.2.1`_. False otherwise (default).
51 :type include_client_id: Boolean
52
53 :param kwargs: Extra credentials to include in the token request.
54
55 The client MUST authenticate with the authorization server as
56 described in `Section 3.2.1`_.
57
58 The prepared body will include all provided credentials as well as
59 the ``grant_type`` parameter set to ``client_credentials``::
60
61 >>> from oauthlib.oauth2 import BackendApplicationClient
62 >>> client = BackendApplicationClient('your_id')
63 >>> client.prepare_request_body(scope=['hello', 'world'])
64 'grant_type=client_credentials&scope=hello+world'
65
66 .. _`Appendix B`: https://tools.ietf.org/html/rfc6749#appendix-B
67 .. _`Section 3.3`: https://tools.ietf.org/html/rfc6749#section-3.3
68 .. _`Section 3.2.1`: https://tools.ietf.org/html/rfc6749#section-3.2.1
69 """
70 kwargs['client_id'] = self.client_id
71 kwargs['include_client_id'] = include_client_id
72 scope = self.scope if scope is None else scope
73 return prepare_token_request(self.grant_type, body=body,
74 scope=scope, **kwargs)