feat(auth): allow disabling built-in basic auth
This commit is contained in:
@@ -18,3 +18,5 @@ npm run dev
|
|||||||
docker build -t trade-frontend .
|
docker build -t trade-frontend .
|
||||||
docker run --rm -p 8081:8081 trade-frontend
|
docker run --rm -p 8081:8081 trade-frontend
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Jeśli auth jest realizowany przed aplikacją (np. Traefik `basicAuth`), ustaw `BASIC_AUTH_MODE=off`, żeby wyłączyć wbudowany basic auth w serwerze.
|
||||||
|
|||||||
@@ -15,6 +15,10 @@ const STATIC_DIR = process.env.STATIC_DIR || '/srv';
|
|||||||
const BASIC_AUTH_FILE = process.env.BASIC_AUTH_FILE || '/tokens/frontend.json';
|
const BASIC_AUTH_FILE = process.env.BASIC_AUTH_FILE || '/tokens/frontend.json';
|
||||||
const API_READ_TOKEN_FILE = process.env.API_READ_TOKEN_FILE || '/tokens/read.json';
|
const API_READ_TOKEN_FILE = process.env.API_READ_TOKEN_FILE || '/tokens/read.json';
|
||||||
const API_UPSTREAM = process.env.API_UPSTREAM || process.env.API_URL || 'http://api:8787';
|
const API_UPSTREAM = process.env.API_UPSTREAM || process.env.API_URL || 'http://api:8787';
|
||||||
|
const BASIC_AUTH_MODE = String(process.env.BASIC_AUTH_MODE || 'on')
|
||||||
|
.trim()
|
||||||
|
.toLowerCase();
|
||||||
|
const BASIC_AUTH_ENABLED = !['off', 'false', '0', 'disabled', 'none'].includes(BASIC_AUTH_MODE);
|
||||||
|
|
||||||
function readJson(filePath) {
|
function readJson(filePath) {
|
||||||
const raw = fs.readFileSync(filePath, 'utf8');
|
const raw = fs.readFileSync(filePath, 'utf8');
|
||||||
@@ -226,17 +230,19 @@ function handler(req, res) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let creds;
|
if (BASIC_AUTH_ENABLED) {
|
||||||
try {
|
let creds;
|
||||||
creds = loadBasicAuth();
|
try {
|
||||||
} catch (e) {
|
creds = loadBasicAuth();
|
||||||
send(res, 500, { 'content-type': 'text/plain; charset=utf-8' }, String(e?.message || e));
|
} catch (e) {
|
||||||
return;
|
send(res, 500, { 'content-type': 'text/plain; charset=utf-8' }, String(e?.message || e));
|
||||||
}
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (!isAuthorized(req, creds)) {
|
if (!isAuthorized(req, creds)) {
|
||||||
basicAuthRequired(res);
|
basicAuthRequired(res);
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (req.url?.startsWith('/api') && (req.url === '/api' || req.url.startsWith('/api/'))) {
|
if (req.url?.startsWith('/api') && (req.url === '/api' || req.url.startsWith('/api/'))) {
|
||||||
@@ -264,6 +270,7 @@ server.listen(PORT, () => {
|
|||||||
staticDir: STATIC_DIR,
|
staticDir: STATIC_DIR,
|
||||||
apiUpstream: API_UPSTREAM,
|
apiUpstream: API_UPSTREAM,
|
||||||
basicAuthFile: BASIC_AUTH_FILE,
|
basicAuthFile: BASIC_AUTH_FILE,
|
||||||
|
basicAuthMode: BASIC_AUTH_MODE,
|
||||||
apiReadTokenFile: API_READ_TOKEN_FILE,
|
apiReadTokenFile: API_READ_TOKEN_FILE,
|
||||||
},
|
},
|
||||||
null,
|
null,
|
||||||
|
|||||||
Reference in New Issue
Block a user