feat: 取消 Web 挑战头鉴权

This commit is contained in:
2026-06-27 16:35:58 +08:00
parent ecca396792
commit e03a7a141c
4 changed files with 12 additions and 36 deletions
-1
View File
@@ -13,7 +13,6 @@
"axios": "^1.6.2",
"dotenv": "^16.3.1",
"express": "^4.18.2",
"express-basic-auth": "^1.2.1",
"node-pdu": "^2.1.2",
"nodemailer": "^6.9.7",
"serialport": "^12.0.0",
-1
View File
@@ -23,7 +23,6 @@
"@serialport/parser-readline": "^12.0.0",
"node-pdu": "^2.1.2",
"express": "^4.18.2",
"express-basic-auth": "^1.2.1",
"nodemailer": "^6.9.7",
"axios": "^1.6.2",
"winston": "^3.11.0",
+1 -1
View File
@@ -411,7 +411,7 @@
</svg>
<span>SMS Gateway</span>
</span>
<span class="footer__meta">© <span id="footerYear">2026</span> · Basic Auth protected · Built on Node.js +
<span class="footer__meta">© <span id="footerYear">2026</span> · Token protected · Built on Node.js +
ML307A</span>
</div>
</footer>
+9 -31
View File
@@ -50,7 +50,7 @@ class APIServer {
// JSON解析
this.app.use(express.json());
// Web Token认证API保留Basic Auth兼容
// Web Token认证
this.app.use((req, res, next) => {
this.authenticateRequest(req, res, next);
});
@@ -75,22 +75,23 @@ class APIServer {
return next();
}
if (this.isWebRoute(req) && this.getConfiguredWebToken()) {
return this.sendTokenGate(res);
}
if (this.isWebRoute(req)) {
const webToken = this.getConfiguredWebToken();
if (webToken) {
return this.sendTokenGate(res);
}
if (this.hasValidBasicAuth(req)) {
return next();
return res.status(401).send('未配置 webToken');
}
if (req.path.startsWith('/api/')) {
return res.status(401).json({
success: false,
error: this.getConfiguredWebToken() ? '需要有效token或Basic Auth' : '需要Basic Auth'
error: this.getConfiguredWebToken() ? '需要有效token' : '未配置webToken'
});
}
return this.sendBasicAuthChallenge(res);
return res.status(401).send(this.getConfiguredWebToken() ? '需要有效token' : '未配置 webToken');
}
getConfiguredWebToken() {
@@ -143,24 +144,6 @@ class APIServer {
return `${url.pathname}${url.search}`;
}
hasValidBasicAuth(req) {
const auth = req.get('authorization') || '';
if (!auth.startsWith('Basic ')) {
return false;
}
const decoded = Buffer.from(auth.slice(6), 'base64').toString('utf8');
const separatorIndex = decoded.indexOf(':');
if (separatorIndex === -1) {
return false;
}
const username = decoded.slice(0, separatorIndex);
const password = decoded.slice(separatorIndex + 1);
return this.safeEqual(username, this.config.api.auth.username) &&
this.safeEqual(password, this.config.api.auth.password);
}
safeEqual(actual, expected) {
const actualBuffer = Buffer.from(String(actual));
const expectedBuffer = Buffer.from(String(expected));
@@ -176,11 +159,6 @@ class APIServer {
return req.path === '/' || req.path === '/admin' || req.path.startsWith('/assets/');
}
sendBasicAuthChallenge(res) {
res.set('WWW-Authenticate', 'Basic realm="SMS Gateway"');
return res.status(401).send('Authentication required');
}
sendTokenGate(res) {
res.set('Cache-Control', 'no-store');
return res.status(401).type('html').send(`<!doctype html>