diff options
author | Danny Hermes <daniel.j.hermes@gmail.com> | 2016-02-17 14:22:10 -0800 |
---|---|---|
committer | Danny Hermes <daniel.j.hermes@gmail.com> | 2016-02-17 14:22:10 -0800 |
commit | d13fc7dbbd00ce411f734653fb711e48dff5ba6d (patch) | |
tree | f01e2ea2dbafa176ca7dae708ad4205fda124ea2 /oauth2client | |
parent | 32de1342e656362dc75d3dd13a3a99a9496bebe6 (diff) | |
download | oauth2client-d13fc7dbbd00ce411f734653fb711e48dff5ba6d.tar.gz |
Implement ServiceAccountCredentials.from_p12_keyfile_buffer().
Fixes #412.
Diffstat (limited to 'oauth2client')
-rw-r--r-- | oauth2client/service_account.py | 65 |
1 files changed, 60 insertions, 5 deletions
diff --git a/oauth2client/service_account.py b/oauth2client/service_account.py index f18f192..3c9bffe 100644 --- a/oauth2client/service_account.py +++ b/oauth2client/service_account.py @@ -216,14 +216,15 @@ class ServiceAccountCredentials(AssertionCredentials): return cls._from_parsed_json_keyfile(keyfile_dict, scopes) @classmethod - def from_p12_keyfile(cls, service_account_email, filename, - private_key_password=None, scopes=''): + def _from_p12_keyfile_contents(cls, service_account_email, + private_key_pkcs12, + private_key_password=None, scopes=''): """Factory constructor from JSON keyfile. Args: service_account_email: string, The email associated with the service account. - filename: string, The location of the PKCS#12 keyfile. + private_key_pkcs12: string, The contents of a PKCS#12 keyfile. private_key_password: string, (Optional) Password for PKCS#12 private key. Defaults to ``notasecret``. scopes: List or string, (Optional) Scopes to use when acquiring an @@ -237,8 +238,6 @@ class ServiceAccountCredentials(AssertionCredentials): NotImplementedError if pyOpenSSL is not installed / not the active crypto library. """ - with open(filename, 'rb') as file_obj: - private_key_pkcs12 = file_obj.read() if private_key_password is None: private_key_password = _PASSWORD_DEFAULT signer = crypt.Signer.from_string(private_key_pkcs12, @@ -248,6 +247,62 @@ class ServiceAccountCredentials(AssertionCredentials): credentials._private_key_password = private_key_password return credentials + @classmethod + def from_p12_keyfile(cls, service_account_email, filename, + private_key_password=None, scopes=''): + """Factory constructor from JSON keyfile. + + Args: + service_account_email: string, The email associated with the + service account. + filename: string, The location of the PKCS#12 keyfile. + private_key_password: string, (Optional) Password for PKCS#12 + private key. Defaults to ``notasecret``. + scopes: List or string, (Optional) Scopes to use when acquiring an + access token. + + Returns: + ServiceAccountCredentials, a credentials object created from + the keyfile. + + Raises: + NotImplementedError if pyOpenSSL is not installed / not the + active crypto library. + """ + with open(filename, 'rb') as file_obj: + private_key_pkcs12 = file_obj.read() + return cls._from_p12_keyfile_contents( + service_account_email, private_key_pkcs12, + private_key_password=private_key_password, scopes=scopes) + + @classmethod + def from_p12_keyfile_buffer(cls, service_account_email, file_buffer, + private_key_password=None, scopes=''): + """Factory constructor from JSON keyfile. + + Args: + service_account_email: string, The email associated with the + service account. + file_buffer: stream, A buffer that implements ``read()`` + and contains the PKCS#12 key contents. + private_key_password: string, (Optional) Password for PKCS#12 + private key. Defaults to ``notasecret``. + scopes: List or string, (Optional) Scopes to use when acquiring an + access token. + + Returns: + ServiceAccountCredentials, a credentials object created from + the keyfile. + + Raises: + NotImplementedError if pyOpenSSL is not installed / not the + active crypto library. + """ + private_key_pkcs12 = file_buffer.read() + return cls._from_p12_keyfile_contents( + service_account_email, private_key_pkcs12, + private_key_password=private_key_password, scopes=scopes) + def _generate_assertion(self): """Generate the assertion that will be used in the request.""" now = int(time.time()) |