anything-llm/server/utils/http/index.js

73 lines
1.7 KiB
JavaScript
Raw Normal View History

process.env.NODE_ENV === "development"
? require("dotenv").config({ path: `.env.${process.env.NODE_ENV}` })
: require("dotenv").config();
const JWT = require("jsonwebtoken");
const { User } = require("../../models/user");
2023-06-04 04:28:07 +02:00
function reqBody(request) {
2023-06-08 06:31:35 +02:00
return typeof request.body === "string"
2023-06-04 04:28:07 +02:00
? JSON.parse(request.body)
: request.body;
}
function queryParams(request) {
return request.query;
}
function makeJWT(info = {}, expiry = "30d") {
if (!process.env.JWT_SECRET)
throw new Error("Cannot create JWT as JWT_SECRET is unset.");
return JWT.sign(info, process.env.JWT_SECRET, { expiresIn: expiry });
}
// Note: Only valid for finding users in multi-user mode
// as single-user mode with password is not a "user"
async function userFromSession(request, response = null) {
if (!!response && !!response.locals?.user) {
return response.locals.user;
}
const auth = request.header("Authorization");
const token = auth ? auth.split(" ")[1] : null;
if (!token) {
return null;
}
const valid = decodeJWT(token);
if (!valid || !valid.id) {
return null;
}
Replace custom sqlite dbms with prisma (#239) * WIP converted all sqlite models into prisma calls * modify db setup and fix ApiKey model calls in admin.js * renaming function params to be consistent * converted adminEndpoints to utilize prisma orm * converted chatEndpoints to utilize prisma orm * converted inviteEndpoints to utilize prisma orm * converted systemEndpoints to utilize prisma orm * converted workspaceEndpoints to utilize prisma orm * converting sql queries to prisma calls * fixed default param bug for orderBy and limit * fixed typo for workspace chats * fixed order of deletion to account for sql relations * fix invite CRUD and workspace management CRUD * fixed CRUD for api keys * created prisma setup scripts/docs for understanding how to use prisma * prisma dependency change * removing unneeded console.logs * removing unneeded sql escape function * linting and creating migration script * migration from depreciated sqlite script update * removing unneeded migrations in prisma folder * create backup of old sqlite db and use transactions to ensure all operations complete successfully * adding migrations to gitignore * updated PRISMA.md docs for info on how to use sqlite migration script * comment changes * adding back migrations folder to repo * Reviewing SQL and prisma integraiton on fresh repo * update inline key replacement * ensure migration script executes and maps foreign_keys regardless of db ordering * run migration endpoint * support new prisma backend * bump version * change migration call --------- Co-authored-by: timothycarambat <rambat1010@gmail.com>
2023-09-28 23:00:03 +02:00
const user = await User.get({ id: valid.id });
return user;
}
function decodeJWT(jwtToken) {
try {
return JWT.verify(jwtToken, process.env.JWT_SECRET);
} catch {}
return { p: null, id: null, username: null };
}
function multiUserMode(response) {
return response?.locals?.multiUserMode;
}
function parseAuthHeader(headerValue = null, apiKey = null) {
if (headerValue === null || apiKey === null) return {};
if (headerValue === "Authorization")
return { Authorization: `Bearer ${apiKey}` };
return { [headerValue]: apiKey };
}
2023-06-04 04:28:07 +02:00
module.exports = {
reqBody,
multiUserMode,
2023-06-04 04:28:07 +02:00
queryParams,
makeJWT,
decodeJWT,
userFromSession,
parseAuthHeader,
2023-06-04 04:28:07 +02:00
};