Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add crypt.Signer.from_service_account_file #95

Merged
merged 1 commit into from
Dec 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions google/auth/crypt.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,18 @@ def from_string(cls, key, key_id=None):
raise ValueError('No key could be detected.')

return cls(private_key, key_id=key_id)

@classmethod
def from_service_account_file(cls, filename):
"""Creates a Signer instance from a service account .json file
in Google format.

Args:
filename (str): The path to the service account .json file.

Returns:
Signer: The constructed signer.
"""
from google.auth import _service_account_info

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

_, signer = _service_account_info.from_filename(filename)
return signer
14 changes: 14 additions & 0 deletions tests/test_crypt.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

import os
import json

import mock
from pyasn1_modules import pem
Expand Down Expand Up @@ -59,6 +60,12 @@
with open(os.path.join(DATA_DIR, 'privatekey.p12'), 'rb') as fh:
PKCS12_KEY_BYTES = fh.read()

# The service account JSON file can be generated from the Google Cloud Console.
SERVICE_ACCOUNT_JSON_FILE = os.path.join(DATA_DIR, 'service_account.json')

with open(SERVICE_ACCOUNT_JSON_FILE, 'r') as fh:
SERVICE_ACCOUNT_INFO = json.load(fh)


def test_verify_signature():
to_sign = b'foo'
Expand Down Expand Up @@ -191,3 +198,10 @@ def test_from_string_bogus_key(self):
key_bytes = 'bogus-key'
with pytest.raises(ValueError):
crypt.Signer.from_string(key_bytes)

def test_from_service_account_file(self):
signer = crypt.Signer.from_service_account_file(
SERVICE_ACCOUNT_JSON_FILE)

assert signer.key_id == SERVICE_ACCOUNT_INFO['private_key_id']
assert isinstance(signer._key, rsa.key.PrivateKey)