add authentication support
This commit is contained in:
79
dist/auth/JLINCAuthDecision.js
vendored
Normal file
79
dist/auth/JLINCAuthDecision.js
vendored
Normal file
@@ -0,0 +1,79 @@
|
||||
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,
|
||||
};
|
||||
Reference in New Issue
Block a user