Skip to content

Commit 400e7f8

Browse files
committed
added support for extra/custom query params with test
1 parent afbc66d commit 400e7f8

File tree

5 files changed

+85
-6
lines changed

5 files changed

+85
-6
lines changed

src/OidcClient.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ export default class OidcClient {
4040

4141
createSigninRequest({
4242
response_type, scope, redirect_uri,
43-
// data was meant to be the place a caller could indiate the data to
43+
// data was meant to be the place a caller could indicate the data to
4444
// have round tripped, but people were getting confused, so i added state (since that matches the spec)
4545
// and so now if data is not passed, but state is then state will be used
46-
data, state,
47-
prompt, display, max_age, ui_locales, id_token_hint, login_hint, acr_values, resource, request, request_uri} = {},
46+
data, state, prompt, display, max_age, ui_locales, id_token_hint, login_hint, acr_values,
47+
resource, request, request_uri, extraQueryParams } = {},
4848
stateStore
4949
) {
5050
Log.debug("OidcClient.createSigninRequest");
@@ -61,6 +61,7 @@ export default class OidcClient {
6161
ui_locales = ui_locales || this._settings.ui_locales;
6262
acr_values = acr_values || this._settings.acr_values;
6363
resource = resource || this._settings.resource;
64+
extraQueryParams = extraQueryParams || this._settings.extraQueryParams;
6465

6566
let authority = this._settings.authority;
6667

@@ -75,7 +76,8 @@ export default class OidcClient {
7576
scope,
7677
data: data || state,
7778
authority,
78-
prompt, display, max_age, ui_locales, id_token_hint, login_hint, acr_values, resource, request, request_uri
79+
prompt, display, max_age, ui_locales, id_token_hint, login_hint, acr_values,
80+
resource, request, request_uri, extraQueryParams,
7981
});
8082

8183
var signinState = signinRequest.state;

src/OidcClientSettings.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ export default class OidcClientSettings {
2828
// other behavior
2929
stateStore = new WebStorageStateStore(),
3030
ResponseValidatorCtor = ResponseValidator,
31-
MetadataServiceCtor = MetadataService
31+
MetadataServiceCtor = MetadataService,
32+
// extra query params
33+
extraQueryParams = {}
3234
} = {}) {
3335

3436
this._authority = authority;
@@ -58,6 +60,8 @@ export default class OidcClientSettings {
5860
this._stateStore = stateStore;
5961
this._validator = new ResponseValidatorCtor(this);
6062
this._metadataService = new MetadataServiceCtor(this);
63+
64+
this._extraQueryParams = typeof extraQueryParams === 'object' ? extraQueryParams : {};
6165
}
6266

6367
// client config
@@ -179,4 +183,16 @@ export default class OidcClientSettings {
179183
get metadataService() {
180184
return this._metadataService;
181185
}
186+
187+
// extra query params
188+
get extraQueryParams() {
189+
return this._extraQueryParams;
190+
}
191+
set extraQueryParams(value) {
192+
if (typeof value === 'object'){
193+
this._extraQueryParams = value;
194+
} else {
195+
this._extraQueryParams = {};
196+
}
197+
}
182198
}

src/SigninRequest.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ export default class SigninRequest {
1010
// mandatory
1111
url, client_id, redirect_uri, response_type, scope, authority,
1212
// optional
13-
data, prompt, display, max_age, ui_locales, id_token_hint, login_hint, acr_values, resource, request, request_uri
13+
data, prompt, display, max_age, ui_locales, id_token_hint, login_hint, acr_values, resource,
14+
request, request_uri, extraQueryParams,
1415
}) {
1516
if (!url) {
1617
Log.error("No url passed to SigninRequest");
@@ -57,6 +58,10 @@ export default class SigninRequest {
5758
}
5859
}
5960

61+
for(let key in extraQueryParams){
62+
url = UrlUtility.addQueryParam(url, key, extraQueryParams[key])
63+
}
64+
6065
this.url = url;
6166
}
6267

test/unit/OidcClientSettings.spec.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,4 +392,51 @@ describe("OidcClientSettings", function () {
392392
});
393393
});
394394

395+
describe("extraQueryParams", function() {
396+
397+
it("should use default value", function () {
398+
let subject = new OidcClientSettings({
399+
client_id: 'client'
400+
});
401+
subject.extraQueryParams.should.deep.equal({});
402+
});
403+
404+
it("should return value from initial settings", function () {
405+
let subject = new OidcClientSettings({
406+
client_id: 'client',
407+
extraQueryParams: {
408+
'hd': 'domain.com'
409+
}
410+
});
411+
subject.extraQueryParams.should.deep.equal({ 'hd': 'domain.com' });
412+
});
413+
414+
it("should not set value from initial settings if not object, but set default value ({})", function () {
415+
let subject = new OidcClientSettings({
416+
client_id: 'client',
417+
extraQueryParams: 123456
418+
});
419+
subject.extraQueryParams.should.deep.equal({});
420+
});
421+
422+
it("should set it if object", function () {
423+
let subject = new OidcClientSettings({
424+
client_id: 'client',
425+
});
426+
subject.extraQueryParams = { 'hd': 'domain.com' };
427+
subject.extraQueryParams.should.deep.equal({ 'hd': 'domain.com' });
428+
});
429+
430+
it("should clear it if not object", function() {
431+
let subject = new OidcClientSettings({
432+
client_id: 'client',
433+
extraQueryParams: {
434+
'hd': 'domain.com',
435+
}
436+
});
437+
subject.extraQueryParams = undefined;
438+
subject.extraQueryParams.should.deep.equal({});
439+
});
440+
})
441+
395442
});

test/unit/SigninRequest.spec.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,15 @@ describe("SigninRequest", function() {
187187
subject.url.should.contain("request_uri=foo");
188188
});
189189

190+
it("should include extra query params", function() {
191+
settings.extraQueryParams = {
192+
'hd': 'domain.com',
193+
'foo': 'bar'
194+
};
195+
subject = new SigninRequest(settings);
196+
subject.url.should.contain('hd=domain.com&foo=bar');
197+
});
198+
190199
});
191200

192201
describe("isOidc", function() {

0 commit comments

Comments
 (0)