From c52765b21a44ae765c483f7c8c132b163edfebfa Mon Sep 17 00:00:00 2001 From: Rupenieks Date: Wed, 10 Jun 2020 11:49:48 +0200 Subject: [PATCH] OAuth2: generic functions, credentials, UI --- .../ClearbitOAuth2Api.credentials.ts | 60 +++++++++++++++++++ .../nodes/Clearbit/Clearbit.node.ts | 37 +++++++++++- .../nodes/Clearbit/GenericFunctions.ts | 24 +++++--- packages/nodes-base/package.json | 1 + 4 files changed, 114 insertions(+), 8 deletions(-) create mode 100644 packages/nodes-base/credentials/ClearbitOAuth2Api.credentials.ts diff --git a/packages/nodes-base/credentials/ClearbitOAuth2Api.credentials.ts b/packages/nodes-base/credentials/ClearbitOAuth2Api.credentials.ts new file mode 100644 index 0000000000..44668dea37 --- /dev/null +++ b/packages/nodes-base/credentials/ClearbitOAuth2Api.credentials.ts @@ -0,0 +1,60 @@ +import { + ICredentialType, + NodePropertyTypes, +} from 'n8n-workflow'; + + +export class ClearbitOAuth2Api implements ICredentialType { + name = 'clearbitOAuth2Api'; + extends = [ + 'oAuth2Api', + ]; + displayName = 'Clearbit OAuth2 API'; + properties = [ + { + displayName: 'Authorization URL', + name: 'authUrl', + type: 'hidden' as NodePropertyTypes, + default: 'https://clearbit.com/oauth/authorize', + required: true, + }, + { + displayName: 'Access Token URL', + name: 'accessTokenUrl', + type: 'hidden' as NodePropertyTypes, + default: 'https://clearbit.com/oauth/access_token', + required: true, + }, + { + displayName: 'Scope', + name: 'scope', + type: 'hidden' as NodePropertyTypes, + default: '', + }, + { + displayName: 'Auth URI Query Parameters', + name: 'authQueryParameters', + type: 'hidden' as NodePropertyTypes, + default: '', + }, + { + displayName: 'Authentication', + name: 'authentication', + type: 'options' as NodePropertyTypes, + options: [ + { + name: 'Body', + value: 'body', + description: 'Send credentials in body', + }, + { + name: 'Header', + value: 'header', + description: 'Send credentials as Basic Auth header', + }, + ], + default: 'header', + description: 'Resource to consume.', + }, + ]; +} diff --git a/packages/nodes-base/nodes/Clearbit/Clearbit.node.ts b/packages/nodes-base/nodes/Clearbit/Clearbit.node.ts index 4ed7bbc7d5..3f3742a8b1 100644 --- a/packages/nodes-base/nodes/Clearbit/Clearbit.node.ts +++ b/packages/nodes-base/nodes/Clearbit/Clearbit.node.ts @@ -38,9 +38,44 @@ export class Clearbit implements INodeType { { name: 'clearbitApi', required: true, - } + displayOptions: { + show: { + authentication: [ + 'accessToken', + ], + }, + }, + }, + { + name: 'clearbitOAuth2Api', + 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/Clearbit/GenericFunctions.ts b/packages/nodes-base/nodes/Clearbit/GenericFunctions.ts index f516c0dd88..bfdd3f04d8 100644 --- a/packages/nodes-base/nodes/Clearbit/GenericFunctions.ts +++ b/packages/nodes-base/nodes/Clearbit/GenericFunctions.ts @@ -8,12 +8,10 @@ import { import { IDataObject } from 'n8n-workflow'; export async function clearbitApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, method: string, api: string, resource: string, body: any = {}, qs: IDataObject = {}, uri?: string, option: IDataObject = {}): Promise { // tslint:disable-line:no-any - const credentials = this.getCredentials('clearbitApi'); - if (credentials === undefined) { - throw new Error('No credentials got returned!'); - } + const authenticationMethod = this.getNodeParameter('authentication', 0); + let options: OptionsWithUri = { - headers: { Authorization: `Bearer ${credentials.apiKey}`}, + headers: {}, method, qs, body, @@ -24,9 +22,21 @@ export async function clearbitApiRequest(this: IHookFunctions | IExecuteFunction if (Object.keys(options.body).length === 0) { delete options.body; } + try { - return await this.helpers.request!(options); - } catch (error) { + if (authenticationMethod === 'accessToken') { + const credentials = this.getCredentials('clearbitApi'); + if (credentials === undefined) { + throw new Error('No credentials got returned!'); + } + //@ts-ignore + options.headers['Authorization'] = `Bearer ${credentials.apiKey}`; + // @ts-ignore + return await this.helpers.request(options); + } else { + return await this.helpers.requestOAuth2?.call(this, 'clearbitOAuth2Api', options); + } + } catch(error) { if (error.statusCode === 401) { // Return a clear error throw new Error('The Clearbit credentials are not valid!'); diff --git a/packages/nodes-base/package.json b/packages/nodes-base/package.json index 3f28db9dec..459bf27ac9 100644 --- a/packages/nodes-base/package.json +++ b/packages/nodes-base/package.json @@ -39,6 +39,7 @@ "dist/credentials/BitlyApi.credentials.js", "dist/credentials/ChargebeeApi.credentials.js", "dist/credentials/ClearbitApi.credentials.js", + "dist/credentials/ClearbitOAuth2Api.credentials.js", "dist/credentials/ClickUpApi.credentials.js", "dist/credentials/ClockifyApi.credentials.js", "dist/credentials/CockpitApi.credentials.js",