| 6 | import storage from 'node-persist'; | 6 | import storage from 'node-persist'; |
| 7 | import { getAllUserHandles, toKey, getPasswordHash } from '../users.js'; | 7 | import { getAllUserHandles, toKey, getPasswordHash } from '../users.js'; |
| 8 | import { getConfig, getConfigValue } from '../util.js'; | 8 | import { getConfig, getConfigValue } from '../util.js'; |
| | 9 | import { readFileSync } from 'node:fs'; |
| 9 | | 10 | |
| 10 | const PER_USER_BASIC_AUTH = getConfigValue('perUserBasicAuth', false); | 11 | const PER_USER_BASIC_AUTH = getConfigValue('perUserBasicAuth', false); |
| 11 | const ENABLE_ACCOUNTS = getConfigValue('enableUserAccounts', false); | 12 | const ENABLE_ACCOUNTS = getConfigValue('enableUserAccounts', false); |
| 12 | | 13 | |
| | 14 | const unauthorizedWebpage = readFileSync('./public/error/unauthorized.html', { encoding: 'utf-8' }); |
| 13 | const unauthorizedResponse = (res) => { | 15 | const 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 | }; |
| 17 | | 19 | |
| 18 | const basicAuthMiddleware = async function (request, response, callback) { | 20 | const basicAuthMiddleware = async function (request, response, callback) { |