feat(Hubspot): Add support for Private App Token Authentication

* add Hubspot Private App Token Authentication

*  Add credential verification

*  Rename app token

Co-authored-by: Rene Wagner <wagner@villacircle.com>
Co-authored-by: Jan Oberhauser <janober@users.noreply.github.com>
This commit is contained in:
Ricardo Espinoza 2022-03-13 06:52:47 -04:00 committed by GitHub
parent 2ff13a6842
commit f73100a0bd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 68 additions and 5 deletions

View file

@ -9,7 +9,7 @@ export class HubspotAppToken implements ICredentialType {
documentationUrl = 'hubspot'; documentationUrl = 'hubspot';
properties: INodeProperties[] = [ properties: INodeProperties[] = [
{ {
displayName: 'App Token', displayName: 'APP Token',
name: 'appToken', name: 'appToken',
type: 'string', type: 'string',
default: '', default: '',

View file

@ -10,7 +10,10 @@ import {
} from 'n8n-core'; } from 'n8n-core';
import { import {
ICredentialDataDecryptedObject,
ICredentialTestFunctions,
IDataObject, IDataObject,
JsonObject,
NodeApiError, NodeApiError,
} from 'n8n-workflow'; } from 'n8n-workflow';
@ -59,7 +62,7 @@ export async function hubspotApiRequest(this: IHookFunctions | IExecuteFunctions
return await this.helpers.requestOAuth2!.call(this, 'hubspotOAuth2Api', options, { tokenType: 'Bearer', includeCredentialsOnRefreshOnBody: true }); return await this.helpers.requestOAuth2!.call(this, 'hubspotOAuth2Api', options, { tokenType: 'Bearer', includeCredentialsOnRefreshOnBody: true });
} }
} catch (error) { } catch (error) {
throw new NodeApiError(this.getNode(), error); throw new NodeApiError(this.getNode(), error as JsonObject);
} }
} }
@ -1969,3 +1972,33 @@ export const getAssociations = (associations: {
...(associations.ticketIds && { ticketIds: associations.ticketIds.toString().split(',') }), ...(associations.ticketIds && { ticketIds: associations.ticketIds.toString().split(',') }),
}; };
}; };
export async function validateCredentials(
this: ICredentialTestFunctions,
decryptedCredentials: ICredentialDataDecryptedObject,
): Promise<any> { // tslint:disable-line:no-any
const credentials = decryptedCredentials;
const {
apiKey,
appToken,
} = credentials as {
appToken: string,
apiKey: string,
};
const options: OptionsWithUri = {
method: 'GET',
headers: {},
uri: `https://api.hubapi.com/deals/v1/deal/paged`,
json: true,
};
if (apiKey) {
options.qs = { hapikey: apiKey };
} else {
options.headers = { Authorization: `Bearer ${appToken}` };
}
return await this.helpers.request(options);
}

View file

@ -3,12 +3,17 @@ import {
} from 'n8n-core'; } from 'n8n-core';
import { import {
ICredentialDataDecryptedObject,
ICredentialsDecrypted,
ICredentialTestFunctions,
IDataObject, IDataObject,
ILoadOptionsFunctions, ILoadOptionsFunctions,
INodeCredentialTestResult,
INodeExecutionData, INodeExecutionData,
INodePropertyOptions, INodePropertyOptions,
INodeType, INodeType,
INodeTypeDescription, INodeTypeDescription,
JsonObject,
NodeOperationError, NodeOperationError,
} from 'n8n-workflow'; } from 'n8n-workflow';
@ -71,6 +76,9 @@ import {
snakeCase, snakeCase,
} from 'change-case'; } from 'change-case';
import {
validateCredentials
} from './GenericFunctions';
export class Hubspot implements INodeType { export class Hubspot implements INodeType {
description: INodeTypeDescription = { description: INodeTypeDescription = {
displayName: 'HubSpot', displayName: 'HubSpot',
@ -89,6 +97,7 @@ export class Hubspot implements INodeType {
{ {
name: 'hubspotApi', name: 'hubspotApi',
required: true, required: true,
testedBy: 'hubspotApiTest',
displayOptions: { displayOptions: {
show: { show: {
authentication: [ authentication: [
@ -100,6 +109,7 @@ export class Hubspot implements INodeType {
{ {
name: 'hubspotAppToken', name: 'hubspotAppToken',
required: true, required: true,
testedBy: 'hubspotApiTest',
displayOptions: { displayOptions: {
show: { show: {
authentication: [ authentication: [
@ -131,7 +141,7 @@ export class Hubspot implements INodeType {
value: 'apiKey', value: 'apiKey',
}, },
{ {
name: 'App Token', name: 'APP Token',
value: 'appToken', value: 'appToken',
}, },
{ {
@ -204,6 +214,26 @@ export class Hubspot implements INodeType {
}; };
methods = { methods = {
credentialTest: {
async hubspotApiTest(this: ICredentialTestFunctions, credential: ICredentialsDecrypted): Promise<INodeCredentialTestResult> {
try {
await validateCredentials.call(this, credential.data as ICredentialDataDecryptedObject);
} catch (error) {
const err = error as JsonObject;
if (err.statusCode === 401) {
return {
status: 'Error',
message: `Invalid credentials`,
};
}
}
return {
status: 'OK',
message: 'Authentication successful',
};
},
},
loadOptions: { loadOptions: {
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* CONTACT */ /* CONTACT */
@ -938,7 +968,7 @@ export class Hubspot implements INodeType {
} }
} catch (error) { } catch (error) {
if (this.continueOnFail()) { if (this.continueOnFail()) {
returnData.push({ error: error.message }); returnData.push({ error: (error as JsonObject).message });
} else { } else {
throw error; throw error;
} }
@ -2526,7 +2556,7 @@ export class Hubspot implements INodeType {
} }
} catch (error) { } catch (error) {
if (this.continueOnFail()) { if (this.continueOnFail()) {
returnData.push({ error: error.message }); returnData.push({ error: (error as JsonObject).message });
continue; continue;
} }
throw error; throw error;