This repository has been archived by the owner on Nov 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 430
Support token_uri and revoke_uri in ServiceAccountCredentials #510
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
427bdeb
Support token_uri and revoke_uri in ServiceAccountCredentials
thobrla 11a0cc7
Revert publicizing _from_p12_keyfile_buffer
thobrla 973d1dd
Accept token_uri/revoke_uri from JSON keyfile (untested)
thobrla 4919cac
Fix test bug
thobrla 85ca4cd
Verbose comments for revoke_uri
thobrla 7cc435b
Use dict.get for json token/revoke_uri
thobrla File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -96,14 +96,16 @@ def test_service_account_email(self): | |
self.credentials.service_account_email) | ||
|
||
@staticmethod | ||
def _from_json_keyfile_name_helper(payload, scopes=None): | ||
def _from_json_keyfile_name_helper(payload, scopes=None, | ||
token_uri=None, revoke_uri=None): | ||
filehandle, filename = tempfile.mkstemp() | ||
os.close(filehandle) | ||
try: | ||
with open(filename, 'w') as file_obj: | ||
json.dump(payload, file_obj) | ||
return ServiceAccountCredentials.from_json_keyfile_name( | ||
filename, scopes=scopes) | ||
filename, scopes=scopes, token_uri=token_uri, | ||
revoke_uri=revoke_uri) | ||
finally: | ||
os.remove(filename) | ||
|
||
|
@@ -122,13 +124,19 @@ def test_from_json_keyfile_name_factory(self, signer_factory): | |
'private_key': private_key, | ||
} | ||
scopes = ['foo', 'bar'] | ||
creds = self._from_json_keyfile_name_helper(payload, scopes=scopes) | ||
token_uri = 'baz' | ||
revoke_uri = 'qux' | ||
creds = self._from_json_keyfile_name_helper(payload, scopes=scopes, | ||
token_uri=token_uri, | ||
revoke_uri=revoke_uri) | ||
self.assertIsInstance(creds, ServiceAccountCredentials) | ||
self.assertEqual(creds.client_id, client_id) | ||
self.assertEqual(creds._service_account_email, client_email) | ||
self.assertEqual(creds._private_key_id, private_key_id) | ||
self.assertEqual(creds._private_key_pkcs8_pem, private_key) | ||
self.assertEqual(creds._scopes, ' '.join(scopes)) | ||
self.assertEqual(creds.token_uri, token_uri) | ||
self.assertEqual(creds.revoke_uri, revoke_uri) | ||
# Check stub. | ||
self.assertEqual(creds._signer, signer_factory.return_value) | ||
signer_factory.assert_called_once_with(private_key) | ||
|
@@ -148,24 +156,33 @@ def test_from_json_keyfile_name_factory_missing_field(self): | |
with self.assertRaises(KeyError): | ||
self._from_json_keyfile_name_helper(payload) | ||
|
||
def _from_p12_keyfile_helper(self, private_key_password=None, scopes=''): | ||
def _from_p12_keyfile_helper(self, private_key_password=None, scopes='', | ||
token_uri=None, revoke_uri=None): | ||
service_account_email = '[email protected]' | ||
filename = data_filename('privatekey.p12') | ||
with open(filename, 'rb') as file_obj: | ||
key_contents = file_obj.read() | ||
creds = ServiceAccountCredentials.from_p12_keyfile( | ||
creds_from_filename = ServiceAccountCredentials.from_p12_keyfile( | ||
service_account_email, filename, | ||
private_key_password=private_key_password, | ||
scopes=scopes) | ||
self.assertIsInstance(creds, ServiceAccountCredentials) | ||
self.assertIsNone(creds.client_id) | ||
self.assertEqual(creds._service_account_email, service_account_email) | ||
self.assertIsNone(creds._private_key_id) | ||
self.assertIsNone(creds._private_key_pkcs8_pem) | ||
self.assertEqual(creds._private_key_pkcs12, key_contents) | ||
if private_key_password is not None: | ||
self.assertEqual(creds._private_key_password, private_key_password) | ||
self.assertEqual(creds._scopes, ' '.join(scopes)) | ||
scopes=scopes, token_uri=token_uri, revoke_uri=revoke_uri) | ||
creds_from_file_contents = ( | ||
ServiceAccountCredentials.from_p12_keyfile_contents( | ||
service_account_email, key_contents, | ||
private_key_password=private_key_password, | ||
scopes=scopes, token_uri=token_uri, revoke_uri=revoke_uri)) | ||
for creds in (creds_from_filename, creds_from_file_contents): | ||
self.assertIsInstance(creds, ServiceAccountCredentials) | ||
self.assertIsNone(creds.client_id) | ||
self.assertEqual(creds._service_account_email, service_account_email) | ||
self.assertIsNone(creds._private_key_id) | ||
self.assertIsNone(creds._private_key_pkcs8_pem) | ||
self.assertEqual(creds._private_key_pkcs12, key_contents) | ||
if private_key_password is not None: | ||
self.assertEqual(creds._private_key_password, private_key_password) | ||
self.assertEqual(creds._scopes, ' '.join(scopes)) | ||
self.assertEqual(creds.token_uri, token_uri) | ||
self.assertEqual(creds.revoke_uri, revoke_uri) | ||
|
||
def _p12_not_implemented_helper(self): | ||
service_account_email = '[email protected]' | ||
|
@@ -188,13 +205,16 @@ def test_from_p12_keyfile_defaults(self): | |
def test_from_p12_keyfile_explicit(self): | ||
password = 'notasecret' | ||
self._from_p12_keyfile_helper(private_key_password=password, | ||
scopes=['foo', 'bar']) | ||
scopes=['foo', 'bar'], | ||
token_uri='baz', revoke_uri='qux') | ||
|
||
def test_from_p12_keyfile_buffer(self): | ||
service_account_email = '[email protected]' | ||
filename = data_filename('privatekey.p12') | ||
private_key_password = 'notasecret' | ||
scopes = ['foo', 'bar'] | ||
token_uri = 'baz' | ||
revoke_uri = 'qux' | ||
with open(filename, 'rb') as file_obj: | ||
key_contents = file_obj.read() | ||
# Seek back to the beginning so the buffer can be | ||
|
@@ -203,7 +223,7 @@ def test_from_p12_keyfile_buffer(self): | |
creds = ServiceAccountCredentials.from_p12_keyfile_buffer( | ||
service_account_email, file_obj, | ||
private_key_password=private_key_password, | ||
scopes=scopes) | ||
scopes=scopes, token_uri=token_uri, revoke_uri=revoke_uri) | ||
# Check the created object. | ||
self.assertIsInstance(creds, ServiceAccountCredentials) | ||
self.assertIsNone(creds.client_id) | ||
|
@@ -213,6 +233,8 @@ def test_from_p12_keyfile_buffer(self): | |
self.assertEqual(creds._private_key_pkcs12, key_contents) | ||
self.assertEqual(creds._private_key_password, private_key_password) | ||
self.assertEqual(creds._scopes, ' '.join(scopes)) | ||
self.assertEqual(creds.token_uri, token_uri) | ||
self.assertEqual(creds.revoke_uri, revoke_uri) | ||
|
||
def test_create_scoped_required_without_scopes(self): | ||
self.assertTrue(self.credentials.create_scoped_required()) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.