Skip to content

Commit

Permalink
Prevent default value for opts.tokenKey from overwriting `ctx.state…
Browse files Browse the repository at this point in the history
….token` (#78)

* Fix issue 77

Prevent default value for `opts.tokenKey` from overwriting `ctx.state.token`

* Add test

* add missing `return next()` to test
  • Loading branch information
sdd authored Jan 24, 2017
1 parent ba704b1 commit 908fcb6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
1 change: 0 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const unless = require('koa-unless');
module.exports = opts => {
opts = opts || {};
opts.key = opts.key || 'user';
opts.tokenKey = opts.tokenKey || 'token';

const tokenResolvers = [resolveCookies, resolveAuthorizationHeader];

Expand Down
25 changes: 25 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,31 @@ describe('success tests', () => {
.expect(validUserResponse)
.end(done);
});

it('should not overwrite ctx.state.token on successful token verification if opts.tokenKey is undefined', done => {
const validUserResponse = res => res.body.token === "DONT_CLOBBER_ME" && "ctx.state.token not clobbered";

const secret = 'shhhhhh';
const token = jwt.sign({foo: 'bar'}, secret);

const app = new Koa();

app.use((ctx, next) => {
ctx.state = { token: 'DONT_CLOBBER_ME' };
return next();
});
app.use(koajwt({ secret: secret, key: 'jwtdata' }));
app.use(ctx => {
ctx.body = { token: ctx.state.token };
});

request(app.listen())
.get('/')
.set('Authorization', 'Bearer ' + token)
.expect(200)
.expect(validUserResponse)
.end(done);
});

it('should populate the raw token to ctx.state, in key from opts.tokenKey', done => {
const validUserResponse = res => res.body.token !== token && "Token not passed through";
Expand Down

0 comments on commit 908fcb6

Please sign in to comment.