anything-llm/server/utils/middleware/validApiKey.js
Timothy Carambat defe6054b3
Full developer api (#221)
* Autodocument Swagger API with JSDocs on /v1/ endpoints for API access
implement single-player API keys
WIP Admin API Keys

* Create new api keys as both single and multi-user

* Add boot and telem

* Complete Admin API

* Complete endpoints
dark mode swagger

* update docs

* undo debug

* update docs and readme
2023-08-23 19:15:07 -07:00

31 lines
733 B
JavaScript

const { ApiKey } = require("../../models/apiKeys");
const { SystemSettings } = require("../../models/systemSettings");
async function validApiKey(request, response, next) {
const multiUserMode = await SystemSettings.isMultiUserMode();
response.locals.multiUserMode = multiUserMode;
const auth = request.header("Authorization");
const bearerKey = auth ? auth.split(" ")[1] : null;
if (!bearerKey) {
response.status(403).json({
error: "No valid api key found.",
});
return;
}
const apiKey = await ApiKey.get(`secret = '${bearerKey}'`);
if (!apiKey) {
response.status(403).json({
error: "No valid api key found.",
});
return;
}
next();
}
module.exports = {
validApiKey,
};