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
5 changed files with 108 additions and 45 deletions

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);
}