Skip to content
This repository has been archived by the owner on Mar 3, 2022. It is now read-only.

Commit

Permalink
Merge pull request #1068 from dopry/feat/1067/merge-settings
Browse files Browse the repository at this point in the history
feat: merge openid-configuration and metdata
  • Loading branch information
brockallen authored Jan 29, 2021
2 parents 65d15e4 + 36d5add commit 87f21c6
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 8 deletions.
24 changes: 20 additions & 4 deletions src/MetadataService.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export class MetadataService {

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

get metadataUrl() {
Expand Down Expand Up @@ -43,24 +44,39 @@ export class MetadataService {
}

getMetadata() {
if (this._settings.metadata) {
// metadata was preloaded and no url was provided, so use the supplied data.
if (!this.metadataUrl && this._settings.metadata) {
Log.debug("MetadataService.getMetadata: Returning metadata from settings");
return Promise.resolve(this._settings.metadata);
}

// no url was provided and settings were not pre-loaded then throw an error.
if (!this.metadataUrl) {
Log.error("MetadataService.getMetadata: No authority or metadataUrl configured on settings");
return Promise.reject(new Error("No authority or metadataUrl configured on settings"));
}

// if we've already started fetching metadata return the existing promise so we don't call it again.
if (this._metadata_promise) {
Log.debug("MetadataService.getMetadata: getting metadata from cache promise", this.metadataUrl);
return this._metadata_promise
}

Log.debug("MetadataService.getMetadata: getting metadata from", this.metadataUrl);

return this._jsonService.getJson(this.metadataUrl)
this._metadata_promise = this._jsonService.getJson(this.metadataUrl)
.then(metadata => {
Log.debug("MetadataService.getMetadata: json received");
this._settings.metadata = metadata;
return metadata;
// overlay .well-known/openid-configuration over seeded setting. this allows consumers to set values
// like end_session_url for Auth0 when it is not available in the configuration endpoint.
// precedence was set on the assumption the issuers hosted configuration is always more accurate
// than what the developer seeded the client with.
if (!this._settings.metadata) this._settings.metadata = {}
Object.assign(this._settings.metadata, metadata);
return this._settings.metadata;
});

return this._metadata_promise;
}

getIssuer() {
Expand Down
31 changes: 27 additions & 4 deletions test/unit/MetadataService.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,24 +93,26 @@ describe("MetadataService", function() {

it("should return metadata from json call", function(done) {
settings.metadataUrl = "http://sts/metadata";
stubJsonService.result = Promise.resolve("test");
const expected = { test: "test" };
stubJsonService.result = Promise.resolve(expected);

let p = subject.getMetadata();

p.then(result => {
result.should.equal("test");
result.should.deep.equal(expected);
done();
});
});

it("should cache metadata from json call", function(done) {
settings.metadataUrl = "http://sts/metadata";
stubJsonService.result = Promise.resolve("test");
const expected = { test: "test" };
stubJsonService.result = Promise.resolve(expected);

let p = subject.getMetadata();

p.then(result => {
settings.metadata.should.equal("test");
settings.metadata.should.deep.equal(expected);
done();
});
});
Expand All @@ -127,6 +129,27 @@ describe("MetadataService", function() {
});
});

it("should return merge openid-configuration from json call and injected metadata", function(done) {
settings.metadataUrl = "http://sts/metadata";
settings.metadata = {
property1: "injected",
property2: "injected"
}
const response = { property2: "merged" };
const expected = {
property1: "injected",
property2: "merged"
}
stubJsonService.result = Promise.resolve(response);

let p = subject.getMetadata();

p.then(result => {
result.should.deep.equal(expected);
done();
});
});

});

describe("_getMetadataProperty", function() {
Expand Down

0 comments on commit 87f21c6

Please sign in to comment.