Skip to content

Commit

Permalink
Merge pull request #1574 from simov/oauth-redirect
Browse files Browse the repository at this point in the history
Refresh the oauth_nonce on redirect (#1573)
  • Loading branch information
simov committed May 17, 2015
2 parents 1c6c132 + 195d4c0 commit fdf29a1
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/oauth.js
Expand Up @@ -9,6 +9,7 @@ var qs = require('qs')

function OAuth (request) {
this.request = request
this.params = null
}

OAuth.prototype.buildParams = function (_oauth, uri, method, query, form, qsLib) {
Expand Down Expand Up @@ -90,6 +91,7 @@ OAuth.prototype.concatParams = function (oa, sep, wrap) {

OAuth.prototype.onRequest = function (_oauth) {
var self = this
self.params = _oauth

var uri = self.request.uri || {}
, method = self.request.method || ''
Expand Down
2 changes: 2 additions & 0 deletions request.js
Expand Up @@ -628,6 +628,8 @@ Request.prototype.init = function (options) {

if (options.oauth) {
self.oauth(options.oauth)
} else if (self._oauth.params && self.hasHeader('authorization')) {
self.oauth(self._oauth.params)
}

var protocol = self.proxy && !self.tunnel ? self.proxy.protocol : self.uri.protocol
Expand Down
65 changes: 65 additions & 0 deletions tests/test-oauth.js
Expand Up @@ -7,6 +7,7 @@ var oauth = require('oauth-sign')
, request = require('../index')
, tape = require('tape')
, crypto = require('crypto')
, http = require('http')

function getSignature(r) {
var sign
Expand Down Expand Up @@ -587,3 +588,67 @@ tape('body_hash PLAINTEXT signature_method', function(t) {
}, /oauth: PLAINTEXT signature_method not supported with body_hash signing/)
t.end()
})

tape('refresh oauth_nonce on redirect', function(t) {
var oauth_nonce1, oauth_nonce2
var s = http.createServer(function (req, res) {
if (req.url === '/redirect') {
oauth_nonce1 = req.headers.authorization.replace(/.*oauth_nonce="([^"]+)".*/, '$1')
res.writeHead(302, {location:'http://localhost:6767/response'})
res.end()
} else if (req.url === '/response') {
oauth_nonce2 = req.headers.authorization.replace(/.*oauth_nonce="([^"]+)".*/, '$1')
res.writeHead(200, {'content-type':'text/plain'})
res.end()
}
})
s.listen(6767, function () {
request.get(
{ url: 'http://localhost:6767/redirect'
, oauth:
{ consumer_key: 'consumer_key'
, consumer_secret: 'consumer_secret'
, token: 'token'
, token_secret: 'token_secret'
}
}, function (err, res, body) {
t.equal(err, null)
t.notEqual(oauth_nonce1, oauth_nonce2)
s.close(function () {
t.end()
})
})
})
})

tape('no credentials on external redirect', function(t) {
var s1 = http.createServer(function (req, res) {
res.writeHead(302, {location:'http://127.0.0.1:6768'})
res.end()
})
var s2 = http.createServer(function (req, res) {
res.writeHead(200, {'content-type':'text/plain'})
res.end()
})
s1.listen(6767, function () {
s2.listen(6768, function () {
request.get(
{ url: 'http://localhost:6767'
, oauth:
{ consumer_key: 'consumer_key'
, consumer_secret: 'consumer_secret'
, token: 'token'
, token_secret: 'token_secret'
}
}, function (err, res, body) {
t.equal(err, null)
t.equal(res.request.headers.Authorization, undefined)
s1.close(function () {
s2.close(function () {
t.end()
})
})
})
})
})
})

0 comments on commit fdf29a1

Please sign in to comment.