UNPKG

auth0-api-client

Version:

A Node.js module for making authenticated API calls using Auth0 Machine-to-Machine JWT tokens

157 lines 5.7 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.WebDataExporter = exports.Auth0Event = exports.EventType = void 0; const auth0_1 = require("auth0"); const axios_1 = __importDefault(require("axios")); const crypto_1 = require("crypto"); var EventType; (function (EventType) { EventType["LOGIN"] = "login"; EventType["LOGOUT"] = "logout"; EventType["SIGNUP"] = "signup"; EventType["PASSWORD_CHANGE"] = "password_change"; EventType["PROFILE_UPDATE"] = "profile_update"; EventType["API_ACCESS"] = "api_access"; })(EventType || (exports.EventType = EventType = {})); class Auth0Event { constructor(message, timestamp, eventType) { this.message = message; this.timestamp = timestamp; this.eventType = eventType; } // This method is automatically called by JSON.stringify() // Converts the Date to ISO string format like "2025-08-12T14:46:07.69767153Z" toJSON() { return { message: this.message, timestamp: this.timestamp.toISOString(), eventType: this.eventType }; } } exports.Auth0Event = Auth0Event; class WebDataExporter { constructor(config) { this.accessToken = null; this.tokenExpiry = null; this.validateConfig(config); this.auth0Config = { domain: config.auth0Domain, clientId: config.auth0ClientId, clientSecret: config.auth0ClientSecret, audience: config.auth0Audience }; this.apiBaseUrl = config.apiBaseUrl; this.managementClient = new auth0_1.ManagementClient({ domain: this.auth0Config.domain, clientId: this.auth0Config.clientId, clientSecret: this.auth0Config.clientSecret, audience: this.auth0Config.audience }); } validateConfig(config) { const requiredFields = [ 'auth0Domain', 'auth0ClientId', 'auth0ClientSecret', 'auth0Audience', 'apiBaseUrl' ]; for (const field of requiredFields) { if (!config[field]) { throw new Error(`Missing required configuration: ${field}`); } } } async getAccessToken() { try { if (this.accessToken && this.tokenExpiry && Date.now() < this.tokenExpiry) { return this.accessToken; } const response = await axios_1.default.post(`https://${this.auth0Config.domain}/oauth/token`, { client_id: this.auth0Config.clientId, client_secret: this.auth0Config.clientSecret, audience: this.auth0Config.audience, grant_type: 'client_credentials' }, { headers: { 'Content-Type': 'application/json' } }); this.accessToken = response.data.access_token; this.tokenExpiry = Date.now() + ((response.data.expires_in - 300) * 1000); return this.accessToken; } catch (error) { throw new Error(`Failed to obtain Auth0 access token: ${error.message}`); } } async postData(endpoint, data, options = {}) { try { // Check if access token is provided in options const token = options.headers?.Authorization?.startsWith('Bearer ') ? options.headers.Authorization.replace('Bearer ', '') : await this.getAccessToken(); // Check if X-Flow-ID is provided in options, otherwise generate UUID const flowId = options.headers?.['X-Flow-ID'] || (0, crypto_1.randomUUID)(); const url = `${this.apiBaseUrl}${endpoint.startsWith('/') ? endpoint : '/' + endpoint}`; const headers = { ...options.headers, 'Authorization': `Bearer ${token}`, 'Content-Type': 'application/json', 'X-Flow-ID': flowId, }; const response = await axios_1.default.post(url, data, { headers, timeout: options.timeout || 30000, ...options.axiosConfig }); return { success: true, data: response.data, status: response.status, headers: response.headers }; } catch (error) { if (error.response) { return { success: false, error: { message: error.message, status: error.response.status, data: error.response.data } }; } else if (error.request) { return { success: false, error: { message: 'No response received from API', type: 'network_error' } }; } else { return { success: false, error: { message: error.message, type: 'unknown_error' } }; } } } clearToken() { this.accessToken = null; this.tokenExpiry = null; } } exports.WebDataExporter = WebDataExporter; exports.default = WebDataExporter; //# sourceMappingURL=index.js.map