Skip to content

Commit

Permalink
feat: authts#23 avoid multiple metadataUrl implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
pamapa committed Jul 27, 2021
1 parent 8de0cf6 commit a575bce
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 68 deletions.
39 changes: 14 additions & 25 deletions src/MetadataService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class MetadataService {
private _jsonService: JsonService

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

Expand All @@ -26,7 +26,16 @@ export class MetadataService {
this._settings = settings;
this._jsonService = new JsonServiceCtor(['application/jwk-set+json']);

this._metadataUrl = undefined
this._metadataUrl = null;
if (this._settings.metadataUrl) {
this._metadataUrl = this._settings.metadataUrl;
} else if (this._settings.authority) {
this._metadataUrl = this._settings.authority;
if (this._metadataUrl[this._metadataUrl.length - 1] !== '/') {
this._metadataUrl += '/';
}
this._metadataUrl += OidcMetadataUrlPath;
}

this._signingKeys = null;
if (this._settings.signingKeys) {
Expand All @@ -41,26 +50,6 @@ export class MetadataService {
}
}

get metadataUrl(): string {
if (!this._metadataUrl) {
if (this._settings.metadataUrl) {
this._metadataUrl = this._settings.metadataUrl;
}
else {
this._metadataUrl = this._settings.authority;

if (this._metadataUrl && this._metadataUrl.indexOf(OidcMetadataUrlPath) < 0) {
if (this._metadataUrl[this._metadataUrl.length - 1] !== '/') {
this._metadataUrl += '/';
}
this._metadataUrl += OidcMetadataUrlPath;
}
}
}

return this._metadataUrl || "";
}

resetSigningKeys() {
this._signingKeys = null
}
Expand All @@ -71,13 +60,13 @@ export class MetadataService {
return this._metadata;
}

if (!this.metadataUrl) {
if (!this._metadataUrl) {
Log.error("MetadataService.getMetadata: No authority or metadataUrl configured on settings");
throw new Error("No authority or metadataUrl configured on settings");
}

Log.debug("MetadataService.getMetadata: getting metadata from", this.metadataUrl);
const metadata = await this._jsonService.getJson(this.metadataUrl);
Log.debug("MetadataService.getMetadata: getting metadata from", this._metadataUrl);
const metadata = await this._jsonService.getJson(this._metadataUrl);

Log.debug("MetadataService.getMetadata: json received");
var seed = this._settings.metadataSeed || {};
Expand Down
15 changes: 0 additions & 15 deletions src/OidcClientSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import { MetadataService } from './MetadataService';
import { OidcMetadata } from './OidcMetadata'
import { StateStore } from './StateStore'

const OidcMetadataUrlPath = '.well-known/openid-configuration';

const DefaultResponseType = "id_token";
const DefaultScope = "openid";
const DefaultClientAuthentication = "client_secret_post" // The default value must be client_secret_basic, as explained in https://openid.net/specs/openid-connect-core-1_0.html#ClientAuthentication
Expand Down Expand Up @@ -193,7 +191,6 @@ export class OidcClientSettingsStore {
return this._client_authentication;
}


// optional protocol params
get prompt() {
return this._prompt;
Expand All @@ -217,23 +214,11 @@ export class OidcClientSettingsStore {
return this._response_mode;
}


// metadata
get authority() {
return this._authority;
}
get metadataUrl() {
if (!this._metadataUrl) {
this._metadataUrl = this.authority;

if (this._metadataUrl && this._metadataUrl.indexOf(OidcMetadataUrlPath) < 0) {
if (this._metadataUrl[this._metadataUrl.length - 1] !== '/') {
this._metadataUrl += '/';
}
this._metadataUrl += OidcMetadataUrlPath;
}
}

return this._metadataUrl;
}
get metadata() {
Expand Down
52 changes: 35 additions & 17 deletions test/unit/MetadataService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,17 @@

import { Log } from '../../src/utils';
import { MetadataService } from '../../src/MetadataService';
import { JsonService } from '../../src/JsonService';

describe("MetadataService", () => {
let settings: any
let subject: MetadataService;

let jsonService: JsonService;

beforeEach(() => {
Log.logger = console;
Log.level = Log.NONE;

settings = {};
subject = new MetadataService(settings);

// access private member
jsonService = subject["_jsonService"];
});

describe("getMetadata", () => {
Expand Down Expand Up @@ -58,7 +52,11 @@ describe("MetadataService", () => {

it("should use metadataUrl to make json call", async () => {
// arrange
settings.metadataUrl = "http://sts/metadata";
settings = {
metadataUrl: "http://sts/metadata"
};
subject = new MetadataService(settings);
const jsonService = subject["_jsonService"]; // access private member
const getJsonMock = jest.spyOn(jsonService, "getJson")
.mockImplementation(() => Promise.resolve('test'));

Expand All @@ -71,7 +69,11 @@ describe("MetadataService", () => {

it("should return metadata from json call", async () => {
// arrange
settings.metadataUrl = "http://sts/metadata";
settings = {
metadataUrl: "http://sts/metadata"
};
subject = new MetadataService(settings);
const jsonService = subject["_jsonService"]; // access private member
const json = { "test": "data" };
jest.spyOn(jsonService, "getJson").mockImplementation(() => Promise.resolve(json));

Expand All @@ -84,7 +86,11 @@ describe("MetadataService", () => {

it("should cache metadata from json call", async () => {
// arrange
settings.metadataUrl = "http://sts/metadata";
settings = {
metadataUrl: "http://sts/metadata"
};
subject = new MetadataService(settings);
const jsonService = subject["_jsonService"]; // access private member
const json = { test: "value" };
jest.spyOn(jsonService, "getJson").mockImplementation(() => Promise.resolve(json));

Expand All @@ -98,8 +104,12 @@ describe("MetadataService", () => {

it("should merge metadata from seed", async () => {
// arrange
settings.metadataUrl = "http://sts/metadata";
settings.metadataSeed = {test1:"one"};
settings = {
metadataUrl: "http://sts/metadata",
metadataSeed: {test1:"one"}
};
subject = new MetadataService(settings);
const jsonService = subject["_jsonService"]; // access private member
jest.spyOn(jsonService, "getJson").mockImplementation(() => Promise.resolve({test2:"two"}));

// act
Expand All @@ -113,7 +123,11 @@ describe("MetadataService", () => {

it("should fail if json call fails", async () => {
// arrange
settings.metadataUrl = "http://sts/metadata";
settings = {
metadataUrl: "http://sts/metadata"
};
subject = new MetadataService(settings);
const jsonService = subject["_jsonService"]; // access private member
jest.spyOn(jsonService, "getJson").mockRejectedValue(new Error("test"));

// act
Expand Down Expand Up @@ -172,7 +186,11 @@ describe("MetadataService", () => {

it("should fail if json call to load metadata fails", async () => {
// arrange
settings.metadataUrl = "http://sts/metadata";
settings = {
metadataUrl: "http://sts/metadata"
};
subject = new MetadataService(settings);
const jsonService = subject["_jsonService"]; // access private member
jest.spyOn(jsonService, "getJson").mockRejectedValue(new Error("test"));

// act
Expand Down Expand Up @@ -364,7 +382,7 @@ describe("MetadataService", () => {
}
};
subject = new MetadataService(settings);
jsonService = subject["_jsonService"]; // access private member
const jsonService = subject["_jsonService"]; // access private member
jest.spyOn(jsonService, "getJson").mockImplementation(() => Promise.resolve({}));

// act
Expand All @@ -384,7 +402,7 @@ describe("MetadataService", () => {
}
};
subject = new MetadataService(settings);
jsonService = subject["_jsonService"]; // access private member
const jsonService = subject["_jsonService"]; // access private member
const json = {
keys: [{
use:'sig',
Expand All @@ -409,7 +427,7 @@ describe("MetadataService", () => {
}
};
subject = new MetadataService(settings);
jsonService = subject["_jsonService"]; // access private member
const jsonService = subject["_jsonService"]; // access private member
const expectedKeys = [{
use:'sig',
kid:"test"
Expand All @@ -434,7 +452,7 @@ describe("MetadataService", () => {
}
};
subject = new MetadataService(settings);
jsonService = subject["_jsonService"]; // access private member
const jsonService = subject["_jsonService"]; // access private member
const expectedKeys = [{
use:'sig',
kid:"test"
Expand Down
11 changes: 0 additions & 11 deletions test/unit/OidcClientSettings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,17 +230,6 @@ describe("OidcClientSettings", () => {
// assert
expect(subject.metadataUrl).toEqual("http://sts/metadata");
});

it("should infer value from authority", () => {
// act
let subject = new OidcClientSettingsStore({
client_id: 'client',
authority: "http://sts"
});

// assert
expect(subject.metadataUrl).toEqual("http://sts/.well-known/openid-configuration");
});
});

describe("metadata", () => {
Expand Down

0 comments on commit a575bce

Please sign in to comment.