From 176c7b22aaa80250850760f1641fa91ac388cbf8 Mon Sep 17 00:00:00 2001 From: Roman Puchkovskiy Date: Wed, 3 Apr 2019 10:12:16 +0400 Subject: [PATCH 1/2] Add support for async signature computation --- oauth-1.0a.js | 52 +++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 46 insertions(+), 6 deletions(-) diff --git a/oauth-1.0a.js b/oauth-1.0a.js index 5c15dd4..de5e782 100644 --- a/oauth-1.0a.js +++ b/oauth-1.0a.js @@ -60,6 +60,52 @@ function OAuth(opts) { * @return {Object} OAuth Authorized data */ OAuth.prototype.authorize = function(request, token) { + var oauth_data = this._prepareSyncAuthorizationPart(request, token); + + if(request.includeBodyHash) { + oauth_data.oauth_body_hash = this.getBodyHash(request, token.secret) + } + + oauth_data.oauth_signature = this.getSignature(request, token.secret, oauth_data); + + return oauth_data; +}; + +/** + * Promise for OAuth request authorize. It is required that + * oauth_body_hash and oauth_signature functions return + * promises. + * @param {Object} request data + * { + * method, + * url, + * data + * } + * @param {Object} key and secret token + * @return {Object} Promise to produce OAuth Authorized data + */ +OAuth.prototype.promiseAuthorization = function(request, token) { + var self = this; + var oauth_data = this._prepareSyncAuthorizationPart(request, token); + + var promise = Promise.resolve(); + if(request.includeBodyHash) { + promise = promise.then(function() { + return self.getBodyHash(request, token.secret); + }).then(function(body_hash) { + oauth_data.oauth_body_hash = body_hash; + }) + } + + return promise.then(function() { + return self.getSignature(request, token.secret, oauth_data); + }).then(function(signature) { + oauth_data.oauth_signature = signature; + return oauth_data; + }) +}; + +OAuth.prototype._prepareSyncAuthorizationPart = function(request, token) { var oauth_data = { oauth_consumer_key: this.consumer.key, oauth_nonce: this.getNonce(), @@ -80,12 +126,6 @@ OAuth.prototype.authorize = function(request, token) { request.data = {}; } - if(request.includeBodyHash) { - oauth_data.oauth_body_hash = this.getBodyHash(request, token.secret) - } - - oauth_data.oauth_signature = this.getSignature(request, token.secret, oauth_data); - return oauth_data; }; From 1c0735d924e9abb4c53084f26b870c6af3859f17 Mon Sep 17 00:00:00 2001 From: Roman Puchkovskiy Date: Sat, 8 Jun 2019 16:44:15 +0400 Subject: [PATCH 2/2] Initialize token with {} if it is not provided --- oauth-1.0a.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/oauth-1.0a.js b/oauth-1.0a.js index de5e782..5dcc448 100644 --- a/oauth-1.0a.js +++ b/oauth-1.0a.js @@ -60,6 +60,10 @@ function OAuth(opts) { * @return {Object} OAuth Authorized data */ OAuth.prototype.authorize = function(request, token) { + if(!token) { + token = {}; + } + var oauth_data = this._prepareSyncAuthorizationPart(request, token); if(request.includeBodyHash) { @@ -85,6 +89,10 @@ OAuth.prototype.authorize = function(request, token) { * @return {Object} Promise to produce OAuth Authorized data */ OAuth.prototype.promiseAuthorization = function(request, token) { + if(!token) { + token = {}; + } + var self = this; var oauth_data = this._prepareSyncAuthorizationPart(request, token); @@ -114,10 +122,6 @@ OAuth.prototype._prepareSyncAuthorizationPart = function(request, token) { oauth_version: this.version }; - if(!token) { - token = {}; - } - if(token.key !== undefined) { oauth_data.oauth_token = token.key; }