Merge pull request #674 from n8n-io/Drift-OAuth2-support

Drift OAuth2 support
This commit is contained in:
Ricardo Espinoza 2020-06-25 22:30:45 -04:00 committed by GitHub
commit d221f6dd97
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 105 additions and 16 deletions

View file

@ -0,0 +1,47 @@
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: 'https://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: 'Scope',
name: 'scope',
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',
required: true,
displayOptions: {
show: {
authentication: [
'accessToken',
],
},
},
},
{
name: 'driftOAuth2Api',
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',

View file

@ -12,25 +12,15 @@ import {
} 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
const credentials = this.getCredentials('driftApi');
if (credentials === undefined) {
throw new Error('No credentials got returned!');
}
const endpoint = 'https://driftapi.com';
let options: OptionsWithUri = {
headers: {
Authorization: `Bearer ${credentials.accessToken}`,
},
headers: {},
method,
body,
qs: query,
uri: uri || `${endpoint}${resource}`,
uri: uri || `https://driftapi.com${resource}`,
json: true
};
if (!Object.keys(body).length) {
delete options.form;
}
@ -38,11 +28,27 @@ export async function driftApiRequest(this: IExecuteFunctions | IWebhookFunction
delete options.qs;
}
options = Object.assign({}, options, option);
const authenticationMethod = this.getNodeParameter('authentication', 0);
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) {
if (error.response) {
const errorMessage = error.message || (error.response.body && error.response.body.message );
if (error.response && error.response.body && error.response.body.error) {
const errorMessage = error.response.body.error.message;
throw new Error(`Drift error response [${error.statusCode}]: ${errorMessage}`);
}
throw error;

View file

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