Files
jlinc-langchain-js/dist/auth/JLINCAuthDecision.js
2025-11-24 14:21:49 +00:00

80 lines
1.8 KiB
JavaScript

const { Tool } = require("@langchain/core/tools");
const axios = require("axios");
/**
* @typedef {import('./common').JLINCConfig} JLINCConfig
*/
class JLINCAuthDecision extends Tool {
/**
* @param {JLINCConfig} fields
*/
constructor(config) {
super({
name: "jlinc_auth",
description: "Authorization via AuthZEN format.",
});
Object.defineProperty(this, "name", {
enumerable: true,
configurable: true,
writable: true,
value: "jlinc-auth"
});
Object.defineProperty(this, "description", {
enumerable: true,
configurable: true,
writable: true,
value: "Authorization via AuthZEN format."
});
/** @type {JLINCConfig} */
this.config = config;
/** @type {Boolean} */
this.authenticated = false;
}
/**
* @param {any} payload
* @returns {Promise<any>}
*/
async postToApi(auth) {
const response = await axios.post(
`${this.config.dataStoreApiUrl}/api/v1/auth`,
auth,
{
headers: {
'Authorization': `Bearer ${this.config.dataStoreApiKey}`,
}
}
);
return response.data;
}
/**
* @param {auth} auth
* @returns {Promise<boolean>} - authenticated
*/
async evaluate(auth) {
this.authenticated = false;
try {
this.authenticated = (await this.postToApi(auth)).decision;
} catch (e) {
console.log(`[JLINCAuthTool] Error connecting to API, flagging as unauthorized`);
if (this.config.debug) console.error(e)
}
if (this.config.debug)
console.log(`[JLINCAuth] Got authorization of: ${this.authenticated}`);
return this.authenticated;
}
/**
* @returns {boolean} - authenticated
*/
getAuth() {
return this.authenticated;
}
}
module.exports = {
JLINCAuthDecision,
};