@@ -81,6 +81,12 @@ class ServiceAccountCredentials(AssertionCredentials):
81
81
service account.
82
82
user_agent: string, (Optional) User agent to use when sending
83
83
request.
84
+ token_uri: string, URI for token endpoint. For convenience defaults
85
+ to Google's endpoints but any OAuth 2.0 provider can be
86
+ used.
87
+ revoke_uri: string, URI for revoke endpoint. For convenience defaults
88
+ to Google's endpoints but any OAuth 2.0 provider can be
89
+ used.
84
90
kwargs: dict, Extra key-value pairs (both strings) to send in the
85
91
payload body when making an assertion.
86
92
"""
@@ -106,10 +112,13 @@ def __init__(self,
106
112
private_key_id = None ,
107
113
client_id = None ,
108
114
user_agent = None ,
115
+ token_uri = GOOGLE_TOKEN_URI ,
116
+ revoke_uri = GOOGLE_REVOKE_URI ,
109
117
** kwargs ):
110
118
111
119
super (ServiceAccountCredentials , self ).__init__ (
112
- None , user_agent = user_agent )
120
+ None , user_agent = user_agent , token_uri = token_uri ,
121
+ revoke_uri = revoke_uri )
113
122
114
123
self ._service_account_email = service_account_email
115
124
self ._signer = signer
@@ -145,14 +154,21 @@ def _to_json(self, strip, to_serialize=None):
145
154
strip , to_serialize = to_serialize )
146
155
147
156
@classmethod
148
- def _from_parsed_json_keyfile (cls , keyfile_dict , scopes ):
157
+ def _from_parsed_json_keyfile (cls , keyfile_dict , scopes ,
158
+ token_uri = None , revoke_uri = None ):
149
159
"""Helper for factory constructors from JSON keyfile.
150
160
151
161
Args:
152
162
keyfile_dict: dict-like object, The parsed dictionary-like object
153
163
containing the contents of the JSON keyfile.
154
164
scopes: List or string, Scopes to use when acquiring an
155
165
access token.
166
+ token_uri: string, URI for OAuth 2.0 provider token endpoint.
167
+ If unset and not present in keyfile_dict, defaults
168
+ to Google's endpoints.
169
+ revoke_uri: string, URI for OAuth 2.0 provider revoke endpoint.
170
+ If unset and not present in keyfile_dict, defaults
171
+ to Google's endpoints.
156
172
157
173
Returns:
158
174
ServiceAccountCredentials, a credentials object created from
@@ -172,22 +188,35 @@ def _from_parsed_json_keyfile(cls, keyfile_dict, scopes):
172
188
private_key_pkcs8_pem = keyfile_dict ['private_key' ]
173
189
private_key_id = keyfile_dict ['private_key_id' ]
174
190
client_id = keyfile_dict ['client_id' ]
191
+ if not token_uri :
192
+ token_uri = keyfile_dict .get ('token_uri' , GOOGLE_TOKEN_URI )
193
+ if not revoke_uri :
194
+ revoke_uri = keyfile_dict .get ('revoke_uri' , GOOGLE_REVOKE_URI )
175
195
176
196
signer = crypt .Signer .from_string (private_key_pkcs8_pem )
177
197
credentials = cls (service_account_email , signer , scopes = scopes ,
178
198
private_key_id = private_key_id ,
179
- client_id = client_id )
199
+ client_id = client_id , token_uri = token_uri ,
200
+ revoke_uri = revoke_uri )
180
201
credentials ._private_key_pkcs8_pem = private_key_pkcs8_pem
181
202
return credentials
182
203
183
204
@classmethod
184
- def from_json_keyfile_name (cls , filename , scopes = '' ):
205
+ def from_json_keyfile_name (cls , filename , scopes = '' ,
206
+ token_uri = None , revoke_uri = None ):
207
+
185
208
"""Factory constructor from JSON keyfile by name.
186
209
187
210
Args:
188
211
filename: string, The location of the keyfile.
189
212
scopes: List or string, (Optional) Scopes to use when acquiring an
190
213
access token.
214
+ token_uri: string, URI for OAuth 2.0 provider token endpoint.
215
+ If unset and not present in the key file, defaults
216
+ to Google's endpoints.
217
+ revoke_uri: string, URI for OAuth 2.0 provider revoke endpoint.
218
+ If unset and not present in the key file, defaults
219
+ to Google's endpoints.
191
220
192
221
Returns:
193
222
ServiceAccountCredentials, a credentials object created from
@@ -200,17 +229,26 @@ def from_json_keyfile_name(cls, filename, scopes=''):
200
229
"""
201
230
with open (filename , 'r' ) as file_obj :
202
231
client_credentials = json .load (file_obj )
203
- return cls ._from_parsed_json_keyfile (client_credentials , scopes )
232
+ return cls ._from_parsed_json_keyfile (client_credentials , scopes ,
233
+ token_uri = token_uri ,
234
+ revoke_uri = revoke_uri )
204
235
205
236
@classmethod
206
- def from_json_keyfile_dict (cls , keyfile_dict , scopes = '' ):
237
+ def from_json_keyfile_dict (cls , keyfile_dict , scopes = '' ,
238
+ token_uri = None , revoke_uri = None ):
207
239
"""Factory constructor from parsed JSON keyfile.
208
240
209
241
Args:
210
242
keyfile_dict: dict-like object, The parsed dictionary-like object
211
243
containing the contents of the JSON keyfile.
212
244
scopes: List or string, (Optional) Scopes to use when acquiring an
213
245
access token.
246
+ token_uri: string, URI for OAuth 2.0 provider token endpoint.
247
+ If unset and not present in keyfile_dict, defaults
248
+ to Google's endpoints.
249
+ revoke_uri: string, URI for OAuth 2.0 provider revoke endpoint.
250
+ If unset and not present in keyfile_dict, defaults
251
+ to Google's endpoints.
214
252
215
253
Returns:
216
254
ServiceAccountCredentials, a credentials object created from
@@ -221,12 +259,16 @@ def from_json_keyfile_dict(cls, keyfile_dict, scopes=''):
221
259
KeyError, if one of the expected keys is not present in
222
260
the keyfile.
223
261
"""
224
- return cls ._from_parsed_json_keyfile (keyfile_dict , scopes )
262
+ return cls ._from_parsed_json_keyfile (keyfile_dict , scopes ,
263
+ token_uri = token_uri ,
264
+ revoke_uri = revoke_uri )
225
265
226
266
@classmethod
227
267
def _from_p12_keyfile_contents (cls , service_account_email ,
228
268
private_key_pkcs12 ,
229
- private_key_password = None , scopes = '' ):
269
+ private_key_password = None , scopes = '' ,
270
+ token_uri = GOOGLE_TOKEN_URI ,
271
+ revoke_uri = GOOGLE_REVOKE_URI ):
230
272
"""Factory constructor from JSON keyfile.
231
273
232
274
Args:
@@ -237,6 +279,12 @@ def _from_p12_keyfile_contents(cls, service_account_email,
237
279
private key. Defaults to ``notasecret``.
238
280
scopes: List or string, (Optional) Scopes to use when acquiring an
239
281
access token.
282
+ token_uri: string, URI for token endpoint. For convenience defaults
283
+ to Google's endpoints but any OAuth 2.0 provider can be
284
+ used.
285
+ revoke_uri: string, URI for revoke endpoint. For convenience
286
+ defaults to Google's endpoints but any OAuth 2.0
287
+ provider can be used.
240
288
241
289
Returns:
242
290
ServiceAccountCredentials, a credentials object created from
@@ -252,14 +300,18 @@ def _from_p12_keyfile_contents(cls, service_account_email,
252
300
raise NotImplementedError (_PKCS12_ERROR )
253
301
signer = crypt .Signer .from_string (private_key_pkcs12 ,
254
302
private_key_password )
255
- credentials = cls (service_account_email , signer , scopes = scopes )
303
+ credentials = cls (service_account_email , signer , scopes = scopes ,
304
+ token_uri = token_uri , revoke_uri = revoke_uri )
256
305
credentials ._private_key_pkcs12 = private_key_pkcs12
257
306
credentials ._private_key_password = private_key_password
258
307
return credentials
259
308
260
309
@classmethod
261
310
def from_p12_keyfile (cls , service_account_email , filename ,
262
- private_key_password = None , scopes = '' ):
311
+ private_key_password = None , scopes = '' ,
312
+ token_uri = GOOGLE_TOKEN_URI ,
313
+ revoke_uri = GOOGLE_REVOKE_URI ):
314
+
263
315
"""Factory constructor from JSON keyfile.
264
316
265
317
Args:
@@ -270,6 +322,12 @@ def from_p12_keyfile(cls, service_account_email, filename,
270
322
private key. Defaults to ``notasecret``.
271
323
scopes: List or string, (Optional) Scopes to use when acquiring an
272
324
access token.
325
+ token_uri: string, URI for token endpoint. For convenience defaults
326
+ to Google's endpoints but any OAuth 2.0 provider can be
327
+ used.
328
+ revoke_uri: string, URI for revoke endpoint. For convenience
329
+ defaults to Google's endpoints but any OAuth 2.0
330
+ provider can be used.
273
331
274
332
Returns:
275
333
ServiceAccountCredentials, a credentials object created from
@@ -283,11 +341,14 @@ def from_p12_keyfile(cls, service_account_email, filename,
283
341
private_key_pkcs12 = file_obj .read ()
284
342
return cls ._from_p12_keyfile_contents (
285
343
service_account_email , private_key_pkcs12 ,
286
- private_key_password = private_key_password , scopes = scopes )
344
+ private_key_password = private_key_password , scopes = scopes ,
345
+ token_uri = token_uri , revoke_uri = revoke_uri )
287
346
288
347
@classmethod
289
348
def from_p12_keyfile_buffer (cls , service_account_email , file_buffer ,
290
- private_key_password = None , scopes = '' ):
349
+ private_key_password = None , scopes = '' ,
350
+ token_uri = GOOGLE_TOKEN_URI ,
351
+ revoke_uri = GOOGLE_REVOKE_URI ):
291
352
"""Factory constructor from JSON keyfile.
292
353
293
354
Args:
@@ -299,6 +360,12 @@ def from_p12_keyfile_buffer(cls, service_account_email, file_buffer,
299
360
private key. Defaults to ``notasecret``.
300
361
scopes: List or string, (Optional) Scopes to use when acquiring an
301
362
access token.
363
+ token_uri: string, URI for token endpoint. For convenience defaults
364
+ to Google's endpoints but any OAuth 2.0 provider can be
365
+ used.
366
+ revoke_uri: string, URI for revoke endpoint. For convenience
367
+ defaults to Google's endpoints but any OAuth 2.0
368
+ provider can be used.
302
369
303
370
Returns:
304
371
ServiceAccountCredentials, a credentials object created from
@@ -311,7 +378,8 @@ def from_p12_keyfile_buffer(cls, service_account_email, file_buffer,
311
378
private_key_pkcs12 = file_buffer .read ()
312
379
return cls ._from_p12_keyfile_contents (
313
380
service_account_email , private_key_pkcs12 ,
314
- private_key_password = private_key_password , scopes = scopes )
381
+ private_key_password = private_key_password , scopes = scopes ,
382
+ token_uri = token_uri , revoke_uri = revoke_uri )
315
383
316
384
def _generate_assertion (self ):
317
385
"""Generate the assertion that will be used in the request."""
@@ -508,6 +576,8 @@ def __init__(self,
508
576
private_key_id = None ,
509
577
client_id = None ,
510
578
user_agent = None ,
579
+ token_uri = GOOGLE_TOKEN_URI ,
580
+ revoke_uri = GOOGLE_REVOKE_URI ,
511
581
additional_claims = None ):
512
582
if additional_claims is None :
513
583
additional_claims = {}
@@ -517,6 +587,8 @@ def __init__(self,
517
587
private_key_id = private_key_id ,
518
588
client_id = client_id ,
519
589
user_agent = user_agent ,
590
+ token_uri = token_uri ,
591
+ revoke_uri = revoke_uri ,
520
592
** additional_claims )
521
593
522
594
def authorize (self , http ):
@@ -595,17 +667,18 @@ def create_scoped_required(self):
595
667
# JWTAccessCredentials are unscoped by definition
596
668
return True
597
669
598
- def create_scoped (self , scopes ):
670
+ def create_scoped (self , scopes , token_uri = GOOGLE_TOKEN_URI ,
671
+ revoke_uri = GOOGLE_REVOKE_URI ):
599
672
# Returns an OAuth2 credentials with the given scope
600
673
result = ServiceAccountCredentials (self ._service_account_email ,
601
674
self ._signer ,
602
675
scopes = scopes ,
603
676
private_key_id = self ._private_key_id ,
604
677
client_id = self .client_id ,
605
678
user_agent = self ._user_agent ,
679
+ token_uri = token_uri ,
680
+ revoke_uri = revoke_uri ,
606
681
** self ._kwargs )
607
- result .token_uri = self .token_uri
608
- result .revoke_uri = self .revoke_uri
609
682
if self ._private_key_pkcs8_pem is not None :
610
683
result ._private_key_pkcs8_pem = self ._private_key_pkcs8_pem
611
684
if self ._private_key_pkcs12 is not None :
0 commit comments