Skip to content
This repository has been archived by the owner on Jan 10, 2024. It is now read-only.

Commit

Permalink
feat: #23 cache matadata and signingKeys in MetadataService
Browse files Browse the repository at this point in the history
  • Loading branch information
pamapa committed Jul 27, 2021
1 parent 00b7319 commit 5479346
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 86 deletions.
39 changes: 28 additions & 11 deletions src/MetadataService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ const OidcMetadataUrlPath = '.well-known/openid-configuration';
export class MetadataService {
private _settings: OidcClientSettingsStore
private _jsonService: JsonService

// cache
private _metadataUrl: string | undefined
private _signingKeys: any[] | null;
private _metadata: Partial<OidcMetadata> | null;

constructor(settings: OidcClientSettingsStore, JsonServiceCtor = JsonService) {
if (!settings) {
Expand All @@ -21,7 +25,20 @@ export class MetadataService {

this._settings = settings;
this._jsonService = new JsonServiceCtor(['application/jwk-set+json']);

this._metadataUrl = undefined

this._signingKeys = null;
if (this._settings.signingKeys) {
Log.debug("MetadataService.ctor: Using signingKeys from settings");
this._signingKeys = this._settings.signingKeys;
}

this._metadata = null;
if (this._settings.metadata) {
Log.debug("MetadataService.ctor: Using metadata from settings");
this._metadata = this._settings.metadata;
}
}

get metadataUrl(): string {
Expand All @@ -45,13 +62,13 @@ export class MetadataService {
}

resetSigningKeys() {
this._settings.signingKeys = undefined
this._signingKeys = null
}

async getMetadata(): Promise<Partial<OidcMetadata>> {
if (this._settings.metadata) {
Log.debug("MetadataService.getMetadata: Returning metadata from settings");
return this._settings.metadata;
if (this._metadata) {
Log.debug("MetadataService.getMetadata: Returning metadata from cache");
return this._metadata;
}

if (!this.metadataUrl) {
Expand All @@ -64,8 +81,8 @@ export class MetadataService {

Log.debug("MetadataService.getMetadata: json received");
var seed = this._settings.metadataSeed || {};
this._settings.metadata = Object.assign({}, seed, metadata) as Partial<OidcMetadata>;
return this._settings.metadata;
this._metadata = Object.assign({}, seed, metadata) as Partial<OidcMetadata>;
return this._metadata;
}

getIssuer() {
Expand Down Expand Up @@ -121,9 +138,9 @@ export class MetadataService {
}

async getSigningKeys() {
if (this._settings.signingKeys) {
Log.debug("MetadataService.getSigningKeys: Returning signingKeys from settings");
return this._settings.signingKeys;
if (this._signingKeys) {
Log.debug("MetadataService.getSigningKeys: Returning signingKeys from cache");
return this._signingKeys;
}

const jwks_uri = await this.getKeysEndpoint(false);
Expand All @@ -137,7 +154,7 @@ export class MetadataService {
throw new Error("Missing keys on keyset");
}

this._settings.signingKeys = keySet.keys;
return this._settings.signingKeys;
this._signingKeys = keySet.keys;
return this._signingKeys;
}
}
12 changes: 0 additions & 12 deletions src/OidcClientSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,27 +257,15 @@ export class OidcClientSettingsStore {

return this._metadataUrl;
}

// settable/cachable metadata values
get metadata() {
return this._metadata;
}
set metadata(value) {
this._metadata = value;
}
get metadataSeed() {
return this._metadataSeed;
}
set metadataSeed(value) {
this._metadataSeed = value;
}

get signingKeys() {
return this._signingKeys;
}
set signingKeys(value) {
this._signingKeys = value;
}

// behavior flags
get filterProtocolClaims() {
Expand Down
Loading

0 comments on commit 5479346

Please sign in to comment.