Skip to content

Commit

Permalink
add merge claims (with default to false) to deal with merging objects…
Browse files Browse the repository at this point in the history
… from user info IdentityModel#861
  • Loading branch information
brockallen committed Jan 28, 2021
1 parent 938f5bb commit 7b16f46
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ export interface OidcClientSettings {
readonly clockService?: ClockService;
readonly stateStore?: StateStore;
readonly userInfoJwtIssuer?: 'ANY' | 'OP' | string;
readonly mergeClaims?: boolean;
ResponseValidatorCtor?: ResponseValidatorCtor;
MetadataServiceCtor?: MetadataServiceCtor;
/** An object containing additional query string parameters to be including in the authorization request */
Expand Down
7 changes: 6 additions & 1 deletion src/OidcClientSettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export class OidcClientSettings {
clockSkew = DefaultClockSkewInSeconds,
clockService = new ClockService(),
userInfoJwtIssuer = 'OP',
mergeClaims = false,
// other behavior
stateStore = new WebStorageStateStore(),
ResponseValidatorCtor = ResponseValidator,
Expand Down Expand Up @@ -67,6 +68,7 @@ export class OidcClientSettings {
this._clockSkew = clockSkew;
this._clockService = clockService;
this._userInfoJwtIssuer = userInfoJwtIssuer;
this._mergeClaims = !!mergeClaims;

this._stateStore = stateStore;
this._validator = new ResponseValidatorCtor(this);
Expand Down Expand Up @@ -194,7 +196,10 @@ export class OidcClientSettings {
get userInfoJwtIssuer() {
return this._userInfoJwtIssuer;
}

get mergeClaims() {
return this._mergeClaims;
}

get stateStore() {
return this._stateStore;
}
Expand Down
4 changes: 2 additions & 2 deletions src/ResponseValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,9 @@ export class ResponseValidator {
}
}
else if (result[name] !== value) {
if (typeof value === 'object') {
if (typeof value === 'object' && this._settings.mergeClaims) {
result[name] = this._mergeClaims(result[name], value);
}
}
else {
result[name] = [result[name], value];
}
Expand Down
14 changes: 12 additions & 2 deletions test/unit/ResponseValidator.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -544,13 +544,24 @@ describe("ResponseValidator", function () {

var result = subject._mergeClaims(c1, c2);
result.should.deep.equal({ a: 'apple', c: 'carrot', b: 'banana' });
});

it("should not merge claims when claim types are objects", function () {

var c1 = { custom: {'apple': 'foo', 'pear': 'bar'} };
var c2 = { custom: {'apple': 'foo', 'orange': 'peel'}, b: 'banana' };

var result = subject._mergeClaims(c1, c2);
result.should.deep.equal({ custom: [{'apple': 'foo', 'pear': 'bar'}, {'apple': 'foo', 'orange': 'peel'}], b: 'banana' });
});

it("should merge claims when claim types are objects", function () {
it("should merge claims when claim types are objects when mergeClaims settings is true", function () {

settings.mergeClaims = true;

var c1 = { custom: {'apple': 'foo', 'pear': 'bar'} };
var c2 = { custom: {'apple': 'foo', 'orange': 'peel'}, b: 'banana' };

var result = subject._mergeClaims(c1, c2);
result.should.deep.equal({ custom: {'apple': 'foo', 'pear': 'bar', 'orange': 'peel'}, b: 'banana' });
});
Expand All @@ -562,7 +573,6 @@ describe("ResponseValidator", function () {

var result = subject._mergeClaims(c1, c2);
result.should.deep.equal({ a: ['apple', 'carrot'], b: 'banana' });

});

it("should merge arrays of same claim types into array", function () {
Expand Down

0 comments on commit 7b16f46

Please sign in to comment.