Skip to content

Commit 4c41593

Browse files
committed
Merge pull request googleapis#27 from orestica/compacted_3
Introduce the save_to_well_known_file() method.
2 parents 9316f50 + df03c9c commit 4c41593

File tree

7 files changed

+99
-8
lines changed

7 files changed

+99
-8
lines changed

oauth2client/appengine.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,10 @@ def _refresh(self, http_request):
197197
raise AccessTokenRefreshError(str(e))
198198
self.access_token = token
199199

200+
@property
201+
def serialization_data(self):
202+
raise NotImplementedError('Cannot serialize credentials for AppEngine.')
203+
200204
def create_scoped_required(self):
201205
return not self.scope
202206

oauth2client/client.py

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -985,6 +985,16 @@ def create_scoped(self, scopes):
985985
"""
986986
return self
987987

988+
@property
989+
def serialization_data(self):
990+
"""Get the fields and their values identifying the current credentials."""
991+
return {
992+
'type': 'authorized_user',
993+
'client_id': self.client_id,
994+
'client_secret': self.client_secret,
995+
'refresh_token': self.refresh_token
996+
}
997+
988998
@staticmethod
989999
def get_application_default():
9901000
"""Get the Application Default Credentials for the current environment.
@@ -1004,6 +1014,8 @@ def get_application_default():
10041014
else:
10051015
application_default_credential_filename = _get_environment_variable_file()
10061016
well_known_file = _get_well_known_file()
1017+
if not os.path.isfile(well_known_file):
1018+
well_known_file = None
10071019

10081020
if application_default_credential_filename:
10091021
try:
@@ -1064,6 +1076,29 @@ def from_stream(credential_filename):
10641076
'method should point to a file.')
10651077

10661078

