Skip to content

Commit

Permalink
Real fix for encoding issues in javascript and oauth.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeal committed Dec 1, 2011
1 parent ddc4e45 commit feee5eb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
4 changes: 2 additions & 2 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,8 @@ Request.prototype.request = function () {
}
}
self.headers.authorization =
'OAuth '+Object.keys(oa).sort().map(function (i) {return i+'="'+encodeURIComponent(oa[i])+'"'}).join(',')
self.headers.authorization += ',oauth_signature="'+encodeURIComponent(signature)+'"'
'OAuth '+Object.keys(oa).sort().map(function (i) {return i+'="'+oauth.rfc3986(oa[i])+'"'}).join(',')
self.headers.authorization += ',oauth_signature="'+oauth.rfc3986(signature)+'"'
}

if (self.uri.auth && !self.headers.authorization) {
Expand Down
15 changes: 13 additions & 2 deletions oauth.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,29 @@ function sha1 (key, body) {
return crypto.createHmac('sha1', key).update(body).digest('base64')
}

function rfc3986 (str) {
return encodeURIComponent(str)
.replace('!','%21')
.replace('*','%2A')
.replace('(','%28')
.replace(')','%29')
.replace("'",'%27')
;
}

function hmacsign (httpMethod, base_uri, params, consumer_secret, token_secret, body) {
// adapted from https://dev.twitter.com/docs/auth/oauth
var base =
httpMethod + "&" +
encodeURIComponent( base_uri ) + "&" +
Object.keys(params).sort().map(function (i) {
// big WTF here with the escape + encoding but it's what twitter wants
return encodeURIComponent(escape(i)) + "%3D" + encodeURIComponent(escape(params[i]))
return escape(rfc3986(i)) + "%3D" + escape(rfc3986(params[i]))
}).join("%26")
var key = consumer_secret + '&'
if (token_secret) key += token_secret
return sha1(key, base)
}

exports.hmacsign = hmacsign
exports.hmacsign = hmacsign
exports.rfc3986 = rfc3986

0 comments on commit feee5eb

Please sign in to comment.