Skip to content

Commit

Permalink
refactor: only allow objects as claims configuration parameter
Browse files Browse the repository at this point in the history
BREAKING CHANGE: The `claims` configuration property can no longer be
a `Map` instance, only plain objects are allowed.
  • Loading branch information
panva committed Aug 14, 2020
1 parent e7309af commit 2ac59b7
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 59 deletions.
2 changes: 1 addition & 1 deletion lib/helpers/claims.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module.exports = function getClaims(provider) {
scope(value = '') {
assert(!Object.keys(this.filter).length, 'scope cannot be assigned after mask has been set');
value.split(' ').forEach((scope) => {
this.mask(claimConfig.get(scope));
this.mask(claimConfig[scope]);
});
return this;
}
Expand Down
42 changes: 10 additions & 32 deletions lib/helpers/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,8 @@ class Configuration {
featuresTypeErrorCheck(this);
clientAuthDefaults(this.clientDefaults);

this.fixMapClaims(); // TODO: remove the Map option in 7.x

this.logDraftNotice();

this.ensureMaps();
this.ensureSets();

this.fixResponseTypes();
Expand Down Expand Up @@ -110,25 +107,6 @@ class Configuration {
}
}

fixMapClaims() { // TODO: remove the Map option in 7.x
if (this.claims instanceof Map) {
Object.entries(this.defaults.claims).forEach(([key, value]) => {
if (!this.claims.has(key)) {
this.claims.set(key, value);
}
});
}
}

ensureMaps() {
if (!(this.claims instanceof Map)) {
if (!isPlainObject(this.claims)) {
throw new TypeError('claims must be a plain javascript object or Map');
}
this.claims = new Map(Object.entries(this.claims));
}
}

ensureSets() {
[
'scopes', 'subjectTypes', 'extraParams', 'acrValues',
Expand Down Expand Up @@ -191,7 +169,7 @@ class Configuration {

collectScopes() {
const claimDefinedScopes = [];
this.claims.forEach((value, key) => {
Object.entries(this.claims).forEach(([key, value]) => {
if (isPlainObject(value) || Array.isArray(value)) {
claimDefinedScopes.push(key);
}
Expand All @@ -213,38 +191,38 @@ class Configuration {
}

unpackArrayClaims() {
this.claims.forEach((value, key) => {
Object.entries(this.claims).forEach(([key, value]) => {
if (Array.isArray(value)) {
this.claims.set(key, value.reduce((accumulator, claim) => {
this.claims[key] = value.reduce((accumulator, claim) => {
const scope = accumulator;
scope[claim] = null;
return scope;
}, {}));
}, {});
}
});
}

ensureOpenIdSub() {
if (!Object.keys(this.claims.get('openid')).includes('sub')) {
this.claims.get('openid').sub = null;
if (!Object.keys(this.claims.openid).includes('sub')) {
this.claims.openid.sub = null;
}
}

removeAcrIfEmpty() {
if (!this.acrValues.size) {
this.claims.delete('acr');
delete this.claims.acr;
}
}

collectClaims() {
const claims = new Set();
this.scopes.forEach((scope) => {
if (this.claims.has(scope)) {
Object.keys(this.claims.get(scope)).forEach(Set.prototype.add.bind(claims));
if (scope in this.claims) {
Object.keys(this.claims[scope]).forEach(Set.prototype.add.bind(claims));
}
});

this.claims.forEach((value, key) => {
Object.entries(this.claims).forEach(([key, value]) => {
if (value === null) claims.add(key);
});

Expand Down
16 changes: 0 additions & 16 deletions test/configuration/constructor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,6 @@ describe('Provider configuration', () => {
});
});

describe('claims', () => {
it('only accepts maps and objects', () => {
new Provider('http://localhost:3000', { claims: { foo: null } });
new Provider('http://localhost:3000', { claims: new Map(Object.entries({ foo: null })) });
expect(() => {
const claims = new class {
constructor() {
this.foo = null;
}
}();

new Provider('http://localhost:3000', { claims });
}).to.throw('claims must be a plain javascript object or Map');
});
});

describe('ttl', () => {
it('checks the values are positive safe integers or functions', () => {
let throws = [
Expand Down
18 changes: 8 additions & 10 deletions test/configuration/scopes_claims.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('custom claims', () => {
},
});

expect(i(provider).configuration('claims').get('openid')).to.eql({
expect(i(provider).configuration('claims').openid).to.eql({
sub: null,
foo: null,
});
Expand All @@ -23,22 +23,20 @@ describe('custom claims', () => {
},
});

expect(i(provider).configuration('claims').get('openid')).to.eql({
expect(i(provider).configuration('claims').openid).to.eql({
sub: null,
foo: null,
});
});

it('detects new scopes from claims definition', () => {
const claims = new Map(Object.entries({
insurance: ['company_name', 'coverage'],
payment: {
preferred_method: null,
},
}));

const provider = new Provider('https://op.example.com', {
claims,
claims: {
insurance: ['company_name', 'coverage'],
payment: {
preferred_method: null,
},
},
});

expect(i(provider).configuration('scopes')).to.contain('insurance', 'payment');
Expand Down

0 comments on commit 2ac59b7

Please sign in to comment.