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 ¯\_(ツ)_/¯

538d66191e9558b4424d07fbf66aac40185f1c98

Spappz <34202141+Spappz@users.noreply.github.com>

Signed
2 files changed, +20 -1Showing whitespace changes
default/public/error/unauthorized.html+17 -0
@@ -0,0 +1,17 @@
1<!DOCTYPE html>
2<html>
3
4<head>
5 <title>Unauthorized</title>
6</head>
7
8<body>
9 <h1>Unauthorized</h1>
10 <p>
11 If you are the system administrator, you can configure the
12 <code>basicAuthUser</code> credentials by editing
13 <code>config.yaml</code> in the root directory of your installation.
14 </p>
15</body>
16
17</html>
src/middleware/basicAuth.js+3 -1
@@ -6,13 +6,15 @@ import { Buffer } from 'node:buffer';
6import storage from 'node-persist';6import storage from 'node-persist';
7import { getAllUserHandles, toKey, getPasswordHash } from '../users.js';7import { getAllUserHandles, toKey, getPasswordHash } from '../users.js';
8import { getConfig, getConfigValue } from '../util.js';8import { getConfig, getConfigValue } from '../util.js';
9import { readFileSync } from 'node:fs';
910
10const PER_USER_BASIC_AUTH = getConfigValue('perUserBasicAuth', false);11const PER_USER_BASIC_AUTH = getConfigValue('perUserBasicAuth', false);
11const ENABLE_ACCOUNTS = getConfigValue('enableUserAccounts', false);12const ENABLE_ACCOUNTS = getConfigValue('enableUserAccounts', false);
1213
14const unauthorizedWebpage = readFileSync('./public/error/unauthorized.html', { encoding: 'utf-8' });
13const unauthorizedResponse = (res) => {15const unauthorizedResponse = (res) => {
14 res.set('WWW-Authenticate', 'Basic realm="SillyTavern", charset="UTF-8"');16 res.set('WWW-Authenticate', 'Basic realm="SillyTavern", charset="UTF-8"');
15 return res.status(401).send('Authentication required');17 return res.status(401).send(unauthorizedWebpage);
16};18};
1719
18const basicAuthMiddleware = async function (request, response, callback) {20const basicAuthMiddleware = async function (request, response, callback) {