UNPKG

n8n-nodes-arubaclearpass

Version:

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

64 lines (63 loc) 2.39 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getToken = getToken; // helpers/tokenManager.ts const n8n_workflow_1 = require("n8n-workflow"); // Store tokens in memory with expiration const tokenCache = {}; /** * Get a bearer token using client credentials */ async function getToken(credentials) { const clientId = credentials.clientId; const clientSecret = credentials.clientSecret; const baseUrl = credentials.url; // Generate a cache key based on client credentials const cacheKey = `${baseUrl}:${clientId}:${clientSecret}`; // Check if we have a valid cached token const cachedToken = tokenCache[cacheKey]; if (cachedToken && cachedToken.expiresAt > Date.now()) { return cachedToken.token; } // If no valid token in cache, request a new one try { const options = { headers: { 'Content-Type': 'application/json', }, method: 'POST', body: { client_id: clientId, client_secret: clientSecret, grant_type: 'client_credentials', }, uri: `${baseUrl}/api/oauth`, json: true, }; const response = (await this.helpers.request(options)); // Cache the token with expiration (add some buffer to ensure we refresh before it expires) const expiresInMs = response.expires_in * 1000 - 60000; // Subtract 1 minute as buffer tokenCache[cacheKey] = { token: response.access_token, expiresAt: Date.now() + expiresInMs, }; return response.access_token; } catch (error) { // Fix error handling to use JsonObject without undefined properties const errorObject = {}; if (error instanceof Error) { errorObject.message = error.message; // Only add stack if it exists, and convert to string to ensure it's a JsonValue if (error.stack) { errorObject.stackTrace = error.stack; // Changed property name and ensured it's not undefined } } else { errorObject.message = 'Unknown error when obtaining token'; } throw new n8n_workflow_1.NodeApiError(this.getNode(), errorObject, { message: 'Failed to obtain access token', }); } }