Skip to content

Commit

Permalink
fix: allow empty body without content-type on userinfo
Browse files Browse the repository at this point in the history
  • Loading branch information
panva committed Dec 9, 2019
1 parent 5a43ca7 commit d5148ad
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
9 changes: 6 additions & 3 deletions lib/shared/selective_body.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ const { InvalidRequest } = require('../helpers/errors');
let warned;

async function selectiveBody(cty, ctx, next) {
if (ctx.is(cty)) {
if (ctx.request.length && ctx.is(cty)) {
try {
let usedFallback;
const body = await (() => {
if (ctx.req.readable) {
return raw(ctx.req, {
length: ctx.length,
length: ctx.request.length,
limit: '56kb',
encoding: ctx.charset,
});
Expand Down Expand Up @@ -47,8 +47,11 @@ is not recommended, resolving to use req.body or request.body instead');
}

await next();
} else {
} else if (ctx.request.length) {
throw new InvalidRequest(`only ${cty} content-type bodies are supported on ${ctx.method} ${ctx.path}`);
} else {
ctx.oidc.body = {};
await next();
}
}

Expand Down
5 changes: 5 additions & 0 deletions test/userinfo/bearer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,10 @@ describe('providing Bearer token', () => {
.send('access_token=')
.expect(this.failWith(400, 'invalid_request', 'no access token provided'));
});

it('empty body w/ auth header', function () {
return this.agent.post('/me')
.expect(this.failWith(400, 'invalid_request', 'no access token provided'));
});
});
});
12 changes: 11 additions & 1 deletion test/userinfo/userinfo.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('userinfo /me', () => {
}).to.throw('jwtUserinfo is only available in conjuction with userinfo');
});

it('returns 200 OK and user claims except the rejected ones', function () {
it('[get] returns 200 OK and user claims except the rejected ones', function () {
return this.agent.get('/me')
.auth(this.access_token, { type: 'bearer' })
.expect(200)
Expand All @@ -46,6 +46,16 @@ describe('userinfo /me', () => {
});
});

it('[post] returns 200 OK and user claims except the rejected ones', function () {
return this.agent.post('/me')
.auth(this.access_token, { type: 'bearer' })
.expect(200)
.expect((response) => {
expect(response.body).to.have.keys(['sub', 'email']);
expect(response.body).not.to.have.keys(['email_verified']);
});
});

it('populates ctx.oidc.entities', function (done) {
this.provider.use(this.assertOnce((ctx) => {
expect(ctx.oidc.entities).to.have.keys('Client', 'AccessToken', 'Account');
Expand Down

0 comments on commit d5148ad

Please sign in to comment.