diff --git a/lib/passport-http/strategies/basic.js b/lib/passport-http/strategies/basic.js index 5b1f049..aacd651 100644 --- a/lib/passport-http/strategies/basic.js +++ b/lib/passport-http/strategies/basic.js @@ -50,6 +50,7 @@ function BasicStrategy(options, verify) { this._verify = verify; this._realm = options.realm || 'Users'; this._passReqToCallback = options.passReqToCallback; + this._acceptEmptyPassword = options.acceptEmptyPassword; } /** @@ -75,11 +76,11 @@ BasicStrategy.prototype.authenticate = function(req) { , credentials = new Buffer(parts[1], 'base64').toString().split(':'); if (!/Basic/i.test(scheme)) { return this.fail(this._challenge()); } - if (credentials.length < 2) { return this.fail(400); } + if (!this._acceptEmptyPassword && (credentials.length < 2)) { return this.fail(400); } var userid = credentials[0]; var password = credentials[1]; - if (!userid || !password) { + if (!userid || (!password && !this._acceptEmptyPassword)) { return this.fail(this._challenge()); } diff --git a/package.json b/package.json index 74bd040..8d82093 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,15 @@ "name": "passport-http", "version": "0.2.2", "description": "HTTP Basic and Digest authentication strategies for Passport.", - "keywords": ["passport", "http", "basic", "digest", "auth", "authn", "authentication"], + "keywords": [ + "passport", + "http", + "basic", + "digest", + "auth", + "authn", + "authentication" + ], "repository": { "type": "git", "url": "git://github.com/jaredhanson/passport-http.git" @@ -15,20 +23,24 @@ "email": "jaredhanson@gmail.com", "url": "http://www.jaredhanson.net/" }, - "licenses": [ { - "type": "MIT", - "url": "http://www.opensource.org/licenses/MIT" - } ], + "licenses": [ + { + "type": "MIT", + "url": "http://www.opensource.org/licenses/MIT" + } + ], "main": "./lib/passport-http", "dependencies": { "pkginfo": "0.2.x", "passport": "~0.1.3" }, "devDependencies": { - "vows": "0.6.x" + "vows": "^0.7.0" }, "scripts": { "test": "NODE_PATH=lib node_modules/.bin/vows test/*-test.js test/**/*-test.js" }, - "engines": { "node": ">= 0.4.0" } + "engines": { + "node": ">= 0.4.0" + } } diff --git a/test/strategies/basic-test.js b/test/strategies/basic-test.js index 6a5b453..20ca4c1 100644 --- a/test/strategies/basic-test.js +++ b/test/strategies/basic-test.js @@ -440,4 +440,40 @@ vows.describe('BasicStrategy').addBatch({ }, }, + 'strategy handling a request with empty password with acceptEmptyPassword=true option': { + topic: function() { + var strategy = new BasicStrategy({acceptEmptyPassword:true}, function(userid, password, done) { + done(null, { username: userid, password: password }); + }); + return strategy; + }, + + 'after augmenting with actions': { + topic: function(strategy) { + var self = this; + var req = {}; + strategy.success = function(user) { + self.callback(null, user); + } + strategy.fail = function() { + self.callback(new Error('should not be called')); + } + + req.headers = {}; + req.headers.authorization = 'Basic Ym9iOg=='; + process.nextTick(function () { + strategy.authenticate(req); + }); + }, + + 'should not generate an error' : function(err, user) { + assert.isNull(err); + }, + 'should authenticate' : function(err, user) { + assert.equal(user.username, 'bob'); + assert.equal(user.password, ''); + }, + }, + }, + }).export(module);