Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add authorize async function for asynchronous signature functions #107

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 31 additions & 5 deletions oauth-1.0a.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function OAuth(opts) {
* @param {Object} key and secret token
* @return {Object} OAuth Authorized data
*/
OAuth.prototype.authorize = function(request, token) {
OAuth.prototype.authorize = function(request, token, isAsync) {
var oauth_data = {
oauth_consumer_key: this.consumer.key,
oauth_nonce: this.getNonce(),
Expand All @@ -80,15 +80,41 @@ OAuth.prototype.authorize = function(request, token) {
request.data = {};
}

if(request.includeBodyHash) {
oauth_data.oauth_body_hash = this.getBodyHash(request, token.secret)
}
if(!isAsync) {
if(request.includeBodyHash) {
oauth_data.oauth_body_hash = this.getBodyHash(request, token.secret)
}

oauth_data.oauth_signature = this.getSignature(request, token.secret, oauth_data);
oauth_data.oauth_signature = this.getSignature(request, token.secret, oauth_data);
}

return oauth_data;
};

OAuth.prototype.authorizeAsync = function(request, token) {
return new Promise((resolve) => {
var oauth_data_sync = this.authorize(request, token, true);

this.getSignature(request, token.secret, oauth_data_sync)
.then((oauth_signature) => {
var oauth_data = {
...oauth_data_sync,
oauth_signature,
};

if(request.includeBodyHash) {
oauth_data.oauth_body_hash = this.getBodyHash(request, token.secret)
.then((hash) => {
oauth_data.oauth_body_hash = hash;
resolve(oauth_data);
})
} else {
resolve(oauth_data);
}
});
})
}

/**
* Create a OAuth Signature
* @param {Object} request data
Expand Down