From 9398b1704383a02f59bdb3e757a165f501a382c0 Mon Sep 17 00:00:00 2001 From: Alexander Ivanov Date: Mon, 1 Apr 2019 16:50:09 +0100 Subject: [PATCH 1/3] Add OAuth1 realm parameter. Adds an option to send a `realm="..."` parameter in the Authentication headers, for example: ``` function getService() { return OAuth1.createService("NetSuite") .setRealm('1234567_SB1') .setConsumerKey(CONSUMER_KEY) .setConsumerSecret(CONSUMER_SECRET) .setAccessToken(TOKEN, TOKEN_SECRET); } ``` --- package.json | 2 +- src/Service.gs | 13 +++++++++++++ src/Signer.gs | 2 +- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 8108cba..c605409 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "apps-script-oauth1", - "version": "1.15.0", + "version": "1.16.0", "description": "OAuth1 for Apps Script is a library for Google Apps Script that provides the ability to create and authorize OAuth1 tokens. ", "repository": { "type": "git", diff --git a/src/Service.gs b/src/Service.gs index cedde21..72a2b59 100644 --- a/src/Service.gs +++ b/src/Service.gs @@ -101,6 +101,16 @@ Service_.prototype.setMethod = function(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 @@ -415,6 +425,9 @@ Service_.prototype.fetchInternal_ = function(url, params, opt_token, 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 = _.extend({}, params.headers, diff --git a/src/Signer.gs b/src/Signer.gs index f1285cd..bbbd834 100644 --- a/src/Signer.gs +++ b/src/Signer.gs @@ -267,7 +267,7 @@ var header_value = 'OAuth '; for(var key in oauth_data) { - if (key.indexOf('oauth_') === -1) + if (key !== 'realm' && key.indexOf('oauth_') === -1) continue; header_value += this.percentEncode(key) + '="' + this.percentEncode(oauth_data[key]) + '"' + this.parameter_seperator; } From 908dfe521d68362eef5dfa8ee21d99bdf8304f99 Mon Sep 17 00:00:00 2001 From: Alexander Ivanov Date: Tue, 2 Apr 2019 10:33:21 +0100 Subject: [PATCH 2/3] Revert package.json --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index c605409..8108cba 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "apps-script-oauth1", - "version": "1.16.0", + "version": "1.15.0", "description": "OAuth1 for Apps Script is a library for Google Apps Script that provides the ability to create and authorize OAuth1 tokens. ", "repository": { "type": "git", From edcdf0c1bdca87f7f59625ed13ac18a831413eab Mon Sep 17 00:00:00 2001 From: Alexander Ivanov Date: Wed, 3 Apr 2019 14:00:21 +0100 Subject: [PATCH 3/3] Add comment on realm parameter --- src/Signer.gs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Signer.gs b/src/Signer.gs index bbbd834..7d3d565 100644 --- a/src/Signer.gs +++ b/src/Signer.gs @@ -24,6 +24,7 @@ * 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) {