node/deps/npm/node_modules/request/oauth.js

34 lines
1012 B
JavaScript
Raw Normal View History

2011-11-23 08:56:59 +08:00
var crypto = require('crypto')
, qs = require('querystring')
;
function sha1 (key, body) {
return crypto.createHmac('sha1', key).update(body).digest('base64')
}
2012-01-17 07:06:16 +08:00
function rfc3986 (str) {
return encodeURIComponent(str)
2012-06-11 12:29:47 +08:00
.replace(/!/g,'%21')
.replace(/\*/g,'%2A')
.replace(/\(/g,'%28')
.replace(/\)/g,'%29')
.replace(/'/g,'%27')
2012-01-17 07:06:16 +08:00
;
}
2011-11-23 08:56:59 +08:00
function hmacsign (httpMethod, base_uri, params, consumer_secret, token_secret, body) {
// adapted from https://dev.twitter.com/docs/auth/oauth
var base =
(httpMethod || 'GET') + "&" +
2011-11-23 08:56:59 +08:00
encodeURIComponent( base_uri ) + "&" +
Object.keys(params).sort().map(function (i) {
// big WTF here with the escape + encoding but it's what twitter wants
2012-01-17 07:06:16 +08:00
return escape(rfc3986(i)) + "%3D" + escape(rfc3986(params[i]))
2011-11-23 08:56:59 +08:00
}).join("%26")
2012-07-08 03:40:06 +08:00
var key = encodeURIComponent(consumer_secret) + '&'
if (token_secret) key += encodeURIComponent(token_secret)
2011-11-23 08:56:59 +08:00
return sha1(key, base)
}
2012-01-17 07:06:16 +08:00
exports.hmacsign = hmacsign
exports.rfc3986 = rfc3986