Drift OAuth2 support

This commit is contained in:
Rupenieks 2020-06-16 11:45:47 +02:00
parent 346ae8efc9
commit 9a449284ae
4 changed files with 96 additions and 14 deletions

View file

@ -0,0 +1,41 @@
import {
ICredentialType,
NodePropertyTypes,
} from 'n8n-workflow';
export class DriftOAuth2Api implements ICredentialType {
name = 'driftOAuth2Api';
extends = [
'oAuth2Api',
];
displayName = 'Drift OAuth2 API';
properties = [
{
displayName: 'Authorization URL',
name: 'authUrl',
type: 'hidden' as NodePropertyTypes,
default: 'ttps://dev.drift.com/authorize',
required: true,
},
{
displayName: 'Access Token URL',
name: 'accessTokenUrl',
type: 'hidden' as NodePropertyTypes,
default: 'https://driftapi.com/oauth2/token',
required: true,
},
{
displayName: 'Auth URI Query Parameters',
name: 'authQueryParameters',
type: 'hidden' as NodePropertyTypes,
default: '',
},
{
displayName: 'Authentication',
name: 'authentication',
type: 'hidden' as NodePropertyTypes,
default: 'body',
},
];
}

View file

@ -37,9 +37,44 @@ export class Drift implements INodeType {
{ {
name: 'driftApi', name: 'driftApi',
required: true, required: true,
displayOptions: {
show: {
authentication: [
'accessToken',
],
},
},
},
{
name: 'driftOAuth2Api',
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

@ -12,25 +12,15 @@ import {
} from 'n8n-workflow'; } from 'n8n-workflow';
export async function driftApiRequest(this: IExecuteFunctions | IWebhookFunctions | IHookFunctions | ILoadOptionsFunctions, method: string, resource: string, body: any = {}, query: IDataObject = {}, uri?: string, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any export async function driftApiRequest(this: IExecuteFunctions | IWebhookFunctions | IHookFunctions | ILoadOptionsFunctions, method: string, resource: string, body: any = {}, query: IDataObject = {}, uri?: string, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
const credentials = this.getCredentials('driftApi');
if (credentials === undefined) {
throw new Error('No credentials got returned!');
}
const endpoint = 'https://driftapi.com';
let options: OptionsWithUri = { let options: OptionsWithUri = {
headers: { headers: {},
Authorization: `Bearer ${credentials.accessToken}`,
},
method, method,
body, body,
qs: query, qs: query,
uri: uri || `${endpoint}${resource}`, uri: uri || `https://driftapi.com${resource}`,
json: true json: true
}; };
if (!Object.keys(body).length) { if (!Object.keys(body).length) {
delete options.form; delete options.form;
} }
@ -38,8 +28,23 @@ export async function driftApiRequest(this: IExecuteFunctions | IWebhookFunction
delete options.qs; delete options.qs;
} }
options = Object.assign({}, options, option); options = Object.assign({}, options, option);
const authenticationMethod = this.getNodeParameter('authentication', 0);
try { try {
return await this.helpers.request!(options); if (authenticationMethod === 'accessToken') {
const credentials = this.getCredentials('driftApi');
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, 'driftOAuth2Api', options);
}
} catch (error) { } catch (error) {
if (error.response) { if (error.response) {
const errorMessage = error.message || (error.response.body && error.response.body.message ); const errorMessage = error.message || (error.response.body && error.response.body.message );

View file

@ -47,6 +47,7 @@
"dist/credentials/CalendlyApi.credentials.js", "dist/credentials/CalendlyApi.credentials.js",
"dist/credentials/DisqusApi.credentials.js", "dist/credentials/DisqusApi.credentials.js",
"dist/credentials/DriftApi.credentials.js", "dist/credentials/DriftApi.credentials.js",
"dist/credentials/DriftOAuth2Api.credentials.js",
"dist/credentials/DropboxApi.credentials.js", "dist/credentials/DropboxApi.credentials.js",
"dist/credentials/EventbriteApi.credentials.js", "dist/credentials/EventbriteApi.credentials.js",
"dist/credentials/FacebookGraphApi.credentials.js", "dist/credentials/FacebookGraphApi.credentials.js",