Introduce the save_to_well_known_file() method.
This commit is contained in:
@@ -197,6 +197,10 @@ class AppAssertionCredentials(AssertionCredentials):
|
|||||||
raise AccessTokenRefreshError(str(e))
|
raise AccessTokenRefreshError(str(e))
|
||||||
self.access_token = token
|
self.access_token = token
|
||||||
|
|
||||||
|
@property
|
||||||
|
def serialization_data(self):
|
||||||
|
raise NotImplementedError('Cannot serialize credentials for AppEngine.')
|
||||||
|
|
||||||
def create_scoped_required(self):
|
def create_scoped_required(self):
|
||||||
return not self.scope
|
return not self.scope
|
||||||
|
|
||||||
|
|||||||
@@ -985,6 +985,16 @@ class GoogleCredentials(OAuth2Credentials):
|
|||||||
"""
|
"""
|
||||||
return self
|
return self
|
||||||
|
|
||||||
|
@property
|
||||||
|
def serialization_data(self):
|
||||||
|
"""Get the fields and their values identifying the current credentials."""
|
||||||
|
return {
|
||||||
|
'type': 'authorized_user',
|
||||||
|
'client_id': self.client_id,
|
||||||
|
'client_secret': self.client_secret,
|
||||||
|
'refresh_token': self.refresh_token
|
||||||
|
}
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_application_default():
|
def get_application_default():
|
||||||
"""Get the Application Default Credentials for the current environment.
|
"""Get the Application Default Credentials for the current environment.
|
||||||
@@ -1004,6 +1014,8 @@ class GoogleCredentials(OAuth2Credentials):
|
|||||||
else:
|
else:
|
||||||
application_default_credential_filename = _get_environment_variable_file()
|
application_default_credential_filename = _get_environment_variable_file()
|
||||||
well_known_file = _get_well_known_file()
|
well_known_file = _get_well_known_file()
|
||||||
|
if not os.path.isfile(well_known_file):
|
||||||
|
well_known_file = None
|
||||||
|
|
||||||
if application_default_credential_filename:
|
if application_default_credential_filename:
|
||||||
try:
|
try:
|
||||||
@@ -1064,6 +1076,26 @@ class GoogleCredentials(OAuth2Credentials):
|
|||||||
'method should point to a file.')
|
'method should point to a file.')
|
||||||
|
|
||||||
|
|
||||||
|
def save_to_well_known_file(credentials, well_known_file=None):
|
||||||
|
"""Save the provided GoogleCredentials to the well known file.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
credentials:
|
||||||
|
the credentials to be saved to the well known file;
|
||||||
|
it should be an instance of GoogleCredentials
|
||||||
|
well_known_file:
|
||||||
|
the name of the file where the credentials are to be saved;
|
||||||
|
this parameter is supposed to be used for testing only
|
||||||
|
"""
|
||||||
|
if well_known_file is None:
|
||||||
|
well_known_file = _get_well_known_file()
|
||||||
|
|
||||||
|
credentials_data = credentials.serialization_data
|
||||||
|
|
||||||
|
with open(well_known_file, 'w') as f:
|
||||||
|
simplejson.dump(credentials_data, f, sort_keys=True, indent=2)
|
||||||
|
|
||||||
|
|
||||||
def _get_environment_variable_file():
|
def _get_environment_variable_file():
|
||||||
application_default_credential_filename = (
|
application_default_credential_filename = (
|
||||||
os.environ.get(GOOGLE_APPLICATION_CREDENTIALS,
|
os.environ.get(GOOGLE_APPLICATION_CREDENTIALS,
|
||||||
@@ -1103,8 +1135,7 @@ def _get_well_known_file():
|
|||||||
default_config_path = os.path.join(default_config_path,
|
default_config_path = os.path.join(default_config_path,
|
||||||
WELL_KNOWN_CREDENTIALS_FILE)
|
WELL_KNOWN_CREDENTIALS_FILE)
|
||||||
|
|
||||||
if os.path.isfile(default_config_path):
|
return default_config_path
|
||||||
return default_config_path
|
|
||||||
|
|
||||||
|
|
||||||
def _get_application_default_credential_from_file(
|
def _get_application_default_credential_from_file(
|
||||||
|
|||||||
@@ -93,6 +93,11 @@ class AppAssertionCredentials(AssertionCredentials):
|
|||||||
' with no service account or scopes.')
|
' with no service account or scopes.')
|
||||||
raise AccessTokenRefreshError(content)
|
raise AccessTokenRefreshError(content)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def serialization_data(self):
|
||||||
|
raise NotImplementedError(
|
||||||
|
'Cannot serialize credentials for GCE service accounts.')
|
||||||
|
|
||||||
def create_scoped_required(self):
|
def create_scoped_required(self):
|
||||||
return not self.scope
|
return not self.scope
|
||||||
|
|
||||||
|
|||||||
@@ -92,6 +92,16 @@ class _ServiceAccountCredentials(AssertionCredentials):
|
|||||||
def service_account_email(self):
|
def service_account_email(self):
|
||||||
return self._service_account_email
|
return self._service_account_email
|
||||||
|
|
||||||
|
@property
|
||||||
|
def serialization_data(self):
|
||||||
|
return {
|
||||||
|
'type': 'service_account',
|
||||||
|
'client_id': self._service_account_id,
|
||||||
|
'client_email': self._service_account_email,
|
||||||
|
'private_key_id': self._private_key_id,
|
||||||
|
'private_key': self._private_key_pkcs8_text
|
||||||
|
}
|
||||||
|
|
||||||
def create_scoped_required(self):
|
def create_scoped_required(self):
|
||||||
return not self._scopes
|
return not self._scopes
|
||||||
|
|
||||||
|
|||||||
@@ -69,6 +69,7 @@ from oauth2client.client import Credentials
|
|||||||
from oauth2client.client import FlowExchangeError
|
from oauth2client.client import FlowExchangeError
|
||||||
from oauth2client.client import OAuth2Credentials
|
from oauth2client.client import OAuth2Credentials
|
||||||
from oauth2client.client import flow_from_clientsecrets
|
from oauth2client.client import flow_from_clientsecrets
|
||||||
|
from oauth2client.client import save_to_well_known_file
|
||||||
from webtest import TestApp
|
from webtest import TestApp
|
||||||
|
|
||||||
|
|
||||||
@@ -248,6 +249,10 @@ class TestAppAssertionCredentials(unittest.TestCase):
|
|||||||
self.assertEqual('a_token_123', token.access_token)
|
self.assertEqual('a_token_123', token.access_token)
|
||||||
self.assertEqual(None, token.expires_in)
|
self.assertEqual(None, token.expires_in)
|
||||||
|
|
||||||
|
def test_save_to_well_known_file(self):
|
||||||
|
credentials = AppAssertionCredentials([])
|
||||||
|
self.assertRaises(NotImplementedError, save_to_well_known_file, credentials)
|
||||||
|
|
||||||
|
|
||||||
class TestFlowModel(db.Model):
|
class TestFlowModel(db.Model):
|
||||||
flow = FlowProperty()
|
flow = FlowProperty()
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ import unittest
|
|||||||
|
|
||||||
from oauth2client.client import AccessTokenRefreshError
|
from oauth2client.client import AccessTokenRefreshError
|
||||||
from oauth2client.client import Credentials
|
from oauth2client.client import Credentials
|
||||||
|
from oauth2client.client import save_to_well_known_file
|
||||||
from oauth2client.gce import AppAssertionCredentials
|
from oauth2client.gce import AppAssertionCredentials
|
||||||
|
|
||||||
|
|
||||||
@@ -131,4 +132,7 @@ class AssertionCredentialsTests(unittest.TestCase):
|
|||||||
|
|
||||||
m.UnsetStubs()
|
m.UnsetStubs()
|
||||||
m.VerifyAll()
|
m.VerifyAll()
|
||||||
|
|
||||||
|
def test_save_to_well_known_file(self):
|
||||||
|
credentials = AppAssertionCredentials([])
|
||||||
|
self.assertRaises(NotImplementedError, save_to_well_known_file, credentials)
|
||||||
|
|||||||
@@ -67,6 +67,7 @@ from oauth2client.client import _update_query_params
|
|||||||
from oauth2client.client import credentials_from_clientsecrets_and_code
|
from oauth2client.client import credentials_from_clientsecrets_and_code
|
||||||
from oauth2client.client import credentials_from_code
|
from oauth2client.client import credentials_from_code
|
||||||
from oauth2client.client import flow_from_clientsecrets
|
from oauth2client.client import flow_from_clientsecrets
|
||||||
|
from oauth2client.client import save_to_well_known_file
|
||||||
from oauth2client.clientsecrets import _loadfile
|
from oauth2client.clientsecrets import _loadfile
|
||||||
from oauth2client.service_account import _ServiceAccountCredentials
|
from oauth2client.service_account import _ServiceAccountCredentials
|
||||||
|
|
||||||
@@ -260,11 +261,6 @@ class GoogleCredentialsTests(unittest.TestCase):
|
|||||||
os.environ['APPDATA'] = DATA_DIR
|
os.environ['APPDATA'] = DATA_DIR
|
||||||
self.assertEqual(well_known_file, _get_well_known_file())
|
self.assertEqual(well_known_file, _get_well_known_file())
|
||||||
|
|
||||||
def test_get_well_known_file_on_windows_no_file(self):
|
|
||||||
os.name = 'nt'
|
|
||||||
os.environ['APPDATA'] = os.path.join(DATA_DIR, 'nonexistentpath')
|
|
||||||
self.assertEqual(None, _get_well_known_file())
|
|
||||||
|
|
||||||
def test_get_application_default_credential_from_file_service_account(self):
|
def test_get_application_default_credential_from_file_service_account(self):
|
||||||
credentials_file = datafile(
|
credentials_file = datafile(
|
||||||
os.path.join('gcloud', 'application_default_credentials.json'))
|
os.path.join('gcloud', 'application_default_credentials.json'))
|
||||||
@@ -272,6 +268,22 @@ class GoogleCredentialsTests(unittest.TestCase):
|
|||||||
credentials_file)
|
credentials_file)
|
||||||
self.validate_service_account_credentials(credentials)
|
self.validate_service_account_credentials(credentials)
|
||||||
|
|
||||||
|
def test_save_to_well_known_file_service_account(self):
|
||||||
|
credential_file = datafile(
|
||||||
|
os.path.join('gcloud', 'application_default_credentials.json'))
|
||||||
|
credentials = _get_application_default_credential_from_file(
|
||||||
|
credential_file)
|
||||||
|
temp_credential_file = datafile(
|
||||||
|
os.path.join('gcloud', 'temp_well_known_file_service_account.json'))
|
||||||
|
save_to_well_known_file(credentials, temp_credential_file)
|
||||||
|
with open(temp_credential_file) as f:
|
||||||
|
d = simplejson.load(f)
|
||||||
|
self.assertEqual('service_account', d['type'])
|
||||||
|
self.assertEqual('123', d['client_id'])
|
||||||
|
self.assertEqual('dummy@google.com', d['client_email'])
|
||||||
|
self.assertEqual('ABCDEF', d['private_key_id'])
|
||||||
|
os.remove(temp_credential_file)
|
||||||
|
|
||||||
def test_get_application_default_credential_from_file_authorized_user(self):
|
def test_get_application_default_credential_from_file_authorized_user(self):
|
||||||
credentials_file = datafile(
|
credentials_file = datafile(
|
||||||
os.path.join('gcloud',
|
os.path.join('gcloud',
|
||||||
@@ -280,6 +292,23 @@ class GoogleCredentialsTests(unittest.TestCase):
|
|||||||
credentials_file)
|
credentials_file)
|
||||||
self.validate_google_credentials(credentials)
|
self.validate_google_credentials(credentials)
|
||||||
|
|
||||||
|
def test_save_to_well_known_file_authorized_user(self):
|
||||||
|
credentials_file = datafile(
|
||||||
|
os.path.join('gcloud',
|
||||||
|
'application_default_credentials_authorized_user.json'))
|
||||||
|
credentials = _get_application_default_credential_from_file(
|
||||||
|
credentials_file)
|
||||||
|
temp_credential_file = datafile(
|
||||||
|
os.path.join('gcloud', 'temp_well_known_file_authorized_user.json'))
|
||||||
|
save_to_well_known_file(credentials, temp_credential_file)
|
||||||
|
with open(temp_credential_file) as f:
|
||||||
|
d = simplejson.load(f)
|
||||||
|
self.assertEqual('authorized_user', d['type'])
|
||||||
|
self.assertEqual('123', d['client_id'])
|
||||||
|
self.assertEqual('secret', d['client_secret'])
|
||||||
|
self.assertEqual('alabalaportocala', d['refresh_token'])
|
||||||
|
os.remove(temp_credential_file)
|
||||||
|
|
||||||
def test_get_application_default_credential_from_malformed_file_1(self):
|
def test_get_application_default_credential_from_malformed_file_1(self):
|
||||||
credentials_file = datafile(
|
credentials_file = datafile(
|
||||||
os.path.join('gcloud',
|
os.path.join('gcloud',
|
||||||
|
|||||||
Reference in New Issue
Block a user