Fix cyclic import and add Signer.from_service_account_info (#99)
diff --git a/google/auth/crypt.py b/google/auth/crypt.py
index 618afe5..c425045 100644
--- a/google/auth/crypt.py
+++ b/google/auth/crypt.py
@@ -38,6 +38,8 @@
signature = signer.sign(message)
"""
+import io
+import json
from pyasn1.codec.der import decoder
from pyasn1_modules import pem
@@ -55,6 +57,8 @@
_PKCS8_MARKER = ('-----BEGIN PRIVATE KEY-----',
'-----END PRIVATE KEY-----')
_PKCS8_SPEC = PrivateKeyInfo()
+_JSON_FILE_PRIVATE_KEY = 'private_key'
+_JSON_FILE_PRIVATE_KEY_ID = 'private_key_id'
def _bit_list_to_bytes(bit_list):
@@ -229,6 +233,30 @@
return cls(private_key, key_id=key_id)
@classmethod
+ def from_service_account_info(cls, info):
+ """Creates a Signer instance instance from a dictionary containing
+ service account info in Google format.
+
+ Args:
+ info (Mapping[str, str]): The service account info in Google
+ format.
+
+ Returns:
+ Signer: The constructed signer.
+
+ Raises:
+ ValueError: If the info is not in the expected format.
+ """
+ if _JSON_FILE_PRIVATE_KEY not in info:
+ raise ValueError(
+ 'The private_key field was not found in the service account '
+ 'info.')
+
+ return cls.from_string(
+ info[_JSON_FILE_PRIVATE_KEY],
+ info.get(_JSON_FILE_PRIVATE_KEY_ID))
+
+ @classmethod
def from_service_account_file(cls, filename):
"""Creates a Signer instance from a service account .json file
in Google format.
@@ -239,6 +267,7 @@
Returns:
Signer: The constructed signer.
"""
- from google.auth import _service_account_info
- _, signer = _service_account_info.from_filename(filename)
- return signer
+ with io.open(filename, 'r', encoding='utf-8') as json_file:
+ data = json.load(json_file)
+
+ return cls.from_service_account_info(data)