Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initial support for oauth_body_hash on json payloads #1543

Merged
merged 8 commits into from Apr 28, 2015
20 changes: 20 additions & 0 deletions lib/oauth.js
Expand Up @@ -4,6 +4,7 @@ var qs = require('qs')
, caseless = require('caseless')
, uuid = require('node-uuid')
, oauth = require('oauth-sign')
, crypto = require('crypto')


function OAuth (request) {
Expand Down Expand Up @@ -57,6 +58,21 @@ OAuth.prototype.buildParams = function (_oauth, uri, method, query, form, qsLib)
return oa
}

OAuth.prototype.buildBodyHash = function(_oauth, body) {
var acceptedSignatureMethods = ['HMAC-SHA1', 'RSA-SHA1']
var index = acceptedSignatureMethods.indexOf(_oauth.signature_method)

if (!_oauth.signature_method || index > -1) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can flip this condition and make it throw here. The rest of the code should be placed after that if statement. I think it will be easier to follow. Also you probably don't need the index as a separate variable, if you don't use it anywhere else.

var shasum = crypto.createHash('sha1')
shasum.update(body || '')
var sha1 = shasum.digest('hex')

return new Buffer(sha1).toString('base64')
} else {
throw new Error('oauth: ' + _oauth.signature_method + ' signature_method not supported with body_hash signing.')
}
}

OAuth.prototype.concatParams = function (oa, sep, wrap) {
wrap = wrap || ''

Expand Down Expand Up @@ -102,6 +118,10 @@ OAuth.prototype.onRequest = function (_oauth) {
'and content-type \'' + formContentType + '\'')
}

if (!form && typeof _oauth.body_hash === 'boolean') {
_oauth.body_hash = this.buildBodyHash(_oauth, this.request.body.toString())
}

var oa = this.buildParams(_oauth, uri, method, query, form, qsLib)

switch (transport) {
Expand Down
10 changes: 5 additions & 5 deletions request.js
Expand Up @@ -541,11 +541,6 @@ Request.prototype.init = function (options) {
self.path = '/'
}

// Auth must happen last in case signing is dependent on other headers
if (options.oauth) {
self.oauth(options.oauth)
}

if (options.aws) {
self.aws(options.aws)
}
Expand Down Expand Up @@ -630,6 +625,11 @@ Request.prototype.init = function (options) {
}
}

// Auth must happen last in case signing is dependent on other headers
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment should be moved back to the place where it was. It is related to all of the auth schemes not only oauth. Here you can write a short single line comment saying that oauth's body_hash might need the body.

if (options.oauth) {
self.oauth(options.oauth)
}

var protocol = self.proxy && !self.tunnel ? self.proxy.protocol : self.uri.protocol
, defaultModules = {'http:':http, 'https:':https}
, httpModules = self.httpModules || {}
Expand Down