Skip to content

Commit

Permalink
feat(Hubspot): Add support for Private App Token Authentication
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
3 people authored Mar 13, 2022
1 parent 2ff13a6 commit f73100a
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class HubspotAppToken implements ICredentialType {
documentationUrl = 'hubspot';
properties: INodeProperties[] = [
{
displayName: 'App Token',
displayName: 'APP Token',
name: 'appToken',
type: 'string',
default: '',
Expand Down
35 changes: 34 additions & 1 deletion packages/nodes-base/nodes/Hubspot/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import {
} from 'n8n-core';

import {
ICredentialDataDecryptedObject,
ICredentialTestFunctions,
IDataObject,
JsonObject,
NodeApiError,
} from 'n8n-workflow';

Expand Down Expand Up @@ -59,7 +62,7 @@ export async function hubspotApiRequest(this: IHookFunctions | IExecuteFunctions
return await this.helpers.requestOAuth2!.call(this, 'hubspotOAuth2Api', options, { tokenType: 'Bearer', includeCredentialsOnRefreshOnBody: true });
}
} catch (error) {
throw new NodeApiError(this.getNode(), error);
throw new NodeApiError(this.getNode(), error as JsonObject);
}
}

Expand Down Expand Up @@ -1969,3 +1972,33 @@ export const getAssociations = (associations: {
...(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);
}
36 changes: 33 additions & 3 deletions packages/nodes-base/nodes/Hubspot/Hubspot.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ import {
} from 'n8n-core';

import {
ICredentialDataDecryptedObject,
ICredentialsDecrypted,
ICredentialTestFunctions,
IDataObject,
ILoadOptionsFunctions,
INodeCredentialTestResult,
INodeExecutionData,
INodePropertyOptions,
INodeType,
INodeTypeDescription,
JsonObject,
NodeOperationError,
} from 'n8n-workflow';

Expand Down Expand Up @@ -71,6 +76,9 @@ import {
snakeCase,
} from 'change-case';

import {
validateCredentials
} from './GenericFunctions';
export class Hubspot implements INodeType {
description: INodeTypeDescription = {
displayName: 'HubSpot',
Expand All @@ -89,6 +97,7 @@ export class Hubspot implements INodeType {
{
name: 'hubspotApi',
required: true,
testedBy: 'hubspotApiTest',
displayOptions: {
show: {
authentication: [
Expand All @@ -100,6 +109,7 @@ export class Hubspot implements INodeType {
{
name: 'hubspotAppToken',
required: true,
testedBy: 'hubspotApiTest',
displayOptions: {
show: {
authentication: [
Expand Down Expand Up @@ -131,7 +141,7 @@ export class Hubspot implements INodeType {
value: 'apiKey',
},
{
name: 'App Token',
name: 'APP Token',
value: 'appToken',
},
{
Expand Down Expand Up @@ -204,6 +214,26 @@ export class Hubspot implements INodeType {
};

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: {
/* -------------------------------------------------------------------------- */
/* CONTACT */
Expand Down Expand Up @@ -938,7 +968,7 @@ export class Hubspot implements INodeType {
}
} catch (error) {
if (this.continueOnFail()) {
returnData.push({ error: error.message });
returnData.push({ error: (error as JsonObject).message });
} else {
throw error;
}
Expand Down Expand Up @@ -2526,7 +2556,7 @@ export class Hubspot implements INodeType {
}
} catch (error) {
if (this.continueOnFail()) {
returnData.push({ error: error.message });
returnData.push({ error: (error as JsonObject).message });
continue;
}
throw error;
Expand Down

0 comments on commit f73100a

Please sign in to comment.