Skip to content

Commit b22fa2f

Browse files
committed
Fix auth error logs. Closes hapijs#2564
1 parent a92a254 commit b22fa2f

File tree

6 files changed

+68
-28
lines changed

6 files changed

+68
-28
lines changed

API.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1761,7 +1761,9 @@ Emitted by the server `'request-internal'` event:
17611761
- `auth` `unauthenticated` `response` `{strategy}` - the authentication strategy listed returned a
17621762
non-error response (e.g. a redirect to a login page).
17631763
- `auth` `unauthenticated` `error` `{strategy}` - the request failed to pass the listed
1764-
authentication strategy.
1764+
authentication strategy (invalid credentials).
1765+
- `auth` `unauthenticated` `missing` `{strategy}` - the request failed to pass the listed
1766+
authentication strategy (no credentials found).
17651767
- `auth` `unauthenticated` `try` `{strategy}` - the request failed to pass the listed
17661768
authentication strategy in `'try'` mode and will continue.
17671769
- `auth` `scope` `error` `{strategy}` - the request authenticated but failed to meet the scope

lib/auth.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,12 +250,11 @@ internals.Auth.prototype._authenticate = function (request, next) {
250250
return next(err);
251251
}
252252

253-
request._log(['auth', 'unauthenticated', 'error', name], err);
254-
255253
if (err.isMissing) {
256254

257255
// Try next name
258256

257+
request._log(['auth', 'unauthenticated', 'missing', name], err);
259258
authErrors.push(err.output.headers['WWW-Authenticate']);
260259
return authenticate();
261260
}
@@ -270,6 +269,7 @@ internals.Auth.prototype._authenticate = function (request, next) {
270269
return next();
271270
}
272271

272+
request._log(['auth', 'unauthenticated', 'error', name], err);
273273
return next(err);
274274
}
275275

