Skip to content

Commit

Permalink
remove the hard-coding of the word Token` from all boom (raiseError…
Browse files Browse the repository at this point in the history
…) calls. fixes #180
  • Loading branch information
nelsonic committed Aug 22, 2016
1 parent 00b92b3 commit 3a1fe06
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,14 @@ internals.implementation = function (server, options) {

var token = extract(request, options); // extract token from Header, Cookie or Query param

var tokenType = options.tokenType || 'Token'; //

if (!token) {
return reply(raiseError('unauthorized', null, 'Token'));
return reply(raiseError('unauthorized', null, tokenType));
}

if (!extract.isValid(token)) { // quick check for validity of token format
return reply(raiseError('unauthorized', 'Invalid token format', 'Token'));
return reply(raiseError('unauthorized', 'Invalid token format', tokenType));
} // verification is done later, but we want to avoid decoding if malformed
request.auth.token = token; // keep encoded JWT available in the request lifecycle
// otherwise use the same key (String) to validate all JWTs
Expand All @@ -61,7 +63,7 @@ internals.implementation = function (server, options) {
decoded = JWT.decode(token, { complete: options.complete || false });
}
catch(e) { // request should still FAIL if the token does not decode.
return reply(raiseError('unauthorized', 'Invalid token format', 'Token'));
return reply(raiseError('unauthorized', 'Invalid token format', tokenType));
}

if(options.key && typeof options.validateFunc === 'function') {
Expand All @@ -78,15 +80,15 @@ internals.implementation = function (server, options) {
var verifyOptions = options.verifyOptions || {};
JWT.verify(token, key, verifyOptions, function (err, decoded) {
if (err) {
return reply(raiseError('unauthorized', 'Invalid token', 'Token'), null, { credentials: null });
return reply(raiseError('unauthorized', 'Invalid token', tokenType), null, { credentials: null });
}
else { // see: http://hapijs.com/tutorials/auth for validateFunc signature
options.validateFunc(decoded, request, function (err, valid, credentials) { // bring your own checks
if (err) {
return reply(raiseError('wrap', err));
}
else if (!valid) {
return reply(raiseError('unauthorized', 'Invalid credentials', 'Token'), null, { credentials: credentials || decoded });
return reply(raiseError('unauthorized', 'Invalid credentials', tokenType), null, { credentials: credentials || decoded });
}
else {
return reply.continue({ credentials: credentials || decoded, artifacts: token });
Expand All @@ -102,7 +104,7 @@ internals.implementation = function (server, options) {
return reply(raiseError('wrap', err));
}
else if (!valid) {
return reply(raiseError('unauthorized', 'Invalid credentials', 'Token'), null, { credentials: decoded });
return reply(raiseError('unauthorized', 'Invalid credentials', tokenType), null, { credentials: decoded });
} else {
return reply.continue({ credentials: credentials, artifacts: token });
}
Expand Down

0 comments on commit 3a1fe06

Please sign in to comment.