diff --git a/packages/nodes-base/credentials/ZendeskApi.credentials.ts b/packages/nodes-base/credentials/ZendeskApi.credentials.ts index 29048c1172..4f285912b6 100644 --- a/packages/nodes-base/credentials/ZendeskApi.credentials.ts +++ b/packages/nodes-base/credentials/ZendeskApi.credentials.ts @@ -8,10 +8,11 @@ export class ZendeskApi implements ICredentialType { displayName = 'Zendesk API'; properties = [ { - displayName: 'URL', - name: 'url', + displayName: 'Subdomain', + name: 'subdomain', type: 'string' as NodePropertyTypes, - default: '', + description: 'The subdomain of your Zendesk work environment.', + default: 'n8n', }, { displayName: 'Email', diff --git a/packages/nodes-base/credentials/ZendeskOAuth2Api.credentials.ts b/packages/nodes-base/credentials/ZendeskOAuth2Api.credentials.ts new file mode 100644 index 0000000000..b530404a0d --- /dev/null +++ b/packages/nodes-base/credentials/ZendeskOAuth2Api.credentials.ts @@ -0,0 +1,74 @@ +import { + ICredentialType, + NodePropertyTypes, +} from 'n8n-workflow'; + + +export class ZendeskOAuth2Api implements ICredentialType { + name = 'zendeskOAuth2Api'; + extends = [ + 'oAuth2Api', + ]; + displayName = 'Zendesk OAuth2 API'; + properties = [ + { + displayName: 'Subdomain', + name: 'subdomain', + type: 'string' as NodePropertyTypes, + default: 'n8n', + description: 'The subdomain of your Zendesk work environment.', + required: true, + }, + { + displayName: 'Authorization URL', + name: 'authUrl', + type: 'string' as NodePropertyTypes, + default: 'https://{SUBDOMAIN_HERE}.zendesk.com/oauth/authorizations/new', + description: 'URL to get authorization code. Replace {SUBDOMAIN_HERE} with your subdomain.', + required: true, + }, + { + displayName: 'Access Token URL', + name: 'accessTokenUrl', + type: 'string' as NodePropertyTypes, + default: 'https://{SUBDOMAIN_HERE}.zendesk.com/oauth/tokens', + description: 'URL to get access token. Replace {SUBDOMAIN_HERE} with your subdomain.', + required: true, + }, + { + displayName: 'Client ID', + name: 'clientId', + type: 'string' as NodePropertyTypes, + default: '', + required: true, + }, + { + displayName: 'Client Secret', + name: 'clientSecret', + type: 'string' as NodePropertyTypes, + default: '', + required: true, + }, + { + displayName: 'Scope', + name: 'scope', + type: 'string' as NodePropertyTypes, + default: 'write read', + }, + { + 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: '', + }, + { + displayName: 'Authentication', + name: 'authentication', + type: 'hidden' as NodePropertyTypes, + default: 'body', + description: 'Resource to consume.', + }, + ]; +} diff --git a/packages/nodes-base/nodes/Zendesk/GenericFunctions.ts b/packages/nodes-base/nodes/Zendesk/GenericFunctions.ts index ffe371e160..007741813f 100644 --- a/packages/nodes-base/nodes/Zendesk/GenericFunctions.ts +++ b/packages/nodes-base/nodes/Zendesk/GenericFunctions.ts @@ -14,26 +14,48 @@ import { } from 'n8n-workflow'; export async function zendeskApiRequest(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('zendeskApi'); - if (credentials === undefined) { - throw new Error('No credentials got returned!'); - } - const base64Key = Buffer.from(`${credentials.email}/token:${credentials.apiToken}`).toString('base64'); + const authenticationMethod = this.getNodeParameter('authentication', 0); + let options: OptionsWithUri = { - headers: { 'Authorization': `Basic ${base64Key}`}, + headers: {}, method, qs, body, - uri: uri ||`${credentials.url}/api/v2${resource}.json`, + //@ts-ignore + uri, json: true }; + options = Object.assign({}, options, option); if (Object.keys(options.body).length === 0) { delete options.body; } + try { - return await this.helpers.request!(options); - } catch (err) { + if (authenticationMethod === 'accessToken') { + const credentials = this.getCredentials('zendeskApi'); + + if (credentials === undefined) { + throw new Error('No credentials got returned!'); + } + + const base64Key = Buffer.from(`${credentials.email}/token:${credentials.apiToken}`).toString('base64'); + options.uri = `https://${credentials.subdomain}.zendesk.com/api/v2${resource}.json`; + options.headers!['Authorization'] = `Basic ${base64Key}`; + + return await this.helpers.request!(options); + } else { + const credentials = this.getCredentials('zendeskOAuth2Api'); + + if (credentials === undefined) { + throw new Error('No credentials got returned!'); + } + + options.uri = `https://${credentials.subdomain}.zendesk.com/api/v2${resource}.json`; + + return await this.helpers.requestOAuth2!.call(this, 'zendeskOAuth2Api', options); + } + } catch(err) { let errorMessage = err.message; if (err.response && err.response.body && err.response.body.error) { errorMessage = err.response.body.error; diff --git a/packages/nodes-base/nodes/Zendesk/Zendesk.node.ts b/packages/nodes-base/nodes/Zendesk/Zendesk.node.ts index 8a2586d8d8..d0ea357b5a 100644 --- a/packages/nodes-base/nodes/Zendesk/Zendesk.node.ts +++ b/packages/nodes-base/nodes/Zendesk/Zendesk.node.ts @@ -52,9 +52,44 @@ export class Zendesk implements INodeType { { name: 'zendeskApi', required: true, - } + displayOptions: { + show: { + authentication: [ + 'accessToken', + ], + }, + }, + }, + { + name: 'zendeskOAuth2Api', + 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/package.json b/packages/nodes-base/package.json index 3f28db9dec..03f9d8c1b1 100644 --- a/packages/nodes-base/package.json +++ b/packages/nodes-base/package.json @@ -126,6 +126,7 @@ "dist/credentials/WooCommerceApi.credentials.js", "dist/credentials/WordpressApi.credentials.js", "dist/credentials/ZendeskApi.credentials.js", + "dist/credentials/ZendeskOAuth2Api.credentials.js", "dist/credentials/ZohoOAuth2Api.credentials.js", "dist/credentials/ZulipApi.credentials.js" ],