add 401 error page for basicAuth mode

Most modern browsers don't actually show users 401 responses, but it doesn't hurt to have it in there anyway ¯\_(ツ)_/¯
This commit is contained in:
Spappz
2025-01-25 03:40:47 +00:00
parent 928487985d
commit 538d66191e
2 changed files with 20 additions and 1 deletions

View File

@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html>
<head>
<title>Unauthorized</title>
</head>
<body>
<h1>Unauthorized</h1>
<p>
If you are the system administrator, you can configure the
<code>basicAuthUser</code> credentials by editing
<code>config.yaml</code> in the root directory of your installation.
</p>
</body>
</html>

View File

@@ -6,13 +6,15 @@ import { Buffer } from 'node:buffer';
import storage from 'node-persist'; import storage from 'node-persist';
import { getAllUserHandles, toKey, getPasswordHash } from '../users.js'; import { getAllUserHandles, toKey, getPasswordHash } from '../users.js';
import { getConfig, getConfigValue } from '../util.js'; import { getConfig, getConfigValue } from '../util.js';
import { readFileSync } from 'node:fs';
const PER_USER_BASIC_AUTH = getConfigValue('perUserBasicAuth', false); const PER_USER_BASIC_AUTH = getConfigValue('perUserBasicAuth', false);
const ENABLE_ACCOUNTS = getConfigValue('enableUserAccounts', false); const ENABLE_ACCOUNTS = getConfigValue('enableUserAccounts', false);
const unauthorizedWebpage = readFileSync('./public/error/unauthorized.html', { encoding: 'utf-8' });
const unauthorizedResponse = (res) => { const unauthorizedResponse = (res) => {
res.set('WWW-Authenticate', 'Basic realm="SillyTavern", charset="UTF-8"'); res.set('WWW-Authenticate', 'Basic realm="SillyTavern", charset="UTF-8"');
return res.status(401).send('Authentication required'); return res.status(401).send(unauthorizedWebpage);
}; };
const basicAuthMiddleware = async function (request, response, callback) { const basicAuthMiddleware = async function (request, response, callback) {