4747
4848from google .auth import _helpers
4949from google .auth import _service_account_info
50- from google .auth import credentials
5150from google .auth import crypt
51+ import google .auth .credentials
5252
5353
5454_DEFAULT_TOKEN_LIFETIME_SECS = 3600 # 1 hour in sections
@@ -239,8 +239,8 @@ def decode(token, certs=None, verify=True, audience=None):
239239 return payload
240240
241241
242- class Credentials (credentials .Signing ,
243- credentials .Credentials ):
242+ class Credentials (google . auth . credentials .Signing ,
243+ google . auth . credentials .Credentials ):
244244 """Credentials that use a JWT as the bearer token.
245245
246246 These credentials require an "audience" claim. This claim identifies the
@@ -253,23 +253,24 @@ class Credentials(credentials.Signing,
253253 To create JWT credentials using a Google service account private key
254254 JSON file::
255255
256+ audience = 'https://pubsub.googleapis.com/google.pubsub.v1.Publisher'
256257 credentials = jwt.Credentials.from_service_account_file(
257258 'service-account.json',
258- audience='https://speech.googleapis.com' )
259+ audience=audience )
259260
260261 If you already have the service account file loaded and parsed::
261262
262263 service_account_info = json.load(open('service_account.json'))
263264 credentials = jwt.Credentials.from_service_account_info(
264265 service_account_info,
265- audience='https://speech.googleapis.com' )
266+ audience=audience )
266267
267268 Both helper methods pass on arguments to the constructor, so you can
268269 specify the JWT claims::
269270
270271 credentials = jwt.Credentials.from_service_account_file(
271272 'service-account.json',
272- audience='https://speech.googleapis.com' ,
273+ audience=audience ,
273274 additional_claims={'meta': 'data'})
274275
275276 You can also construct the credentials directly if you have a
@@ -279,13 +280,14 @@ class Credentials(credentials.Signing,
279280 signer,
280281 issuer='your-issuer',
281282 subject='your-subject',
282- audience=''https://speech.googleapis.com'' )
283+ audience=audience )
283284
284285 The claims are considered immutable. If you want to modify the claims,
285286 you can easily create another instance using :meth:`with_claims`::
286287
287- new_credentials = credentials.with_claims(
288- audience='https://vision.googleapis.com')
288+ new_audience = (
289+ 'https://pubsub.googleapis.com/google.pubsub.v1.Subscriber')
290+ new_credentials = credentials.with_claims(audience=new_audience)
289291 """
290292
291293 def __init__ (self , signer , issuer , subject , audience ,
@@ -371,6 +373,41 @@ def from_service_account_file(cls, filename, **kwargs):
371373 filename , require = ['client_email' ])
372374 return cls ._from_signer_and_info (signer , info , ** kwargs )
373375
376+ @classmethod
377+ def from_signing_credentials (cls , credentials , audience , ** kwargs ):
378+ """Creates a new :class:`google.auth.jwt.Credentials` instance from an
379+ existing :class:`google.auth.credentials.Signing` instance.
380+
381+ The new instance will use the same signer as the existing instance and
382+ will use the existing instance's signer email as the issuer and
383+ subject by default.
384+
385+ Example::
386+
387+ svc_creds = service_account.Credentials.from_service_account_file(
388+ 'service_account.json')
389+ audience = (
390+ 'https://pubsub.googleapis.com/google.pubsub.v1.Publisher')
391+ jwt_creds = jwt.Credentials.from_signing_credentials(
392+ svc_creds, audience=audience)
393+
394+ Args:
395+ credentials (google.auth.credentials.Signing): The credentials to
396+ use to construct the new credentials.
397+ audience (str): the `aud` claim. The intended audience for the
398+ credentials.
399+ kwargs: Additional arguments to pass to the constructor.
400+
401+ Returns:
402+ google.auth.jwt.Credentials: A new Credentials instance.
403+ """
404+ kwargs .setdefault ('issuer' , credentials .signer_email )
405+ kwargs .setdefault ('subject' , credentials .signer_email )
406+ return cls (
407+ credentials .signer ,
408+ audience = audience ,
409+ ** kwargs )
410+
374411 def with_claims (self , issuer = None , subject = None , audience = None ,
375412 additional_claims = None ):
376413 """Returns a copy of these credentials with modified claims.
@@ -431,16 +468,16 @@ def refresh(self, request):
431468 # (pylint doesn't correctly recognize overridden methods.)
432469 self .token , self .expiry = self ._make_jwt ()
433470
434- @_helpers .copy_docstring (credentials .Signing )
471+ @_helpers .copy_docstring (google . auth . credentials .Signing )
435472 def sign_bytes (self , message ):
436473 return self ._signer .sign (message )
437474
438475 @property
439- @_helpers .copy_docstring (credentials .Signing )
476+ @_helpers .copy_docstring (google . auth . credentials .Signing )
440477 def signer_email (self ):
441478 return self ._issuer
442479
443480 @property
444- @_helpers .copy_docstring (credentials .Signing )
481+ @_helpers .copy_docstring (google . auth . credentials .Signing )
445482 def signer (self ):
446483 return self ._signer
0 commit comments