diff --git a/packages/nodes-base/credentials/PipedriveOAuth2Api.credentials.ts b/packages/nodes-base/credentials/PipedriveOAuth2Api.credentials.ts new file mode 100644 index 0000000000..813202739f --- /dev/null +++ b/packages/nodes-base/credentials/PipedriveOAuth2Api.credentials.ts @@ -0,0 +1,47 @@ +import { + ICredentialType, + NodePropertyTypes, +} from 'n8n-workflow'; + + +export class PipedriveOAuth2Api implements ICredentialType { + name = 'pipedriveOAuth2Api'; + extends = [ + 'oAuth2Api', + ]; + displayName = 'Pipedrive OAuth2 API'; + properties = [ + { + displayName: 'Authorization URL', + name: 'authUrl', + type: 'hidden' as NodePropertyTypes, + default: 'https://oauth.pipedrive.com/oauth/authorize', + required: true, + }, + { + displayName: 'Access Token URL', + name: 'accessTokenUrl', + type: 'hidden' as NodePropertyTypes, + default: 'https://oauth.pipedrive.com/oauth/token', + required: true, + }, + { + displayName: 'Scope', + name: 'scope', + type: 'string' as NodePropertyTypes, + default: '', + }, + { + displayName: 'Auth URI Query Parameters', + name: 'authQueryParameters', + type: 'hidden' as NodePropertyTypes, + default: '', + }, + { + displayName: 'Authentication', + name: 'authentication', + type: 'hidden' as NodePropertyTypes, + default: 'header', + }, + ]; +} diff --git a/packages/nodes-base/nodes/Pipedrive/GenericFunctions.ts b/packages/nodes-base/nodes/Pipedrive/GenericFunctions.ts index 32e8194359..9809d0feb3 100644 --- a/packages/nodes-base/nodes/Pipedrive/GenericFunctions.ts +++ b/packages/nodes-base/nodes/Pipedrive/GenericFunctions.ts @@ -23,7 +23,6 @@ export interface ICustomProperties { [key: string]: ICustomInterface; } - /** * Make an API request to Pipedrive * @@ -34,16 +33,7 @@ export interface ICustomProperties { * @returns {Promise} */ export async function pipedriveApiRequest(this: IHookFunctions | IExecuteFunctions, method: string, endpoint: string, body: IDataObject, query?: IDataObject, formData?: IDataObject, downloadFile?: boolean): Promise { // tslint:disable-line:no-any - const credentials = this.getCredentials('pipedriveApi'); - if (credentials === undefined) { - throw new Error('No credentials got returned!'); - } - - if (query === undefined) { - query = {}; - } - - query.api_token = credentials.apiToken; + const authenticationMethod = this.getNodeParameter('authentication', 0); const options: OptionsWithUri = { method, @@ -65,8 +55,27 @@ export async function pipedriveApiRequest(this: IHookFunctions | IExecuteFunctio options.formData = formData; } + if (query === undefined) { + query = {}; + } + + let responseData; + try { - const responseData = await this.helpers.request(options); + if (authenticationMethod === 'accessToken') { + + const credentials = this.getCredentials('pipedriveApi'); + if (credentials === undefined) { + throw new Error('No credentials got returned!'); + } + + query.api_token = credentials.apiToken; + + responseData = await this.helpers.request(options); + + } else { + responseData = await this.helpers.requestOAuth2!.call(this, 'pipedriveOAuth2Api', options); + } if (downloadFile === true) { return { @@ -82,7 +91,7 @@ export async function pipedriveApiRequest(this: IHookFunctions | IExecuteFunctio additionalData: responseData.additional_data, data: responseData.data, }; - } catch (error) { + } catch(error) { if (error.statusCode === 401) { // Return a clear error throw new Error('The Pipedrive credentials are not valid!'); @@ -102,8 +111,6 @@ export async function pipedriveApiRequest(this: IHookFunctions | IExecuteFunctio } } - - /** * Make an API request to paginated Pipedrive endpoint * and return all results diff --git a/packages/nodes-base/nodes/Pipedrive/Pipedrive.node.ts b/packages/nodes-base/nodes/Pipedrive/Pipedrive.node.ts index 8ae9c42630..3aef48ce59 100644 --- a/packages/nodes-base/nodes/Pipedrive/Pipedrive.node.ts +++ b/packages/nodes-base/nodes/Pipedrive/Pipedrive.node.ts @@ -61,9 +61,44 @@ export class Pipedrive implements INodeType { { name: 'pipedriveApi', required: true, - } + displayOptions: { + show: { + authentication: [ + 'accessToken', + ], + }, + }, + }, + { + name: 'pipedriveOAuth2Api', + required: true, + displayOptions: { + show: { + authentication: [ + 'oAuth2', + ], + }, + }, + }, ], properties: [ + { + displayName: 'Authentication', + name: 'authentication', + type: 'options', + options: [ + { + name: 'Access Token', + value: 'accessToken', + }, + { + name: 'OAuth2', + value: 'oAuth2', + }, + ], + default: 'accessToken', + description: 'The resource to operate on.', + }, { displayName: 'Resource', name: 'resource', diff --git a/packages/nodes-base/nodes/Pipedrive/PipedriveTrigger.node.ts b/packages/nodes-base/nodes/Pipedrive/PipedriveTrigger.node.ts index 2c55246a32..0fb3d37f99 100644 --- a/packages/nodes-base/nodes/Pipedrive/PipedriveTrigger.node.ts +++ b/packages/nodes-base/nodes/Pipedrive/PipedriveTrigger.node.ts @@ -52,14 +52,21 @@ export class PipedriveTrigger implements INodeType { { name: 'pipedriveApi', required: true, + displayOptions: { + show: { + authentication: [ + 'accessToken', + ], + }, + }, }, { - name: 'httpBasicAuth', + name: 'pipedriveOAuth2Api', required: true, displayOptions: { show: { authentication: [ - 'basicAuth', + 'oAuth2', ], }, }, @@ -80,15 +87,16 @@ export class PipedriveTrigger implements INodeType { type: 'options', options: [ { - name: 'Basic Auth', - value: 'basicAuth' + name: 'Access Token', + value: 'accessToken' }, { - name: 'None', - value: 'none' + name: 'OAuth2', + value: 'oAuth2' }, + ], - default: 'none', + default: 'accessToken', description: 'If authentication should be activated for the webhook (makes it more scure).', }, { diff --git a/packages/nodes-base/package.json b/packages/nodes-base/package.json index 3f28db9dec..57332957ee 100644 --- a/packages/nodes-base/package.json +++ b/packages/nodes-base/package.json @@ -99,6 +99,7 @@ "dist/credentials/PagerDutyApi.credentials.js", "dist/credentials/PayPalApi.credentials.js", "dist/credentials/PipedriveApi.credentials.js", + "dist/credentials/PipedriveOAuth2Api.credentials.js", "dist/credentials/Postgres.credentials.js", "dist/credentials/Redis.credentials.js", "dist/credentials/RocketchatApi.credentials.js",