diff --git a/packages/core/src/NodeExecuteFunctions.ts b/packages/core/src/NodeExecuteFunctions.ts index 846338e6f1..681d2228f0 100644 --- a/packages/core/src/NodeExecuteFunctions.ts +++ b/packages/core/src/NodeExecuteFunctions.ts @@ -1117,7 +1117,6 @@ export async function requestOAuth2( [oAuth2Options.keyToIncludeInAccessTokenHeader]: token.accessToken, }); } - if (isN8nRequest) { return this.helpers.httpRequest(newRequestOptions).catch(async (error: AxiosError) => { if (error.response?.status === 401) { @@ -1179,85 +1178,95 @@ export async function requestOAuth2( throw error; }); } - - return this.helpers.request(newRequestOptions).catch(async (error: IResponseError) => { - const statusCodeReturned = - oAuth2Options?.tokenExpiredStatusCode === undefined - ? 401 - : oAuth2Options?.tokenExpiredStatusCode; - - if (error.statusCode === statusCodeReturned) { - // Token is probably not valid anymore. So try refresh it. - - const tokenRefreshOptions: IDataObject = {}; - - if (oAuth2Options?.includeCredentialsOnRefreshOnBody) { - const body: IDataObject = { - client_id: credentials.clientId, - client_secret: credentials.clientSecret, - }; - tokenRefreshOptions.body = body; - // Override authorization property so the credentials are not included in it - tokenRefreshOptions.headers = { - Authorization: '', - }; + return this.helpers + .request(newRequestOptions) + .then((response) => { + const requestOptions = newRequestOptions as any; + if ( + requestOptions.resolveWithFullResponse === true && + requestOptions.simple === false && + response.statusCode === + (oAuth2Options?.tokenExpiredStatusCode === undefined + ? 401 + : oAuth2Options?.tokenExpiredStatusCode) + ) { + throw response; } - - Logger.debug( - `OAuth2 token for "${credentialsType}" used by node "${node.name}" expired. Should revalidate.`, - ); - - let newToken; - - // if it's OAuth2 with client credentials grant type, get a new token - // instead of refreshing it. - if (OAuth2GrantType.clientCredentials === credentials.grantType) { - newToken = await getClientCredentialsToken(token.client, credentials); - } else { - newToken = await token.refresh(tokenRefreshOptions); - } - - Logger.debug( - `OAuth2 token for "${credentialsType}" used by node "${node.name}" has been renewed.`, - ); - - credentials.oauthTokenData = newToken.data; - - // Find the credentials - if (!node.credentials?.[credentialsType]) { - throw new Error( - `The node "${node.name}" does not have credentials of type "${credentialsType}"!`, + return response; + }) + .catch(async (error: IResponseError) => { + const statusCodeReturned = + oAuth2Options?.tokenExpiredStatusCode === undefined + ? 401 + : oAuth2Options?.tokenExpiredStatusCode; + if (error.statusCode === statusCodeReturned) { + // Token is probably not valid anymore. So try refresh it. + const tokenRefreshOptions: IDataObject = {}; + if (oAuth2Options?.includeCredentialsOnRefreshOnBody) { + const body: IDataObject = { + client_id: credentials.clientId, + client_secret: credentials.clientSecret, + }; + tokenRefreshOptions.body = body; + // Override authorization property so the credentials are not included in it + tokenRefreshOptions.headers = { + Authorization: '', + }; + } + Logger.debug( + `OAuth2 token for "${credentialsType}" used by node "${node.name}" expired. Should revalidate.`, ); - } - const nodeCredentials = node.credentials[credentialsType]; - // Save the refreshed token - await additionalData.credentialsHelper.updateCredentials( - nodeCredentials, - credentialsType, - credentials as unknown as ICredentialDataDecryptedObject, - ); + let newToken; - Logger.debug( - `OAuth2 token for "${credentialsType}" used by node "${node.name}" has been saved to database successfully.`, - ); + // if it's OAuth2 with client credentials grant type, get a new token + // instead of refreshing it. + if (OAuth2GrantType.clientCredentials === credentials.grantType) { + newToken = await getClientCredentialsToken(token.client, credentials); + } else { + newToken = await token.refresh(tokenRefreshOptions); + } + Logger.debug( + `OAuth2 token for "${credentialsType}" used by node "${node.name}" has been renewed.`, + ); - // Make the request again with the new token - const newRequestOptions = newToken.sign(requestOptions as clientOAuth2.RequestObject); - newRequestOptions.headers = newRequestOptions.headers ?? {}; + credentials.oauthTokenData = newToken.data; - if (oAuth2Options?.keyToIncludeInAccessTokenHeader) { - Object.assign(newRequestOptions.headers, { - [oAuth2Options.keyToIncludeInAccessTokenHeader]: token.accessToken, - }); + // Find the credentials + if (!node.credentials?.[credentialsType]) { + throw new Error( + `The node "${node.name}" does not have credentials of type "${credentialsType}"!`, + ); + } + const nodeCredentials = node.credentials[credentialsType]; + + // Save the refreshed token + await additionalData.credentialsHelper.updateCredentials( + nodeCredentials, + credentialsType, + credentials as unknown as ICredentialDataDecryptedObject, + ); + + Logger.debug( + `OAuth2 token for "${credentialsType}" used by node "${node.name}" has been saved to database successfully.`, + ); + + // Make the request again with the new token + const newRequestOptions = newToken.sign(requestOptions as clientOAuth2.RequestObject); + newRequestOptions.headers = newRequestOptions.headers ?? {}; + + if (oAuth2Options?.keyToIncludeInAccessTokenHeader) { + Object.assign(newRequestOptions.headers, { + [oAuth2Options.keyToIncludeInAccessTokenHeader]: token.accessToken, + }); + } + + return this.helpers.request(newRequestOptions); } - return this.helpers.request(newRequestOptions); - } - - // Unknown error so simply throw it - throw error; - }); + // Unknown error so simply throw it + throw error; + }); } /**