4545import datetime
4646import json
4747
48- from six .moves import urllib
49-
5048from google .auth import _helpers
5149from google .auth import _service_account_info
5250from google .auth import credentials
@@ -246,11 +244,7 @@ class Credentials(credentials.Signing,
246244 """Credentials that use a JWT as the bearer token.
247245
248246 These credentials require an "audience" claim. This claim identifies the
249- intended recipient of the bearer token. You can set the audience when
250- you construct these credentials, however, these credentials can also set
251- the audience claim automatically if not specified. In this case, whenever
252- a request is made the credentials will automatically generate a one-time
253- JWT with the request URI as the audience.
247+ intended recipient of the bearer token.
254248
255249 The constructor arguments determine the claims for the JWT that is
256250 sent with requests. Usually, you'll construct these credentials with
@@ -260,13 +254,15 @@ class Credentials(credentials.Signing,
260254 JSON file::
261255
262256 credentials = jwt.Credentials.from_service_account_file(
263- 'service-account.json')
257+ 'service-account.json',
258+ audience='https://speech.googleapis.com')
264259
265260 If you already have the service account file loaded and parsed::
266261
267262 service_account_info = json.load(open('service_account.json'))
268263 credentials = jwt.Credentials.from_service_account_info(
269- service_account_info)
264+ service_account_info,
265+ audience='https://speech.googleapis.com')
270266
271267 Both helper methods pass on arguments to the constructor, so you can
272268 specify the JWT claims::
@@ -280,7 +276,10 @@ class Credentials(credentials.Signing,
280276 :class:`~google.auth.crypt.Signer` instance::
281277
282278 credentials = jwt.Credentials(
283- signer, issuer='your-issuer', subject='your-subject')
279+ signer,
280+ issuer='your-issuer',
281+ subject='your-subject',
282+ audience=''https://speech.googleapis.com'')
284283
285284 The claims are considered immutable. If you want to modify the claims,
286285 you can easily create another instance using :meth:`with_claims`::
@@ -289,7 +288,7 @@ class Credentials(credentials.Signing,
289288 audience='https://vision.googleapis.com')
290289 """
291290
292- def __init__ (self , signer , issuer = None , subject = None , audience = None ,
291+ def __init__ (self , signer , issuer , subject , audience ,
293292 additional_claims = None ,
294293 token_lifetime = _DEFAULT_TOKEN_LIFETIME_SECS ):
295294 """
@@ -298,8 +297,7 @@ def __init__(self, signer, issuer=None, subject=None, audience=None,
298297 issuer (str): The `iss` claim.
299298 subject (str): The `sub` claim.
300299 audience (str): the `aud` claim. The intended audience for the
301- credentials. If not specified, a new JWT will be generated for
302- every request and will use the request URI as the audience.
300+ credentials.
303301 additional_claims (Mapping[str, str]): Any additional claims for
304302 the JWT payload.
305303 token_lifetime (int): The amount of time in seconds for
@@ -334,7 +332,8 @@ def _from_signer_and_info(cls, signer, info, **kwargs):
334332 ValueError: If the info is not in the expected format.
335333 """
336334 kwargs .setdefault ('subject' , info ['client_email' ])
337- return cls (signer , issuer = info ['client_email' ], ** kwargs )
335+ kwargs .setdefault ('issuer' , info ['client_email' ])
336+ return cls (signer , ** kwargs )
338337
339338 @classmethod
340339 def from_service_account_info (cls , info , ** kwargs ):
@@ -381,9 +380,8 @@ def with_claims(self, issuer=None, subject=None, audience=None,
381380 claim will be used.
382381 subject (str): The `sub` claim. If unspecified the current subject
383382 claim will be used.
384- audience (str): the `aud` claim. If not specified, a new
385- JWT will be generated for every request and will use
386- the request URI as the audience.
383+ audience (str): the `aud` claim. If unspecified the current
384+ audience claim will be used.
387385 additional_claims (Mapping[str, str]): Any additional claims for
388386 the JWT payload. This will be merged with the current
389387 additional claims.
@@ -399,12 +397,9 @@ def with_claims(self, issuer=None, subject=None, audience=None,
399397 additional_claims = self ._additional_claims .copy ().update (
400398 additional_claims or {}))
401399
402- def _make_jwt (self , audience = None ):
400+ def _make_jwt (self ):
403401 """Make a signed JWT.
404402
405- Args:
406- audience (str): Overrides the instance's current audience claim.
407-
408403 Returns:
409404 Tuple[bytes, datetime]: The encoded JWT and the expiration.
410405 """
@@ -414,10 +409,10 @@ def _make_jwt(self, audience=None):
414409
415410 payload = {
416411 'iss' : self ._issuer ,
417- 'sub' : self ._subject or self . _issuer ,
412+ 'sub' : self ._subject ,
418413 'iat' : _helpers .datetime_to_secs (now ),
419414 'exp' : _helpers .datetime_to_secs (expiry ),
420- 'aud' : audience or self ._audience ,
415+ 'aud' : self ._audience ,
421416 }
422417
423418 payload .update (self ._additional_claims )
@@ -426,22 +421,6 @@ def _make_jwt(self, audience=None):
426421
427422 return jwt , expiry
428423
429- def _make_one_time_jwt (self , uri ):
430- """Makes a one-off JWT with the URI as the audience.
431-
432- Args:
433- uri (str): The request URI.
434-
435- Returns:
436- bytes: The encoded JWT.
437- """
438- parts = urllib .parse .urlsplit (uri )
439- # Strip query string and fragment
440- audience = urllib .parse .urlunsplit (
441- (parts .scheme , parts .netloc , parts .path , None , None ))
442- token , _ = self ._make_jwt (audience = audience )
443- return token
444-
445424 def refresh (self , request ):
446425 """Refreshes the access token.
447426
@@ -452,15 +431,8 @@ def refresh(self, request):
452431 # (pylint doesn't correctly recognize overridden methods.)
453432 self .token , self .expiry = self ._make_jwt ()
454433
434+ @_helpers .copy_docstring (credentials .Signing )
455435 def sign_bytes (self , message ):
456- """Signs the given message.
457-
458- Args:
459- message (bytes): The message to sign.
460-
461- Returns:
462- bytes: The message signature.
463- """
464436 return self ._signer .sign (message )
465437
466438 @property
@@ -472,32 +444,3 @@ def signer_email(self):
472444 @_helpers .copy_docstring (credentials .Signing )
473445 def signer (self ):
474446 return self ._signer
475-
476- def before_request (self , request , method , url , headers ):
477- """Performs credential-specific before request logic.
478-
479- If an audience is specified it will refresh the credentials if
480- necessary. If no audience is specified it will generate a one-time
481- token for the request URI. In either case, it will set the
482- authorization header in headers to the token.
483-
484- Args:
485- request (Any): Unused.
486- method (str): The request's HTTP method.
487- url (str): The request's URI.
488- headers (Mapping): The request's headers.
489- """
490- # pylint: disable=unused-argument
491- # (pylint doesn't correctly recognize overridden methods.)
492-
493- # If this set of credentials has a pre-set audience, just ensure that
494- # there is a valid token and apply the auth headers.
495- if self ._audience :
496- if not self .valid :
497- self .refresh (request )
498- self .apply (headers )
499- # Otherwise, generate a one-time token using the URL
500- # (without the query string and fragment) as the audience.
501- else :
502- token = self ._make_one_time_jwt (url )
503- self .apply (headers , token = token )
0 commit comments