import { HuntoClient } from "./sdk"; import axios, { AxiosRequestConfig } from "axios"; const AUTH_HEADER = "x-tik-session"; export class Hunto { client: HuntoClient | null; private baseUrl: string | null; private token: string | null; // Monitor Services monitorActivity: any; monitorAdvisory: any; monitorAssets: any; monitorControl: any; monitorDetection: any; monitorDiscovery: any; monitorClassification: any; monitorIncident: any; monitorIntegration: any; monitorIntel: any; monitorInternal: any; monitorNotificationModel: any; monitorReportIncident: any; monitorReports: any; monitorScamx: any; monitorSchedule: any; monitorScheduleReport: any; monitorScore: any; monitorStats: any; monitorTags: any; monitorTasks: any; monitorTemplates: any; monitorViews: any; // Sys Services sysAuth: any; sysManage: any; sysPage: any; // Groups Services groupsUser: any; constructor( config: any = { env: "sandbox", }, ) { this.baseUrl = config.baseUrl || { sandbox: "https://hunto-eu-dev.osm.run/api", "prod-in": "https://in.hunto.ai/api", "prod-eu": "https://eu.hunto.ai/api", }[config.env]; this.token = null; this.client = null; } async connect(): Promise { let options: any = { BASE: `${this.baseUrl}`, }; if (this.token) options.HEADERS = { [AUTH_HEADER as string]: this.token, } as { string: string }; this.client = new HuntoClient(options); // Export services this.monitorActivity = this.client.monitorActivity; this.monitorAdvisory = this.client.monitorAdvisory; this.monitorAssets = this.client.monitorAssets; this.monitorControl = this.client.monitorControl; this.monitorDetection = this.client.monitorDetection; this.monitorDiscovery = this.client.monitorDiscovery; this.monitorClassification = this.client.monitorClassification; this.monitorIncident = this.client.monitorIncident; this.monitorIntegration = this.client.monitorIntegration; this.monitorIntel = this.client.monitorIntel; this.monitorInternal = this.client.monitorInternal; this.monitorNotificationModel = this.client.monitorNotificationModel; this.monitorReportIncident = this.client.monitorReportIncident; this.monitorReports = this.client.monitorReports; this.monitorScamx = this.client.monitorScamx; this.monitorSchedule = this.client.monitorSchedule; this.monitorScheduleReport = this.client.monitorScheduleReport; this.monitorScore = this.client.monitorScore; this.monitorStats = this.client.monitorStats; this.monitorTags = this.client.monitorTags; this.monitorTasks = this.client.monitorTasks; this.monitorTemplates = this.client.monitorTemplates; this.monitorViews = this.client.monitorViews; this.sysAuth = this.client.sysAuth; this.sysManage = this.client.sysManage; this.sysPage = this.client.sysPage; this.groupsUser = this.client.groupsUser; return this; } async clear() { this.token = null; } async authenticate(apiKey: { secretId: string; clientId: string }) { if (this.token) throw new Error(`token is already defined, clear the session.`); const response = await axios({ method: "POST", url: `${this.baseUrl}/sys/auth/validate`, headers: { "Content-Type": "application/json" }, data: { using: "api", ...apiKey }, }); const authentication = response.data; this.token = authentication?.session?.context?.sessionID; if (!this.token) throw new Error( "Authentication failed: " + JSON.stringify(authentication?.error || authentication), ); await this.connect(); } async getProfile() { let response = await this.sysAuth.basicProfile(); return response.data; } async fetch( method: "GET" | "POST" | "PUT" | "DELETE", path: string, data?: any, ): Promise { if (!this.baseUrl || !this.token) { throw new Error( "Client not authenticated. Call .connect() and .authenticate() first.", ); } const url = `${this.baseUrl}${path}`; const config: AxiosRequestConfig = { method: method, url: url, headers: { "Content-Type": "application/json", [AUTH_HEADER]: this.token, }, }; if (method === "GET" && data) { // Use params for GET requests config.params = data; } else if (data) { // Use data for other request types config.data = data; } try { const response = await axios(config); return response.data; } catch (error: any) { if (error.response) { // The request was made and the server responded with a status code // that falls out of the range of 2xx throw new Error( `HTTP Error: ${error.response.status} - ${JSON.stringify(error.response.data)}`, ); } else if (error.request) { // The request was made but no response was received throw new Error(`Network Error: No response received`); } else { // Something happened in setting up the request that triggered an Error throw new Error(`Request Error: ${error.message}`); } } } }