From aa6c786041d754bb795e3afd52e76c09541ffb63 Mon Sep 17 00:00:00 2001 From: Jonathan Bennetts Date: Tue, 11 Oct 2022 08:49:51 +0100 Subject: [PATCH] fix(HTTP Request Node): fix oauth credentials not working properly for some predefined credentials (#4277) --- .../nodes/HttpRequest/GenericFunctions.ts | 64 ++++++++++++++++++- .../HttpRequest/V2/HttpRequestV2.node.ts | 23 +------ .../HttpRequest/V3/HttpRequestV3.node.ts | 25 +------- 3 files changed, 68 insertions(+), 44 deletions(-) diff --git a/packages/nodes-base/nodes/HttpRequest/GenericFunctions.ts b/packages/nodes-base/nodes/HttpRequest/GenericFunctions.ts index 067ce5da35..7b4a9edd14 100644 --- a/packages/nodes-base/nodes/HttpRequest/GenericFunctions.ts +++ b/packages/nodes-base/nodes/HttpRequest/GenericFunctions.ts @@ -1,4 +1,4 @@ -import { INodeExecutionData } from 'n8n-workflow'; +import { INodeExecutionData, IOAuth2Options } from 'n8n-workflow'; export const replaceNullValues = (item: INodeExecutionData) => { if (item.json === null) { @@ -6,3 +6,65 @@ export const replaceNullValues = (item: INodeExecutionData) => { } return item; }; + +export const getOAuth2AdditionalParameters = (nodeCredentialType: string) => { + const oAuth2Options: { [credentialType: string]: IOAuth2Options } = { + bitlyOAuth2Api: { + tokenType: 'Bearer', + }, + boxOAuth2Api: { + includeCredentialsOnRefreshOnBody: true, + }, + ciscoWebexOAuth2Api: { + tokenType: 'Bearer', + }, + clickUpOAuth2Api: { + keepBearer: false, + tokenType: 'Bearer', + }, + goToWebinarOAuth2Api: { + tokenExpiredStatusCode: 403, + }, + hubspotDeveloperApi: { + tokenType: 'Bearer', + includeCredentialsOnRefreshOnBody: true, + }, + hubspotOAuth2Api: { + tokenType: 'Bearer', + includeCredentialsOnRefreshOnBody: true, + }, + lineNotifyOAuth2Api: { + tokenType: 'Bearer', + }, + linkedInOAuth2Api: { + tokenType: 'Bearer', + }, + mailchimpOAuth2Api: { + tokenType: 'Bearer', + }, + mauticOAuth2Api: { + includeCredentialsOnRefreshOnBody: true, + }, + microsoftDynamicsOAuth2Api: { + property: 'id_token', + }, + philipsHueOAuth2Api: { + tokenType: 'Bearer', + }, + raindropOAuth2Api: { + includeCredentialsOnRefreshOnBody: true, + }, + shopifyOAuth2Api: { + tokenType: 'Bearer', + keyToIncludeInAccessTokenHeader: 'X-Shopify-Access-Token', + }, + slackOAuth2Api: { + tokenType: 'Bearer', + property: 'authed_user.access_token', + }, + stravaOAuth2Api: { + includeCredentialsOnRefreshOnBody: true, + }, + }; + return oAuth2Options[nodeCredentialType]; +}; diff --git a/packages/nodes-base/nodes/HttpRequest/V2/HttpRequestV2.node.ts b/packages/nodes-base/nodes/HttpRequest/V2/HttpRequestV2.node.ts index 27c2c6c8a4..ad954e361a 100644 --- a/packages/nodes-base/nodes/HttpRequest/V2/HttpRequestV2.node.ts +++ b/packages/nodes-base/nodes/HttpRequest/V2/HttpRequestV2.node.ts @@ -6,13 +6,12 @@ import { INodeType, INodeTypeBaseDescription, INodeTypeDescription, - IOAuth2Options, NodeApiError, NodeOperationError, } from 'n8n-workflow'; import { OptionsWithUri } from 'request'; -import { replaceNullValues } from '../GenericFunctions'; +import { getOAuth2AdditionalParameters, replaceNullValues } from '../GenericFunctions'; interface OptionData { name: string; @@ -1010,25 +1009,7 @@ export class HttpRequestV2 implements INodeType { requestPromises.push(request); } } else if (authentication === 'predefinedCredentialType' && nodeCredentialType) { - const oAuth2Options: { [credentialType: string]: IOAuth2Options } = { - clickUpOAuth2Api: { - keepBearer: false, - tokenType: 'Bearer', - }, - slackOAuth2Api: { - tokenType: 'Bearer', - property: 'authed_user.access_token', - }, - boxOAuth2Api: { - includeCredentialsOnRefreshOnBody: true, - }, - shopifyOAuth2Api: { - tokenType: 'Bearer', - keyToIncludeInAccessTokenHeader: 'X-Shopify-Access-Token', - }, - }; - - const additionalOAuth2Options = oAuth2Options[nodeCredentialType]; + const additionalOAuth2Options = getOAuth2AdditionalParameters(nodeCredentialType); // service-specific cred: OAuth1, OAuth2, plain const requestWithAuthentication = this.helpers.requestWithAuthentication.call( diff --git a/packages/nodes-base/nodes/HttpRequest/V3/HttpRequestV3.node.ts b/packages/nodes-base/nodes/HttpRequest/V3/HttpRequestV3.node.ts index b728088ebc..e87183993a 100644 --- a/packages/nodes-base/nodes/HttpRequest/V3/HttpRequestV3.node.ts +++ b/packages/nodes-base/nodes/HttpRequest/V3/HttpRequestV3.node.ts @@ -6,14 +6,13 @@ import { INodeType, INodeTypeBaseDescription, INodeTypeDescription, - IOAuth2Options, NodeApiError, NodeOperationError, } from 'n8n-workflow'; import { OptionsWithUri } from 'request-promise-native'; -import { replaceNullValues } from '../GenericFunctions'; +import { getOAuth2AdditionalParameters, replaceNullValues } from '../GenericFunctions'; export class HttpRequestV3 implements INodeType { description: INodeTypeDescription; @@ -1145,25 +1144,7 @@ export class HttpRequestV3 implements INodeType { requestPromises.push(request); } } else if (authentication === 'predefinedCredentialType' && nodeCredentialType) { - const oAuth2Options: { [credentialType: string]: IOAuth2Options } = { - clickUpOAuth2Api: { - keepBearer: false, - tokenType: 'Bearer', - }, - slackOAuth2Api: { - tokenType: 'Bearer', - property: 'authed_user.access_token', - }, - boxOAuth2Api: { - includeCredentialsOnRefreshOnBody: true, - }, - shopifyOAuth2Api: { - tokenType: 'Bearer', - keyToIncludeInAccessTokenHeader: 'X-Shopify-Access-Token', - }, - }; - - const additionalOAuth2Options = oAuth2Options[nodeCredentialType]; + const additionalOAuth2Options = getOAuth2AdditionalParameters(nodeCredentialType); // service-specific cred: OAuth1, OAuth2, plain @@ -1232,7 +1213,7 @@ export class HttpRequestV3 implements INodeType { } else { responseFormat = 'text'; const data = Buffer.from(response.body).toString(); - response.body = (!data) ? undefined : data; + response.body = !data ? undefined : data; } }