-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathOAuth1.gs
1120 lines (1007 loc) · 33.3 KB
/
OAuth1.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 *********/
/**
* Creates a new MemoryProperties, an implementation of the Properties
* interface that stores values in memory.
* @constructor
*/
var MemoryProperties = function() {
this.properties = {};
};
/**
* @see {@link https://developers.google.com/apps-script/reference/properties/properties#deleteallproperties}
*/
MemoryProperties.prototype.deleteAllProperties = function() {
this.properties = {};
};
/**
* @see {@link https://developers.google.com/apps-script/reference/properties/properties#deletepropertykey}
*/
MemoryProperties.prototype.deleteProperty = function(key) {
delete this.properties[key];
};
/**
* @see {@link https://developers.google.com/apps-script/reference/properties/properties#getkeys}
*/
MemoryProperties.prototype.getKeys = function() {
return Object.keys(this.properties);
};
/**
* @see {@link https://developers.google.com/apps-script/reference/properties/properties#getproperties}
*/
MemoryProperties.prototype.getProperties = function() {
return extend_({}, this.properties);
};
/**
* @see {@link https://developers.google.com/apps-script/reference/properties/properties#getproperty}
*/
MemoryProperties.prototype.getProperty = function(key) {
return this.properties[key];
};
/**
* @see {@link https://developers.google.com/apps-script/reference/properties/properties#setpropertiesproperties-deleteallothers}
*/
MemoryProperties.prototype.setProperties = function(properties, opt_deleteAllOthers) {
if (opt_deleteAllOthers) {
this.deleteAllProperties();
}
Object.keys(properties).forEach(function(key) {
this.setProperty(key, properties[key]);
});
};
/**
* @see {@link https://developers.google.com/apps-script/reference/properties/properties#setpropertykey-value}
*/
MemoryProperties.prototype.setProperty = function(key, value) {
this.properties[key] = String(value);
};
// Copyright 2015 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.
/**
* @fileoverview Contains the methods exposed by the library, and performs any
* required setup.
*/
/**
* Creates a new OAuth1 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 it 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 callback URL that will be used for a given script. Often this URL
* needs to be entered into a configuration screen of your OAuth provider.
* @param {string} scriptId The ID of your script, which can be found in the
* Script Editor UI under "File > Project properties".
* @return {string} The callback URL.
*/
function getCallbackUrl(scriptId) {
return Utilities.formatString(
'https://script.google.com/macros/d/%s/usercallback', scriptId);
}
if (typeof module != 'undefined') {
module.exports = {
createService: createService,
getCallbackUrl: getCallbackUrl,
MemoryProperties: MemoryProperties
};
}
// Copyright 2015 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.
/**
* @fileoverview 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 OAuth1 service.
* @param {string} serviceName The name of the service.
* @constructor
*/
var Service_ = function(serviceName) {
validate_({
'Service name': serviceName
});
this.serviceName_ = serviceName;
this.paramLocation_ = 'auth-header';
this.method_ = 'get';
this.oauthVersion_ = '1.0a';
this.scriptId_ = eval('Script' + 'App').getScriptId();
this.signatureMethod_ = 'HMAC-SHA1';
this.propertyStore_ = new MemoryProperties();
};
/**
* The maximum amount of time that information can be cached.
* @type {Number}
*/
Service_.MAX_CACHE_TIME = 21600;
/**
* Sets the request URL for the OAuth service (required).
* @param {string} url The URL given by the OAuth service provider for obtaining
* a request token.
* @return {Service_} This service, for chaining.
*/
Service_.prototype.setRequestTokenUrl = function(url) {
this.requestTokenUrl_ = url;
return this;
};
/**
* Sets the URL for the OAuth authorization service (required).
* @param {string} url The URL given by the OAuth service provider for
* authorizing a token.
* @return {Service_} This service, for chaining.
*/
Service_.prototype.setAuthorizationUrl = function(url) {
this.authorizationUrl_ = url;
return this;
};
/**
* Sets the URL to get an OAuth access token from (required).
* @param {string} url The URL given by the OAuth service provider for obtaining
* an access token.
* @return {Service_} This service, for chaining.
*/
Service_.prototype.setAccessTokenUrl = function(url) {
this.accessTokenUrl_ = url;
return this;
};
/**
* Sets the parameter location in OAuth protocol requests (optional). The
* default parameter location is 'auth-header'.
* @param {string} location The parameter location for the OAuth request.
* Allowed values are 'post-body', 'uri-query' and 'auth-header'.
* @return {Service_} This service, for chaining.
*/
Service_.prototype.setParamLocation = function(location) {
this.paramLocation_ = location;
return this;
};
/**
* Sets the HTTP method used to complete the OAuth protocol (optional). The
* default method is 'get'.
* @param {string} method The method to be used with this service. Allowed
* values are 'get' and 'post'.
* @return {Service_} This service, for chaining.
*/
Service_.prototype.setMethod = function(method) {
this.method_ = method;
return this;
};
/**
* Sets the OAuth realm parameter to be used with this service (optional).
* @param {string} realm The realm to be used with this service.
* @return {Service_} This service, for chaining.
*/
Service_.prototype.setRealm = function(realm) {
this.realm_ = realm;
return this;
};
/**
* Sets the OAuth signature method to use. 'HMAC-SHA1' is the default.
* @param {string} signatureMethod The OAuth signature method. Allowed values
* are 'HMAC-SHA1', 'RSA-SHA1' and 'PLAINTEXT'.
* @return {Service_} This service, for chaining.
*/
Service_.prototype.setSignatureMethod = function(signatureMethod) {
this.signatureMethod_ = signatureMethod;
return this;
};
/**
* Sets the specific OAuth version to use. The default is '1.0a'.
* @param {string} oauthVersion The OAuth version. Allowed values are '1.0a'
* and '1.0'.
* @return {Service_} This service, for chaining.
*/
Service_.prototype.setOAuthVersion = function(oauthVersion) {
this.oauthVersion_ = oauthVersion;
return this;
};
/**
* Sets the ID of the script that contains the authorization callback
* function (required). The script ID can be found in the Script Editor UI
* under "File > Project properties".
* @param {string} scriptId The ID of the script containing the callback
* function.
* @return {Service_} This service, for chaining.
* @deprecated The script ID is now be determined automatically.
*/
Service_.prototype.setScriptId = function(scriptId) {
this.scriptId_ = scriptId;
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 consumer key, which is provided when you register with an OAuth
* service (required).
* @param {string} consumerKey The consumer key provided by the OAuth service
* provider.
* @return {Service_} This service, for chaining.
*/
Service_.prototype.setConsumerKey = function(consumerKey) {
this.consumerKey_ = consumerKey;
return this;
};
/**
* Sets the consumer secret, which is provided when you register with an OAuth
* service (required).
* @param {string} consumerSecret The consumer secret provided by the OAuth
* service provider.
* @return {Service_} This service, for chaining.
*/
Service_.prototype.setConsumerSecret = function(consumerSecret) {
this.consumerSecret_ = consumerSecret;
return this;
};
/**
* Sets the property store to use when persisting credentials (optional). In
* most cases this should be user properties, but document or script properties
* may be appropriate if you want to share access across users. If not set tokens
* will be stored in memory only.
* @param {PropertiesService.Properties} propertyStore The property store to use
* when persisting credentials.
* @return {Service_} This service, for chaining.
*/
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.
*/
Service_.prototype.setCache = function(cache) {
this.cache_ = cache;
return this;
};
/**
* Sets the access token and token secret to use (optional). For use with APIs
* that support a 1-legged flow where no user interaction is required.
* @param {string} token The access token.
* @param {string} secret The token secret.
* @return {Service_} This service, for chaining.
*/
Service_.prototype.setAccessToken = function(token, secret) {
this.saveToken_({
public: token,
secret: secret,
type: 'access'
});
return this;
};
/**
* Starts the authorization process. A new token will be generated and the
* authorization URL for that token will be returned. Have the user visit this
* URL and approve the authorization request. The user will then be redirected
* back to your application using the script ID and callback function name
* specified, so that the flow may continue.
* @returns {string} The authorization URL for a new token.
*/
Service_.prototype.authorize = function() {
validate_({
'Authorization URL': this.authorizationUrl_
});
var token = this.getRequestToken_();
this.saveToken_(token);
var oauthParams = {
oauth_token: token.public
};
if (this.oauthVersion_ == '1.0') {
oauthParams.oauth_callback = this.getCallbackUrl();
}
return buildUrl_(this.authorizationUrl_, oauthParams);
};
/**
* Completes the OAuth1 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 requestToken = callbackRequest.parameter.oauth_token;
var verifier = callbackRequest.parameter.oauth_verifier;
var token = this.getToken_();
if (!token || (requestToken && requestToken != token.public)) {
throw 'Error handling callback: token mismatch';
}
if (this.oauthVersion_ == '1.0a' && !verifier) {
return false;
}
token = this.getAccessToken_(verifier);
this.saveToken_(token);
return true;
};
/**
* Determines if the service has access (has been authorized).
* @return {boolean} true if the user has access to the service, false
* otherwise.
*/
Service_.prototype.hasAccess = function() {
var token = this.getToken_();
return token && token.type == 'access';
};
/**
* Fetches a URL using the OAuth1 credentials of the service. Use this method
* the same way you would use `UrlFetchApp.fetch()`.
* @param {string} url The URL to fetch.
* @param {Object} params The request parameters. See the corresponding method
* in `UrlFetchApp`.
* @returns {UrlFetchApp.HTTPResponse} The response.
*/
Service_.prototype.fetch = function(url, params) {
if (!this.hasAccess()) {
throw 'Service not authorized.';
}
var token = this.getToken_();
return this.fetchInternal_(url, params, token);
};
/**
* Resets the service, removing access and requiring the service to be
* re-authorized.
*/
Service_.prototype.reset = function() {
validate_({
'Property store': this.propertyStore_
});
var key = this.getPropertyKey_();
this.propertyStore_.deleteProperty(key);
if (this.cache_) {
this.cache_.remove(key);
}
};
/**
* Get a new request token.
* @returns {Object} A request token.
*/
Service_.prototype.getRequestToken_ = function() {
validate_({
'Request Token URL': this.requestTokenUrl_,
'Method': this.method_,
});
var url = this.requestTokenUrl_;
var params = {
method: this.method_,
muteHttpExceptions: true
};
var oauthParams = {};
if (this.oauthVersion_ == '1.0a') {
oauthParams.oauth_callback = this.getCallbackUrl();
}
var response = this.fetchInternal_(url, params, null, oauthParams);
if (response.getResponseCode() >= 400) {
throw 'Error starting OAuth flow: ' + response.getContentText();
}
var token = this.parseToken_(response.getContentText());
token.type = 'request';
return token;
};
/**
* Get a new access token.
* @param {string} opt_verifier The value of the `oauth_verifier` URL parameter
* in the callback. Not used by OAuth version '1.0'.
* @returns {Object} An access token.
*/
Service_.prototype.getAccessToken_ = function(opt_verifier) {
validate_({
'Access Token URL': this.accessTokenUrl_,
'Method': this.method_
});
var url = this.accessTokenUrl_;
var params = {
method: this.method_,
muteHttpExceptions: true
};
var token = this.getToken_();
var oauthParams = {};
if (opt_verifier) {
oauthParams.oauth_verifier = opt_verifier;
}
var response = this.fetchInternal_(url, params, token, oauthParams);
if (response.getResponseCode() >= 400) {
throw 'Error completing OAuth flow: ' + response.getContentText();
}
token = this.parseToken_(response.getContentText());
token.type = 'access';
return token;
};
/**
* Makes a `UrlFetchApp` request using the optional OAuth1 token and/or
* additional parameters.
* @param {string} url The URL to fetch.
* @param {Object} params The request parameters. See the corresponding method
* in `UrlFetchApp`.
* @params {Object} opt_token OAuth token to use to sign the request (optional).
* @param {Object} opt_oauthParams Additional OAuth parameters to use when
* signing the request (optional).
* @returns {UrlFetchApp.HTTPResponse} The response.
*/
Service_.prototype.fetchInternal_ = function(url, params, opt_token,
opt_oauthParams) {
validate_({
'URL': url,
'OAuth Parameter Location': this.paramLocation_,
'Consumer Key': this.consumerKey_,
'Consumer Secret': this.consumerSecret_
});
params = params || {};
params.method = params.method || 'get';
var token = opt_token || null;
var oauthParams = opt_oauthParams || null;
var signer = new Signer({
signature_method: this.signatureMethod_,
consumer: {
public: this.consumerKey_,
secret: this.consumerSecret_
}
});
var request = {
url: url,
method: params.method
};
if (params.payload && (!params.contentType ||
params.contentType == 'application/x-www-form-urlencoded')) {
var data = params.payload;
if (typeof(data) == 'string') {
data = signer.deParam(data);
}
request.data = data;
}
oauthParams = signer.authorize(request, token, oauthParams);
if (this.realm_ != null) {
oauthParams.realm = this.realm_;
}
switch (this.paramLocation_) {
case 'auth-header':
params.headers =
assign_({}, params.headers, signer.toHeader(oauthParams));
break;
case 'uri-query':
url = buildUrl_(url, oauthParams);
break;
case 'post-body':
// Clone the payload.
params.payload = assign_({}, params.payload, oauthParams);
break;
default:
throw 'Unknown param location: ' + this.paramLocation_;
}
if (params.payload && (!params.contentType ||
params.contentType == 'application/x-www-form-urlencoded')) {
// Disable UrlFetchApp escaping and use the signer's escaping instead.
// This will ensure that the escaping is consistent between the signature and the request.
var payload = request.data;
payload = Object.keys(payload).map(function(key) {
return signer.percentEncode(key) + '=' + signer.percentEncode(payload[key]);
}).join('&');
params.payload = payload;
params.escaping = false;
}
return UrlFetchApp.fetch(url, params);
};
/**
* Parses the token from the response.
* @param {string} content The serialized token content.
* @return {Object} The parsed token.
* @private
*/
Service_.prototype.parseToken_ = function(content) {
var token = content.split('&').reduce(function(result, pair) {
var parts = pair.split('=');
result[decodeURIComponent(parts[0])] = decodeURIComponent(parts[1]);
return result;
}, {});
// Verify that the response contains a token.
if (!token.oauth_token) {
throw 'Error parsing token: key "oauth_token" not found';
}
// Set fields that the signing library expects.
token.public = token.oauth_token;
token.secret = token.oauth_token_secret;
return token;
};
/**
* Saves a token to the service's property store and cache.
* @param {Object} token The token to save.
* @private
*/
Service_.prototype.saveToken_ = function(token) {
validate_({
'Property store': this.propertyStore_
});
var key = this.getPropertyKey_();
var value = JSON.stringify(token);
this.propertyStore_.setProperty(key, value);
if (this.cache_) {
this.cache_.put(key, value, Service_.MAX_CACHE_TIME);
}
};
/**
* Gets the token from the service's property store or cache.
* @return {Object} The token, or null if no token was found.
* @private
*/
Service_.prototype.getToken_ = function() {
validate_({
'Property store': this.propertyStore_
});
var key = this.getPropertyKey_();
var token;
if (this.cache_) {
token = this.cache_.get(key);
}
if (!token) {
token = this.propertyStore_.getProperty(key);
}
if (token) {
if (this.cache_) {
this.cache_.put(key, token, Service_.MAX_CACHE_TIME);
}
return JSON.parse(token);
} else {
return null;
}
};
/**
* Generates the property key for this service.
* @return {string} The property key.
* @private
*/
Service_.prototype.getPropertyKey_ = function() {
return 'oauth1.' + this.serviceName_;
};
/**
* Gets a callback URL to use for the OAuth flow.
* @return {string} A callback URL.
*/
Service_.prototype.getCallbackUrl = function() {
validate_({
'Callback Function Name': this.callbackFunctionName_,
'Service Name': this.serviceName_,
'Script ID': this.scriptId_
});
var stateToken = eval('Script' + 'App').newStateToken()
.withMethod(this.callbackFunctionName_)
.withArgument('serviceName', this.serviceName_)
.withTimeout(3600)
.createToken();
return buildUrl_(getCallbackUrl(this.scriptId_), {
state: stateToken
});
};
// The MIT License (MIT)
//
// Copyright (c) 2014 Ddo
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
/**
* A modified version of the oauth-1.0a javascript library:
* https://github.com/ddo/oauth-1.0a
* The cryptojs dependency was removed in favor of native Apps Script functions.
* A new parameter was added to authorize() for additional oauth params.
* Support for realm authorization parameter was added in toHeader().
*/
(function(global) {
/**
* Constructor
* @param {Object} opts consumer key and secret
*/
function OAuth(opts) {
if(!(this instanceof OAuth)) {
return new OAuth(opts);
}
if(!opts) {
opts = {};
}
if(!opts.consumer) {
throw new Error('consumer option is required');
}
this.consumer = opts.consumer;
this.signature_method = opts.signature_method || 'HMAC-SHA1';
this.nonce_length = opts.nonce_length || 32;
this.version = opts.version || '1.0';
this.parameter_seperator = opts.parameter_seperator || ', ';
if(typeof opts.last_ampersand === 'undefined') {
this.last_ampersand = true;
} else {
this.last_ampersand = opts.last_ampersand;
}
switch (this.signature_method) {
case 'HMAC-SHA1':
this.hash = function(base_string, key) {
var sig = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_1, base_string, key);
return Utilities.base64Encode(sig);
};
break;
case 'PLAINTEXT':
this.hash = function(base_string, key) {
return key;
};
break;
case 'RSA-SHA1':
this.hash = function(base_string, key) {
var sig = Utilities.computeRsaSignature(Utilities.RsaAlgorithm.RSA_SHA_1, base_string, key);
return Utilities.base64Encode(sig);
};
break;
default:
throw new Error('The OAuth 1.0a protocol defines three signature methods: HMAC-SHA1, RSA-SHA1, and PLAINTEXT only');
}
}
/**
* OAuth request authorize
* @param {Object} request data
* {
* method,
* url,
* data
* }
* @param {Object} public and secret token
* @return {Object} OAuth Authorized data
*/
OAuth.prototype.authorize = function(request, token, opt_oauth_data) {
var oauth_data = {
oauth_consumer_key: this.consumer.public,
oauth_nonce: this.getNonce(),
oauth_signature_method: this.signature_method,
oauth_timestamp: this.getTimeStamp(),
oauth_version: this.version
};
if (opt_oauth_data) {
oauth_data = this.mergeObject(oauth_data, opt_oauth_data);
}
if(!token) {
token = {};
}
if(token.public) {
oauth_data.oauth_token = token.public;
}
if(!request.data) {
request.data = {};
}
oauth_data.oauth_signature = this.getSignature(request, token.secret, oauth_data);
return oauth_data;
};
/**
* Create a OAuth Signature
* @param {Object} request data
* @param {Object} token_secret public and secret token
* @param {Object} oauth_data OAuth data
* @return {String} Signature
*/
OAuth.prototype.getSignature = function(request, token_secret, oauth_data) {
return this.hash(this.getBaseString(request, oauth_data), this.getSigningKey(token_secret));
};
/**
* Base String = Method + Base Url + ParameterString
* @param {Object} request data
* @param {Object} OAuth data
* @return {String} Base String
*/
OAuth.prototype.getBaseString = function(request, oauth_data) {
return request.method.toUpperCase() + '&' + this.percentEncode(this.getBaseUrl(request.url)) + '&' + this.percentEncode(this.getParameterString(request, oauth_data));
};
/**
* Get data from url
* -> merge with oauth data
* -> percent encode key & value
* -> sort
*
* @param {Object} request data
* @param {Object} OAuth data
* @return {Object} Parameter string data
*/
OAuth.prototype.getParameterString = function(request, oauth_data) {
var base_string_data = this.sortObject(this.percentEncodeData(this.mergeObject(oauth_data, this.mergeObject(request.data, this.deParamUrl(request.url)))));
var data_str = '';
//base_string_data to string
for(var key in base_string_data) {
data_str += key + '=' + base_string_data[key] + '&';
}
//remove the last character
data_str = data_str.substr(0, data_str.length - 1);
return data_str;
};
/**
* Create a Signing Key
* @param {String} token_secret Secret Token
* @return {String} Signing Key
*/
OAuth.prototype.getSigningKey = function(token_secret) {
token_secret = token_secret || '';
// Don't percent encode the signing key (PKCS#8 PEM private key) when using
// the RSA-SHA1 method. The token secret is never used with the RSA-SHA1
// method.
if (this.signature_method === 'RSA-SHA1') {
return this.consumer.secret;
}
if(!this.last_ampersand && !token_secret) {
return this.percentEncode(this.consumer.secret);
}
return this.percentEncode(this.consumer.secret) + '&' + this.percentEncode(token_secret);
};
/**
* Get base url
* @param {String} url
* @return {String}
*/
OAuth.prototype.getBaseUrl = function(url) {
return url.split('?')[0];
};
/**
* Get data from String
* @param {String} string
* @return {Object}
*/
OAuth.prototype.deParam = function(string) {
var arr = string.replace(/\+/g, ' ').split('&');
var data = {};
for(var i = 0; i < arr.length; i++) {
var item = arr[i].split('=');
data[item[0]] = decodeURIComponent(item[1]);
}
return data;
};
/**
* Get data from url
* @param {String} url
* @return {Object}
*/
OAuth.prototype.deParamUrl = function(url) {
var tmp = url.split('?');
if (tmp.length === 1)
return {};
return this.deParam(tmp[1]);
};
/**
* Percent Encode
* @param {String} str
* @return {String} percent encoded string
*/
OAuth.prototype.percentEncode = function(str) {
return encodeURIComponent(str)
.replace(/\!/g, "%21")
.replace(/\*/g, "%2A")
.replace(/\'/g, "%27")
.replace(/\(/g, "%28")
.replace(/\)/g, "%29");
};
/**
* Percent Encode Object
* @param {Object} data
* @return {Object} percent encoded data
*/
OAuth.prototype.percentEncodeData = function(data) {
var result = {};
for(var key in data) {
result[this.percentEncode(key)] = this.percentEncode(data[key]);
}
return result;
};
/**
* Get OAuth data as Header
* @param {Object} oauth_data
* @return {String} Header data key - value
*/
OAuth.prototype.toHeader = function(oauth_data) {
oauth_data = this.sortObject(oauth_data);
var header_value = 'OAuth ';
for(var key in oauth_data) {
if (key !== 'realm' && key.indexOf('oauth_') === -1)
continue;
header_value += this.percentEncode(key) + '="' + this.percentEncode(oauth_data[key]) + '"' + this.parameter_seperator;
}
return {
Authorization: header_value.substr(0, header_value.length - this.parameter_seperator.length) //cut the last chars
};
};
/**
* Create a random word characters string with input length
* @return {String} a random word characters string
*/
OAuth.prototype.getNonce = function() {
var word_characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
var result = '';
for(var i = 0; i < this.nonce_length; i++) {
result += word_characters[parseInt(Math.random() * word_characters.length, 10)];
}
return result;
};
/**
* Get Current Unix TimeStamp
* @return {Int} current unix timestamp
*/
OAuth.prototype.getTimeStamp = function() {
return parseInt(new Date().getTime()/1000, 10);
};
////////////////////// HELPER FUNCTIONS //////////////////////
/**
* Merge object
* @param {Object} obj1
* @param {Object} obj2
* @return {Object}
*/
OAuth.prototype.mergeObject = function(obj1, obj2) {
var merged_obj = obj1;
for(var key in obj2) {
merged_obj[key] = obj2[key];
}
return merged_obj;
};
/**
* Sort object by key
* @param {Object} data
* @return {Object} sorted object
*/
OAuth.prototype.sortObject = function(data) {
var keys = Object.keys(data);
var result = {};
keys.sort();