OAuth2 support

This commit is contained in:
Rupenieks 2020-06-08 14:40:23 +02:00
parent c383853423
commit 3f9bdd2e41
5 changed files with 143 additions and 7 deletions

View file

@ -0,0 +1,59 @@
import {
ICredentialType,
NodePropertyTypes,
} from 'n8n-workflow';
export class HubspotOAuth2Api implements ICredentialType {
name = 'hubspotOAuth2Api';
extends = [
'oAuth2Api',
];
displayName = 'Hubspot OAuth2 API';
properties = [
{
displayName: 'Authorization URL',
name: 'authUrl',
type: 'hidden' as NodePropertyTypes,
default: 'https://app.hubspot.com/oauth/authorize',
required: true,
},
{
displayName: 'Access Token URL',
name: 'accessTokenUrl',
type: 'hidden' as NodePropertyTypes,
default: 'https://api.hubapi.com/oauth/v1/token',
required: true,
},
{
displayName: 'Scope',
name: 'scope',
type: 'string' as NodePropertyTypes,
default: '',
},
{
displayName: 'Auth URI Query Parameters',
name: 'authQueryParameters',
type: 'hidden' as NodePropertyTypes,
default: 'grant_type=authorization_code',
},
{
displayName: 'Authentication',
name: 'authentication',
type: 'options' as NodePropertyTypes,
options: [
{
name: 'Body',
value: 'body',
description: 'Send credentials in body',
},
{
name: 'Header',
value: 'header',
description: 'Send credentials as Basic Auth header',
},
],
default: 'header',
description: 'Resource to consume.',
},
];
}

View file

@ -14,12 +14,8 @@ import {
} from 'n8n-workflow';
export async function hubspotApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, method: string, endpoint: string, body: any = {}, query: IDataObject = {}, uri?: string): Promise<any> { // tslint:disable-line:no-any
const authenticationMethod = this.getNodeParameter('authentication', 0);
const node = this.getNode();
const credentialName = Object.keys(node.credentials!)[0];
const credentials = this.getCredentials(credentialName);
query!.hapikey = credentials!.apiKey as string;
const options: OptionsWithUri = {
method,
qs: query,
@ -28,8 +24,18 @@ export async function hubspotApiRequest(this: IHookFunctions | IExecuteFunctions
json: true,
useQuerystring: true,
};
try {
return await this.helpers.request!(options);
if (authenticationMethod === 'accessToken') {
const credentials = this.getCredentials('hubspotApi');
options.qs.hapikey = credentials!.apiKey as string;
return await this.helpers.request!(options);
} else {
// @ts-ignore
return await this.helpers.requestOAuth2!.call(this, 'hubspotOAuth2Api', options, 'Bearer');
}
} catch (error) {
if (error.response && error.response.body && error.response.body.errors) {
// Try to return the error prettier

View file

@ -73,9 +73,44 @@ export class Hubspot implements INodeType {
{
name: 'hubspotApi',
required: true,
}
displayOptions: {
show: {
authentication: [
'accessToken',
],
},
},
},
{
name: 'hubspotOAuth2Api',
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 method of authentication.',
},
{
displayName: 'Resource',
name: 'resource',

View file

@ -37,6 +37,24 @@ export class HubspotTrigger implements INodeType {
{
name: 'hubspotDeveloperApi',
required: true,
displayOptions: {
show: {
authentication: [
'developerApi',
],
},
},
},
{
name: 'hubspotOAuth2Api',
required: true,
displayOptions: {
show: {
authentication: [
'oAuth2',
],
},
},
},
],
webhooks: [
@ -54,6 +72,23 @@ export class HubspotTrigger implements INodeType {
},
],
properties: [
{
displayName: 'Authentication',
name: 'authentication',
type: 'options',
options: [
{
name: 'Developer API',
value: 'developerApi',
},
{
name: 'OAuth2',
value: 'oAuth2',
},
],
default: 'developerApi',
description: 'The method of authentication.',
},
{
displayName: 'App ID',
name: 'appId',

View file

@ -68,6 +68,7 @@
"dist/credentials/HttpHeaderAuth.credentials.js",
"dist/credentials/HubspotApi.credentials.js",
"dist/credentials/HubspotDeveloperApi.credentials.js",
"dist/credentials/HubspotOAuth2Api.credentials.js",
"dist/credentials/HunterApi.credentials.js",
"dist/credentials/Imap.credentials.js",
"dist/credentials/IntercomApi.credentials.js",