Skip to content

Commit

Permalink
fix: authts#23 avoid multiple instances of MetaDataService
Browse files Browse the repository at this point in the history
  • Loading branch information
pamapa committed Jul 27, 2021
1 parent cad3903 commit 00b7319
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/OidcClientSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 : {};
Expand Down
8 changes: 4 additions & 4 deletions src/ResponseValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class ResponseValidator {
private _tokenClient: TokenClient;

constructor(settings: OidcClientSettingsStore,
MetadataServiceCtor = MetadataService,
metadataService: MetadataService,
UserInfoServiceCtor = UserInfoService,
TokenClientCtor = TokenClient) {
if (!settings) {
Expand All @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/TokenClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ 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");
}

this._settings = settings;
this._jsonService = new JsonServiceCtor();
this._metadataService = new MetadataServiceCtor(this._settings);
this._metadataService = metadataService;
}

async exchangeCode(args:any = {}): Promise<any> {
Expand Down
4 changes: 2 additions & 2 deletions src/TokenRevocationClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down
10 changes: 3 additions & 7 deletions src/UserInfoService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/UserManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion test/unit/OidcClientSettings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 4 additions & 5 deletions test/unit/ResponseValidator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"];
});

Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
8 changes: 5 additions & 3 deletions test/unit/UserInfoService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"];
});

Expand Down

0 comments on commit 00b7319

Please sign in to comment.