Skip to content

Commit

Permalink
feat(Calendly Trigger Node): Add OAuth Credentials Support (#10251)
Browse files Browse the repository at this point in the history
  • Loading branch information
thewizarodofoz authored Jul 31, 2024
1 parent cf73e29 commit 326c983
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 44 deletions.
52 changes: 52 additions & 0 deletions packages/nodes-base/credentials/CalendlyOAuth2Api.credentials.ts
Original file line number Diff line number Diff line change
@@ -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: '',
},
];
}
1 change: 1 addition & 0 deletions packages/nodes-base/credentials/icons/Calendly.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 34 additions & 6 deletions packages/nodes-base/nodes/Calendly/CalendlyTrigger.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand All @@ -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',
Expand Down Expand Up @@ -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') {
Expand Down Expand Up @@ -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') {
Expand Down Expand Up @@ -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') {
Expand Down
57 changes: 19 additions & 38 deletions packages/nodes-base/nodes/Calendly/GenericFunctions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import type {
ICredentialDataDecryptedObject,
ICredentialTestFunctions,
IDataObject,
IExecuteFunctions,
ILoadOptionsFunctions,
Expand All @@ -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,
Expand All @@ -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',
Expand Down Expand Up @@ -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);
}
1 change: 1 addition & 0 deletions packages/nodes-base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 326c983

Please sign in to comment.