Skip to content

Instantly share code, notes, and snippets.

@alejandrolechuga
Forked from keiver/oauth.js
Created November 18, 2016 15:24
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alejandrolechuga/1ceba91bf870db2a389734f873a93478 to your computer and use it in GitHub Desktop.
Save alejandrolechuga/1ceba91bf870db2a389734f873a93478 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