3434from google .auth import _helpers
3535from google .auth import exceptions
3636from google .auth import jwt
37+ from google .auth import metrics
3738from google .auth import transport
3839
3940_URLENCODED_CONTENT_TYPE = "application/x-www-form-urlencoded"
@@ -146,6 +147,7 @@ def _token_endpoint_request_no_throw(
146147 access_token = None ,
147148 use_json = False ,
148149 can_retry = True ,
150+ headers = None ,
149151 ** kwargs
150152):
151153 """Makes a request to the OAuth 2.0 authorization server's token endpoint.
@@ -161,6 +163,7 @@ def _token_endpoint_request_no_throw(
161163 use_json (Optional(bool)): Use urlencoded format or json format for the
162164 content type. The default value is False.
163165 can_retry (bool): Enable or disable request retry behavior.
166+ headers (Optional[Mapping[str, str]]): The headers for the request.
164167 kwargs: Additional arguments passed on to the request method. The
165168 kwargs will be passed to `requests.request` method, see:
166169 https://docs.python-requests.org/en/latest/api/#requests.request.
@@ -176,18 +179,21 @@ def _token_endpoint_request_no_throw(
176179 is retryable.
177180 """
178181 if use_json :
179- headers = {"Content-Type" : _JSON_CONTENT_TYPE }
182+ headers_to_use = {"Content-Type" : _JSON_CONTENT_TYPE }
180183 body = json .dumps (body ).encode ("utf-8" )
181184 else :
182- headers = {"Content-Type" : _URLENCODED_CONTENT_TYPE }
185+ headers_to_use = {"Content-Type" : _URLENCODED_CONTENT_TYPE }
183186 body = urllib .parse .urlencode (body ).encode ("utf-8" )
184187
185188 if access_token :
186- headers ["Authorization" ] = "Bearer {}" .format (access_token )
189+ headers_to_use ["Authorization" ] = "Bearer {}" .format (access_token )
190+
191+ if headers :
192+ headers_to_use .update (headers )
187193
188194 def _perform_request ():
189195 response = request (
190- method = "POST" , url = token_uri , headers = headers , body = body , ** kwargs
196+ method = "POST" , url = token_uri , headers = headers_to_use , body = body , ** kwargs
191197 )
192198 response_body = (
193199 response .data .decode ("utf-8" )
@@ -231,6 +237,7 @@ def _token_endpoint_request(
231237 access_token = None ,
232238 use_json = False ,
233239 can_retry = True ,
240+ headers = None ,
234241 ** kwargs
235242):
236243 """Makes a request to the OAuth 2.0 authorization server's token endpoint.
@@ -245,6 +252,7 @@ def _token_endpoint_request(
245252 use_json (Optional(bool)): Use urlencoded format or json format for the
246253 content type. The default value is False.
247254 can_retry (bool): Enable or disable request retry behavior.
255+ headers (Optional[Mapping[str, str]]): The headers for the request.
248256 kwargs: Additional arguments passed on to the request method. The
249257 kwargs will be passed to `requests.request` method, see:
250258 https://docs.python-requests.org/en/latest/api/#requests.request.
@@ -268,6 +276,7 @@ def _token_endpoint_request(
268276 access_token = access_token ,
269277 use_json = use_json ,
270278 can_retry = can_retry ,
279+ headers = headers ,
271280 ** kwargs
272281 )
273282 if not response_status_ok :
@@ -301,7 +310,13 @@ def jwt_grant(request, token_uri, assertion, can_retry=True):
301310 body = {"assertion" : assertion , "grant_type" : _JWT_GRANT_TYPE }
302311
303312 response_data = _token_endpoint_request (
304- request , token_uri , body , can_retry = can_retry
313+ request ,
314+ token_uri ,
315+ body ,
316+ can_retry = can_retry ,
317+ headers = {
318+ metrics .API_CLIENT_HEADER : metrics .token_request_access_token_sa_assertion ()
319+ },
305320 )
306321
307322 try :
@@ -384,7 +399,13 @@ def id_token_jwt_grant(request, token_uri, assertion, can_retry=True):
384399 body = {"assertion" : assertion , "grant_type" : _JWT_GRANT_TYPE }
385400
386401 response_data = _token_endpoint_request (
387- request , token_uri , body , can_retry = can_retry
402+ request ,
403+ token_uri ,
404+ body ,
405+ can_retry = can_retry ,
406+ headers = {
407+ metrics .API_CLIENT_HEADER : metrics .token_request_id_token_sa_assertion ()
408+ },
388409 )
389410
390411 try :
0 commit comments