From 71ddd418778776c1ca0100d950de3180e729a8c6 Mon Sep 17 00:00:00 2001 From: Rory Powell Date: Mon, 5 Jul 2021 17:28:55 +0100 Subject: [PATCH] Improve error handling on openid-configuration request --- packages/auth/src/middleware/passport/oidc.js | 34 +++++++++++-------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/packages/auth/src/middleware/passport/oidc.js b/packages/auth/src/middleware/passport/oidc.js index 78a11784e4..d9a86ce574 100644 --- a/packages/auth/src/middleware/passport/oidc.js +++ b/packages/auth/src/middleware/passport/oidc.js @@ -103,23 +103,27 @@ exports.strategyFactory = async function (callbackUrl) { } const response = await fetch(configurationUrl) - if (response.ok) { - const body = await response.json() - return new OIDCStrategy( - { - issuer: body.issuer, - authorizationURL: body.authorization_endpoint, - tokenURL: body.token_endpoint, - userInfoURL: body.userinfo_endpoint, - clientID: clientId, - clientSecret: clientSecret, - callbackURL: callbackUrl, - scope: "profile email", - }, - authenticate - ) + if (!response.ok) { + throw new Error(`Unexpected response when fetching openid-configuration: ${response.statusText}`) } + + const body = await response.json() + + return new OIDCStrategy( + { + issuer: body.issuer, + authorizationURL: body.authorization_endpoint, + tokenURL: body.token_endpoint, + userInfoURL: body.userinfo_endpoint, + clientID: clientId, + clientSecret: clientSecret, + callbackURL: callbackUrl, + scope: "profile email", + }, + authenticate + ) + } catch (err) { console.error(err) throw new Error("Error constructing OIDC authentication strategy", err)