Browse Source

sessionless auth

pull/597/head
babadzhanov 3 years ago
parent
commit
260b790a3e
  1. 55
      src/lib/Server.js

55
src/lib/Server.js

@ -1,6 +1,7 @@
'use strict'; 'use strict';
const path = require('path'); const path = require('path');
const crypto = require('crypto');
const express = require('express'); const express = require('express');
const expressSession = require('express-session'); const expressSession = require('express-session');
@ -75,6 +76,22 @@ module.exports = class Server {
return next(); return next();
} }
if (req.path.startsWith('/api/') && req.headers['authorization']) {
const authorizationHash = crypto.createHash('sha256')
.update(req.headers['authorization'])
.digest('hex');
const passwordHash = crypto.createHash('sha256')
.update(PASSWORD)
.digest('hex');
if (crypto.timingSafeEqual(Buffer.from(authorizationHash), Buffer.from(passwordHash))) {
return next();
}
return res.status(401).json({
error: 'Incorrect Password',
});
}
return res.status(401).json({ return res.status(401).json({
error: 'Not Logged In', error: 'Not Logged In',
}); });
@ -90,15 +107,15 @@ module.exports = class Server {
return WireGuard.getClients(); return WireGuard.getClients();
})) }))
.get('/api/wireguard/client/:clientId/qrcode.svg', Util.promisify(async (req, res) => { .get('/api/wireguard/client/:clientId/qrcode.svg', Util.promisify(async (req, res) => {
const { clientId } = req.params; const {clientId} = req.params;
const svg = await WireGuard.getClientQRCodeSVG({ clientId }); const svg = await WireGuard.getClientQRCodeSVG({clientId});
res.header('Content-Type', 'image/svg+xml'); res.header('Content-Type', 'image/svg+xml');
res.send(svg); res.send(svg);
})) }))
.get('/api/wireguard/client/:clientId/configuration', Util.promisify(async (req, res) => { .get('/api/wireguard/client/:clientId/configuration', Util.promisify(async (req, res) => {
const { clientId } = req.params; const {clientId} = req.params;
const client = await WireGuard.getClient({ clientId }); const client = await WireGuard.getClient({clientId});
const config = await WireGuard.getClientConfiguration({ clientId }); const config = await WireGuard.getClientConfiguration({clientId});
const configName = client.name const configName = client.name
.replace(/[^a-zA-Z0-9_=+.-]/g, '-') .replace(/[^a-zA-Z0-9_=+.-]/g, '-')
.replace(/(-{2,}|-$)/g, '-') .replace(/(-{2,}|-$)/g, '-')
@ -109,30 +126,30 @@ module.exports = class Server {
res.send(config); res.send(config);
})) }))
.post('/api/wireguard/client', Util.promisify(async req => { .post('/api/wireguard/client', Util.promisify(async req => {
const { name } = req.body; const {name} = req.body;
return WireGuard.createClient({ name }); return WireGuard.createClient({name});
})) }))
.delete('/api/wireguard/client/:clientId', Util.promisify(async req => { .delete('/api/wireguard/client/:clientId', Util.promisify(async req => {
const { clientId } = req.params; const {clientId} = req.params;
return WireGuard.deleteClient({ clientId }); return WireGuard.deleteClient({clientId});
})) }))
.post('/api/wireguard/client/:clientId/enable', Util.promisify(async req => { .post('/api/wireguard/client/:clientId/enable', Util.promisify(async req => {
const { clientId } = req.params; const {clientId} = req.params;
return WireGuard.enableClient({ clientId }); return WireGuard.enableClient({clientId});
})) }))
.post('/api/wireguard/client/:clientId/disable', Util.promisify(async req => { .post('/api/wireguard/client/:clientId/disable', Util.promisify(async req => {
const { clientId } = req.params; const {clientId} = req.params;
return WireGuard.disableClient({ clientId }); return WireGuard.disableClient({clientId});
})) }))
.put('/api/wireguard/client/:clientId/name', Util.promisify(async req => { .put('/api/wireguard/client/:clientId/name', Util.promisify(async req => {
const { clientId } = req.params; const {clientId} = req.params;
const { name } = req.body; const {name} = req.body;
return WireGuard.updateClientName({ clientId, name }); return WireGuard.updateClientName({clientId, name});
})) }))
.put('/api/wireguard/client/:clientId/address', Util.promisify(async req => { .put('/api/wireguard/client/:clientId/address', Util.promisify(async req => {
const { clientId } = req.params; const {clientId} = req.params;
const { address } = req.body; const {address} = req.body;
return WireGuard.updateClientAddress({ clientId, address }); return WireGuard.updateClientAddress({clientId, address});
})) }))
.listen(PORT, () => { .listen(PORT, () => {

Loading…
Cancel
Save