Skip to content

Instantly share code, notes, and snippets.

@keiver
Created November 17, 2016 18:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save keiver/d735f931c26d8868283eebfa9558487f to your computer and use it in GitHub Desktop.
Save keiver/d735f931c26d8868283eebfa9558487f to your computer and use it in GitHub Desktop.
Get Authentication header with OAuth using oauth-1.0a and crypto-js
import OAuth from 'oauth-1.0a';
import CryptoJS from 'crypto-js';
/**
* oAuthHeader - Get Authentication header with OAuth.
*
* @param {string} url Request URL
* @param {string} method HTTP method.
* @return {object} Authentication header object
*/
function oAuthHeader(url, method, session, token) {
if (!session || !token) {
throw new Error('Traying to generate OAuth headers without valid access token or session.');
}
let oauthObject = new OAuth({
consumer: {
key: session,
secret: token
},
signature_method: 'HMAC-SHA1',
nonce_length: 6,
version: '1.0',
hash_function: function(base_string, key) {
return CryptoJS.HmacSHA1(base_string, key).toString(CryptoJS.enc.Base64);
}
});
return oauthObject.toHeader(oauthObject.authorize({url: url, method: method}));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment