8
8
"""
9
9
from __future__ import absolute_import , unicode_literals
10
10
11
+ import warnings
12
+
11
13
from ..parameters import (parse_authorization_code_response ,
12
14
parse_token_response , prepare_grant_uri ,
13
15
prepare_token_request )
@@ -85,24 +87,30 @@ def prepare_request_uri(self, uri, redirect_uri=None, scope=None,
85
87
return prepare_grant_uri (uri , self .client_id , 'code' ,
86
88
redirect_uri = redirect_uri , scope = scope , state = state , ** kwargs )
87
89
88
- def prepare_request_body (self , client_id = None , code = None , body = '' ,
89
- redirect_uri = None , ** kwargs ):
90
+ def prepare_request_body (self , code = None , redirect_uri = None , body = '' ,
91
+ include_client_id = True , ** kwargs ):
90
92
"""Prepare the access token request body.
91
93
92
94
The client makes a request to the token endpoint by adding the
93
95
following parameters using the "application/x-www-form-urlencoded"
94
96
format in the HTTP request entity-body:
95
97
96
- :param client_id: REQUIRED, if the client is not authenticating with the
97
- authorization server as described in `Section 3.2.1`_.
98
-
99
98
:param code: REQUIRED. The authorization code received from the
100
99
authorization server.
101
100
102
101
:param redirect_uri: REQUIRED, if the "redirect_uri" parameter was included in the
103
102
authorization request as described in `Section 4.1.1`_, and their
104
103
values MUST be identical.
105
104
105
+ :param body: Existing request body (URL encoded string) to embed parameters
106
+ into. This may contain extra paramters. Default ''.
107
+
108
+ :param include_client_id: `True` (default) to send the `client_id` in the
109
+ body of the upstream request. This is required
110
+ if the client is not authenticating with the
111
+ authorization server as described in `Section 3.2.1`_.
112
+ :type include_client_id: Boolean
113
+
106
114
:param kwargs: Extra parameters to include in the token request.
107
115
108
116
In addition OAuthLib will add the ``grant_type`` parameter set to
@@ -120,12 +128,31 @@ def prepare_request_body(self, client_id=None, code=None, body='',
120
128
>>> client.prepare_request_body(code='sh35ksdf09sf', foo='bar')
121
129
'grant_type=authorization_code&code=sh35ksdf09sf&foo=bar'
122
130
131
+ `Section 3.2.1` also states:
132
+ In the "authorization_code" "grant_type" request to the token
133
+ endpoint, an unauthenticated client MUST send its "client_id" to
134
+ prevent itself from inadvertently accepting a code intended for a
135
+ client with a different "client_id". This protects the client from
136
+ substitution of the authentication code. (It provides no additional
137
+ security for the protected resource.)
138
+
123
139
.. _`Section 4.1.1`: https://tools.ietf.org/html/rfc6749#section-4.1.1
124
140
.. _`Section 3.2.1`: https://tools.ietf.org/html/rfc6749#section-3.2.1
125
141
"""
126
142
code = code or self .code
143
+ if 'client_id' in kwargs :
144
+ warnings .warn ("`client_id` has been deprecated in favor of "
145
+ "`include_client_id`, a boolean value which will "
146
+ "include the already configured `self.client_id`." ,
147
+ DeprecationWarning )
148
+ if kwargs ['client_id' ] != self .client_id :
149
+ raise ValueError ("`client_id` was supplied as an argument, but "
150
+ "it does not match `self.client_id`" )
151
+
152
+ kwargs ['client_id' ] = self .client_id
153
+ kwargs ['include_client_id' ] = include_client_id
127
154
return prepare_token_request ('authorization_code' , code = code , body = body ,
128
- client_id = client_id , redirect_uri = redirect_uri , ** kwargs )
155
+ redirect_uri = redirect_uri , ** kwargs )
129
156
130
157
def parse_request_uri_response (self , uri , state = None ):
131
158
"""Parse the URI query for code and state.
0 commit comments