Skip to content

Commit

Permalink
fix: allow distributed claims to be missing from the response
Browse files Browse the repository at this point in the history
fixes #197
  • Loading branch information
panva committed Oct 24, 2019
1 parent 1b182e1 commit 48d6633
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
11 changes: 7 additions & 4 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,14 @@ function cleanUpClaims(claims) {
}
}

function assignClaim(target, source, sourceName) {
function assignClaim(target, source, sourceName, throwOnMissing = true) {
return ([claim, inSource]) => {
if (inSource === sourceName) {
if (source[claim] === undefined) {
if (throwOnMissing && source[claim] === undefined) {
throw new RPError(`expected claim "${claim}" in "${sourceName}"`);
} else if (source[claim] !== undefined) {
target[claim] = source[claim];
}
target[claim] = source[claim];
delete target._claim_names[claim];
}
};
Expand Down Expand Up @@ -1196,7 +1197,9 @@ module.exports = (issuer, aadIssValidation = false) => class Client extends Base

const decoded = await claimJWT.call(this, body);
delete claims._claim_sources[sourceName];
Object.entries(claims._claim_names).forEach(assignClaim(claims, decoded, sourceName));
Object.entries(claims._claim_names).forEach(
assignClaim(claims, decoded, sourceName, false),
);
} catch (err) {
err.src = sourceName;
throw err;
Expand Down
8 changes: 4 additions & 4 deletions test/client/client_instance.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2442,7 +2442,7 @@ describe('Client', () => {
});
});

it('validates claims that should be present are', function () {
it('allows claims to be omitted', function () {
return getJWT({
// credit_history: 'foobar',
}, 'src1').then((jwt) => {
Expand All @@ -2460,9 +2460,9 @@ describe('Client', () => {
};

return this.client.fetchDistributedClaims(userinfo)
.then(fail, function (error) {
expect(error).to.have.property('src', 'src1');
expect(error.message).to.equal('expected claim "credit_history" in "src1"');
.then((result) => {
expect(result).to.eql({ sub: 'userID' });
expect(result).to.equal(userinfo);
});
});
});
Expand Down

0 comments on commit 48d6633

Please sign in to comment.