Created
November 17, 2016 18:41
-
-
Save keiver/d735f931c26d8868283eebfa9558487f to your computer and use it in GitHub Desktop.
Get Authentication header with OAuth using oauth-1.0a and crypto-js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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