@@ -222,10 +222,20 @@ Service_.prototype.setMethod = function(method) {
222222 return this ;
223223} ;
224224
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+
225235/**
226236 * Sets the OAuth signature method to use. 'HMAC-SHA1' is the default.
227237 * @param {string } signatureMethod The OAuth signature method. Allowed values
228- * are 'HMAC-SHA1' and 'PLAINTEXT'.
238+ * are 'HMAC-SHA1', 'RSA-SHA1' and 'PLAINTEXT'.
229239 * @return {Service_ } This service, for chaining.
230240 */
231241Service_ . prototype . setSignatureMethod = function ( signatureMethod ) {
@@ -377,7 +387,7 @@ Service_.prototype.handleCallback = function(callbackRequest) {
377387 var verifier = callbackRequest . parameter . oauth_verifier ;
378388 var token = this . getToken_ ( ) ;
379389
380- if ( requestToken && requestToken != token . public ) {
390+ if ( ! token || ( requestToken && requestToken != token . public ) ) {
381391 throw 'Error handling callback: token mismatch' ;
382392 }
383393
@@ -536,6 +546,9 @@ Service_.prototype.fetchInternal_ = function(url, params, opt_token,
536546 request . data = data ;
537547 }
538548 oauthParams = signer . authorize ( request , token , oauthParams ) ;
549+ if ( this . realm_ != null ) {
550+ oauthParams . realm = this . realm_ ;
551+ }
539552 switch ( this . paramLocation_ ) {
540553 case 'auth-header' :
541554 params . headers = _ . extend ( { } , params . headers ,
@@ -576,6 +589,10 @@ Service_.prototype.parseToken_ = function(content) {
576589 result [ decodeURIComponent ( parts [ 0 ] ) ] = decodeURIComponent ( parts [ 1 ] ) ;
577590 return result ;
578591 } , { } ) ;
592+ // Verify that the response contains a token.
593+ if ( ! token . oauth_token ) {
594+ throw 'Error parsing token: key "oauth_token" not found' ;
595+ }
579596 // Set fields that the signing library expects.
580597 token . public = token . oauth_token ;
581598 token . secret = token . oauth_token_secret ;
@@ -681,6 +698,7 @@ Service_.prototype.getCallbackUrl = function() {
681698 * https://github.com/ddo/oauth-1.0a
682699 * The cryptojs dependency was removed in favor of native Apps Script functions.
683700 * A new parameter was added to authorize() for additional oauth params.
701+ * Support for realm authorization parameter was added in toHeader().
684702 */
685703
686704( function ( global ) {
@@ -726,7 +744,11 @@ Service_.prototype.getCallbackUrl = function() {
726744 } ;
727745 break ;
728746 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 ;
730752 default :
731753 throw new Error ( 'The OAuth 1.0a protocol defines three signature methods: HMAC-SHA1, RSA-SHA1, and PLAINTEXT only' ) ;
732754 }
@@ -827,6 +849,13 @@ Service_.prototype.getCallbackUrl = function() {
827849 OAuth . prototype . getSigningKey = function ( token_secret ) {
828850 token_secret = token_secret || '' ;
829851
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+
830859 if ( ! this . last_ampersand && ! token_secret ) {
831860 return this . percentEncode ( this . consumer . secret ) ;
832861 }
@@ -913,7 +942,7 @@ Service_.prototype.getCallbackUrl = function() {
913942 var header_value = 'OAuth ' ;
914943
915944 for ( var key in oauth_data ) {
916- if ( key . indexOf ( 'oauth_' ) === - 1 )
945+ if ( key !== 'realm' && key . indexOf ( 'oauth_' ) === - 1 )
917946 continue ;
918947 header_value += this . percentEncode ( key ) + '="' + this . percentEncode ( oauth_data [ key ] ) + '"' + this . parameter_seperator ;
919948 }
0 commit comments