1079+
def save_to_well_known_file(credentials, well_known_file=None):
1080+
"""Save the provided GoogleCredentials to the well known file.
1081+
1082+
Args:
1083+
credentials:
1084+
the credentials to be saved to the well known file;
1085+
it should be an instance of GoogleCredentials
1086+
well_known_file:
1087+
the name of the file where the credentials are to be saved;
1088+
this parameter is supposed to be used for testing only
1089+
"""
1090+
# TODO(orestica): move this method to tools.py
1091+
# once the argparse import gets fixed (it is not present in Python 2.6)
1092+
1093+
if well_known_file is None:
1094+
well_known_file = _get_well_known_file()
1095+
1096+
credentials_data = credentials.serialization_data
1097+
1098+
with open(well_known_file, 'w') as f:
1099+
simplejson.dump(credentials_data, f, sort_keys=True, indent=2)
1100+
1101+
10671102
def _get_environment_variable_file():
10681103
application_default_credential_filename = (
10691104
os.environ.get(GOOGLE_APPLICATION_CREDENTIALS,
@@ -1103,8 +1138,7 @@ def _get_well_known_file():
11031138
default_config_path = os.path.join(default_config_path,
11041139
WELL_KNOWN_CREDENTIALS_FILE)
11051140

1106-
if os.path.isfile(default_config_path):
1107-
return default_config_path
1141+
return default_config_path
11081142

11091143

11101144
def _get_application_default_credential_from_file(

oauth2client/gce.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ def _refresh(self, http_request):
9393
' with no service account or scopes.')
9494
raise AccessTokenRefreshError(content)
9595

96+
@property
97+
def serialization_data(self):
98+
raise NotImplementedError(
99+
'Cannot serialize credentials for GCE service accounts.')
100+
96101
def create_scoped_required(self):
97102
return not self.scope
98103

oauth2client/service_account.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,16 @@ def sign_blob(self, blob):
9292
def service_account_email(self):
9393
return self._service_account_email
9494

95+
@property
96+
def serialization_data(self):
97+
return {
98+
'type': 'service_account',
99+
'client_id': self._service_account_id,
100+
'client_email': self._service_account_email,
101+
'private_key_id': self._private_key_id,
102+
'private_key': self._private_key_pkcs8_text
103+
}
104+
95105
def create_scoped_required(self):
96106
return not self._scopes
97107

tests/test_appengine.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
from oauth2client.client import FlowExchangeError
7070
from oauth2client.client import OAuth2Credentials
7171
from oauth2client.client import flow_from_clientsecrets
72+
from oauth2client.client import save_to_well_known_file
7273
from webtest import TestApp
7374

7475

@@ -248,6 +249,10 @@ def test_get_access_token(self):
248249
self.assertEqual('a_token_123', token.access_token)
249250
self.assertEqual(None, token.expires_in)
250251

252+
def test_save_to_well_known_file(self):
253+
credentials = AppAssertionCredentials([])
254+
self.assertRaises(NotImplementedError, save_to_well_known_file, credentials)
255+
251256

252257
class TestFlowModel(db.Model):
253258
flow = FlowProperty()

tests/test_gce.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
from oauth2client.client import AccessTokenRefreshError
2828
from oauth2client.client import Credentials
29+
from oauth2client.client import save_to_well_known_file
2930
from oauth2client.gce import AppAssertionCredentials
3031

3132

@@ -131,4 +132,7 @@ def test_get_access_token(self):
131132

132133
m.UnsetStubs()
133134
m.VerifyAll()
134-
135+
136+
def test_save_to_well_known_file(self):
137+
credentials = AppAssertionCredentials([])
138+
self.assertRaises(NotImplementedError, save_to_well_known_file, credentials)

tests/test_oauth2client.py

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
from oauth2client.client import credentials_from_clientsecrets_and_code
6868
from oauth2client.client import credentials_from_code
6969
from oauth2client.client import flow_from_clientsecrets
70+
from oauth2client.client import save_to_well_known_file
7071
from oauth2client.clientsecrets import _loadfile
7172
from oauth2client.service_account import _ServiceAccountCredentials
7273

@@ -260,18 +261,29 @@ def test_get_well_known_file_on_windows(self):
260261
os.environ['APPDATA'] = DATA_DIR
261262
self.assertEqual(well_known_file, _get_well_known_file())
262263

263-
def test_get_well_known_file_on_windows_no_file(self):
264-
os.name = 'nt'
265-
os.environ['APPDATA'] = os.path.join(DATA_DIR, 'nonexistentpath')
266-
self.assertEqual(None, _get_well_known_file())
267-
268264
def test_get_application_default_credential_from_file_service_account(self):
269265
credentials_file = datafile(
270266
os.path.join('gcloud', 'application_default_credentials.json'))
271267
credentials = _get_application_default_credential_from_file(
272268
credentials_file)
273269
self.validate_service_account_credentials(credentials)
274270

271+
def test_save_to_well_known_file_service_account(self):
272+
credential_file = datafile(
273+
os.path.join('gcloud', 'application_default_credentials.json'))
274+
credentials = _get_application_default_credential_from_file(
275+
credential_file)
276+
temp_credential_file = datafile(
277+
os.path.join('gcloud', 'temp_well_known_file_service_account.json'))
278+
save_to_well_known_file(credentials, temp_credential_file)
279+
with open(temp_credential_file) as f:
280+
d = simplejson.load(f)
281+
self.assertEqual('service_account', d['type'])
282+
self.assertEqual('123', d['client_id'])
283+
self.assertEqual('[email protected]', d['client_email'])
284+
self.assertEqual('ABCDEF', d['private_key_id'])
285+
os.remove(temp_credential_file)
286+
275287
def test_get_application_default_credential_from_file_authorized_user(self):
276288
credentials_file = datafile(
277289
os.path.join('gcloud',
@@ -280,6 +292,23 @@ def test_get_application_default_credential_from_file_authorized_user(self):
280292
credentials_file)
281293
self.validate_google_credentials(credentials)
282294

295+
def test_save_to_well_known_file_authorized_user(self):
296+
credentials_file = datafile(
297+
os.path.join('gcloud',
298+
'application_default_credentials_authorized_user.json'))
299+
credentials = _get_application_default_credential_from_file(
300+
credentials_file)
301+
temp_credential_file = datafile(
302+
os.path.join('gcloud', 'temp_well_known_file_authorized_user.json'))
303+
save_to_well_known_file(credentials, temp_credential_file)
304+
with open(temp_credential_file) as f:
305+
d = simplejson.load(f)
306+
self.assertEqual('authorized_user', d['type'])
307+
self.assertEqual('123', d['client_id'])
308+
self.assertEqual('secret', d['client_secret'])
309+
self.assertEqual('alabalaportocala', d['refresh_token'])
310+
os.remove(temp_credential_file)
311+
283312
def test_get_application_default_credential_from_malformed_file_1(self):
284313
credentials_file = datafile(
285314
os.path.join('gcloud',

0 commit comments

Comments
 (0)