Skip to content

Commit 186016f

Browse files
authored
Merge pull request #94 from koajs/consistent-errors-100-percent-coverage
Consistent error messages, 100% coverage, misc tweaks
2 parents a19d21f + e34844e commit 186016f

File tree

7 files changed

+116
-55
lines changed

7 files changed

+116
-55
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ node_modules/*
33
npm-debug.log
44
coverage
55
.nyc_output
6+
.idea

lib/get-secret.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
const { decode } = require('jsonwebtoken');
22

33
module.exports = async (provider, token) => {
4-
const { header } = decode(token, { complete: true });
54

6-
return provider(header);
7-
}
5+
const decoded = decode(token, { complete: true });
6+
7+
if (!decoded || !decoded.header) {
8+
throw new Error('Invalid token');
9+
}
10+
11+
return provider(decoded.header);
12+
};

lib/index.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,38 +19,37 @@ module.exports = (opts = {}) => {
1919
tokenResolvers.find(resolver => token = resolver(ctx, opts));
2020

2121
if (!token && !passthrough) {
22-
ctx.throw(401, 'No authentication token found\n');
22+
ctx.throw(401, debug ? 'Token not found' : 'Authentication Error');
2323
}
2424

25-
let { state: { secret = opts.secret } = {} } = ctx;
26-
if (!secret) {
27-
ctx.throw(401, 'Invalid secret\n');
28-
}
25+
let { state: { secret = opts.secret } } = ctx;
2926

3027
try {
3128
if(typeof secret === 'function') {
3229
secret = await getSecret(secret, token);
3330
}
3431

32+
if (!secret) {
33+
ctx.throw(401, 'Secret not provided');
34+
}
35+
3536
const decodedToken = await verify(token, secret, opts);
3637

3738
if (isRevoked) {
3839
const tokenRevoked = await isRevoked(ctx, decodedToken, token);
3940
if (tokenRevoked) {
40-
throw new Error('Revoked token');
41+
throw new Error('Token revoked');
4142
}
4243
}
4344

44-
ctx.state = ctx.state || {};
4545
ctx.state[key] = decodedToken;
4646
if (tokenKey) {
4747
ctx.state[tokenKey] = token;
4848
}
4949

5050
} catch (e) {
5151
if (!passthrough) {
52-
const debugString = debug ? ` - ${e.message}` : '';
53-
const msg = `Invalid token${debugString}\n`;
52+
const msg = debug ? e.message : 'Authentication Error';
5453
ctx.throw(401, msg);
5554
}
5655
}

lib/resolvers/auth-header.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ module.exports = function resolveAuthorizationHeader(ctx, opts) {
2323
}
2424
}
2525
if (!opts.passthrough) {
26-
ctx.throw(401, 'Bad Authorization header format. Format is "Authorization: Bearer <token>"\n');
26+
ctx.throw(401, 'Bad Authorization header format. Format is "Authorization: Bearer <token>"');
2727
}
2828
};

lib/resolvers/cookie.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@
99
*/
1010
module.exports = function resolveCookies(ctx, opts) {
1111
return opts.cookie && ctx.cookies.get(opts.cookie);
12-
}
12+
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,6 @@
5353
},
5454
"scripts": {
5555
"test": "nyc npm run test-only",
56-
"test-only": "mocha --reporter spec --bail test/test.js"
56+
"test-only": "mocha --reporter spec test/test.js"
5757
}
5858
}

0 commit comments

Comments
 (0)