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

Adding handling for no auth method and null bearer #1500

Merged
merged 1 commit into from Mar 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/auth.js
Expand Up @@ -42,7 +42,7 @@ Auth.prototype.bearer = function (bearer, sendImmediately) {
if (typeof bearer === 'function') {
bearer = bearer()
}
var authHeader = 'Bearer ' + bearer
var authHeader = 'Bearer ' + (bearer || '')
self.sentAuth = true
return authHeader
}
Expand Down Expand Up @@ -114,7 +114,9 @@ Auth.prototype.onRequest = function (user, pass, sendImmediately, bearer) {
, request = self.request

var authHeader
if (bearer !== undefined) {
if (bearer === undefined && user === undefined) {
throw new Error('no auth mechanism defined')
} else if (bearer !== undefined) {
authHeader = self.bearer(bearer, sendImmediately)
} else {
authHeader = self.basic(user, pass, sendImmediately)
Expand Down
42 changes: 36 additions & 6 deletions tests/test-bearer-auth.js
Expand Up @@ -49,7 +49,7 @@ tape('setup', function(t) {
})
})

tape('', function(t) {
tape('bearer auth', function(t) {
request({
'method': 'GET',
'uri': 'http://localhost:6767/test/',
Expand All @@ -64,7 +64,7 @@ tape('', function(t) {
})
})

tape('', function(t) {
tape('bearer auth with default sendImmediately', function(t) {
// If we don't set sendImmediately = false, request will send bearer auth
request({
'method': 'GET',
Expand Down Expand Up @@ -95,7 +95,7 @@ tape('', function(t) {
})
})

tape('', function(t) {
tape('using .auth, sendImmediately = false', function(t) {
request
.get('http://localhost:6767/test/')
.auth(null, null, false, 'theToken')
Expand All @@ -106,7 +106,7 @@ tape('', function(t) {
})
})

tape('', function(t) {
tape('using .auth, sendImmediately = true', function(t) {
request
.get('http://localhost:6767/test/')
.auth(null, null, true, 'theToken')
Expand All @@ -117,7 +117,7 @@ tape('', function(t) {
})
})

tape('', function(t) {
tape('bearer is a function', function(t) {
request({
'method': 'GET',
'uri': 'http://localhost:6767/test/',
Expand All @@ -132,7 +132,7 @@ tape('', function(t) {
})
})

tape('', function(t) {
tape('bearer is a function, path = test2', function(t) {
// If we don't set sendImmediately = false, request will send bearer auth
request({
'method': 'GET',
Expand All @@ -147,6 +147,36 @@ tape('', function(t) {
})
})

tape('no auth method', function(t) {
t.throws(function() {
request({
'method': 'GET',
'uri': 'http://localhost:6767/test2/',
'auth': {
'bearer': undefined
}
}, function(error, res, body) {
t.fail('Requests without a valid auth mechanism are not valid')
t.end()
})
}, /no auth mechanism defined/)
t.end()
})

tape('null bearer', function(t) {
request({
'method': 'GET',
'uri': 'http://localhost:6767/test2/',
'auth': {
'bearer': null
}
}, function(error, res, body) {
t.equal(res.statusCode, 401)
t.equal(numBearerRequests, 12)
t.end()
})
})

tape('cleanup', function(t) {
bearerServer.close(function() {
t.end()
Expand Down