From e5683a90ab8192c739ba4edcddfed4c2ba67d0b7 Mon Sep 17 00:00:00 2001 From: Rupenieks Date: Tue, 16 Jun 2020 09:27:47 +0200 Subject: [PATCH] OAuth2 support --- .../AcuitySchedulingOAuth2Api.credentials.ts | 48 +++++++++++++++++++ .../AcuitySchedulingTrigger.node.ts | 37 +++++++++++++- .../AcuityScheduling/GenericFunctions.ts | 35 ++++++++------ packages/nodes-base/package.json | 1 + 4 files changed, 105 insertions(+), 16 deletions(-) create mode 100644 packages/nodes-base/credentials/AcuitySchedulingOAuth2Api.credentials.ts diff --git a/packages/nodes-base/credentials/AcuitySchedulingOAuth2Api.credentials.ts b/packages/nodes-base/credentials/AcuitySchedulingOAuth2Api.credentials.ts new file mode 100644 index 0000000000..305aefcfe5 --- /dev/null +++ b/packages/nodes-base/credentials/AcuitySchedulingOAuth2Api.credentials.ts @@ -0,0 +1,48 @@ +import { + ICredentialType, + NodePropertyTypes, +} from 'n8n-workflow'; + + +export class AcuitySchedulingOAuth2Api implements ICredentialType { + name = 'acuitySchedulingOAuth2Api'; + extends = [ + 'oAuth2Api', + ]; + displayName = 'AcuityScheduling OAuth2 API'; + properties = [ + { + displayName: 'Authorization URL', + name: 'authUrl', + type: 'hidden' as NodePropertyTypes, + default: 'https://acuityscheduling.com/oauth2/authorize', + required: true, + }, + { + displayName: 'Access Token URL', + name: 'accessTokenUrl', + type: 'hidden' as NodePropertyTypes, + default: 'https://acuityscheduling.com/oauth2/token', + required: true, + }, + { + displayName: 'Scope', + name: 'scope', + type: 'hidden' as NodePropertyTypes, + default: 'api-v1', + 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/AcuityScheduling/AcuitySchedulingTrigger.node.ts b/packages/nodes-base/nodes/AcuityScheduling/AcuitySchedulingTrigger.node.ts index 2166b5cc85..5c72f02d13 100644 --- a/packages/nodes-base/nodes/AcuityScheduling/AcuitySchedulingTrigger.node.ts +++ b/packages/nodes-base/nodes/AcuityScheduling/AcuitySchedulingTrigger.node.ts @@ -32,7 +32,25 @@ export class AcuitySchedulingTrigger implements INodeType { { name: 'acuitySchedulingApi', required: true, - } + displayOptions: { + show: { + authentication: [ + 'accessToken', + ], + }, + }, + }, + { + name: 'acuitySchedulingOAuth2Api', + required: true, + displayOptions: { + show: { + authentication: [ + 'oAuth2', + ], + }, + }, + }, ], webhooks: [ { @@ -43,6 +61,23 @@ export class AcuitySchedulingTrigger implements INodeType { }, ], 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: 'Event', name: 'event', diff --git a/packages/nodes-base/nodes/AcuityScheduling/GenericFunctions.ts b/packages/nodes-base/nodes/AcuityScheduling/GenericFunctions.ts index 9a2d0fc597..49d96e395c 100644 --- a/packages/nodes-base/nodes/AcuityScheduling/GenericFunctions.ts +++ b/packages/nodes-base/nodes/AcuityScheduling/GenericFunctions.ts @@ -9,34 +9,39 @@ import { import { IDataObject } from 'n8n-workflow'; export async function acuitySchedulingApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions | IWebhookFunctions, method: string, resource: string, body: any = {}, qs: IDataObject = {}, uri?: string, option: IDataObject = {}): Promise { // tslint:disable-line:no-any - const credentials = this.getCredentials('acuitySchedulingApi'); - if (credentials === undefined) { - throw new Error('No credentials got returned!'); - } + const authenticationMethod = this.getNodeParameter('authentication', 0); const options: OptionsWithUri = { headers: { 'Content-Type': 'application/json', }, - auth: { - user: credentials.userId as string, - password: credentials.apiKey as string, - }, + auth: {}, method, qs, body, uri: uri ||`https://acuityscheduling.com/api/v1${resource}`, json: true }; + try { - return await this.helpers.request!(options); - } catch (error) { + if (authenticationMethod === 'accessToken') { + const credentials = this.getCredentials('acuitySchedulingApi'); + if (credentials === undefined) { + throw new Error('No credentials got returned!'); + } - let errorMessage = error.message; - if (error.response.body && error.response.body.message) { - errorMessage = `[${error.response.body.status_code}] ${error.response.body.message}`; + options.auth = { + user: credentials.userId as string, + password: credentials.apiKey as string, + }; + + return await this.helpers.request!(options); + } else { + delete options.auth; + //@ts-ignore + return await this.helpers.requestOAuth2!.call(this, 'acuitySchedulingOAuth2Api', options, true); } - - throw new Error('Acuity Scheduling Error: ' + errorMessage); + } catch (error) { + throw new Error('Acuity Scheduling Error: ' + error.message); } } diff --git a/packages/nodes-base/package.json b/packages/nodes-base/package.json index 649bde4837..4494504fab 100644 --- a/packages/nodes-base/package.json +++ b/packages/nodes-base/package.json @@ -29,6 +29,7 @@ "dist/credentials/ActiveCampaignApi.credentials.js", "dist/credentials/AgileCrmApi.credentials.js", "dist/credentials/AcuitySchedulingApi.credentials.js", + "dist/credentials/AcuitySchedulingOAuth2Api.credentials.js", "dist/credentials/AirtableApi.credentials.js", "dist/credentials/Amqp.credentials.js", "dist/credentials/AsanaApi.credentials.js",