From 00b73194f851ed6651a6eee6c01cd970e56650fb Mon Sep 17 00:00:00 2001 From: pamapa Date: Tue, 27 Jul 2021 08:44:01 +0200 Subject: [PATCH] fix: #23 avoid multiple instances of MetaDataService --- src/OidcClientSettings.ts | 2 +- src/ResponseValidator.ts | 8 ++++---- src/TokenClient.ts | 4 ++-- src/TokenRevocationClient.ts | 4 ++-- src/UserInfoService.ts | 10 +++------- src/UserManager.ts | 4 ++-- test/unit/OidcClientSettings.test.ts | 3 ++- test/unit/ResponseValidator.test.ts | 9 ++++----- test/unit/UserInfoService.test.ts | 8 +++++--- 9 files changed, 25 insertions(+), 27 deletions(-) diff --git a/src/OidcClientSettings.ts b/src/OidcClientSettings.ts index dbc4f601e..f640f8e5e 100644 --- a/src/OidcClientSettings.ts +++ b/src/OidcClientSettings.ts @@ -164,8 +164,8 @@ export class OidcClientSettingsStore { this._mergeClaims = !!mergeClaims; this._stateStore = stateStore; - this._validator = new ResponseValidatorCtor(this); this._metadataService = new MetadataServiceCtor(this); + this._validator = new ResponseValidatorCtor(this, this._metadataService); this._extraQueryParams = typeof extraQueryParams === 'object' ? extraQueryParams : {}; this._extraTokenParams = typeof extraTokenParams === 'object' ? extraTokenParams : {}; diff --git a/src/ResponseValidator.ts b/src/ResponseValidator.ts index 82f23282e..c7735e254 100644 --- a/src/ResponseValidator.ts +++ b/src/ResponseValidator.ts @@ -21,7 +21,7 @@ export class ResponseValidator { private _tokenClient: TokenClient; constructor(settings: OidcClientSettingsStore, - MetadataServiceCtor = MetadataService, + metadataService: MetadataService, UserInfoServiceCtor = UserInfoService, TokenClientCtor = TokenClient) { if (!settings) { @@ -30,9 +30,9 @@ export class ResponseValidator { } this._settings = settings; - this._metadataService = new MetadataServiceCtor(this._settings); - this._userInfoService = new UserInfoServiceCtor(this._settings); - this._tokenClient = new TokenClientCtor(this._settings); + this._metadataService = metadataService; + this._userInfoService = new UserInfoServiceCtor(this._settings, metadataService); + this._tokenClient = new TokenClientCtor(this._settings, metadataService); } async validateSigninResponse(state: SigninState, response: SigninResponse) { diff --git a/src/TokenClient.ts b/src/TokenClient.ts index 2021a8917..95e89b1bd 100644 --- a/src/TokenClient.ts +++ b/src/TokenClient.ts @@ -11,7 +11,7 @@ export class TokenClient { private _jsonService: JsonService; private _metadataService: MetadataService; - constructor(settings: OidcClientSettingsStore, JsonServiceCtor = JsonService, MetadataServiceCtor = MetadataService) { + constructor(settings: OidcClientSettingsStore, metadataService: MetadataService, JsonServiceCtor = JsonService) { if (!settings) { Log.error("TokenClient.ctor: No settings passed"); throw new Error("settings"); @@ -19,7 +19,7 @@ export class TokenClient { this._settings = settings; this._jsonService = new JsonServiceCtor(); - this._metadataService = new MetadataServiceCtor(this._settings); + this._metadataService = metadataService; } async exchangeCode(args:any = {}): Promise { diff --git a/src/TokenRevocationClient.ts b/src/TokenRevocationClient.ts index a643edf39..3bd4e91e4 100644 --- a/src/TokenRevocationClient.ts +++ b/src/TokenRevocationClient.ts @@ -12,14 +12,14 @@ export class TokenRevocationClient { private _settings: OidcClientSettingsStore private _metadataService: MetadataService; - constructor(settings: OidcClientSettingsStore, MetadataServiceCtor = MetadataService) { + constructor(settings: OidcClientSettingsStore, metadataService: MetadataService) { if (!settings) { Log.error("TokenRevocationClient.ctor: No settings provided"); throw new Error("No settings provided."); } this._settings = settings; - this._metadataService = new MetadataServiceCtor(this._settings); + this._metadataService = metadataService; } async revoke(token: string, required: boolean, type = "access_token") { diff --git a/src/UserInfoService.ts b/src/UserInfoService.ts index 15d6417cd..6dccfd141 100644 --- a/src/UserInfoService.ts +++ b/src/UserInfoService.ts @@ -11,19 +11,15 @@ export class UserInfoService { private _jsonService: JsonService; private _metadataService: MetadataService; - constructor( - settings: OidcClientSettingsStore, - JsonServiceCtor = JsonService, - MetadataServiceCtor = MetadataService - ) { + constructor(settings: OidcClientSettingsStore, metadataService: MetadataService) { if (!settings) { Log.error("UserInfoService.ctor: No settings passed"); throw new Error("settings"); } this._settings = settings; - this._jsonService = new JsonServiceCtor(undefined, this._getClaimsFromJwt.bind(this)); - this._metadataService = new MetadataServiceCtor(this._settings); + this._jsonService = new JsonService(undefined, this._getClaimsFromJwt.bind(this)); + this._metadataService = metadataService; } async getClaims(token?: string) { diff --git a/src/UserManager.ts b/src/UserManager.ts index f3c6d4e0c..724b0bf7b 100644 --- a/src/UserManager.ts +++ b/src/UserManager.ts @@ -50,8 +50,8 @@ export class UserManager extends OidcClient { this._sessionMonitor = new SessionMonitorCtor(this); } - this._tokenRevocationClient = new TokenRevocationClientCtor(this._settings); - this._tokenClient = new TokenClientCtor(this._settings); + this._tokenRevocationClient = new TokenRevocationClientCtor(this._settings, this._settings.metadataService); + this._tokenClient = new TokenClientCtor(this._settings, this._settings.metadataService); } get settings(): UserManagerSettingsStore { diff --git a/test/unit/OidcClientSettings.test.ts b/test/unit/OidcClientSettings.test.ts index 5e1b96e48..bc326d017 100644 --- a/test/unit/OidcClientSettings.test.ts +++ b/test/unit/OidcClientSettings.test.ts @@ -507,7 +507,8 @@ describe("OidcClientSettings", () => { it("should return value from initial settings", () => { // arrange const dummy = new OidcClientSettingsStore(); - const mock = mocked(new ResponseValidator(dummy)); + const metadataService = new MetadataService(dummy); + const mock = mocked(new ResponseValidator(dummy, metadataService)); const settings: any = { client_id: 'client', ResponseValidatorCtor: () => mock diff --git a/test/unit/ResponseValidator.test.ts b/test/unit/ResponseValidator.test.ts index 13c91f2e3..f631cb757 100644 --- a/test/unit/ResponseValidator.test.ts +++ b/test/unit/ResponseValidator.test.ts @@ -35,19 +35,18 @@ describe("ResponseValidator", () => { state: 'the_id', isOpenIdConnect: false }; - settings = { authority: "op", client_id: 'client' }; + metadataService = new MetadataService(settings); // restore spyOn jest.restoreAllMocks(); - subject = new ResponseValidator(settings); + subject = new ResponseValidator(settings, metadataService); // access private members - metadataService = subject["_metadataService"]; userInfoService = subject["_userInfoService"]; }); @@ -241,7 +240,7 @@ describe("ResponseValidator", () => { delete settings.authority; stubState.authority = "something different"; stubResponse.id_token = id_token; - subject = new ResponseValidator(settings); + subject = new ResponseValidator(settings, metadataService); // act await subject._processSigninParams(stubState, stubResponse); @@ -255,7 +254,7 @@ describe("ResponseValidator", () => { delete settings.client_id; stubState.client_id = "something different"; stubResponse.id_token = id_token; - subject = new ResponseValidator(settings); + subject = new ResponseValidator(settings, metadataService); // act await subject._processSigninParams(stubState, stubResponse); diff --git a/test/unit/UserInfoService.test.ts b/test/unit/UserInfoService.test.ts index 35bcadf20..dda4b6052 100644 --- a/test/unit/UserInfoService.test.ts +++ b/test/unit/UserInfoService.test.ts @@ -11,11 +11,13 @@ describe("UserInfoService", () => { let jsonService: JsonService; beforeEach(() => { - const settings: any = {}; - subject = new UserInfoService(settings); + const settings: any = { + }; + metadataService = new MetadataService(settings); + + subject = new UserInfoService(settings, metadataService); // access private members - metadataService = subject["_metadataService"]; jsonService = subject["_jsonService"]; });