mirror of
https://github.com/n8n-io/n8n.git
synced 2025-03-05 20:50:17 -08:00
OAuth2 support
This commit is contained in:
parent
516a56ea32
commit
48765b7db6
|
@ -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',
|
||||||
|
},
|
||||||
|
];
|
||||||
|
}
|
|
@ -23,7 +23,6 @@ export interface ICustomProperties {
|
||||||
[key: string]: ICustomInterface;
|
[key: string]: ICustomInterface;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Make an API request to Pipedrive
|
* Make an API request to Pipedrive
|
||||||
*
|
*
|
||||||
|
@ -34,16 +33,7 @@ export interface ICustomProperties {
|
||||||
* @returns {Promise<any>}
|
* @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
|
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');
|
const authenticationMethod = this.getNodeParameter('authentication', 0);
|
||||||
if (credentials === undefined) {
|
|
||||||
throw new Error('No credentials got returned!');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (query === undefined) {
|
|
||||||
query = {};
|
|
||||||
}
|
|
||||||
|
|
||||||
query.api_token = credentials.apiToken;
|
|
||||||
|
|
||||||
const options: OptionsWithUri = {
|
const options: OptionsWithUri = {
|
||||||
method,
|
method,
|
||||||
|
@ -65,8 +55,27 @@ export async function pipedriveApiRequest(this: IHookFunctions | IExecuteFunctio
|
||||||
options.formData = formData;
|
options.formData = formData;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (query === undefined) {
|
||||||
|
query = {};
|
||||||
|
}
|
||||||
|
|
||||||
|
let responseData;
|
||||||
|
|
||||||
try {
|
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) {
|
if (downloadFile === true) {
|
||||||
return {
|
return {
|
||||||
|
@ -82,7 +91,7 @@ export async function pipedriveApiRequest(this: IHookFunctions | IExecuteFunctio
|
||||||
additionalData: responseData.additional_data,
|
additionalData: responseData.additional_data,
|
||||||
data: responseData.data,
|
data: responseData.data,
|
||||||
};
|
};
|
||||||
} catch (error) {
|
} catch(error) {
|
||||||
if (error.statusCode === 401) {
|
if (error.statusCode === 401) {
|
||||||
// Return a clear error
|
// Return a clear error
|
||||||
throw new Error('The Pipedrive credentials are not valid!');
|
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
|
* Make an API request to paginated Pipedrive endpoint
|
||||||
* and return all results
|
* and return all results
|
||||||
|
|
|
@ -61,9 +61,44 @@ export class Pipedrive implements INodeType {
|
||||||
{
|
{
|
||||||
name: 'pipedriveApi',
|
name: 'pipedriveApi',
|
||||||
required: true,
|
required: true,
|
||||||
}
|
displayOptions: {
|
||||||
|
show: {
|
||||||
|
authentication: [
|
||||||
|
'accessToken',
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'pipedriveOAuth2Api',
|
||||||
|
required: true,
|
||||||
|
displayOptions: {
|
||||||
|
show: {
|
||||||
|
authentication: [
|
||||||
|
'oAuth2',
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
],
|
],
|
||||||
properties: [
|
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',
|
displayName: 'Resource',
|
||||||
name: 'resource',
|
name: 'resource',
|
||||||
|
|
|
@ -52,14 +52,21 @@ export class PipedriveTrigger implements INodeType {
|
||||||
{
|
{
|
||||||
name: 'pipedriveApi',
|
name: 'pipedriveApi',
|
||||||
required: true,
|
required: true,
|
||||||
|
displayOptions: {
|
||||||
|
show: {
|
||||||
|
authentication: [
|
||||||
|
'accessToken',
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'httpBasicAuth',
|
name: 'pipedriveOAuth2Api',
|
||||||
required: true,
|
required: true,
|
||||||
displayOptions: {
|
displayOptions: {
|
||||||
show: {
|
show: {
|
||||||
authentication: [
|
authentication: [
|
||||||
'basicAuth',
|
'oAuth2',
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -80,15 +87,16 @@ export class PipedriveTrigger implements INodeType {
|
||||||
type: 'options',
|
type: 'options',
|
||||||
options: [
|
options: [
|
||||||
{
|
{
|
||||||
name: 'Basic Auth',
|
name: 'Access Token',
|
||||||
value: 'basicAuth'
|
value: 'accessToken'
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'None',
|
name: 'OAuth2',
|
||||||
value: 'none'
|
value: 'oAuth2'
|
||||||
},
|
},
|
||||||
|
|
||||||
],
|
],
|
||||||
default: 'none',
|
default: 'accessToken',
|
||||||
description: 'If authentication should be activated for the webhook (makes it more scure).',
|
description: 'If authentication should be activated for the webhook (makes it more scure).',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
|
@ -99,6 +99,7 @@
|
||||||
"dist/credentials/PagerDutyApi.credentials.js",
|
"dist/credentials/PagerDutyApi.credentials.js",
|
||||||
"dist/credentials/PayPalApi.credentials.js",
|
"dist/credentials/PayPalApi.credentials.js",
|
||||||
"dist/credentials/PipedriveApi.credentials.js",
|
"dist/credentials/PipedriveApi.credentials.js",
|
||||||
|
"dist/credentials/PipedriveOAuth2Api.credentials.js",
|
||||||
"dist/credentials/Postgres.credentials.js",
|
"dist/credentials/Postgres.credentials.js",
|
||||||
"dist/credentials/Redis.credentials.js",
|
"dist/credentials/Redis.credentials.js",
|
||||||
"dist/credentials/RocketchatApi.credentials.js",
|
"dist/credentials/RocketchatApi.credentials.js",
|
||||||
|
|
Loading…
Reference in a new issue