UNPKG

n8n-nodes-arubaclearpass

Version:

n8n community node for Aruba ClearPass API integration with comprehensive identity, policy, and enforcement profile management

82 lines (81 loc) 3.72 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.apiRequest = apiRequest; exports.apiRequestAllItems = apiRequestAllItems; // helpers/apiRequest.ts const n8n_workflow_1 = require("n8n-workflow"); const tokenManager_1 = require("./tokenManager"); /** * Make an API request to Aruba ClearPass */ async function apiRequest(method, endpoint, body = {}, query = {}) { var _a, _b, _c, _d, _e, _f, _g; const credentials = (await this.getCredentials('clearPassApi')); const baseUrl = credentials.url; const allowUnauthorizedCerts = credentials.allowUnauthorizedCerts; const options = { method, uri: `${baseUrl}/api${endpoint}`, headers: { 'Content-Type': 'application/json', }, qs: query, body, json: true, rejectUnauthorized: !allowUnauthorizedCerts, }; if (Object.keys(body).length === 0) { delete options.body; } try { // If using Client ID/Secret auth, get token directly. Otherwise, use OAuth2 authentication if (credentials.authType === 'clientCredentials') { const token = await tokenManager_1.getToken.call(this, credentials); options.headers = { ...options.headers, Authorization: `Bearer ${token}`, }; return await this.helpers.request(options); } else { // OAuth2 authentication (processed by n8n) return await this.helpers.requestWithAuthentication.call(this, 'clearPassApi', options); } } catch (error) { // Log the full error so the actual ClearPass API response body is visible on the console const statusCode = (_c = (_a = error === null || error === void 0 ? void 0 : error.statusCode) !== null && _a !== void 0 ? _a : (_b = error === null || error === void 0 ? void 0 : error.response) === null || _b === void 0 ? void 0 : _b.statusCode) !== null && _c !== void 0 ? _c : 'unknown'; const responseBody = (_g = (_f = (_e = (_d = error === null || error === void 0 ? void 0 : error.response) === null || _d === void 0 ? void 0 : _d.body) !== null && _e !== void 0 ? _e : error === null || error === void 0 ? void 0 : error.error) !== null && _f !== void 0 ? _f : error === null || error === void 0 ? void 0 : error.cause) !== null && _g !== void 0 ? _g : null; console.log(`[ClearPass] HTTP ${statusCode} error from ${options.method} ${options.uri}`); if (responseBody) { console.log(`[ClearPass] Response body: ${typeof responseBody === 'string' ? responseBody : JSON.stringify(responseBody)}`); } throw new n8n_workflow_1.NodeApiError(this.getNode(), error); } } /** * Make paginated API requests to Aruba ClearPass and return all results */ async function apiRequestAllItems(method, endpoint, body = {}, query = {}) { const returnData = []; // Set initial parameters for pagination let responseData; query.limit = query.limit || 100; query.offset = 0; do { responseData = await apiRequest.call(this, method, endpoint, body, query); if (responseData._embedded && responseData._embedded.items) { returnData.push.apply(returnData, responseData._embedded.items); } else if (Array.isArray(responseData)) { returnData.push.apply(returnData, responseData); } else { returnData.push(responseData); } query.offset = query.offset + query.limit; } while (responseData._embedded && responseData._embedded.items && responseData._embedded.items.length === query.limit); return returnData; }