From 46147e0d97795f5cf9b2e16b8cf1f9353475df06 Mon Sep 17 00:00:00 2001 From: Rupenieks Date: Mon, 8 Jun 2020 16:58:32 +0200 Subject: [PATCH 1/7] OAuth2 credentials, genericFunctions modded, oauth2 ui changes --- .../PagerDutyOAuth2Api.credentials.ts | 39 +++++++++++++++++++ .../nodes/PagerDuty/GenericFunctions.ts | 26 +++++++++---- .../nodes/PagerDuty/PagerDuty.node.ts | 34 ++++++++++++++++ packages/nodes-base/package.json | 1 + 4 files changed, 92 insertions(+), 8 deletions(-) create mode 100644 packages/nodes-base/credentials/PagerDutyOAuth2Api.credentials.ts diff --git a/packages/nodes-base/credentials/PagerDutyOAuth2Api.credentials.ts b/packages/nodes-base/credentials/PagerDutyOAuth2Api.credentials.ts new file mode 100644 index 0000000000..0bdd68b9c9 --- /dev/null +++ b/packages/nodes-base/credentials/PagerDutyOAuth2Api.credentials.ts @@ -0,0 +1,39 @@ +import { + ICredentialType, + NodePropertyTypes, +} from 'n8n-workflow'; + +export class PagerDutyOAuth2Api implements ICredentialType { + name = 'pagerDutyOAuth2Api'; + extends = [ + 'oAuth2Api', + ]; + displayName = 'PagerDuty OAuth2 API'; + properties = [ + { + displayName: 'Authorization URL', + name: 'authUrl', + type: 'hidden' as NodePropertyTypes, + default: 'https://app.pagerduty.com/oauth/authorize', + }, + { + displayName: 'Access Token URL', + name: 'accessTokenUrl', + type: 'hidden' as NodePropertyTypes, + default: 'https://app.pagerduty.com/oauth/token', + }, + { + displayName: 'Auth URI Query Parameters', + name: 'authQueryParameters', + type: 'hidden' as NodePropertyTypes, + default: 'grant_type=authorization_code', + }, + { + displayName: 'Authentication', + name: 'authentication', + type: 'hidden' as NodePropertyTypes, + default: 'header', + description: 'Method of authentication.', + }, + ]; +} diff --git a/packages/nodes-base/nodes/PagerDuty/GenericFunctions.ts b/packages/nodes-base/nodes/PagerDuty/GenericFunctions.ts index c360114144..716b4e0d5e 100644 --- a/packages/nodes-base/nodes/PagerDuty/GenericFunctions.ts +++ b/packages/nodes-base/nodes/PagerDuty/GenericFunctions.ts @@ -19,16 +19,11 @@ import { export async function pagerDutyApiRequest(this: IExecuteFunctions | IWebhookFunctions | IHookFunctions | ILoadOptionsFunctions, method: string, resource: string, body: any = {}, query: IDataObject = {}, uri?: string, headers: IDataObject = {}): Promise { // tslint:disable-line:no-any - const credentials = this.getCredentials('pagerDutyApi'); - - if (credentials === undefined) { - throw new Error('No credentials got returned!'); - } + const authenticationMethod = this.getNodeParameter('authentication', 0); const options: OptionsWithUri = { headers: { - Accept: 'application/vnd.pagerduty+json;version=2', - Authorization: `Token token=${credentials.apiToken}`, + Accept: 'application/vnd.pagerduty+json;version=2' }, method, body, @@ -39,15 +34,30 @@ export async function pagerDutyApiRequest(this: IExecuteFunctions | IWebhookFunc arrayFormat: 'brackets', }, }; + if (!Object.keys(body).length) { delete options.form; } if (!Object.keys(query).length) { delete options.qs; } + options.headers = Object.assign({}, options.headers, headers); + try { - return await this.helpers.request!(options); + if (authenticationMethod === 'accessToken') { + const credentials = this.getCredentials('pagerDutyApi'); + + if (credentials === undefined) { + throw new Error('No credentials got returned!'); + } + + options.headers!['Authorization'] = `Token token=${credentials.apiToken}`; + + return await this.helpers.request!(options); + } else { + return await this.helpers.requestOAuth2!.call(this, 'pagerDutyOAuth2Api', options); + } } catch (error) { if (error.response && error.response.body && error.response.body.error && error.response.body.error.errors) { // Try to return the error prettier diff --git a/packages/nodes-base/nodes/PagerDuty/PagerDuty.node.ts b/packages/nodes-base/nodes/PagerDuty/PagerDuty.node.ts index d53e5921b8..f9b8b1e3c7 100644 --- a/packages/nodes-base/nodes/PagerDuty/PagerDuty.node.ts +++ b/packages/nodes-base/nodes/PagerDuty/PagerDuty.node.ts @@ -66,9 +66,43 @@ export class PagerDuty implements INodeType { { name: 'pagerDutyApi', required: true, + displayOptions: { + show: { + authentication: [ + 'accessToken', + ], + }, + }, + }, + { + name: 'pagerDutyOAuth2Api', + 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', + }, { displayName: 'Resource', name: 'resource', diff --git a/packages/nodes-base/package.json b/packages/nodes-base/package.json index 3f28db9dec..18314a9488 100644 --- a/packages/nodes-base/package.json +++ b/packages/nodes-base/package.json @@ -97,6 +97,7 @@ "dist/credentials/OAuth2Api.credentials.js", "dist/credentials/OpenWeatherMapApi.credentials.js", "dist/credentials/PagerDutyApi.credentials.js", + "dist/credentials/PagerDutyOAuth2Api.credentials.js", "dist/credentials/PayPalApi.credentials.js", "dist/credentials/PipedriveApi.credentials.js", "dist/credentials/Postgres.credentials.js", From 68f9dd15609f07e91e18f71ef82f299356db5c4d Mon Sep 17 00:00:00 2001 From: Rupenieks Date: Wed, 10 Jun 2020 09:50:50 +0200 Subject: [PATCH 2/7] OAuth2 support --- .../TypeformOAuth2Api.credentials.ts | 46 +++++++++++++++++++ .../nodes/Typeform/GenericFunctions.ts | 29 +++++++----- .../nodes/Typeform/TypeformTrigger.node.ts | 37 ++++++++++++++- packages/nodes-base/package.json | 1 + 4 files changed, 101 insertions(+), 12 deletions(-) create mode 100644 packages/nodes-base/credentials/TypeformOAuth2Api.credentials.ts diff --git a/packages/nodes-base/credentials/TypeformOAuth2Api.credentials.ts b/packages/nodes-base/credentials/TypeformOAuth2Api.credentials.ts new file mode 100644 index 0000000000..5465eee72c --- /dev/null +++ b/packages/nodes-base/credentials/TypeformOAuth2Api.credentials.ts @@ -0,0 +1,46 @@ +import { + ICredentialType, + NodePropertyTypes, +} from 'n8n-workflow'; + + +export class TypeformOAuth2Api implements ICredentialType { + name = 'typeformOAuth2Api'; + extends = [ + 'oAuth2Api', + ]; + properties = [ + { + displayName: 'Authorization URL', + name: 'authUrl', + type: 'hidden' as NodePropertyTypes, + default: 'https://api.typeform.com/oauth/authorize', + required: true, + }, + { + displayName: 'Access Token URL', + name: 'accessTokenUrl', + type: 'hidden' as NodePropertyTypes, + default: 'https://api.typeform.com/oauth/token', + required: true, + }, + { + displayName: 'Scope', + name: 'scope', + type: 'string' as NodePropertyTypes, + default: 'webhooks:write,webhooks:read,forms:read,', + }, + { + displayName: 'Auth URI Query Parameters', + name: 'authQueryParameters', + type: 'hidden' as NodePropertyTypes, + default: '', + }, + { + displayName: 'Authentication', + name: 'authentication', + type: 'hidden' as NodePropertyTypes, + default: 'header', + }, + ]; +} diff --git a/packages/nodes-base/nodes/Typeform/GenericFunctions.ts b/packages/nodes-base/nodes/Typeform/GenericFunctions.ts index c5e9242465..83ca713afe 100644 --- a/packages/nodes-base/nodes/Typeform/GenericFunctions.ts +++ b/packages/nodes-base/nodes/Typeform/GenericFunctions.ts @@ -45,18 +45,10 @@ export interface ITypeformAnswerField { * @returns {Promise} */ export async function apiRequest(this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions, method: string, endpoint: string, body: object, query?: IDataObject): Promise { // tslint:disable-line:no-any - const credentials = this.getCredentials('typeformApi'); - - if (credentials === undefined) { - throw new Error('No credentials got returned!'); - } - - query = query || {}; + const authenticationMethod = this.getNodeParameter('authentication', 0); const options: OptionsWithUri = { - headers: { - 'Authorization': `bearer ${credentials.accessToken}`, - }, + headers: {}, method, body, qs: query, @@ -64,8 +56,23 @@ export async function apiRequest(this: IHookFunctions | IExecuteFunctions | ILoa json: true, }; + query = query || {}; + try { - return await this.helpers.request!(options); + if (authenticationMethod === 'accessToken') { + + const credentials = this.getCredentials('typeformApi'); + + 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, 'typeformOAuth2Api', options); + } } catch (error) { if (error.statusCode === 401) { // Return a clear error diff --git a/packages/nodes-base/nodes/Typeform/TypeformTrigger.node.ts b/packages/nodes-base/nodes/Typeform/TypeformTrigger.node.ts index b127a1f594..9c8f6e16ff 100644 --- a/packages/nodes-base/nodes/Typeform/TypeformTrigger.node.ts +++ b/packages/nodes-base/nodes/Typeform/TypeformTrigger.node.ts @@ -37,7 +37,25 @@ export class TypeformTrigger implements INodeType { { name: 'typeformApi', required: true, - } + displayOptions: { + show: { + authentication: [ + 'accessToken', + ], + }, + }, + }, + { + name: 'typeformOAuth2Api', + required: true, + displayOptions: { + show: { + authentication: [ + 'oAuth2', + ], + }, + }, + }, ], webhooks: [ { @@ -48,6 +66,23 @@ export class TypeformTrigger 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: 'Form', name: 'formId', diff --git a/packages/nodes-base/package.json b/packages/nodes-base/package.json index 3f28db9dec..13b3f0c128 100644 --- a/packages/nodes-base/package.json +++ b/packages/nodes-base/package.json @@ -119,6 +119,7 @@ "dist/credentials/TwilioApi.credentials.js", "dist/credentials/TwitterOAuth1Api.credentials.js", "dist/credentials/TypeformApi.credentials.js", + "dist/credentials/TypeformOAuth2Api.credentials.js", "dist/credentials/TogglApi.credentials.js", "dist/credentials/UpleadApi.credentials.js", "dist/credentials/VeroApi.credentials.js", From 934a4f16d16fff1d5c87db9d7df004838ea37980 Mon Sep 17 00:00:00 2001 From: Rupenieks Date: Wed, 10 Jun 2020 09:51:12 +0200 Subject: [PATCH 3/7] Update TypeformOAuth2Api.credentials.ts --- packages/nodes-base/credentials/TypeformOAuth2Api.credentials.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/nodes-base/credentials/TypeformOAuth2Api.credentials.ts b/packages/nodes-base/credentials/TypeformOAuth2Api.credentials.ts index 5465eee72c..6e95391577 100644 --- a/packages/nodes-base/credentials/TypeformOAuth2Api.credentials.ts +++ b/packages/nodes-base/credentials/TypeformOAuth2Api.credentials.ts @@ -9,6 +9,7 @@ export class TypeformOAuth2Api implements ICredentialType { extends = [ 'oAuth2Api', ]; + displayName = 'Typeform OAuth2 API'; properties = [ { displayName: 'Authorization URL', From a8a2260c586e68d0cf5393b1d51bda404f24dfa0 Mon Sep 17 00:00:00 2001 From: ricardo Date: Sun, 14 Jun 2020 19:30:05 -0400 Subject: [PATCH 4/7] :zap: Small fix --- packages/nodes-base/package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/nodes-base/package.json b/packages/nodes-base/package.json index 7566f70f06..9cb463d8fd 100644 --- a/packages/nodes-base/package.json +++ b/packages/nodes-base/package.json @@ -125,6 +125,7 @@ "dist/credentials/TwilioApi.credentials.js", "dist/credentials/TwitterOAuth1Api.credentials.js", "dist/credentials/TypeformApi.credentials.js", + "dist/credentials/TypeformOAuth2Api.credentials.js", "dist/credentials/TogglApi.credentials.js", "dist/credentials/UpleadApi.credentials.js", "dist/credentials/VeroApi.credentials.js", From c4bbb961a2b41709d525acff7b54e29e1a034596 Mon Sep 17 00:00:00 2001 From: ricardo Date: Sun, 14 Jun 2020 19:31:45 -0400 Subject: [PATCH 5/7] :zap: Improvements to Typeform-Node --- .../credentials/TypeformOAuth2Api.credentials.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/nodes-base/credentials/TypeformOAuth2Api.credentials.ts b/packages/nodes-base/credentials/TypeformOAuth2Api.credentials.ts index 6e95391577..a876e87ed0 100644 --- a/packages/nodes-base/credentials/TypeformOAuth2Api.credentials.ts +++ b/packages/nodes-base/credentials/TypeformOAuth2Api.credentials.ts @@ -3,6 +3,12 @@ import { NodePropertyTypes, } from 'n8n-workflow'; +const scopes = [ + 'webhooks:write', + 'webhooks:read', + 'forms:read', +]; + export class TypeformOAuth2Api implements ICredentialType { name = 'typeformOAuth2Api'; @@ -28,8 +34,8 @@ export class TypeformOAuth2Api implements ICredentialType { { displayName: 'Scope', name: 'scope', - type: 'string' as NodePropertyTypes, - default: 'webhooks:write,webhooks:read,forms:read,', + type: 'hidden' as NodePropertyTypes, + default: scopes.join(','), }, { displayName: 'Auth URI Query Parameters', From 7682fd79e61a46b525f8f68f547f28eba4954b99 Mon Sep 17 00:00:00 2001 From: ricardo Date: Sun, 14 Jun 2020 21:36:56 -0400 Subject: [PATCH 6/7] :zap: Small fix --- packages/nodes-base/package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/nodes-base/package.json b/packages/nodes-base/package.json index 9cb463d8fd..649bde4837 100644 --- a/packages/nodes-base/package.json +++ b/packages/nodes-base/package.json @@ -102,6 +102,7 @@ "dist/credentials/OAuth2Api.credentials.js", "dist/credentials/OpenWeatherMapApi.credentials.js", "dist/credentials/PagerDutyApi.credentials.js", + "dist/credentials/PagerDutyOAuth2Api.credentials.js", "dist/credentials/PayPalApi.credentials.js", "dist/credentials/PipedriveApi.credentials.js", "dist/credentials/Postgres.credentials.js", From acfc4fc39dac367c82facb9d61e0efabe56d257d Mon Sep 17 00:00:00 2001 From: ricardo Date: Sun, 14 Jun 2020 21:39:30 -0400 Subject: [PATCH 7/7] :zap: Improvements to PagerDuty-Node --- .../credentials/PagerDutyOAuth2Api.credentials.ts | 8 +++++++- packages/nodes-base/nodes/PagerDuty/GenericFunctions.ts | 2 +- packages/nodes-base/nodes/PagerDuty/PagerDuty.node.ts | 8 ++++---- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/packages/nodes-base/credentials/PagerDutyOAuth2Api.credentials.ts b/packages/nodes-base/credentials/PagerDutyOAuth2Api.credentials.ts index 0bdd68b9c9..28b44011db 100644 --- a/packages/nodes-base/credentials/PagerDutyOAuth2Api.credentials.ts +++ b/packages/nodes-base/credentials/PagerDutyOAuth2Api.credentials.ts @@ -26,7 +26,13 @@ export class PagerDutyOAuth2Api implements ICredentialType { displayName: 'Auth URI Query Parameters', name: 'authQueryParameters', type: 'hidden' as NodePropertyTypes, - default: 'grant_type=authorization_code', + default: '', + }, + { + displayName: 'Scope', + name: 'scope', + type: 'hidden' as NodePropertyTypes, + default: '', }, { displayName: 'Authentication', diff --git a/packages/nodes-base/nodes/PagerDuty/GenericFunctions.ts b/packages/nodes-base/nodes/PagerDuty/GenericFunctions.ts index 716b4e0d5e..f4832b923c 100644 --- a/packages/nodes-base/nodes/PagerDuty/GenericFunctions.ts +++ b/packages/nodes-base/nodes/PagerDuty/GenericFunctions.ts @@ -45,7 +45,7 @@ export async function pagerDutyApiRequest(this: IExecuteFunctions | IWebhookFunc options.headers = Object.assign({}, options.headers, headers); try { - if (authenticationMethod === 'accessToken') { + if (authenticationMethod === 'apiToken') { const credentials = this.getCredentials('pagerDutyApi'); if (credentials === undefined) { diff --git a/packages/nodes-base/nodes/PagerDuty/PagerDuty.node.ts b/packages/nodes-base/nodes/PagerDuty/PagerDuty.node.ts index f9b8b1e3c7..8d62b8fc6f 100644 --- a/packages/nodes-base/nodes/PagerDuty/PagerDuty.node.ts +++ b/packages/nodes-base/nodes/PagerDuty/PagerDuty.node.ts @@ -69,7 +69,7 @@ export class PagerDuty implements INodeType { displayOptions: { show: { authentication: [ - 'accessToken', + 'apiToken', ], }, }, @@ -93,15 +93,15 @@ export class PagerDuty implements INodeType { type: 'options', options: [ { - name: 'Access Token', - value: 'accessToken', + name: 'API Token', + value: 'apiToken', }, { name: 'OAuth2', value: 'oAuth2', }, ], - default: 'accessToken', + default: 'apiToken', }, { displayName: 'Resource',