react / wstein / node_modules / jest-cli / node_modules / jsdom / node_modules / request / node_modules / oauth-sign / index.js
81145 viewsvar crypto = require('crypto')1, qs = require('querystring')2;34function sha1 (key, body) {5return crypto.createHmac('sha1', key).update(body).digest('base64')6}78function rsa (key, body) {9return crypto.createSign("RSA-SHA1").update(body).sign(key, 'base64');10}1112function rfc3986 (str) {13return encodeURIComponent(str)14.replace(/!/g,'%21')15.replace(/\*/g,'%2A')16.replace(/\(/g,'%28')17.replace(/\)/g,'%29')18.replace(/'/g,'%27')19;20}2122// Maps object to bi-dimensional array23// Converts { foo: 'A', bar: [ 'b', 'B' ]} to24// [ ['foo', 'A'], ['bar', 'b'], ['bar', 'B'] ]25function map (obj) {26var key, val, arr = []27for (key in obj) {28val = obj[key]29if (Array.isArray(val))30for (var i = 0; i < val.length; i++)31arr.push([key, val[i]])32else if (typeof val === "object")33for (var prop in val)34arr.push([key + '[' + prop + ']', val[prop]]);35else36arr.push([key, val])37}38return arr39}4041// Compare function for sort42function compare (a, b) {43return a > b ? 1 : a < b ? -1 : 044}4546function generateBase (httpMethod, base_uri, params) {47// adapted from https://dev.twitter.com/docs/auth/oauth and48// https://dev.twitter.com/docs/auth/creating-signature4950// Parameter normalization51// http://tools.ietf.org/html/rfc5849#section-3.4.1.3.252var normalized = map(params)53// 1. First, the name and value of each parameter are encoded54.map(function (p) {55return [ rfc3986(p[0]), rfc3986(p[1] || '') ]56})57// 2. The parameters are sorted by name, using ascending byte value58// ordering. If two or more parameters share the same name, they59// are sorted by their value.60.sort(function (a, b) {61return compare(a[0], b[0]) || compare(a[1], b[1])62})63// 3. The name of each parameter is concatenated to its corresponding64// value using an "=" character (ASCII code 61) as a separator, even65// if the value is empty.66.map(function (p) { return p.join('=') })67// 4. The sorted name/value pairs are concatenated together into a68// single string by using an "&" character (ASCII code 38) as69// separator.70.join('&')7172var base = [73rfc3986(httpMethod ? httpMethod.toUpperCase() : 'GET'),74rfc3986(base_uri),75rfc3986(normalized)76].join('&')7778return base79}8081function hmacsign (httpMethod, base_uri, params, consumer_secret, token_secret) {82var base = generateBase(httpMethod, base_uri, params)83var key = [84consumer_secret || '',85token_secret || ''86].map(rfc3986).join('&')8788return sha1(key, base)89}9091function rsasign (httpMethod, base_uri, params, private_key, token_secret) {92var base = generateBase(httpMethod, base_uri, params)93var key = private_key || ''9495return rsa(key, base)96}9798function plaintext (consumer_secret, token_secret) {99var key = [100consumer_secret || '',101token_secret || ''102].map(rfc3986).join('&')103104return key105}106107function sign (signMethod, httpMethod, base_uri, params, consumer_secret, token_secret) {108var method109var skipArgs = 1110111switch (signMethod) {112case 'RSA-SHA1':113method = rsasign114break115case 'HMAC-SHA1':116method = hmacsign117break118case 'PLAINTEXT':119method = plaintext120skipArgs = 4121break122default:123throw new Error("Signature method not supported: " + signMethod)124}125126return method.apply(null, [].slice.call(arguments, skipArgs))127}128129exports.hmacsign = hmacsign130exports.rsasign = rsasign131exports.plaintext = plaintext132exports.sign = sign133exports.rfc3986 = rfc3986134135136