insomnia/packages/insomnia-app/app/network/o-auth-1/get-token.js
Gregory Schier 549ce23ce8
Merge All Repositories into Monorepo for easier maintenance (#629)
* All projects into monorepo

* Update CI

* More CI updates

* Extracted a bunch of things into packages

* Publish

 - insomnia-plugin-base64@1.0.1
 - insomnia-plugin-default-headers@1.0.2
 - insomnia-plugin-file@1.0.1
 - insomnia-plugin-hash@1.0.1
 - insomnia-plugin-now@1.0.1
 - insomnia-plugin-request@1.0.1
 - insomnia-plugin-response@1.0.1
 - insomnia-plugin-uuid@1.0.1
 - insomnia-cookies@0.0.2
 - insomnia-importers@1.5.2
 - insomnia-prettify@0.0.3
 - insomnia-url@0.0.2
 - insomnia-xpath@0.0.2

* A bunch of small fixes

* Improved build script

* Fixed

* Merge dangling files

* Usability refactor

* Handle duplicate plugin names
2017-11-26 20:45:40 +00:00

74 lines
2.1 KiB
JavaScript

// @flow
/**
* 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';
import type {OAuth1SignatureMethod} from './constants';
import type {RequestAuthentication} from '../../models/request';
function hashFunction (signatureMethod: OAuth1SignatureMethod) {
if (signatureMethod === SIGNATURE_METHOD_HMAC_SHA1) {
return function (baseString: string, key: string): string {
return crypto.createHmac('sha1', key).update(baseString).digest('base64');
};
}
if (signatureMethod === SIGNATURE_METHOD_PLAINTEXT) {
return function (baseString: string): string {
return baseString;
};
}
throw new Error(`Invalid signature method ${signatureMethod}`);
}
export default async function (
url: string,
method: string,
authentication: RequestAuthentication
): {[string]: string} {
const oauth = new OAuth1({
consumer: {
key: authentication.consumerKey,
secret: authentication.consumerSecret
},
signature_method: authentication.signatureMethod,
version: authentication.version,
hash_function: hashFunction(authentication.signatureMethod),
realm: authentication.realm || null
});
const requestData = {
url: url,
method: method,
data: {
// These are conditionally filled in below
}
};
if (authentication.callback) {
requestData.data.oauth_callback = authentication.callback;
}
if (authentication.nonce) {
requestData.data.oauth_nonce = authentication.nonce;
}
if (authentication.timestamp) {
requestData.data.oauth_timestamp = authentication.timestamp;
}
let token = null;
if (authentication.tokenKey && authentication.tokenSecret) {
token = {key: authentication.tokenKey, secret: authentication.tokenSecret};
} else if (authentication.tokenKey) {
token = {key: authentication.tokenKey};
}
const data = oauth.authorize(requestData, token);
return oauth.toHeader(data);
}