From 1411651878adcd2752fe0045ae04d0a7d0c66b27 Mon Sep 17 00:00:00 2001 From: rishabhpoddar Date: Tue, 10 Oct 2023 19:19:44 +0530 Subject: [PATCH] fixes --- .../recipe/thirdparty/providers/bitbucket.js | 22 +++------ .../recipe/thirdparty/providers/custom.js | 20 +++----- .../recipe/thirdparty/providers/github.js | 14 +++--- .../recipe/thirdparty/providers/linkedin.js | 24 ++++------ .../recipe/thirdparty/providers/twitter.js | 10 ++-- .../recipe/thirdparty/providers/utils.d.ts | 8 ++-- .../recipe/thirdparty/providers/utils.js | 34 ++++++++------ .../recipe/thirdparty/providers/bitbucket.ts | 22 +++------ lib/ts/recipe/thirdparty/providers/custom.ts | 20 +++----- lib/ts/recipe/thirdparty/providers/github.ts | 14 +++--- .../recipe/thirdparty/providers/linkedin.ts | 27 ++++------- lib/ts/recipe/thirdparty/providers/twitter.ts | 10 ++-- lib/ts/recipe/thirdparty/providers/utils.ts | 46 +++++++++++-------- 13 files changed, 112 insertions(+), 159 deletions(-) diff --git a/lib/build/recipe/thirdparty/providers/bitbucket.js b/lib/build/recipe/thirdparty/providers/bitbucket.js index a9b9b254b..27588fc51 100644 --- a/lib/build/recipe/thirdparty/providers/bitbucket.js +++ b/lib/build/recipe/thirdparty/providers/bitbucket.js @@ -66,17 +66,13 @@ function Bitbucket(input) { ); if (userInfoFromAccessToken.status >= 400) { logger_1.logDebugMessage( - `Received response with status ${ - userInfoFromAccessToken.status - } and body ${await userInfoFromAccessToken.rawResponse.text()}` + `Received response with status ${userInfoFromAccessToken.status} and body ${userInfoFromAccessToken.stringResponse}` ); throw new Error( - `Received response with status ${ - userInfoFromAccessToken.status - } and body ${await userInfoFromAccessToken.rawResponse.text()}` + `Received response with status ${userInfoFromAccessToken.status} and body ${userInfoFromAccessToken.stringResponse}` ); } - rawUserInfoFromProvider.fromUserInfoAPI = userInfoFromAccessToken.response; + rawUserInfoFromProvider.fromUserInfoAPI = userInfoFromAccessToken.jsonResponse; const userInfoFromEmail = await utils_1.doGetRequest( "https://api.bitbucket.org/2.0/user/emails", undefined, @@ -84,20 +80,16 @@ function Bitbucket(input) { ); if (userInfoFromEmail.status >= 400) { logger_1.logDebugMessage( - `Received response with status ${ - userInfoFromEmail.status - } and body ${await userInfoFromEmail.rawResponse.text()}` + `Received response with status ${userInfoFromEmail.status} and body ${userInfoFromEmail.stringResponse}` ); throw new Error( - `Received response with status ${ - userInfoFromEmail.status - } and body ${await userInfoFromEmail.rawResponse.text()}` + `Received response with status ${userInfoFromEmail.status} and body ${userInfoFromEmail.stringResponse}` ); } - rawUserInfoFromProvider.fromUserInfoAPI.email = userInfoFromEmail.response; + rawUserInfoFromProvider.fromUserInfoAPI.email = userInfoFromEmail.jsonResponse; let email = undefined; let isVerified = false; - for (const emailInfo of userInfoFromEmail.response.values) { + for (const emailInfo of userInfoFromEmail.jsonResponse.values) { if (emailInfo.is_primary) { email = emailInfo.email; isVerified = emailInfo.is_confirmed; diff --git a/lib/build/recipe/thirdparty/providers/custom.js b/lib/build/recipe/thirdparty/providers/custom.js index 484994732..7dfba4747 100644 --- a/lib/build/recipe/thirdparty/providers/custom.js +++ b/lib/build/recipe/thirdparty/providers/custom.js @@ -254,17 +254,13 @@ function NewProvider(input) { const tokenResponse = await utils_1.doPostRequest(tokenAPIURL, accessTokenAPIParams); if (tokenResponse.status >= 400) { logger_1.logDebugMessage( - `Received response with status ${ - tokenResponse.status - } and body ${await tokenResponse.rawResponse.text()}` + `Received response with status ${tokenResponse.status} and body ${tokenResponse.stringResponse}` ); throw new Error( - `Received response with status ${ - tokenResponse.status - } and body ${await tokenResponse.rawResponse.text()}` + `Received response with status ${tokenResponse.status} and body ${tokenResponse.stringResponse}` ); } - return tokenResponse.response; + return tokenResponse.jsonResponse; }, getUserInfo: async function ({ oAuthTokens, userContext }) { const accessToken = oAuthTokens["access_token"]; @@ -329,17 +325,13 @@ function NewProvider(input) { ); if (userInfoFromAccessToken.status >= 400) { logger_1.logDebugMessage( - `Received response with status ${ - userInfoFromAccessToken.status - } and body ${await userInfoFromAccessToken.rawResponse.text()}` + `Received response with status ${userInfoFromAccessToken.status} and body ${userInfoFromAccessToken.stringResponse}` ); throw new Error( - `Received response with status ${ - userInfoFromAccessToken.status - } and body ${await userInfoFromAccessToken.rawResponse.text()}` + `Received response with status ${userInfoFromAccessToken.status} and body ${userInfoFromAccessToken.stringResponse}` ); } - rawUserInfoFromProvider.fromUserInfoAPI = userInfoFromAccessToken.response; + rawUserInfoFromProvider.fromUserInfoAPI = userInfoFromAccessToken.jsonResponse; } const userInfoResult = getSupertokensUserInfoResultFromRawUserInfo(impl.config, rawUserInfoFromProvider); return { diff --git a/lib/build/recipe/thirdparty/providers/github.js b/lib/build/recipe/thirdparty/providers/github.js index ae04bd4f4..a656dd895 100644 --- a/lib/build/recipe/thirdparty/providers/github.js +++ b/lib/build/recipe/thirdparty/providers/github.js @@ -58,8 +58,8 @@ function Github(input) { throw new Error("Invalid access token"); } if ( - applicationResponse.response.app === undefined || - applicationResponse.response.app.client_id !== clientConfig.clientId + applicationResponse.jsonResponse.app === undefined || + applicationResponse.jsonResponse.app.client_id !== clientConfig.clientId ) { throw new Error("Access token does not belong to your application"); } @@ -84,17 +84,15 @@ function Github(input) { const emailInfoResp = await utils_1.doGetRequest("https://api.github.com/user/emails", undefined, headers); if (emailInfoResp.status >= 400) { throw new Error( - `Getting userInfo failed with ${emailInfoResp.status}: ${await emailInfoResp.rawResponse.text()}` + `Getting userInfo failed with ${emailInfoResp.status}: ${emailInfoResp.stringResponse}` ); } - rawResponse.emails = emailInfoResp.response; + rawResponse.emails = emailInfoResp.jsonResponse; const userInfoResp = await utils_1.doGetRequest("https://api.github.com/user", undefined, headers); if (userInfoResp.status >= 400) { - throw new Error( - `Getting userInfo failed with ${userInfoResp.status}: ${await userInfoResp.rawResponse.text()}` - ); + throw new Error(`Getting userInfo failed with ${userInfoResp.status}: ${userInfoResp.stringResponse}`); } - rawResponse.user = userInfoResp.response; + rawResponse.user = userInfoResp.jsonResponse; const rawUserInfoFromProvider = { fromUserInfoAPI: rawResponse, fromIdTokenPayload: {}, diff --git a/lib/build/recipe/thirdparty/providers/linkedin.js b/lib/build/recipe/thirdparty/providers/linkedin.js index b4a72febc..3b5c26e7f 100644 --- a/lib/build/recipe/thirdparty/providers/linkedin.js +++ b/lib/build/recipe/thirdparty/providers/linkedin.js @@ -59,17 +59,13 @@ function Linkedin(input) { undefined, headers ); - rawUserInfoFromProvider.fromUserInfoAPI = userInfoFromAccessToken.response; + rawUserInfoFromProvider.fromUserInfoAPI = userInfoFromAccessToken.jsonResponse; if (userInfoFromAccessToken.status >= 400) { logger_1.logDebugMessage( - `Received response with status ${ - userInfoFromAccessToken.status - } and body ${await userInfoFromAccessToken.rawResponse.text()}` + `Received response with status ${userInfoFromAccessToken.status} and body ${userInfoFromAccessToken.stringResponse}` ); throw new Error( - `Received response with status ${ - userInfoFromAccessToken.status - } and body ${await userInfoFromAccessToken.rawResponse.text()}` + `Received response with status ${userInfoFromAccessToken.status} and body ${userInfoFromAccessToken.stringResponse}` ); } const emailAPIURL = "https://api.linkedin.com/v2/emailAddress"; @@ -80,23 +76,19 @@ function Linkedin(input) { ); if (userInfoFromEmail.status >= 400) { logger_1.logDebugMessage( - `Received response with status ${ - userInfoFromEmail.status - } and body ${await userInfoFromEmail.rawResponse.text()}` + `Received response with status ${userInfoFromEmail.status} and body ${userInfoFromEmail.stringResponse}` ); throw new Error( - `Received response with status ${ - userInfoFromEmail.status - } and body ${await userInfoFromEmail.rawResponse.text()}` + `Received response with status ${userInfoFromEmail.status} and body ${userInfoFromEmail.stringResponse}` ); } - if (userInfoFromEmail.response.elements && userInfoFromEmail.response.elements.length > 0) { + if (userInfoFromEmail.jsonResponse.elements && userInfoFromEmail.jsonResponse.elements.length > 0) { rawUserInfoFromProvider.fromUserInfoAPI.email = - userInfoFromEmail.response.elements[0]["handle~"].emailAddress; + userInfoFromEmail.jsonResponse.elements[0]["handle~"].emailAddress; } rawUserInfoFromProvider.fromUserInfoAPI = Object.assign( Object.assign({}, rawUserInfoFromProvider.fromUserInfoAPI), - userInfoFromEmail.response + userInfoFromEmail.jsonResponse ); return { thirdPartyUserId: rawUserInfoFromProvider.fromUserInfoAPI.id, diff --git a/lib/build/recipe/thirdparty/providers/twitter.js b/lib/build/recipe/thirdparty/providers/twitter.js index 103379ec0..3e54592c7 100644 --- a/lib/build/recipe/thirdparty/providers/twitter.js +++ b/lib/build/recipe/thirdparty/providers/twitter.js @@ -125,17 +125,13 @@ function Twitter(input) { ); if (tokenResponse.status >= 400) { logger_1.logDebugMessage( - `Received response with status ${ - tokenResponse.status - } and body ${await tokenResponse.rawResponse.text()}` + `Received response with status ${tokenResponse.status} and body ${tokenResponse.stringResponse}` ); throw new Error( - `Received response with status ${ - tokenResponse.status - } and body ${await tokenResponse.rawResponse.text()}` + `Received response with status ${tokenResponse.status} and body ${tokenResponse.stringResponse}` ); } - return tokenResponse.response; + return tokenResponse.jsonResponse; }; if (oOverride !== undefined) { originalImplementation = oOverride(originalImplementation); diff --git a/lib/build/recipe/thirdparty/providers/utils.d.ts b/lib/build/recipe/thirdparty/providers/utils.d.ts index eb218934d..34ac7d565 100644 --- a/lib/build/recipe/thirdparty/providers/utils.d.ts +++ b/lib/build/recipe/thirdparty/providers/utils.d.ts @@ -10,9 +10,9 @@ export declare function doGetRequest( [key: string]: string; } ): Promise<{ - response: any; + jsonResponse: Record | undefined; status: number; - rawResponse: Response; + stringResponse: string; }>; export declare function doPostRequest( url: string, @@ -23,9 +23,9 @@ export declare function doPostRequest( [key: string]: string; } ): Promise<{ - response: any; + jsonResponse: Record | undefined; status: number; - rawResponse: Response; + stringResponse: string; }>; export declare function verifyIdTokenFromJWKSEndpointAndGetPayload( idToken: string, diff --git a/lib/build/recipe/thirdparty/providers/utils.js b/lib/build/recipe/thirdparty/providers/utils.js index cc651b4b2..a561eedf4 100644 --- a/lib/build/recipe/thirdparty/providers/utils.js +++ b/lib/build/recipe/thirdparty/providers/utils.js @@ -59,12 +59,16 @@ async function doGetRequest(url, queryParams, headers) { let response = await cross_fetch_1.default(finalURL.toString(), { headers: headers, }); - const respData = await response.clone().json(); - logger_1.logDebugMessage(`Received response with status ${response.status} and body ${JSON.stringify(respData)}`); + const stringResponse = await response.text(); + let jsonResponse = undefined; + if (response.status < 400) { + jsonResponse = JSON.parse(stringResponse); + } + logger_1.logDebugMessage(`Received response with status ${response.status} and body ${stringResponse}`); return { - response: respData, + stringResponse, status: response.status, - rawResponse: response.clone(), + jsonResponse, }; } exports.doGetRequest = doGetRequest; @@ -83,12 +87,16 @@ async function doPostRequest(url, params, headers) { body, headers, }); - const respData = await response.clone().json(); - logger_1.logDebugMessage(`Received response with status ${response.status} and body ${JSON.stringify(respData)}`); + const stringResponse = await response.text(); + let jsonResponse = undefined; + if (response.status < 400) { + jsonResponse = JSON.parse(stringResponse); + } + logger_1.logDebugMessage(`Received response with status ${response.status} and body ${stringResponse}`); return { - response: respData, + stringResponse, status: response.status, - rawResponse: response.clone(), + jsonResponse, }; } exports.doPostRequest = doPostRequest; @@ -112,14 +120,12 @@ async function getOIDCDiscoveryInfo(issuer) { ); if (oidcInfo.status >= 400) { logger_1.logDebugMessage( - `Received response with status ${oidcInfo.status} and body ${await oidcInfo.rawResponse.text()}` - ); - throw new Error( - `Received response with status ${oidcInfo.status} and body ${await oidcInfo.rawResponse.text()}` + `Received response with status ${oidcInfo.status} and body ${oidcInfo.stringResponse}` ); + throw new Error(`Received response with status ${oidcInfo.status} and body ${oidcInfo.stringResponse}`); } - oidcInfoMap[issuer] = oidcInfo.response; - return oidcInfo.response; + oidcInfoMap[issuer] = oidcInfo.jsonResponse; + return oidcInfo.jsonResponse; } async function discoverOIDCEndpoints(config) { if (config.oidcDiscoveryEndpoint !== undefined) { diff --git a/lib/ts/recipe/thirdparty/providers/bitbucket.ts b/lib/ts/recipe/thirdparty/providers/bitbucket.ts index 27e527ae4..56cc8073f 100644 --- a/lib/ts/recipe/thirdparty/providers/bitbucket.ts +++ b/lib/ts/recipe/thirdparty/providers/bitbucket.ts @@ -78,17 +78,13 @@ export default function Bitbucket(input: ProviderInput): TypeProvider { if (userInfoFromAccessToken.status >= 400) { logDebugMessage( - `Received response with status ${ - userInfoFromAccessToken.status - } and body ${await userInfoFromAccessToken.rawResponse.text()}` + `Received response with status ${userInfoFromAccessToken.status} and body ${userInfoFromAccessToken.stringResponse}` ); throw new Error( - `Received response with status ${ - userInfoFromAccessToken.status - } and body ${await userInfoFromAccessToken.rawResponse.text()}` + `Received response with status ${userInfoFromAccessToken.status} and body ${userInfoFromAccessToken.stringResponse}` ); } - rawUserInfoFromProvider.fromUserInfoAPI = userInfoFromAccessToken.response; + rawUserInfoFromProvider.fromUserInfoAPI = userInfoFromAccessToken.jsonResponse; const userInfoFromEmail = await doGetRequest( "https://api.bitbucket.org/2.0/user/emails", @@ -98,22 +94,18 @@ export default function Bitbucket(input: ProviderInput): TypeProvider { if (userInfoFromEmail.status >= 400) { logDebugMessage( - `Received response with status ${ - userInfoFromEmail.status - } and body ${await userInfoFromEmail.rawResponse.text()}` + `Received response with status ${userInfoFromEmail.status} and body ${userInfoFromEmail.stringResponse}` ); throw new Error( - `Received response with status ${ - userInfoFromEmail.status - } and body ${await userInfoFromEmail.rawResponse.text()}` + `Received response with status ${userInfoFromEmail.status} and body ${userInfoFromEmail.stringResponse}` ); } - rawUserInfoFromProvider.fromUserInfoAPI.email = userInfoFromEmail.response; + rawUserInfoFromProvider.fromUserInfoAPI.email = userInfoFromEmail.jsonResponse; let email = undefined; let isVerified = false; - for (const emailInfo of userInfoFromEmail.response.values) { + for (const emailInfo of userInfoFromEmail.jsonResponse!.values) { if (emailInfo.is_primary) { email = emailInfo.email; isVerified = emailInfo.is_confirmed; diff --git a/lib/ts/recipe/thirdparty/providers/custom.ts b/lib/ts/recipe/thirdparty/providers/custom.ts index b3e46879b..4b1bf65ca 100644 --- a/lib/ts/recipe/thirdparty/providers/custom.ts +++ b/lib/ts/recipe/thirdparty/providers/custom.ts @@ -274,18 +274,14 @@ export default function NewProvider(input: ProviderInput): TypeProvider { if (tokenResponse.status >= 400) { logDebugMessage( - `Received response with status ${ - tokenResponse.status - } and body ${await tokenResponse.rawResponse.text()}` + `Received response with status ${tokenResponse.status} and body ${tokenResponse.stringResponse}` ); throw new Error( - `Received response with status ${ - tokenResponse.status - } and body ${await tokenResponse.rawResponse.text()}` + `Received response with status ${tokenResponse.status} and body ${tokenResponse.stringResponse}` ); } - return tokenResponse.response; + return tokenResponse.jsonResponse; }, getUserInfo: async function ({ oAuthTokens, userContext }): Promise { @@ -359,18 +355,14 @@ export default function NewProvider(input: ProviderInput): TypeProvider { if (userInfoFromAccessToken.status >= 400) { logDebugMessage( - `Received response with status ${ - userInfoFromAccessToken.status - } and body ${await userInfoFromAccessToken.rawResponse.text()}` + `Received response with status ${userInfoFromAccessToken.status} and body ${userInfoFromAccessToken.stringResponse}` ); throw new Error( - `Received response with status ${ - userInfoFromAccessToken.status - } and body ${await userInfoFromAccessToken.rawResponse.text()}` + `Received response with status ${userInfoFromAccessToken.status} and body ${userInfoFromAccessToken.stringResponse}` ); } - rawUserInfoFromProvider.fromUserInfoAPI = userInfoFromAccessToken.response; + rawUserInfoFromProvider.fromUserInfoAPI = userInfoFromAccessToken.jsonResponse; } const userInfoResult = getSupertokensUserInfoResultFromRawUserInfo(impl.config, rawUserInfoFromProvider); diff --git a/lib/ts/recipe/thirdparty/providers/github.ts b/lib/ts/recipe/thirdparty/providers/github.ts index 8def823ba..f556eba71 100644 --- a/lib/ts/recipe/thirdparty/providers/github.ts +++ b/lib/ts/recipe/thirdparty/providers/github.ts @@ -80,8 +80,8 @@ export default function Github(input: ProviderInput): TypeProvider { } if ( - applicationResponse.response.app === undefined || - applicationResponse.response.app.client_id !== clientConfig.clientId + applicationResponse.jsonResponse!.app === undefined || + applicationResponse.jsonResponse!.app.client_id !== clientConfig.clientId ) { throw new Error("Access token does not belong to your application"); } @@ -113,21 +113,19 @@ export default function Github(input: ProviderInput): TypeProvider { if (emailInfoResp.status >= 400) { throw new Error( - `Getting userInfo failed with ${emailInfoResp.status}: ${await emailInfoResp.rawResponse.text()}` + `Getting userInfo failed with ${emailInfoResp.status}: ${emailInfoResp.stringResponse}` ); } - rawResponse.emails = emailInfoResp.response; + rawResponse.emails = emailInfoResp.jsonResponse; const userInfoResp = await doGetRequest("https://api.github.com/user", undefined, headers); if (userInfoResp.status >= 400) { - throw new Error( - `Getting userInfo failed with ${userInfoResp.status}: ${await userInfoResp.rawResponse.text()}` - ); + throw new Error(`Getting userInfo failed with ${userInfoResp.status}: ${userInfoResp.stringResponse}`); } - rawResponse.user = userInfoResp.response; + rawResponse.user = userInfoResp.jsonResponse; const rawUserInfoFromProvider = { fromUserInfoAPI: rawResponse, diff --git a/lib/ts/recipe/thirdparty/providers/linkedin.ts b/lib/ts/recipe/thirdparty/providers/linkedin.ts index 961f4e5d3..9597101ec 100644 --- a/lib/ts/recipe/thirdparty/providers/linkedin.ts +++ b/lib/ts/recipe/thirdparty/providers/linkedin.ts @@ -64,18 +64,14 @@ export default function Linkedin(input: ProviderInput): TypeProvider { }; const userInfoFromAccessToken = await doGetRequest("https://api.linkedin.com/v2/me", undefined, headers); - rawUserInfoFromProvider.fromUserInfoAPI = userInfoFromAccessToken.response; + rawUserInfoFromProvider.fromUserInfoAPI = userInfoFromAccessToken.jsonResponse; if (userInfoFromAccessToken.status >= 400) { logDebugMessage( - `Received response with status ${ - userInfoFromAccessToken.status - } and body ${await userInfoFromAccessToken.rawResponse.text()}` + `Received response with status ${userInfoFromAccessToken.status} and body ${userInfoFromAccessToken.stringResponse}` ); throw new Error( - `Received response with status ${ - userInfoFromAccessToken.status - } and body ${await userInfoFromAccessToken.rawResponse.text()}` + `Received response with status ${userInfoFromAccessToken.status} and body ${userInfoFromAccessToken.stringResponse}` ); } @@ -88,24 +84,21 @@ export default function Linkedin(input: ProviderInput): TypeProvider { if (userInfoFromEmail.status >= 400) { logDebugMessage( - `Received response with status ${ - userInfoFromEmail.status - } and body ${await userInfoFromEmail.rawResponse.text()}` + `Received response with status ${userInfoFromEmail.status} and body ${userInfoFromEmail.stringResponse}` ); throw new Error( - `Received response with status ${ - userInfoFromEmail.status - } and body ${await userInfoFromEmail.rawResponse.text()}` + `Received response with status ${userInfoFromEmail.status} and body ${userInfoFromEmail.stringResponse}` ); } - if (userInfoFromEmail.response.elements && userInfoFromEmail.response.elements.length > 0) { - rawUserInfoFromProvider.fromUserInfoAPI.email = - userInfoFromEmail.response.elements[0]["handle~"].emailAddress; + if (userInfoFromEmail.jsonResponse!.elements && userInfoFromEmail.jsonResponse!.elements.length > 0) { + rawUserInfoFromProvider.fromUserInfoAPI.email = userInfoFromEmail.jsonResponse!.elements[0][ + "handle~" + ].emailAddress; } rawUserInfoFromProvider.fromUserInfoAPI = { ...rawUserInfoFromProvider.fromUserInfoAPI, - ...userInfoFromEmail.response, + ...userInfoFromEmail.jsonResponse!, }; return { diff --git a/lib/ts/recipe/thirdparty/providers/twitter.ts b/lib/ts/recipe/thirdparty/providers/twitter.ts index 875d500da..cb60db8d3 100644 --- a/lib/ts/recipe/thirdparty/providers/twitter.ts +++ b/lib/ts/recipe/thirdparty/providers/twitter.ts @@ -103,18 +103,14 @@ export default function Twitter(input: ProviderInput): TypeProvider { if (tokenResponse.status >= 400) { logDebugMessage( - `Received response with status ${ - tokenResponse.status - } and body ${await tokenResponse.rawResponse.text()}` + `Received response with status ${tokenResponse.status} and body ${tokenResponse.stringResponse}` ); throw new Error( - `Received response with status ${ - tokenResponse.status - } and body ${await tokenResponse.rawResponse.text()}` + `Received response with status ${tokenResponse.status} and body ${tokenResponse.stringResponse}` ); } - return tokenResponse.response; + return tokenResponse.jsonResponse; }; if (oOverride !== undefined) { diff --git a/lib/ts/recipe/thirdparty/providers/utils.ts b/lib/ts/recipe/thirdparty/providers/utils.ts index 3e1a01f0d..8f772a7a5 100644 --- a/lib/ts/recipe/thirdparty/providers/utils.ts +++ b/lib/ts/recipe/thirdparty/providers/utils.ts @@ -11,9 +11,9 @@ export async function doGetRequest( queryParams?: { [key: string]: string }, headers?: { [key: string]: string } ): Promise<{ - response: any; + jsonResponse: Record | undefined; status: number; - rawResponse: Response; + stringResponse: string; }> { logDebugMessage( `GET request to ${url}, with query params ${JSON.stringify(queryParams)} and headers ${JSON.stringify(headers)}` @@ -30,13 +30,18 @@ export async function doGetRequest( headers: headers, }); - const respData = await response.clone().json(); + const stringResponse = await response.text(); + let jsonResponse: Record | undefined = undefined; - logDebugMessage(`Received response with status ${response.status} and body ${JSON.stringify(respData)}`); + if (response.status < 400) { + jsonResponse = JSON.parse(stringResponse); + } + + logDebugMessage(`Received response with status ${response.status} and body ${stringResponse}`); return { - response: respData, + stringResponse, status: response.status, - rawResponse: response.clone(), + jsonResponse, }; } @@ -45,9 +50,9 @@ export async function doPostRequest( params: { [key: string]: any }, headers?: { [key: string]: string } ): Promise<{ - response: any; + jsonResponse: Record | undefined; status: number; - rawResponse: Response; + stringResponse: string; }> { if (headers === undefined) { headers = {}; @@ -67,13 +72,18 @@ export async function doPostRequest( headers, }); - const respData = await response.clone().json(); + const stringResponse = await response.text(); + let jsonResponse: Record | undefined = undefined; + + if (response.status < 400) { + jsonResponse = JSON.parse(stringResponse); + } - logDebugMessage(`Received response with status ${response.status} and body ${JSON.stringify(respData)}`); + logDebugMessage(`Received response with status ${response.status} and body ${stringResponse}`); return { - response: respData, + stringResponse, status: response.status, - rawResponse: response.clone(), + jsonResponse, }; } @@ -105,16 +115,12 @@ async function getOIDCDiscoveryInfo(issuer: string): Promise { ); if (oidcInfo.status >= 400) { - logDebugMessage( - `Received response with status ${oidcInfo.status} and body ${await oidcInfo.rawResponse.text()}` - ); - throw new Error( - `Received response with status ${oidcInfo.status} and body ${await oidcInfo.rawResponse.text()}` - ); + logDebugMessage(`Received response with status ${oidcInfo.status} and body ${oidcInfo.stringResponse}`); + throw new Error(`Received response with status ${oidcInfo.status} and body ${oidcInfo.stringResponse}`); } - oidcInfoMap[issuer] = oidcInfo.response; - return oidcInfo.response; + oidcInfoMap[issuer] = oidcInfo.jsonResponse!; + return oidcInfo.jsonResponse!; } export async function discoverOIDCEndpoints(config: ProviderConfigForClientType): Promise {