Support for sessionless HTTP API authentication

This commit is contained in:
Philip H. 2023-11-09 18:58:48 +00:00 committed by GitHub
parent b56c49c99a
commit bf956e3ae8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 387 additions and 29 deletions

View file

@ -1,6 +1,7 @@
'use strict';
const path = require('path');
const bcrypt = require('bcrypt');
const express = require('express');
const expressSession = require('express-session');
@ -35,7 +36,7 @@ module.exports = class Server {
return RELEASE;
})))
// Authentication
// Authentication
.get('/api/session', Util.promisify(async (req) => {
const requiresPassword = !!process.env.PASSWORD;
const authenticated = requiresPassword
@ -66,7 +67,7 @@ module.exports = class Server {
debug(`New Session: ${req.session.id}`);
}))
// WireGuard
// WireGuard
.use((req, res, next) => {
if (!PASSWORD) {
return next();
@ -76,6 +77,22 @@ module.exports = class Server {
return next();
}
if (req.path.startsWith('/api/') && req.headers['authorization']) {
const authorizationHash = bcrypt.createHash('bcrypt')
.update(req.headers['authorization'])
.digest('hex');
const passwordHash = bcrypt.createHash('bcrypt')
.update(PASSWORD)
.digest('hex');
if (bcrypt.timingSafeEqual(Buffer.from(authorizationHash), Buffer.from(passwordHash))) {
return next();
}
return res.status(401).json({
error: 'Incorrect Password',
});
}
return res.status(401).json({
error: 'Not Logged In',
});

View file

@ -248,8 +248,9 @@ Endpoint = ${WG_HOST}:${WG_PORT}`;
}
// Create Client
const clientId = uuid.v4();
const id = uuid.v4();
const client = {
id,
name,
address,
privateKey,
@ -262,7 +263,7 @@ Endpoint = ${WG_HOST}:${WG_PORT}`;
enabled: true,
};
config.clients[clientId] = client;
config.clients[id] = client;
await this.saveConfig();