OAuth2 support

This commit is contained in:
Rupenieks 2020-06-09 15:48:40 +02:00
parent 516a56ea32
commit 48765b7db6
5 changed files with 121 additions and 23 deletions

View file

@ -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',
},
];
}

View file

@ -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<any>}
*/
export async function pipedriveApiRequest(this: IHookFunctions | IExecuteFunctions, method: string, endpoint: string, body: IDataObject, query?: IDataObject, formData?: IDataObject, downloadFile?: boolean): Promise<any> { // 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

View file

@ -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',

View file

@ -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).',
},
{

View file

@ -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",