forked from mirrors/amnezia-wg-easy
Support for sessionless HTTP API authentication
This commit is contained in:
parent
b56c49c99a
commit
bf956e3ae8
6 changed files with 387 additions and 29 deletions
|
@ -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',
|
||||
});
|
||||
|
|
|
@ -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();
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue