insomnia/app/network/o-auth-1/get-token.js

43 lines
1.1 KiB
JavaScript
Raw Normal View History

2017-11-06 19:26:31 +00:00
/**
* Get an OAuth1Token object and also handle storing/saving/refreshing
* @returns {Promise.<void>}
*/
import crypto from 'crypto';
import OAuth1 from 'oauth-1.0a';
import { SIGNATURE_METHOD_HMAC_SHA1, SIGNATURE_METHOD_PLAINTEXT } from './constants';
function hashFunction (signatureMethod) {
if (signatureMethod === SIGNATURE_METHOD_HMAC_SHA1) {
return function (baseString, key) {
return crypto.createHmac('sha1', key).update(baseString).digest('base64');
};
}
if (signatureMethod === SIGNATURE_METHOD_PLAINTEXT) {
return function (baseString) {
return baseString;
};
}
return null;
}
export default async function (url, method, authentication) {
var oauth = OAuth1({
consumer: {
key: authentication.consumerKey,
secret: authentication.consumerSecret
},
signature_method: authentication.signatureMethod,
version: authentication.version,
hash_function: hashFunction(authentication.signatureMethod)
});
var requestData = {
url: url,
method: method
};
return oauth.toHeader(oauth.authorize(requestData));
}