Skip to content

Commit

Permalink
refactor: issuer discovery (#637)
Browse files Browse the repository at this point in the history
  • Loading branch information
big-kahuna-burger authored Dec 6, 2023
1 parent d58eabd commit c228877
Showing 1 changed file with 16 additions and 28 deletions.
44 changes: 16 additions & 28 deletions lib/issuer.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,35 +138,8 @@ class Issuer {
}

static async discover(uri) {
const parsed = url.parse(uri);

if (parsed.pathname.includes('/.well-known/')) {
const response = await request.call(this, {
method: 'GET',
responseType: 'json',
url: uri,
headers: {
Accept: 'application/json',
},
});
const body = processResponse(response);
return new Issuer({
...ISSUER_DEFAULTS,
...body,
[AAD_MULTITENANT]: !!AAD_MULTITENANT_DISCOVERY.find((discoveryURL) =>
uri.startsWith(discoveryURL),
),
});
}

let pathname;
if (parsed.pathname.endsWith('/')) {
pathname = `${parsed.pathname}.well-known/openid-configuration`;
} else {
pathname = `${parsed.pathname}/.well-known/openid-configuration`;
}

const wellKnownUri = url.format({ ...parsed, pathname });
const wellKnownUri = resolveWellKnownUri(uri);

const response = await request.call(this, {
method: 'GET',
Expand Down Expand Up @@ -201,4 +174,19 @@ class Issuer {
}
}

function resolveWellKnownUri(uri) {
const parsed = url.parse(uri);
if (parsed.pathname.includes('/.well-known/')) {
return uri;
} else {
let pathname;
if (parsed.pathname.endsWith('/')) {
pathname = `${parsed.pathname}.well-known/openid-configuration`;
} else {
pathname = `${parsed.pathname}/.well-known/openid-configuration`;
}
return url.format({ ...parsed, pathname });
}
}

module.exports = Issuer;

0 comments on commit c228877

Please sign in to comment.