From de7a6d6dfb884be6901b0cab23f668fdc8a3e940 Mon Sep 17 00:00:00 2001 From: Rupenieks <32895755+Rupenieks@users.noreply.github.com> Date: Fri, 3 Jul 2020 11:22:39 +0200 Subject: [PATCH] :construction: Setup Added OAuth2 credentials, UI, generic functions changes --- .../credentials/BitlyOAuth2Api.credentials.ts | 80 +++++++++++++++++++ packages/nodes-base/nodes/Bitly/Bitly.node.ts | 37 ++++++++- .../nodes/Bitly/GenericFunctions.ts | 27 ++++--- packages/nodes-base/package.json | 1 + 4 files changed, 135 insertions(+), 10 deletions(-) create mode 100644 packages/nodes-base/credentials/BitlyOAuth2Api.credentials.ts diff --git a/packages/nodes-base/credentials/BitlyOAuth2Api.credentials.ts b/packages/nodes-base/credentials/BitlyOAuth2Api.credentials.ts new file mode 100644 index 0000000000..2ba8ba3d3e --- /dev/null +++ b/packages/nodes-base/credentials/BitlyOAuth2Api.credentials.ts @@ -0,0 +1,80 @@ +import { + ICredentialType, + NodePropertyTypes, +} from 'n8n-workflow'; + + +export class BitlyOAuth2Api implements ICredentialType { + name = 'bitlyOAuth2Api'; + displayName = 'Bitly OAuth2 API'; + + extends = [ + 'oAuth2Api', + ]; + properties = [ + { + displayName: 'Authorization URL', + name: 'authUrl', + type: 'hidden' as NodePropertyTypes, + default: 'https://bitly.com/oauth/authorize', + required: true, + }, + { + displayName: 'Access Token URL', + name: 'accessTokenUrl', + type: 'hidden' as NodePropertyTypes, + default: 'https://api-ssl.bitly.com/oauth/access_token', + required: true, + }, + { + displayName: 'Client ID', + name: 'clientId', + type: 'string' as NodePropertyTypes, + default: '', + required: true, + }, + { + displayName: 'Client Secret', + name: 'clientSecret', + type: 'string' as NodePropertyTypes, + typeOptions: { + password: true, + }, + default: '', + required: true, + }, + { + displayName: 'Scope', + name: 'scope', + type: 'hidden' as NodePropertyTypes, + default: '', + }, + { + displayName: 'Auth URI Query Parameters', + name: 'authQueryParameters', + type: 'hidden' as NodePropertyTypes, + default: '', + description: 'For some services additional query parameters have to be set which can be defined here.', + placeholder: 'access_type=offline', + }, + { + 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/Bitly/Bitly.node.ts b/packages/nodes-base/nodes/Bitly/Bitly.node.ts index 725185300a..9b7aed8859 100644 --- a/packages/nodes-base/nodes/Bitly/Bitly.node.ts +++ b/packages/nodes-base/nodes/Bitly/Bitly.node.ts @@ -37,9 +37,44 @@ export class Bitly implements INodeType { { name: 'bitlyApi', required: true, - } + displayOptions: { + show: { + authentication: [ + 'accessToken', + ], + }, + }, + }, + { + name: 'bitlyOAuth2Api', + 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/Bitly/GenericFunctions.ts b/packages/nodes-base/nodes/Bitly/GenericFunctions.ts index 1564c49497..85a2c70946 100644 --- a/packages/nodes-base/nodes/Bitly/GenericFunctions.ts +++ b/packages/nodes-base/nodes/Bitly/GenericFunctions.ts @@ -6,14 +6,12 @@ import { ILoadOptionsFunctions, } from 'n8n-core'; import { IDataObject } from 'n8n-workflow'; +import { attachmentFields } from '../Salesforce/AttachmentDescription'; export async function bitlyApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, method: string, resource: string, body: any = {}, qs: IDataObject = {}, uri?: string, option: IDataObject = {}): Promise { // tslint:disable-line:no-any - const credentials = this.getCredentials('bitlyApi'); - if (credentials === undefined) { - throw new Error('No credentials got returned!'); - } + const authenticationMethod = this.getNodeParameter('authentication', 0) as string; let options: OptionsWithUri = { - headers: { Authorization: `Bearer ${credentials.accessToken}`}, + headers: {}, method, qs, body, @@ -24,10 +22,21 @@ export async function bitlyApiRequest(this: IHookFunctions | IExecuteFunctions | if (Object.keys(options.body).length === 0) { delete options.body; } - try { - return await this.helpers.request!(options); - } catch (err) { - throw new Error(err); + + try{ + if (authenticationMethod === 'accessToken') { + const credentials = this.getCredentials('bitlyApi'); + 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, 'bitlyOAuth2Api', options); + } + } catch(error) { + throw new Error(error); } } diff --git a/packages/nodes-base/package.json b/packages/nodes-base/package.json index c19d8e2349..d5370e6b7b 100644 --- a/packages/nodes-base/package.json +++ b/packages/nodes-base/package.json @@ -37,6 +37,7 @@ "dist/credentials/BannerbearApi.credentials.js", "dist/credentials/BitbucketApi.credentials.js", "dist/credentials/BitlyApi.credentials.js", + "dist/credentials/BitlyOAuth2Api.credentials.js", "dist/credentials/ChargebeeApi.credentials.js", "dist/credentials/ClearbitApi.credentials.js", "dist/credentials/ClickUpApi.credentials.js",