Skip to content

Commit c228877

Browse files
refactor: issuer discovery (#637)
1 parent d58eabd commit c228877

File tree

1 file changed

+16
-28
lines changed

1 file changed

+16
-28
lines changed

lib/issuer.js

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -138,35 +138,8 @@ class Issuer {
138138
}
139139

140140
static async discover(uri) {
141-
const parsed = url.parse(uri);
142-
143-
if (parsed.pathname.includes('/.well-known/')) {
144-
const response = await request.call(this, {
145-
method: 'GET',
146-
responseType: 'json',
147-
url: uri,
148-
headers: {
149-
Accept: 'application/json',
150-
},
151-
});
152-
const body = processResponse(response);
153-
return new Issuer({
154-
...ISSUER_DEFAULTS,
155-
...body,
156-
[AAD_MULTITENANT]: !!AAD_MULTITENANT_DISCOVERY.find((discoveryURL) =>
157-
uri.startsWith(discoveryURL),
158-
),
159-
});
160-
}
161-
162-
let pathname;
163-
if (parsed.pathname.endsWith('/')) {
164-
pathname = `${parsed.pathname}.well-known/openid-configuration`;
165-
} else {
166-
pathname = `${parsed.pathname}/.well-known/openid-configuration`;
167-
}
168141

169-
const wellKnownUri = url.format({ ...parsed, pathname });
142+
const wellKnownUri = resolveWellKnownUri(uri);
170143

171144
const response = await request.call(this, {
172145
method: 'GET',
@@ -201,4 +174,19 @@ class Issuer {
201174
}
202175
}
203176

177+
function resolveWellKnownUri(uri) {
178+
const parsed = url.parse(uri);
179+
if (parsed.pathname.includes('/.well-known/')) {
180+
return uri;
181+
} else {
182+
let pathname;
183+
if (parsed.pathname.endsWith('/')) {
184+
pathname = `${parsed.pathname}.well-known/openid-configuration`;
185+
} else {
186+
pathname = `${parsed.pathname}/.well-known/openid-configuration`;
187+
}
188+
return url.format({ ...parsed, pathname });
189+
}
190+
}
191+
204192
module.exports = Issuer;

0 commit comments

Comments
 (0)