@@ -298,20 +298,20 @@ def to_json(self):
298298 return self ._to_json (self .NON_SERIALIZED_MEMBERS )
299299
300300 @classmethod
301- def new_from_json (cls , s ):
301+ def new_from_json (cls , json_data ):
302302 """Utility class method to instantiate a Credentials subclass from JSON.
303303
304304 Expects the JSON string to have been produced by to_json().
305305
306306 Args:
307- s : string or bytes, JSON from to_json().
307+ json_data : string or bytes, JSON from to_json().
308308
309309 Returns:
310310 An instance of the subclass of Credentials that was serialized with
311311 to_json().
312312 """
313- json_string_as_unicode = _from_bytes (s )
314- data = json .loads (json_string_as_unicode )
313+ json_data_as_unicode = _from_bytes (json_data )
314+ data = json .loads (json_data_as_unicode )
315315 # Find and call the right classmethod from_json() to restore
316316 # the object.
317317 module_name = data ['_module' ]
@@ -326,8 +326,7 @@ def new_from_json(cls, s):
326326 module_obj = __import__ (module_name ,
327327 fromlist = module_name .split ('.' )[:- 1 ])
328328 kls = getattr (module_obj , data ['_class' ])
329- from_json = getattr (kls , 'from_json' )
330- return from_json (json_string_as_unicode )
329+ return kls .from_json (json_data_as_unicode )
331330
332331 @classmethod
333332 def from_json (cls , unused_data ):
@@ -710,19 +709,18 @@ def retrieve_scopes(self, http):
710709 return self .scopes
711710
712711 @classmethod
713- def from_json (cls , s ):
712+ def from_json (cls , json_data ):
714713 """Instantiate a Credentials object from a JSON description of it.
715714
716715 The JSON should have been produced by calling .to_json() on the object.
717716
718717 Args:
719- data: dict, A deserialized JSON object .
718+ json_data: string or bytes, JSON to deserialize .
720719
721720 Returns:
722721 An instance of a Credentials subclass.
723722 """
724- s = _from_bytes (s )
725- data = json .loads (s )
723+ data = json .loads (_from_bytes (json_data ))
726724 if (data .get ('token_expiry' ) and
727725 not isinstance (data ['token_expiry' ], datetime .datetime )):
728726 try :
@@ -1070,8 +1068,8 @@ def __init__(self, access_token, user_agent, revoke_uri=None):
10701068 revoke_uri = revoke_uri )
10711069
10721070 @classmethod
1073- def from_json (cls , s ):
1074- data = json .loads (_from_bytes (s ))
1071+ def from_json (cls , json_data ):
1072+ data = json .loads (_from_bytes (json_data ))
10751073 retval = AccessTokenCredentials (
10761074 data ['access_token' ],
10771075 data ['user_agent' ])
@@ -1190,7 +1188,7 @@ class GoogleCredentials(OAuth2Credentials):
11901188 NON_SERIALIZED_MEMBERS = (
11911189 frozenset (['_private_key' ]) |
11921190 OAuth2Credentials .NON_SERIALIZED_MEMBERS )
1193-
1191+ """Members that aren't serialized when object is converted to JSON."""
11941192
11951193 def __init__ (self , access_token , client_id , client_secret , refresh_token ,
11961194 token_expiry , token_uri , user_agent ,
@@ -1630,101 +1628,6 @@ def _RequireCryptoOrDie():
16301628 raise CryptoUnavailableError ('No crypto library available' )
16311629
16321630
1633- class SignedJwtAssertionCredentials (AssertionCredentials ):
1634- """Credentials object used for OAuth 2.0 Signed JWT assertion grants.
1635-
1636- This credential does not require a flow to instantiate because it
1637- represents a two legged flow, and therefore has all of the required
1638- information to generate and refresh its own access tokens.
1639-
1640- SignedJwtAssertionCredentials requires either PyOpenSSL, or PyCrypto
1641- 2.6 or later. For App Engine you may also consider using
1642- AppAssertionCredentials.
1643- """
1644-
1645- MAX_TOKEN_LIFETIME_SECS = 3600 # 1 hour in seconds
1646-
1647- @util .positional (4 )
1648- def __init__ (self ,
1649- service_account_name ,
1650- private_key ,
1651- scope ,
1652- private_key_password = 'notasecret' ,
1653- user_agent = None ,
1654- token_uri = GOOGLE_TOKEN_URI ,
1655- revoke_uri = GOOGLE_REVOKE_URI ,
1656- ** kwargs ):
1657- """Constructor for SignedJwtAssertionCredentials.
1658-
1659- Args:
1660- service_account_name: string, id for account, usually an email
1661- address.
1662- private_key: string or bytes, private key in PKCS12 or PEM format.
1663- scope: string or iterable of strings, scope(s) of the credentials
1664- being requested.
1665- private_key_password: string, password for private_key, unused if
1666- private_key is in PEM format.
1667- user_agent: string, HTTP User-Agent to provide for this
1668- application.
1669- token_uri: string, URI for token endpoint. For convenience defaults
1670- to Google's endpoints but any OAuth 2.0 provider can be
1671- used.
1672- revoke_uri: string, URI for revoke endpoint.
1673- kwargs: kwargs, Additional parameters to add to the JWT token, for
1674- example sub=joe@xample.org.
1675-
1676- Raises:
1677- CryptoUnavailableError if no crypto library is available.
1678- """
1679- _RequireCryptoOrDie ()
1680- super (SignedJwtAssertionCredentials , self ).__init__ (
1681- None ,
1682- user_agent = user_agent ,
1683- token_uri = token_uri ,
1684- revoke_uri = revoke_uri ,
1685- )
1686-
1687- self .scope = util .scopes_to_string (scope )
1688-
1689- # Keep base64 encoded so it can be stored in JSON.
1690- self .private_key = base64 .b64encode (_to_bytes (private_key ))
1691- self .private_key_password = private_key_password
1692- self .service_account_name = service_account_name
1693- self .kwargs = kwargs
1694-
1695- @classmethod
1696- def from_json (cls , s ):
1697- data = json .loads (_from_bytes (s ))
1698- retval = SignedJwtAssertionCredentials (
1699- data ['service_account_name' ],
1700- base64 .b64decode (data ['private_key' ]),
1701- data ['scope' ],
1702- private_key_password = data ['private_key_password' ],
1703- user_agent = data ['user_agent' ],
1704- token_uri = data ['token_uri' ],
1705- ** data ['kwargs' ]
1706- )
1707- retval .invalid = data ['invalid' ]
1708- retval .access_token = data ['access_token' ]
1709- return retval
1710-
1711- def _generate_assertion (self ):
1712- """Generate the assertion that will be used in the request."""
1713- now = int (time .time ())
1714- payload = {
1715- 'aud' : self .token_uri ,
1716- 'scope' : self .scope ,
1717- 'iat' : now ,
1718- 'exp' : now + SignedJwtAssertionCredentials .MAX_TOKEN_LIFETIME_SECS ,
1719- 'iss' : self .service_account_name
1720- }
1721- payload .update (self .kwargs )
1722- logger .debug (str (payload ))
1723-
1724- private_key = base64 .b64decode (self .private_key )
1725- return crypt .make_signed_jwt (crypt .Signer .from_string (
1726- private_key , self .private_key_password ), payload )
1727-
17281631# Only used in verify_id_token(), which is always calling to the same URI
17291632# for the certs.
17301633_cached_http = httplib2 .Http (MemoryCache ())
0 commit comments