-
Notifications
You must be signed in to change notification settings - Fork 442
/
Copy pathOAuth2.gs
1348 lines (1246 loc) · 42.3 KB
/
OAuth2.gs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(function (host, expose) {
var module = { exports: {} };
var exports = module.exports;
/****** code begin *********/
// Copyright 2014 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @file Contains the methods exposed by the library, and performs
* any required setup.
*/
/**
* The supported formats for the returned OAuth2 token.
* @enum {string}
*/
var TOKEN_FORMAT = {
/** JSON format, for example <code>{"access_token": "..."}</code> **/
JSON: 'application/json',
/** Form URL-encoded, for example <code>access_token=...</code> **/
FORM_URL_ENCODED: 'application/x-www-form-urlencoded'
};
var STORAGE_PREFIX_ = 'oauth2.';
/**
* Creates a new OAuth2 service with the name specified. It's usually best to
* create and configure your service once at the start of your script, and then
* reference them during the different phases of the authorization flow.
* @param {string} serviceName The name of the service.
* @return {Service_} The service object.
*/
function createService(serviceName) {
return new Service_(serviceName);
}
/**
* Returns the redirect URI that will be used for a given script. Often this URI
* needs to be entered into a configuration screen of your OAuth provider.
* @param {string=} optScriptId The script ID of your script, which can be
* found in the Script Editor UI under "File > Project properties". Defaults
* to the script ID of the script being executed.
* @return {string} The redirect URI.
*/
function getRedirectUri(optScriptId) {
var scriptId = optScriptId || ScriptApp.getScriptId();
return 'https://script.google.com/macros/d/' + encodeURIComponent(scriptId) +
'/usercallback';
}
/**
* Gets the list of services with tokens stored in the given property store.
* This is useful if you connect to the same API with multiple accounts and
* need to keep track of them. If no stored tokens are found this will return
* an empty array.
* @param {PropertiesService.Properties} propertyStore The properties to check.
* @return {Array.<string>} The service names.
*/
function getServiceNames(propertyStore) {
var props = propertyStore.getProperties();
return Object.keys(props).filter(function(key) {
var parts = key.split('.');
return key.indexOf(STORAGE_PREFIX_) == 0 && parts.length > 1 && parts[1];
}).map(function(key) {
return key.split('.')[1];
}).reduce(function(result, key) {
if (result.indexOf(key) < 0) {
result.push(key);
}
return result;
}, []);
}
if (typeof module === 'object') {
module.exports = {
createService: createService,
getRedirectUri: getRedirectUri,
getServiceNames: getServiceNames,
TOKEN_FORMAT: TOKEN_FORMAT
};
}
// Copyright 2014 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @file Contains the Service_ class.
*/
// Disable JSHint warnings for the use of eval(), since it's required to prevent
// scope issues in Apps Script.
// jshint evil:true
/**
* Creates a new OAuth2 service.
* @param {string} serviceName The name of the service.
* @constructor
*/
var Service_ = function(serviceName) {
validate_({
'Service name': serviceName
});
this.serviceName_ = serviceName;
this.params_ = {};
this.tokenFormat_ = TOKEN_FORMAT.JSON;
this.tokenHeaders_ = null;
this.tokenMethod_ = 'post';
this.expirationMinutes_ = 60;
};
/**
* The number of seconds before a token actually expires to consider it expired
* and refresh it.
* @type {number}
* @private
*/
Service_.EXPIRATION_BUFFER_SECONDS_ = 60;
/**
* The number of milliseconds that a token should remain in the cache.
* @type {number}
* @private
*/
Service_.LOCK_EXPIRATION_MILLISECONDS_ = 30 * 1000;
/**
* Sets the service's authorization base URL (required). For Google services
* this URL should be
* https://accounts.google.com/o/oauth2/auth.
* @param {string} authorizationBaseUrl The authorization endpoint base URL.
* @return {!Service_} This service, for chaining.
*/
Service_.prototype.setAuthorizationBaseUrl = function(authorizationBaseUrl) {
this.authorizationBaseUrl_ = authorizationBaseUrl;
return this;
};
/**
* Sets the service's token URL (required). For Google services this URL should
* be https://accounts.google.com/o/oauth2/token.
* @param {string} tokenUrl The token endpoint URL.
* @return {!Service_} This service, for chaining.
*/
Service_.prototype.setTokenUrl = function(tokenUrl) {
this.tokenUrl_ = tokenUrl;
return this;
};
/**
* Sets the service's refresh URL. Some OAuth providers require a different URL
* to be used when generating access tokens from a refresh token.
* @param {string} refreshUrl The refresh endpoint URL.
* @return {!Service_} This service, for chaining.
*/
Service_.prototype.setRefreshUrl = function(refreshUrl) {
this.refreshUrl_ = refreshUrl;
return this;
};
/**
* Sets the format of the returned token. Default: OAuth2.TOKEN_FORMAT.JSON.
* @param {OAuth2.TOKEN_FORMAT} tokenFormat The format of the returned token.
* @return {!Service_} This service, for chaining.
*/
Service_.prototype.setTokenFormat = function(tokenFormat) {
this.tokenFormat_ = tokenFormat;
return this;
};
/**
* Sets the additional HTTP headers that should be sent when retrieving or
* refreshing the access token.
* @param {Object.<string,string>} tokenHeaders A map of header names to values.
* @return {!Service_} This service, for chaining.
*/
Service_.prototype.setTokenHeaders = function(tokenHeaders) {
this.tokenHeaders_ = tokenHeaders;
return this;
};
/**
* Sets the HTTP method to use when retrieving or refreshing the access token.
* Default: "post".
* @param {string} tokenMethod The HTTP method to use.
* @return {!Service_} This service, for chaining.
*/
Service_.prototype.setTokenMethod = function(tokenMethod) {
this.tokenMethod_ = tokenMethod;
return this;
};
/**
* Set the code verifier used for PKCE. For most use cases,
* prefer `generateCodeVerifier` to automatically initialize the
* value with a generated challenge string.
*
* @param {string} codeVerifier A random challenge string
* @return {!Service_} This service, for chaining
*/
Service_.prototype.setCodeVerififer = function(codeVerifier) {
this.codeVerifier_ = codeVerifier;
if (!this.codeChallengeMethod_) {
this.codeChallengeMethod_ = 'S256';
}
return this;
};
/**
* Sets the code verifier to a randomly generated challenge string.
* @return {!Service_} This service, for chaining
*/
Service_.prototype.generateCodeVerifier = function() {
const rawBytes = [];
for (var i = 0; i < 32; ++i) {
const r = Math.floor(Math.random() * 255);
rawBytes[i] = r;
}
const verifier = encodeUrlSafeBase64NoPadding_(rawBytes);
return this.setCodeVerififer(verifier);
};
/**
* Set the code challenge method for PKCE. The default value method
* when a code verifier is set is S256.
* @param {string} method Challenge method, either "S256" or "plain"
* @return {!Service_} This service, for chaining
*/
Service_.prototype.setCodeChallengeMethod = function(method) {
this.codeChallengeMethod_ = method;
return this;
};
/**
* @callback tokenHandler
* @param tokenPayload {Object} A hash of parameters to be sent to the token
* URL.
* @param tokenPayload.code {string} The authorization code.
* @param tokenPayload.client_id {string} The client ID.
* @param tokenPayload.client_secret {string} The client secret.
* @param tokenPayload.redirect_uri {string} The redirect URI.
* @param tokenPayload.grant_type {string} The type of grant requested.
* @returns {Object} A modified hash of parameters to be sent to the token URL.
*/
/**
* Sets an additional function to invoke on the payload of the access token
* request.
* @param {tokenHandler} tokenHandler tokenHandler A function to invoke on the
* payload of the request for an access token.
* @return {!Service_} This service, for chaining.
*/
Service_.prototype.setTokenPayloadHandler = function(tokenHandler) {
this.tokenPayloadHandler_ = tokenHandler;
return this;
};
/**
* Sets the name of the authorization callback function (required). This is the
* function that will be called when the user completes the authorization flow
* on the service provider's website. The callback accepts a request parameter,
* which should be passed to this service's <code>handleCallback()</code> method
* to complete the process.
* @param {string} callbackFunctionName The name of the callback function.
* @return {!Service_} This service, for chaining.
*/
Service_.prototype.setCallbackFunction = function(callbackFunctionName) {
this.callbackFunctionName_ = callbackFunctionName;
return this;
};
/**
* Sets the client ID to use for the OAuth flow (required). You can create
* client IDs in the "Credentials" section of a Google Developers Console
* project. Although you can use any project with this library, it may be
* convinient to use the project that was created for your script. These
* projects are not visible if you visit the console directly, but you can
* access it by click on the menu item "Resources > Advanced Google services" in
* the Script Editor, and then click on the link "Google Developers Console" in
* the resulting dialog.
* @param {string} clientId The client ID to use for the OAuth flow.
* @return {!Service_} This service, for chaining.
*/
Service_.prototype.setClientId = function(clientId) {
this.clientId_ = clientId;
return this;
};
/**
* Sets the client secret to use for the OAuth flow (required). See the
* documentation for <code>setClientId()</code> for more information on how to
* create client IDs and secrets.
* @param {string} clientSecret The client secret to use for the OAuth flow.
* @return {!Service_} This service, for chaining.
*/
Service_.prototype.setClientSecret = function(clientSecret) {
this.clientSecret_ = clientSecret;
return this;
};
/**
* Sets the property store to use when persisting credentials (required). In
* most cases this should be user properties, but document or script properties
* may be appropriate if you want to share access across users.
* @param {PropertiesService.Properties} propertyStore The property store to use
* when persisting credentials.
* @return {!Service_} This service, for chaining.
* @see https://developers.google.com/apps-script/reference/properties/
*/
Service_.prototype.setPropertyStore = function(propertyStore) {
this.propertyStore_ = propertyStore;
return this;
};
/**
* Sets the cache to use when persisting credentials (optional). Using a cache
* will reduce the need to read from the property store and may increase
* performance. In most cases this should be a private cache, but a public cache
* may be appropriate if you want to share access across users.
* @param {CacheService.Cache} cache The cache to use when persisting
* credentials.
* @return {!Service_} This service, for chaining.
* @see https://developers.google.com/apps-script/reference/cache/
*/
Service_.prototype.setCache = function(cache) {
this.cache_ = cache;
return this;
};
/**
* Sets the lock to use when checking and refreshing credentials (optional).
* Using a lock will ensure that only one execution will be able to access the
* stored credentials at a time. This can prevent race conditions that arise
* when two executions attempt to refresh an expired token.
* @param {LockService.Lock} lock The lock to use when accessing credentials.
* @return {!Service_} This service, for chaining.
* @see https://developers.google.com/apps-script/reference/lock/
*/
Service_.prototype.setLock = function(lock) {
this.lock_ = lock;
return this;
};
/**
* Sets the scope or scopes to request during the authorization flow (optional).
* If the scope value is an array it will be joined using the separator before
* being sent to the server, which is is a space character by default.
* @param {string|Array.<string>} scope The scope or scopes to request.
* @param {string} [optSeparator] The optional separator to use when joining
* multiple scopes. Default: space.
* @return {!Service_} This service, for chaining.
*/
Service_.prototype.setScope = function(scope, optSeparator) {
var separator = optSeparator || ' ';
this.params_.scope = Array.isArray(scope) ? scope.join(separator) : scope;
return this;
};
/**
* Sets an additional parameter to use when constructing the authorization URL
* (optional). See the documentation for your service provider for information
* on what parameter values they support.
* @param {string} name The parameter name.
* @param {string} value The parameter value.
* @return {!Service_} This service, for chaining.
*/
Service_.prototype.setParam = function(name, value) {
this.params_[name] = value;
return this;
};
/**
* Sets the private key to use for Service Account authorization.
* @param {string} privateKey The private key.
* @return {!Service_} This service, for chaining.
*/
Service_.prototype.setPrivateKey = function(privateKey) {
this.privateKey_ = privateKey;
return this;
};
/**
* Sets the issuer (iss) value to use for Service Account authorization.
* If not set the client ID will be used instead.
* @param {string} issuer This issuer value
* @return {!Service_} This service, for chaining.
*/
Service_.prototype.setIssuer = function(issuer) {
this.issuer_ = issuer;
return this;
};
/**
* Sets additional JWT claims to use for Service Account authorization.
* @param {Object.<string,string>} additionalClaims The additional claims, as
* key-value pairs.
* @return {!Service_} This service, for chaining.
*/
Service_.prototype.setAdditionalClaims = function(additionalClaims) {
this.additionalClaims_ = additionalClaims;
return this;
};
/**
* Sets the subject (sub) value to use for Service Account authorization.
* @param {string} subject This subject value
* @return {!Service_} This service, for chaining.
*/
Service_.prototype.setSubject = function(subject) {
this.subject_ = subject;
return this;
};
/**
* Sets number of minutes that a token obtained through Service Account
* authorization should be valid. Default: 60 minutes.
* @param {string} expirationMinutes The expiration duration in minutes.
* @return {!Service_} This service, for chaining.
*/
Service_.prototype.setExpirationMinutes = function(expirationMinutes) {
this.expirationMinutes_ = expirationMinutes;
return this;
};
/**
* Sets the OAuth2 grant_type to use when obtaining an access token. This does
* not need to be set when using either the authorization code flow (AKA
* 3-legged OAuth) or the service account flow. The most common usage is to set
* it to "client_credentials" and then also set the token headers to include
* the Authorization header required by the OAuth2 provider.
* @param {string} grantType The OAuth2 grant_type value.
* @return {!Service_} This service, for chaining.
*/
Service_.prototype.setGrantType = function(grantType) {
this.grantType_ = grantType;
return this;
};
/**
* Sets the URI to redirect to when the OAuth flow has completed. By default the
* library will provide this value automatically, but in some rare cases you may
* need to override it.
* @param {string} redirectUri The redirect URI.
* @return {!Service_} This service, for chaining.
*/
Service_.prototype.setRedirectUri = function(redirectUri) {
this.redirectUri_ = redirectUri;
return this;
};
/**
* Returns the redirect URI that will be used for this service. Often this URI
* needs to be entered into a configuration screen of your OAuth provider.
* @return {string} The redirect URI.
*/
Service_.prototype.getRedirectUri = function() {
return this.redirectUri_ || getRedirectUri();
};
/**
* Gets the authorization URL. The first step in getting an OAuth2 token is to
* have the user visit this URL and approve the authorization request. The
* user will then be redirected back to your application using callback function
* name specified, so that the flow may continue.
* @param {Object} optAdditionalParameters Additional parameters that should be
* stored in the state token and made available in the callback function.
* @return {string} The authorization URL.
*/
Service_.prototype.getAuthorizationUrl = function(optAdditionalParameters) {
validate_({
'Client ID': this.clientId_,
'Callback function name': this.callbackFunctionName_,
'Authorization base URL': this.authorizationBaseUrl_
});
var stateTokenBuilder = eval('Script' + 'App').newStateToken()
.withMethod(this.callbackFunctionName_)
.withArgument('serviceName', this.serviceName_)
.withTimeout(3600);
if (this.codeVerifier_) {
stateTokenBuilder.withArgument('codeVerifier_', this.codeVerifier_);
}
if (optAdditionalParameters) {
Object.keys(optAdditionalParameters).forEach(function(key) {
stateTokenBuilder.withArgument(key, optAdditionalParameters[key]);
});
}
var params = {
client_id: this.clientId_,
response_type: 'code',
redirect_uri: this.getRedirectUri(),
state: stateTokenBuilder.createToken()
};
if (this.codeVerifier_) {
params['code_challenge'] = encodeChallenge_(this.codeChallengeMethod_,
this.codeVerifier_);
params['code_challenge_method'] = this.codeChallengeMethod_;
}
params = extend_(params, this.params_);
return buildUrl_(this.authorizationBaseUrl_, params);
};
/**
* Completes the OAuth2 flow using the request data passed in to the callback
* function.
* @param {Object} callbackRequest The request data recieved from the callback
* function.
* @return {boolean} True if authorization was granted, false if it was denied.
*/
Service_.prototype.handleCallback = function(callbackRequest) {
var code = callbackRequest.parameter.code;
var error = callbackRequest.parameter.error;
if (error) {
if (error == 'access_denied') {
return false;
} else {
throw new Error('Error authorizing token: ' + error);
}
}
validate_({
'Client ID': this.clientId_,
'Token URL': this.tokenUrl_
});
var payload = {
code: code,
client_id: this.clientId_,
client_secret: this.clientSecret_,
redirect_uri: this.getRedirectUri(),
grant_type: 'authorization_code'
};
if (callbackRequest.parameter.codeVerifier_) {
payload['code_verifier'] = callbackRequest.parameter.codeVerifier_;
}
var token = this.fetchToken_(payload);
this.saveToken_(token);
return true;
};
/**
* Determines if the service has access (has been authorized and hasn't
* expired). If offline access was granted and the previous token has expired
* this method attempts to generate a new token.
* @return {boolean} true if the user has access to the service, false
* otherwise.
*/
Service_.prototype.hasAccess = function() {
var token = this.getToken();
if (token && !this.isExpired_(token)) return true; // Token still has access.
var canGetToken = (token && this.canRefresh_(token)) ||
this.privateKey_ || this.grantType_;
if (!canGetToken) return false;
return this.lockable_(function() {
// Get the token again, bypassing the local memory cache.
token = this.getToken(true);
// Check to see if the token is no longer missing or expired, as another
// execution may have refreshed it while we were waiting for the lock.
if (token && !this.isExpired_(token)) return true; // Token now has access.
try {
if (token && this.canRefresh_(token)) {
this.refresh();
return true;
} else if (this.privateKey_) {
this.exchangeJwt_();
return true;
} else if (this.grantType_) {
this.exchangeGrant_();
return true;
} else {
// This should never happen, since canGetToken should have been false
// earlier.
return false;
}
} catch (e) {
this.lastError_ = e;
return false;
}
});
};
/**
* Gets an access token for this service. This token can be used in HTTP
* requests to the service's endpoint. This method will throw an error if the
* user's access was not granted or has expired.
* @return {string} An access token.
*/
Service_.prototype.getAccessToken = function() {
if (!this.hasAccess()) {
throw new Error('Access not granted or expired.');
}
var token = this.getToken();
return token.access_token;
};
/**
* Gets an id token for this service. This token can be used in HTTP
* requests to the service's endpoint. This method will throw an error if the
* user's access was not granted or has expired.
* @return {string} An id token.
*/
Service_.prototype.getIdToken = function() {
if (!this.hasAccess()) {
throw new Error('Access not granted or expired.');
}
var token = this.getToken();
return token.id_token;
};
/**
* Resets the service, removing access and requiring the service to be
* re-authorized. Also removes any additional values stored in the service's
* storage.
*/
Service_.prototype.reset = function() {
this.getStorage().reset();
};
/**
* Gets the last error that occurred this execution when trying to automatically
* refresh or generate an access token.
* @return {Exception} An error, if any.
*/
Service_.prototype.getLastError = function() {
return this.lastError_;
};
/**
* Fetches a new token from the OAuth server.
* @param {Object} payload The token request payload.
* @param {string} [optUrl] The URL of the token endpoint.
* @return {Object} The parsed token.
*/
Service_.prototype.fetchToken_ = function(payload, optUrl) {
// Use the configured token URL unless one is specified.
var url = optUrl || this.tokenUrl_;
var headers = {
'Accept': this.tokenFormat_
};
if (this.tokenHeaders_) {
headers = extend_(headers, this.tokenHeaders_);
}
if (this.tokenPayloadHandler_) {
payload = this.tokenPayloadHandler_(payload);
}
var response = UrlFetchApp.fetch(url, {
method: this.tokenMethod_,
headers: headers,
payload: payload,
muteHttpExceptions: true
});
return this.getTokenFromResponse_(response);
};
/**
* Gets the token from a UrlFetchApp response.
* @param {UrlFetchApp.HTTPResponse} response The response object.
* @return {!Object} The parsed token.
* @throws If the token cannot be parsed or the response contained an error.
* @private
*/
Service_.prototype.getTokenFromResponse_ = function(response) {
var token = this.parseToken_(response.getContentText());
var resCode = response.getResponseCode();
if ( resCode < 200 || resCode >= 300 || token.error) {
var reason = [
token.error,
token.message,
token.error_description,
token.error_uri
].filter(Boolean).map(function(part) {
return typeof(part) == 'string' ? part : JSON.stringify(part);
}).join(', ');
if (!reason) {
reason = resCode + ': ' + JSON.stringify(token);
}
throw new Error('Error retrieving token: ' + reason);
}
return token;
};
/**
* Parses the token using the service's token format.
* @param {string} content The serialized token content.
* @return {!Object} The parsed token.
* @private
*/
Service_.prototype.parseToken_ = function(content) {
var token;
if (this.tokenFormat_ == TOKEN_FORMAT.JSON) {
try {
token = JSON.parse(content);
} catch (e) {
throw new Error('Token response not valid JSON: ' + e);
}
} else if (this.tokenFormat_ == TOKEN_FORMAT.FORM_URL_ENCODED) {
token = content.split('&').reduce(function(result, pair) {
var parts = pair.split('=');
result[decodeURIComponent(parts[0])] = decodeURIComponent(parts[1]);
return result;
}, {});
} else {
throw new Error('Unknown token format: ' + this.tokenFormat_);
}
this.ensureExpiresAtSet_(token);
return token;
};
/**
* Adds expiresAt annotations on the token if not set.
* @param {string} token A token.
* @private
*/
Service_.prototype.ensureExpiresAtSet_ = function(token) {
// handle prior migrations
if (token.expiresAt !== undefined) {
return;
}
// granted_time was added in prior versions of this library
var grantedTime = token.granted_time || getTimeInSeconds_(new Date());
var expiresIn = token.expires_in_sec || token.expires_in || token.expires;
if (expiresIn) {
var expiresAt = grantedTime + Number(expiresIn);
token.expiresAt = expiresAt;
}
var refreshTokenExpiresIn = token.refresh_token_expires_in;
if (refreshTokenExpiresIn) {
var refreshTokenExpiresAt = grantedTime + Number(refreshTokenExpiresIn);
token.refreshTokenExpiresAt = refreshTokenExpiresAt;
}
};
/**
* Refreshes a token that has expired. This is only possible if offline access
* was requested when the token was authorized.
*/
Service_.prototype.refresh = function() {
validate_({
'Client ID': this.clientId_,
'Token URL': this.tokenUrl_
});
this.lockable_(function() {
var token = this.getToken();
if (!token.refresh_token) {
throw new Error('Offline access is required.');
}
var payload = {
refresh_token: token.refresh_token,
client_id: this.clientId_,
client_secret: this.clientSecret_,
grant_type: 'refresh_token',
};
var newToken = this.fetchToken_(payload, this.refreshUrl_);
if (!newToken.refresh_token) {
newToken.refresh_token = token.refresh_token;
}
this.ensureExpiresAtSet_(token);
// Propagate refresh token expiry if new token omits it
if (newToken.refreshTokenExpiresAt === undefined) {
newToken.refreshTokenExpiresAt = token.refreshTokenExpiresAt;
}
this.saveToken_(newToken);
});
};
/**
* Gets the storage layer for this service, used to persist tokens.
* Custom values associated with the service can be stored here as well.
* The key <code>null</code> is used to to store the token and should not
* be used.
* @return {Storage_} The service's storage.
*/
Service_.prototype.getStorage = function() {
if (!this.storage_) {
var prefix = STORAGE_PREFIX_ + this.serviceName_;
this.storage_ = new Storage_(prefix, this.propertyStore_, this.cache_);
}
return this.storage_;
};
/**
* Saves a token to the service's property store and cache.
* @param {Object} token The token to save.
* @private
*/
Service_.prototype.saveToken_ = function(token) {
this.getStorage().setValue(null, token);
};
/**
* Gets the token from the service's property store or cache.
* @param {boolean?} optSkipMemoryCheck If true, bypass the local memory cache
* when fetching the token.
* @return {Object} The token, or null if no token was found.
*/
Service_.prototype.getToken = function(optSkipMemoryCheck) {
// Gets the stored value under the null key, which is reserved for the token.
return this.getStorage().getValue(null, optSkipMemoryCheck);
};
/**
* Determines if a retrieved token is still valid. This will return false if
* either the authorization token or the ID token has expired.
* @param {Object} token The token to validate.
* @return {boolean} True if it has expired, false otherwise.
* @private
*/
Service_.prototype.isExpired_ = function(token) {
var expired = false;
var now = getTimeInSeconds_(new Date());
// Check the authorization token's expiration.
if (token.expiresAt) {
if (token.expiresAt - now < Service_.EXPIRATION_BUFFER_SECONDS_) {
expired = true;
}
}
// Previous code path, provided for migration purpose, can be removed later
var expiresIn = token.expires_in_sec || token.expires_in || token.expires;
if (expiresIn) {
var expiresTime = token.granted_time + Number(expiresIn);
if (expiresTime - now < Service_.EXPIRATION_BUFFER_SECONDS_) {
expired = true;
}
}
// Check the ID token's expiration, if it exists.
if (token.id_token) {
var payload = decodeJwt_(token.id_token);
if (payload.exp &&
payload.exp - now < Service_.EXPIRATION_BUFFER_SECONDS_) {
expired = true;
}
}
return expired;
};
/**
* Determines if a retrieved token can be refreshed.
* @param {Object} token The token to inspect.
* @return {boolean} True if it can be refreshed, false otherwise.
* @private
*/
Service_.prototype.canRefresh_ = function(token) {
if (!token.refresh_token) return false;
this.ensureExpiresAtSet_(token);
if (token.refreshTokenExpiresAt === undefined) {
return true;
} else {
var now = getTimeInSeconds_(new Date());
return (
token.refreshTokenExpiresAt - now > Service_.EXPIRATION_BUFFER_SECONDS_
);
}
};
/**
* Uses the service account flow to exchange a signed JSON Web Token (JWT) for
* an access token.
* @private
*/
Service_.prototype.exchangeJwt_ = function() {
validate_({
'Token URL': this.tokenUrl_
});
var jwt = this.createJwt_();
var payload = {
assertion: jwt,
grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer'
};
var token = this.fetchToken_(payload);
this.saveToken_(token);
};
/**
* Creates a signed JSON Web Token (JWT) for use with Service Account
* authorization.
* @return {string} The signed JWT.
* @private
*/
Service_.prototype.createJwt_ = function() {
validate_({
'Private key': this.privateKey_,
'Token URL': this.tokenUrl_,
'Issuer or Client ID': this.issuer_ || this.clientId_
});
var now = new Date();
var expires = new Date(now.getTime());
expires.setMinutes(expires.getMinutes() + this.expirationMinutes_);
var claimSet = {
iss: this.issuer_ || this.clientId_,
aud: this.tokenUrl_,
exp: Math.round(expires.getTime() / 1000),
iat: Math.round(now.getTime() / 1000)
};
if (this.subject_) {
claimSet.sub = this.subject_;
}
if (this.params_.scope) {
claimSet.scope = this.params_.scope;
}
if (this.additionalClaims_) {
var additionalClaims = this.additionalClaims_;
Object.keys(additionalClaims).forEach(function(key) {
claimSet[key] = additionalClaims[key];
});
}
return encodeJwt_(claimSet, this.privateKey_);
};
/**
* Locks access to a block of code if a lock has been set on this service.
* @param {function} func The code to execute.
* @return {*} The result of the code block.
* @private
*/
Service_.prototype.lockable_ = function(func) {
var releaseLock = false;
if (this.lock_ && !this.lock_.hasLock()) {
this.lock_.waitLock(Service_.LOCK_EXPIRATION_MILLISECONDS_);
releaseLock = true;
}
var result = func.apply(this);
if (this.lock_ && releaseLock) {
this.lock_.releaseLock();
}
return result;
};
/**
* Obtain an access token using the custom grant type specified. Most often
* this will be "client_credentials", and a client ID and secret are set an
* "Authorization: Basic ..." header will be added using those values.
*/
Service_.prototype.exchangeGrant_ = function() {
validate_({
'Grant Type': this.grantType_,
'Token URL': this.tokenUrl_
});
var payload = {
grant_type: this.grantType_
};
payload = extend_(payload, this.params_);
// For the client_credentials grant type, add a basic authorization header:
// - If the client ID and client secret are set.
// - No authorization header has been set yet.
var lowerCaseHeaders = toLowerCaseKeys_(this.tokenHeaders_);
if (this.grantType_ === 'client_credentials' &&
this.clientId_ &&
this.clientSecret_ &&
(!lowerCaseHeaders || !lowerCaseHeaders.authorization)) {
this.tokenHeaders_ = this.tokenHeaders_ || {};
this.tokenHeaders_.authorization = 'Basic ' +
Utilities.base64Encode(this.clientId_ + ':' + this.clientSecret_);
}
var token = this.fetchToken_(payload);
this.saveToken_(token);
};
// Copyright 2017 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.