@@ -222,10 +222,20 @@ Service_.prototype.setMethod = function(method) {
222
222
return this ;
223
223
} ;
224
224
225
+ /**
226
+ * Sets the OAuth realm parameter to be used with this service (optional).
227
+ * @param {string } realm The realm to be used with this service.
228
+ * @return {Service_ } This service, for chaining.
229
+ */
230
+ Service_ . prototype . setRealm = function ( realm ) {
231
+ this . realm_ = realm ;
232
+ return this ;
233
+ } ;
234
+
225
235
/**
226
236
* Sets the OAuth signature method to use. 'HMAC-SHA1' is the default.
227
237
* @param {string } signatureMethod The OAuth signature method. Allowed values
228
- * are 'HMAC-SHA1' and 'PLAINTEXT'.
238
+ * are 'HMAC-SHA1', 'RSA-SHA1' and 'PLAINTEXT'.
229
239
* @return {Service_ } This service, for chaining.
230
240
*/
231
241
Service_ . prototype . setSignatureMethod = function ( signatureMethod ) {
@@ -377,7 +387,7 @@ Service_.prototype.handleCallback = function(callbackRequest) {
377
387
var verifier = callbackRequest . parameter . oauth_verifier ;
378
388
var token = this . getToken_ ( ) ;
379
389
380
- if ( requestToken && requestToken != token . public ) {
390
+ if ( ! token || ( requestToken && requestToken != token . public ) ) {
381
391
throw 'Error handling callback: token mismatch' ;
382
392
}
383
393
@@ -536,6 +546,9 @@ Service_.prototype.fetchInternal_ = function(url, params, opt_token,
536
546
request . data = data ;
537
547
}
538
548
oauthParams = signer . authorize ( request , token , oauthParams ) ;
549
+ if ( this . realm_ != null ) {
550
+ oauthParams . realm = this . realm_ ;
551
+ }
539
552
switch ( this . paramLocation_ ) {
540
553
case 'auth-header' :
541
554
params . headers = _ . extend ( { } , params . headers ,
@@ -576,6 +589,10 @@ Service_.prototype.parseToken_ = function(content) {
576
589
result [ decodeURIComponent ( parts [ 0 ] ) ] = decodeURIComponent ( parts [ 1 ] ) ;
577
590
return result ;
578
591
} , { } ) ;
592
+ // Verify that the response contains a token.
593
+ if ( ! token . oauth_token ) {
594
+ throw 'Error parsing token: key "oauth_token" not found' ;
595
+ }
579
596
// Set fields that the signing library expects.
580
597
token . public = token . oauth_token ;
581
598
token . secret = token . oauth_token_secret ;
@@ -681,6 +698,7 @@ Service_.prototype.getCallbackUrl = function() {
681
698
* https://github.com/ddo/oauth-1.0a
682
699
* The cryptojs dependency was removed in favor of native Apps Script functions.
683
700
* A new parameter was added to authorize() for additional oauth params.
701
+ * Support for realm authorization parameter was added in toHeader().
684
702
*/
685
703
686
704
( function ( global ) {
@@ -726,7 +744,11 @@ Service_.prototype.getCallbackUrl = function() {
726
744
} ;
727
745
break ;
728
746
case 'RSA-SHA1' :
729
- throw new Error ( 'oauth-1.0a does not support this signature method right now. Coming Soon...' ) ;
747
+ this . hash = function ( base_string , key ) {
748
+ var sig = Utilities . computeRsaSignature ( Utilities . RsaAlgorithm . RSA_SHA_1 , base_string , key ) ;
749
+ return Utilities . base64Encode ( sig ) ;
750
+ } ;
751
+ break ;
730
752
default :
731
753
throw new Error ( 'The OAuth 1.0a protocol defines three signature methods: HMAC-SHA1, RSA-SHA1, and PLAINTEXT only' ) ;
732
754
}
@@ -827,6 +849,13 @@ Service_.prototype.getCallbackUrl = function() {
827
849
OAuth . prototype . getSigningKey = function ( token_secret ) {
828
850
token_secret = token_secret || '' ;
829
851
852
+ // Don't percent encode the signing key (PKCS#8 PEM private key) when using
853
+ // the RSA-SHA1 method. The token secret is never used with the RSA-SHA1
854
+ // method.
855
+ if ( this . signature_method === 'RSA-SHA1' ) {
856
+ return this . consumer . secret ;
857
+ }
858
+
830
859
if ( ! this . last_ampersand && ! token_secret ) {
831
860
return this . percentEncode ( this . consumer . secret ) ;
832
861
}
@@ -913,7 +942,7 @@ Service_.prototype.getCallbackUrl = function() {
913
942
var header_value = 'OAuth ' ;
914
943
915
944
for ( var key in oauth_data ) {
916
- if ( key . indexOf ( 'oauth_' ) === - 1 )
945
+ if ( key !== 'realm' && key . indexOf ( 'oauth_' ) === - 1 )
917
946
continue ;
918
947
header_value += this . percentEncode ( key ) + '="' + this . percentEncode ( oauth_data [ key ] ) + '"' + this . parameter_seperator ;
919
948
}
0 commit comments