n8n/packages/nodes-base/credentials/ZscalerZiaApi.credentials.ts
Iván Ovejero e77fd5d286
refactor: Switch plain errors in nodes-base to ApplicationError (no-changelog) (#7914)
Ensure all errors in `nodes-base` are `ApplicationError` or children of
it and contain no variables in the message, to continue normalizing all
the backend errors we report to Sentry. Also, skip reporting to Sentry
errors from user input and from external APIs. In future we should
refine `ApplicationError` to more specific errors.

Follow-up to: [#7877](https://github.com/n8n-io/n8n/pull/7877)

- [x] Test workflows:
https://github.com/n8n-io/n8n/actions/runs/7084627970
- [x] e2e: https://github.com/n8n-io/n8n/actions/runs/7084936861

---------

Co-authored-by: Michael Kret <michael.k@radency.com>
2023-12-05 11:17:08 +01:00

147 lines
3.1 KiB
TypeScript

import { ApplicationError } from 'n8n-workflow';
import type {
IAuthenticateGeneric,
ICredentialDataDecryptedObject,
ICredentialTestRequest,
ICredentialType,
IHttpRequestHelper,
INodeProperties,
} from 'n8n-workflow';
export class ZscalerZiaApi implements ICredentialType {
name = 'zscalerZiaApi';
displayName = 'Zscaler ZIA API';
documentationUrl = 'zscalerzia';
icon = 'file:icons/Zscaler.svg';
httpRequestNode = {
name: 'Zscaler ZIA',
docsUrl: 'https://help.zscaler.com/zia/getting-started-zia-api',
apiBaseUrl: '',
};
properties: INodeProperties[] = [
{
displayName: 'Cookie',
name: 'cookie',
type: 'hidden',
typeOptions: {
expirable: true,
},
default: '',
},
{
displayName: 'Base URL',
name: 'baseUrl',
type: 'string',
default: '',
placeholder: 'e.g. zsapi.zscalerthree.net',
required: true,
},
{
displayName: 'Username',
name: 'username',
type: 'string',
default: '',
required: true,
},
{
displayName: 'Password',
name: 'password',
type: 'string',
typeOptions: { password: true },
default: '',
required: true,
},
{
displayName: 'Api Key',
name: 'apiKey',
type: 'string',
typeOptions: {
password: true,
},
default: '',
required: true,
},
];
async preAuthentication(this: IHttpRequestHelper, credentials: ICredentialDataDecryptedObject) {
const { baseUrl, username, password, apiKey } = credentials;
const url = (baseUrl as string).endsWith('/')
? (baseUrl as string).slice(0, -1)
: (baseUrl as string);
const now = Date.now().toString();
const obfuscate = (key: string, timestamp: string) => {
const high = timestamp.substring(timestamp.length - 6);
let low = (parseInt(high) >> 1).toString();
let obfuscatedApiKey = '';
while (low.length < 6) {
low = '0' + low;
}
for (let i = 0; i < high.length; i++) {
obfuscatedApiKey += key.charAt(parseInt(high.charAt(i)));
}
for (let j = 0; j < low.length; j++) {
obfuscatedApiKey += key.charAt(parseInt(low.charAt(j)) + 2);
}
return obfuscatedApiKey;
};
const response = await this.helpers.httpRequest({
method: 'POST',
baseURL: `https://${url}`,
url: '/api/v1/authenticatedSession',
headers: {
'Content-Type': 'application/json',
'Cache-Control': 'no-cache',
},
body: {
apiKey: obfuscate(apiKey as string, now),
username,
password,
timestamp: now,
},
returnFullResponse: true,
});
const headers = response.headers;
const cookie = (headers['set-cookie'] as string[])
?.find((entrt) => entrt.includes('JSESSIONID'))
?.split(';')
?.find((entry) => entry.includes('JSESSIONID'));
if (!cookie) {
throw new ApplicationError('No cookie returned. Please check your credentials.', {
level: 'warning',
});
}
return { cookie };
}
authenticate: IAuthenticateGeneric = {
type: 'generic',
properties: {
headers: {
Cookie: '={{$credentials.cookie}}',
},
},
};
test: ICredentialTestRequest = {
request: {
url: '=https://{{$credentials.baseUrl}}/api/v1/authSettings/exemptedUrls',
},
};
}