feat(Calendly Trigger Node): Add OAuth Credentials Support (#10251)

This commit is contained in:
Oz Weiss 2024-07-31 15:26:24 +03:00 committed by GitHub
parent cf73e29b61
commit 326c983915
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 108 additions and 45 deletions

View file

@ -0,0 +1,52 @@
import type { ICredentialType, INodeProperties, Icon } from 'n8n-workflow';
export class CalendlyOAuth2Api implements ICredentialType {
name = 'calendlyOAuth2Api';
extends = ['oAuth2Api'];
displayName = 'Calendly OAuth2 API';
documentationUrl = 'calendly';
icon: Icon = 'file:icons/Calendly.svg';
properties: INodeProperties[] = [
{
displayName: 'Grant Type',
name: 'grantType',
type: 'hidden',
default: 'authorizationCode',
},
{
displayName: 'Authorization URL',
name: 'authUrl',
type: 'hidden',
default: 'https://auth.calendly.com/oauth/authorize',
},
{
displayName: 'Access Token URL',
name: 'accessTokenUrl',
type: 'hidden',
default: 'https://auth.calendly.com/oauth/token',
},
{
displayName: 'Authentication',
name: 'authentication',
type: 'hidden',
default: 'header',
},
{
displayName: 'Auth URI Query Parameters',
name: 'authQueryParameters',
type: 'hidden',
default: '',
},
{
displayName: 'Scope',
name: 'scope',
type: 'hidden',
default: '',
},
];
}

View file

@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 525.8 535.73"><defs><style>.cls-2{fill:#006bff}.cls-3{fill:#0ae9ef}</style></defs><g id="Layer_2" data-name="Layer 2"><g id="Logo_assets" data-name="Logo assets"><g id="Brand_mark" data-name="Brand mark"><path d="m443.74 337.62-27.16 47.05a139.52 139.52 0 0 1-120.82 69.75h-54.33a139.52 139.52 0 0 1-120.82-69.75l-27.16-47.05a139.52 139.52 0 0 1 0-139.51l27.16-47.05A139.53 139.53 0 0 1 241.43 81.3h54.33a139.53 139.53 0 0 1 120.82 69.76l27.16 47.05a139 139 0 0 1 8.55 17.55c0 .12.09.23.13.35a102.15 102.15 0 0 0 44.33-18.24c0-.14-.08-.28-.13-.43a237.8 237.8 0 0 0-33.29-67.58 240.7 240.7 0 0 0-52-53.48 239.3 239.3 0 0 0-312.68 360.8 239.43 239.43 0 0 0 398-98.69c.05-.15.09-.29.13-.43a102.15 102.15 0 0 0-44.33-18.24c0 .12-.09.23-.13.35a139 139 0 0 1-8.58 17.55" style="fill:none"/><path d="M360.4 347.4c-17 15.09-38.21 33.87-76.78 33.87h-23c-27.88 0-53.23-10.12-71.37-28.49-17.72-17.94-27.48-42.5-27.48-69.16v-31.51c0-26.66 9.76-51.22 27.48-69.16 18.14-18.37 43.49-28.49 71.37-28.49h23c38.57 0 59.76 18.78 76.78 33.87 17.65 15.65 32.9 29.16 73.52 29.16a116 116 0 0 0 18.5-1.48c0-.12-.08-.23-.13-.35a139 139 0 0 0-8.55-17.55l-27.16-47.05A139.53 139.53 0 0 0 295.76 81.3h-54.33a139.53 139.53 0 0 0-120.82 69.76l-27.16 47.05a139.52 139.52 0 0 0 0 139.51l27.16 47.05a139.52 139.52 0 0 0 120.82 69.75h54.33a139.52 139.52 0 0 0 120.82-69.75l27.16-47.05a139 139 0 0 0 8.55-17.55c0-.12.09-.23.13-.35a116 116 0 0 0-18.5-1.48c-40.62 0-55.87 13.51-73.52 29.16" class="cls-2"/><path d="M283.62 183h-23c-42.42 0-70.3 30.3-70.3 69.09v31.51c0 38.79 27.88 69.09 70.3 69.09h23c61.82 0 57-63 150.3-63a144 144 0 0 1 26.37 2.41 139.4 139.4 0 0 0 0-48.46 143.3 143.3 0 0 1-26.37 2.42c-93.33-.01-88.48-63.06-150.3-63.06" class="cls-2"/><path d="M513.91 315.13a130.2 130.2 0 0 0-53.62-23c0 .16-.05.32-.08.47a138.5 138.5 0 0 1-7.79 27.16A102.15 102.15 0 0 1 496.75 338c0 .14-.08.28-.13.43A237.8 237.8 0 0 1 463.33 406a240.7 240.7 0 0 1-52 53.48A239.3 239.3 0 0 1 98.65 98.65a239.43 239.43 0 0 1 398 98.69c.05.15.09.29.13.43A102.15 102.15 0 0 1 452.42 216a139.4 139.4 0 0 1 7.8 27.18c0 .15 0 .3.07.44a129.94 129.94 0 0 0 53.62-23c15.29-11.31 12.33-24.09 10-31.65C490.22 79.52 388.33 0 267.86 0 119.93 0 0 119.93 0 267.86s119.93 267.87 267.86 267.87c120.47 0 222.36-79.52 256-188.94 2.38-7.56 5.34-20.34-9.95-31.66" class="cls-2"/><path d="M452.42 216a116 116 0 0 1-18.5 1.48c-40.62 0-55.87-13.51-73.52-29.16-17-15.09-38.21-33.87-76.78-33.87h-23c-27.88 0-53.23 10.12-71.37 28.49-17.72 17.94-27.48 42.5-27.48 69.16v31.51c0 26.66 9.76 51.22 27.48 69.16 18.14 18.37 43.49 28.49 71.37 28.49h23c38.57 0 59.76-18.78 76.78-33.87 17.65-15.65 32.9-29.16 73.52-29.16a116 116 0 0 1 18.5 1.48 138.5 138.5 0 0 0 7.79-27.16c0-.15.06-.31.08-.47a144 144 0 0 0-26.37-2.41c-93.33 0-88.48 63-150.3 63h-23c-42.42 0-70.3-30.3-70.3-69.09v-31.47c0-38.79 27.88-69.09 70.3-69.09h23c61.82 0 57 63 150.3 63a143.3 143.3 0 0 0 26.37-2.42c0-.14 0-.29-.07-.44a139.4 139.4 0 0 0-7.8-27.16" class="cls-3"/><path d="M452.42 216a116 116 0 0 1-18.5 1.48c-40.62 0-55.87-13.51-73.52-29.16-17-15.09-38.21-33.87-76.78-33.87h-23c-27.88 0-53.23 10.12-71.37 28.49-17.72 17.94-27.48 42.5-27.48 69.16v31.51c0 26.66 9.76 51.22 27.48 69.16 18.14 18.37 43.49 28.49 71.37 28.49h23c38.57 0 59.76-18.78 76.78-33.87 17.65-15.65 32.9-29.16 73.52-29.16a116 116 0 0 1 18.5 1.48 138.5 138.5 0 0 0 7.79-27.16c0-.15.06-.31.08-.47a144 144 0 0 0-26.37-2.41c-93.33 0-88.48 63-150.3 63h-23c-42.42 0-70.3-30.3-70.3-69.09v-31.47c0-38.79 27.88-69.09 70.3-69.09h23c61.82 0 57 63 150.3 63a143.3 143.3 0 0 0 26.37-2.42c0-.14 0-.29-.07-.44a139.4 139.4 0 0 0-7.8-27.16" class="cls-3"/></g></g></g></svg>

After

Width:  |  Height:  |  Size: 3.6 KiB

View file

@ -26,6 +26,20 @@ export class CalendlyTrigger implements INodeType {
{
name: 'calendlyApi',
required: true,
displayOptions: {
show: {
authentication: ['apiKey'],
},
},
},
{
name: 'calendlyOAuth2Api',
required: true,
displayOptions: {
show: {
authentication: ['oAuth2'],
},
},
},
],
webhooks: [
@ -37,6 +51,23 @@ export class CalendlyTrigger implements INodeType {
},
],
properties: [
{
displayName: 'Authentication',
name: 'authentication',
type: 'options',
options: [
{
// eslint-disable-next-line n8n-nodes-base/node-param-display-name-miscased
name: 'OAuth2 (recommended)',
value: 'oAuth2',
},
{
name: 'API Key or Personal Access Token',
value: 'apiKey',
},
],
default: 'apiKey',
},
{
displayName: 'Scope',
name: 'scope',
@ -86,9 +117,8 @@ export class CalendlyTrigger implements INodeType {
const webhookUrl = this.getNodeWebhookUrl('default');
const webhookData = this.getWorkflowStaticData('node');
const events = this.getNodeParameter('events') as string;
const { apiKey } = (await this.getCredentials('calendlyApi')) as { apiKey: string };
const authenticationType = getAuthenticationType(apiKey);
const authenticationType = await getAuthenticationType.call(this);
// remove condition once API Keys are deprecated
if (authenticationType === 'apiKey') {
@ -149,9 +179,8 @@ export class CalendlyTrigger implements INodeType {
const webhookData = this.getWorkflowStaticData('node');
const webhookUrl = this.getNodeWebhookUrl('default');
const events = this.getNodeParameter('events') as string;
const { apiKey } = (await this.getCredentials('calendlyApi')) as { apiKey: string };
const authenticationType = getAuthenticationType(apiKey);
const authenticationType = await getAuthenticationType.call(this);
// remove condition once API Keys are deprecated
if (authenticationType === 'apiKey') {
@ -201,8 +230,7 @@ export class CalendlyTrigger implements INodeType {
},
async delete(this: IHookFunctions): Promise<boolean> {
const webhookData = this.getWorkflowStaticData('node');
const { apiKey } = (await this.getCredentials('calendlyApi')) as { apiKey: string };
const authenticationType = getAuthenticationType(apiKey);
const authenticationType = await getAuthenticationType.call(this);
// remove condition once API Keys are deprecated
if (authenticationType === 'apiKey') {

View file

@ -1,6 +1,4 @@
import type {
ICredentialDataDecryptedObject,
ICredentialTestFunctions,
IDataObject,
IExecuteFunctions,
ILoadOptionsFunctions,
@ -10,12 +8,24 @@ import type {
IRequestOptions,
} from 'n8n-workflow';
export function getAuthenticationType(data: string): 'accessToken' | 'apiKey' {
function getAuthenticationTypeFromApiKey(data: string): 'accessToken' | 'apiKey' {
// The access token is a JWT, so it will always include dots to separate
// header, payoload and signature.
return data.includes('.') ? 'accessToken' : 'apiKey';
}
export async function getAuthenticationType(
this: IExecuteFunctions | IWebhookFunctions | IHookFunctions | ILoadOptionsFunctions,
): Promise<'accessToken' | 'apiKey'> {
const authentication = this.getNodeParameter('authentication', 0) as string;
if (authentication === 'apiKey') {
const { apiKey } = (await this.getCredentials('calendlyApi')) as { apiKey: string };
return getAuthenticationTypeFromApiKey(apiKey);
} else {
return 'accessToken';
}
}
export async function calendlyApiRequest(
this: IExecuteFunctions | IWebhookFunctions | IHookFunctions | ILoadOptionsFunctions,
method: IHttpRequestMethods,
@ -26,9 +36,7 @@ export async function calendlyApiRequest(
uri?: string,
option: IDataObject = {},
): Promise<any> {
const { apiKey } = (await this.getCredentials('calendlyApi')) as { apiKey: string };
const authenticationType = getAuthenticationType(apiKey);
const authenticationType = await getAuthenticationType.call(this);
const headers: IDataObject = {
'Content-Type': 'application/json',
@ -57,37 +65,10 @@ export async function calendlyApiRequest(
delete options.qs;
}
options = Object.assign({}, options, option);
return await this.helpers.requestWithAuthentication.call(this, 'calendlyApi', options);
}
export async function validateCredentials(
this: ICredentialTestFunctions,
decryptedCredentials: ICredentialDataDecryptedObject,
): Promise<any> {
const credentials = decryptedCredentials;
const { apiKey } = credentials as {
apiKey: string;
};
const authenticationType = getAuthenticationType(apiKey);
const options: IRequestOptions = {
method: 'GET',
uri: '',
json: true,
};
if (authenticationType === 'accessToken') {
Object.assign(options, {
headers: { Authorization: `Bearer ${apiKey}` },
uri: 'https://api.calendly.com/users/me',
});
} else {
Object.assign(options, {
headers: { 'X-TOKEN': apiKey },
uri: 'https://calendly.com/api/v1/users/me',
});
}
return await this.helpers.request(options);
const credentialsType =
(this.getNodeParameter('authentication', 0) as string) === 'apiKey'
? 'calendlyApi'
: 'calendlyOAuth2Api';
return await this.helpers.requestWithAuthentication.call(this, credentialsType, options);
}

View file

@ -52,6 +52,7 @@
"dist/credentials/BubbleApi.credentials.js",
"dist/credentials/CalApi.credentials.js",
"dist/credentials/CalendlyApi.credentials.js",
"dist/credentials/CalendlyOAuth2Api.credentials.js",
"dist/credentials/CarbonBlackApi.credentials.js",
"dist/credentials/ChargebeeApi.credentials.js",
"dist/credentials/CircleCiApi.credentials.js",