lib/plugin.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,15 @@ exports = module.exports = internals.Plugin = function (server, connections, env
5858
this.auth = {
5959
default: function (options) {
6060

61-
self._applyChild('auth', 'default', [options]);
61+
self._applyChild('auth.default', 'auth', 'default', [options]);
6262
},
6363
scheme: function (name, scheme) {
6464

65-
self._applyChild('auth', 'scheme', [name, scheme]);
65+
self._applyChild('auth.scheme', 'auth', 'scheme', [name, scheme]);
6666
},
6767
strategy: function (name, scheme, mode, options) {
6868

69-
self._applyChild('auth', 'strategy', [name, scheme, mode, options]);
69+
self._applyChild('auth.strategy', 'auth', 'strategy', [name, scheme, mode, options]);
7070
},
7171
test: function (name, request, next) {
7272

@@ -349,7 +349,7 @@ internals.Plugin.prototype.expose = function (key, value) {
349349

350350
internals.Plugin.prototype.ext = function (event, func, options) {
351351

352-
this._apply(Connection.prototype._ext, [event, func, options, this.realm]);
352+
this._apply('ext', Connection.prototype._ext, [event, func, options, this.realm]);
353353
};
354354

355355

@@ -430,13 +430,13 @@ internals.Plugin.prototype.route = function (options) {
430430
Hoek.assert(typeof options === 'object', 'Invalid route options');
431431
Hoek.assert(this.connections.length, 'Cannot add a route without any connections');
432432

433-
this._apply(Connection.prototype._route, [options, this.realm]);
433+
this._apply('route', Connection.prototype._route, [options, this.realm]);
434434
};
435435

436436

437437
internals.Plugin.prototype.state = function (name, options) {
438438

439-
this._applyChild('states', 'add', [name, options]);
439+
this._applyChild('state', 'states', 'add', [name, options]);
440440
};
441441

442442

@@ -452,15 +452,19 @@ internals.Plugin.prototype.table = function (host) {
452452
};
453453

454454

455-
internals.Plugin.prototype._apply = function (func, args) {
455+
internals.Plugin.prototype._apply = function (type, func, args) {
456+
457+
Hoek.assert(this.connections.length, 'Cannot add ' + type + ' without a connection');
456458

457459
for (var i = 0, il = this.connections.length; i < il; ++i) {
458460
func.apply(this.connections[i], args);
459461
}
460462
};
461463

462464

463-
internals.Plugin.prototype._applyChild = function (child, func, args) {
465+
internals.Plugin.prototype._applyChild = function (type, child, func, args) {
466+
467+
Hoek.assert(this.connections.length, 'Cannot add ' + type + ' without a connection');
464468

465469
for (var i = 0, il = this.connections.length; i < il; ++i) {
466470
var obj = this.connections[i][child];

npm-shrinkwrap.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "hapi",
33
"description": "HTTP Server framework",
44
"homepage": "http://hapijs.com",
5-
"version": "8.5.1",
5+
"version": "8.5.2",
66
"repository": {
77
"type": "git",
88
"url": "git://github.com/hapijs/hapi"

test/plugin.js

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1830,6 +1830,17 @@ describe('Plugin', function () {
18301830
});
18311831
});
18321832
});
1833+
1834+
it('throws when adding ext without connections', function (done) {
1835+
1836+
var server = new Hapi.Server();
1837+
expect(function () {
1838+
1839+
server.ext('onRequest', function () { });
1840+
}).to.throw('Cannot add ext without a connection');
1841+
1842+
done();
1843+
});
18331844
});
18341845

18351846
describe('handler()', function () {
@@ -2538,6 +2549,20 @@ describe('Plugin', function () {
25382549
});
25392550
});
25402551

2552+
describe('state()', function () {
2553+
2554+
it('throws when adding state without connections', function (done) {
2555+
2556+
var server = new Hapi.Server();
2557+
expect(function () {
2558+
2559+
server.state('sid', { encoding: 'base64' });
2560+
}).to.throw('Cannot add state without a connection');
2561+
2562+
done();
2563+
});
2564+
});
2565+
25412566
describe('views()', function () {
25422567

25432568
it('requires plugin with views', function (done) {
@@ -2715,36 +2740,45 @@ internals.plugins = {
27152740
return next();
27162741
});
27172742

2718-
server.select('a').ext('onRequest', function (request, reply) {
2743+
var selection = server.select('a');
2744+
if (selection.connections.length) {
2745+
selection.ext('onRequest', function (request, reply) {
27192746

2720-
request.app.deps = request.app.deps || '|';
2721-
request.app.deps += '1|';
2722-
return reply.continue();
2723-
}, { after: 'deps3' });
2747+
request.app.deps = request.app.deps || '|';
2748+
request.app.deps += '1|';
2749+
return reply.continue();
2750+
}, { after: 'deps3' });
2751+
}
27242752

27252753
return next();
27262754
},
27272755
deps2: function (server, options, next) {
27282756

2729-
server.select('b').ext('onRequest', function (request, reply) {
2757+
var selection = server.select('b');
2758+
if (selection.connections.length) {
2759+
selection.ext('onRequest', function (request, reply) {
27302760

2731-
request.app.deps = request.app.deps || '|';
2732-
request.app.deps += '2|';
2733-
return reply.continue();
2734-
}, { after: 'deps3', before: 'deps1' });
2761+
request.app.deps = request.app.deps || '|';
2762+
request.app.deps += '2|';
2763+
return reply.continue();
2764+
}, { after: 'deps3', before: 'deps1' });
2765+
}
27352766

27362767
server.expose('breaking', 'bad');
27372768

27382769
return next();
27392770
},
27402771
deps3: function (server, options, next) {
27412772

2742-
server.select('c').ext('onRequest', function (request, reply) {
2773+
var selection = server.select('c');
2774+
if (selection.connections.length) {
2775+
selection.ext('onRequest', function (request, reply) {
27432776

2744-
request.app.deps = request.app.deps || '|';
2745-
request.app.deps += '3|';
2746-
return reply.continue();
2747-
});
2777+
request.app.deps = request.app.deps || '|';
2778+
request.app.deps += '3|';
2779+
return reply.continue();
2780+
});
2781+
}
27482782

27492783
return next();
27502784
},

0 commit comments

Comments
 (0)