From 9a449284aedb956bbe9aad94eb64f66f2392e510 Mon Sep 17 00:00:00 2001 From: Rupenieks Date: Tue, 16 Jun 2020 11:45:47 +0200 Subject: [PATCH] Drift OAuth2 support --- .../credentials/DriftOAuth2Api.credentials.ts | 41 +++++++++++++++++++ packages/nodes-base/nodes/Drift/Drift.node.ts | 35 ++++++++++++++++ .../nodes/Drift/GenericFunctions.ts | 33 ++++++++------- packages/nodes-base/package.json | 1 + 4 files changed, 96 insertions(+), 14 deletions(-) create mode 100644 packages/nodes-base/credentials/DriftOAuth2Api.credentials.ts diff --git a/packages/nodes-base/credentials/DriftOAuth2Api.credentials.ts b/packages/nodes-base/credentials/DriftOAuth2Api.credentials.ts new file mode 100644 index 0000000000..5b509c5981 --- /dev/null +++ b/packages/nodes-base/credentials/DriftOAuth2Api.credentials.ts @@ -0,0 +1,41 @@ +import { + ICredentialType, + NodePropertyTypes, +} from 'n8n-workflow'; + + +export class DriftOAuth2Api implements ICredentialType { + name = 'driftOAuth2Api'; + extends = [ + 'oAuth2Api', + ]; + displayName = 'Drift OAuth2 API'; + properties = [ + { + displayName: 'Authorization URL', + name: 'authUrl', + type: 'hidden' as NodePropertyTypes, + default: 'ttps://dev.drift.com/authorize', + required: true, + }, + { + displayName: 'Access Token URL', + name: 'accessTokenUrl', + type: 'hidden' as NodePropertyTypes, + default: 'https://driftapi.com/oauth2/token', + required: true, + }, + { + displayName: 'Auth URI Query Parameters', + name: 'authQueryParameters', + type: 'hidden' as NodePropertyTypes, + default: '', + }, + { + displayName: 'Authentication', + name: 'authentication', + type: 'hidden' as NodePropertyTypes, + default: 'body', + }, + ]; +} diff --git a/packages/nodes-base/nodes/Drift/Drift.node.ts b/packages/nodes-base/nodes/Drift/Drift.node.ts index 53a9a2695e..46afa3d519 100644 --- a/packages/nodes-base/nodes/Drift/Drift.node.ts +++ b/packages/nodes-base/nodes/Drift/Drift.node.ts @@ -37,9 +37,44 @@ export class Drift implements INodeType { { name: 'driftApi', required: true, + displayOptions: { + show: { + authentication: [ + 'accessToken', + ], + }, + }, + }, + { + name: 'driftOAuth2Api', + 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/Drift/GenericFunctions.ts b/packages/nodes-base/nodes/Drift/GenericFunctions.ts index 47b38a86e3..c28d6bd5a2 100644 --- a/packages/nodes-base/nodes/Drift/GenericFunctions.ts +++ b/packages/nodes-base/nodes/Drift/GenericFunctions.ts @@ -12,25 +12,15 @@ import { } from 'n8n-workflow'; export async function driftApiRequest(this: IExecuteFunctions | IWebhookFunctions | IHookFunctions | ILoadOptionsFunctions, method: string, resource: string, body: any = {}, query: IDataObject = {}, uri?: string, option: IDataObject = {}): Promise { // tslint:disable-line:no-any - - const credentials = this.getCredentials('driftApi'); - - if (credentials === undefined) { - throw new Error('No credentials got returned!'); - } - - const endpoint = 'https://driftapi.com'; - let options: OptionsWithUri = { - headers: { - Authorization: `Bearer ${credentials.accessToken}`, - }, + headers: {}, method, body, qs: query, - uri: uri || `${endpoint}${resource}`, + uri: uri || `https://driftapi.com${resource}`, json: true }; + if (!Object.keys(body).length) { delete options.form; } @@ -38,8 +28,23 @@ export async function driftApiRequest(this: IExecuteFunctions | IWebhookFunction delete options.qs; } options = Object.assign({}, options, option); + + const authenticationMethod = this.getNodeParameter('authentication', 0); + try { - return await this.helpers.request!(options); + if (authenticationMethod === 'accessToken') { + const credentials = this.getCredentials('driftApi'); + + if (credentials === undefined) { + throw new Error('No credentials got returned!'); + } + + options.headers!['Authorization'] = `Bearer ${credentials.accessToken}`; + + return await this.helpers.request!(options); + } else { + return await this.helpers.requestOAuth2!.call(this, 'driftOAuth2Api', options); + } } catch (error) { if (error.response) { const errorMessage = error.message || (error.response.body && error.response.body.message ); diff --git a/packages/nodes-base/package.json b/packages/nodes-base/package.json index dcfaeb2269..6c8c541998 100644 --- a/packages/nodes-base/package.json +++ b/packages/nodes-base/package.json @@ -47,6 +47,7 @@ "dist/credentials/CalendlyApi.credentials.js", "dist/credentials/DisqusApi.credentials.js", "dist/credentials/DriftApi.credentials.js", + "dist/credentials/DriftOAuth2Api.credentials.js", "dist/credentials/DropboxApi.credentials.js", "dist/credentials/EventbriteApi.credentials.js", "dist/credentials/FacebookGraphApi.credentials.js",