OAuth2 support, changed url to subdomain in credentials

This commit is contained in:
Rupenieks 2020-06-09 14:00:41 +02:00
parent 516a56ea32
commit bdad41f4e8
5 changed files with 146 additions and 13 deletions

View file

@ -8,10 +8,11 @@ export class ZendeskApi implements ICredentialType {
displayName = 'Zendesk API'; displayName = 'Zendesk API';
properties = [ properties = [
{ {
displayName: 'URL', displayName: 'Subdomain',
name: 'url', name: 'subdomain',
type: 'string' as NodePropertyTypes, type: 'string' as NodePropertyTypes,
default: '', description: 'The subdomain of your Zendesk work environment.',
default: 'n8n',
}, },
{ {
displayName: 'Email', displayName: 'Email',

View file

@ -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.',
},
];
}

View file

@ -14,26 +14,48 @@ import {
} from 'n8n-workflow'; } 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<any> { // tslint:disable-line:no-any export async function zendeskApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, method: string, resource: string, body: any = {}, qs: IDataObject = {}, uri?: string, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
const credentials = this.getCredentials('zendeskApi'); const authenticationMethod = this.getNodeParameter('authentication', 0);
if (credentials === undefined) {
throw new Error('No credentials got returned!');
}
const base64Key = Buffer.from(`${credentials.email}/token:${credentials.apiToken}`).toString('base64');
let options: OptionsWithUri = { let options: OptionsWithUri = {
headers: { 'Authorization': `Basic ${base64Key}`}, headers: {},
method, method,
qs, qs,
body, body,
uri: uri ||`${credentials.url}/api/v2${resource}.json`, //@ts-ignore
uri,
json: true json: true
}; };
options = Object.assign({}, options, option); options = Object.assign({}, options, option);
if (Object.keys(options.body).length === 0) { if (Object.keys(options.body).length === 0) {
delete options.body; delete options.body;
} }
try { try {
return await this.helpers.request!(options); if (authenticationMethod === 'accessToken') {
} catch (err) { 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; let errorMessage = err.message;
if (err.response && err.response.body && err.response.body.error) { if (err.response && err.response.body && err.response.body.error) {
errorMessage = err.response.body.error; errorMessage = err.response.body.error;

View file

@ -52,9 +52,44 @@ export class Zendesk implements INodeType {
{ {
name: 'zendeskApi', name: 'zendeskApi',
required: true, required: true,
} displayOptions: {
show: {
authentication: [
'accessToken',
],
},
},
},
{
name: 'zendeskOAuth2Api',
required: true,
displayOptions: {
show: {
authentication: [
'oAuth2',
],
},
},
},
], ],
properties: [ 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', displayName: 'Resource',
name: 'resource', name: 'resource',

View file

@ -126,6 +126,7 @@
"dist/credentials/WooCommerceApi.credentials.js", "dist/credentials/WooCommerceApi.credentials.js",
"dist/credentials/WordpressApi.credentials.js", "dist/credentials/WordpressApi.credentials.js",
"dist/credentials/ZendeskApi.credentials.js", "dist/credentials/ZendeskApi.credentials.js",
"dist/credentials/ZendeskOAuth2Api.credentials.js",
"dist/credentials/ZohoOAuth2Api.credentials.js", "dist/credentials/ZohoOAuth2Api.credentials.js",
"dist/credentials/ZulipApi.credentials.js" "dist/credentials/ZulipApi.credentials.js"
], ],