Secrets: Add find endpoint

Requires the user to set allowKeysExposure to true before any calls
can work.

Signed-off-by: kingbri <bdashore3@proton.me>
This commit is contained in:
kingbri 2023-11-20 00:34:06 -05:00
parent e81c100e13
commit 6a511fdfcf
2 changed files with 41 additions and 0 deletions

View File

@ -116,6 +116,23 @@ export async function readSecretState() {
}
}
export async function findSecret(key) {
try {
const response = await fetch('/api/secrets/find', {
method: 'POST',
headers: getRequestHeaders(),
body: JSON.stringify({ key }),
});
if (response.ok) {
const data = await response.json();
return data.value
}
} catch {
console.error('Could not find secret value: ', key);
}
}
function authorizeOpenRouter() {
const openRouterUrl = `https://openrouter.ai/auth?callback_url=${encodeURIComponent(location.origin)}`;
location.href = openRouterUrl;

View File

@ -190,6 +190,30 @@ function registerEndpoints(app, jsonParser) {
return response.sendStatus(500);
}
});
app.post('/api/secrets/find', jsonParser, (request, response) => {
const allowKeysExposure = getConfigValue('allowKeysExposure', false);
if (!allowKeysExposure) {
console.error('Cannot fetch secrets unless allowKeysExposure in config.conf is set to true');
return response.sendStatus(403);
}
const key = request.body.key
try {
const secret = readSecret(key)
if (!secret) {
response.sendStatus(404);
}
return response.send({ value: secret });
} catch (error) {
console.error(error);
return response.sendStatus(500);
}
});
}
module.exports = {