Add client token authentication #92

Closed
siburny wants to merge 4 commits from token-authentication into master
5 changed files with 29 additions and 12 deletions
+1
View File
@@ -1 +1,2 @@
node_modules
.vscode/
+3
View File
@@ -42,6 +42,9 @@ const server = CreateServer({
max_tcp_sockets: argv['max-sockets'],
secure: argv.secure,
domain: argv.domain,
tokens: {
// 'clientid': 'token'
}
});
server.listen(argv.port, argv.address, () => {
+10 -5
View File
@@ -11,6 +11,8 @@ class ClientManager {
constructor(opt) {
this.opt = opt || {};
this.opt.tokens = this.opt.tokens || {};
// id -> client instance
this.clients = new Map();
@@ -26,15 +28,18 @@ class ClientManager {
}
// create a new tunnel with `id`
// if the id is already used, a random id is assigned
// if the tunnel could not be created, throws an error
async newClient(id) {
async newClient (id, token) {
const clients = this.clients;
const stats = this.stats;
// can't ask for id already is use
if (clients[id]) {
if (clients[id] !== undefined) {
if(this.opt.tokens[id] !== undefined) {
return Promise.reject(new Error('Client is already connected. Try again later.'));
}
id = hri.random();
} else if(this.opt.tokens[id] !== undefined && this.opt.tokens[id] !== token) {
return Promise.reject(new Error('Token is not found or correct.'));
}
const maxSockets = this.opt.max_tcp_sockets;
+1 -1
View File
@@ -27,7 +27,7 @@
"ws": "5.1.1"
},
"scripts": {
"test": "mocha --check-leaks --require esm './**/*.test.js'",
"test": "mocha --check-leaks --require esm ./**/*.test.js",
"start": "./bin/server",
"dev": "node-dev bin/server --port 3000"
}
+9 -1
View File
@@ -104,13 +104,21 @@ export default function(opt) {
return;
}
try {
debug('making new client with id %s', reqId);
const info = await manager.newClient(reqId);
const url = schema + '://' + info.id + '.' + ctx.request.host;
info.url = url;
ctx.body = info;
return;
}
catch(err) {
debug('Error: %s with id %s', err.message, reqId);
ctx.status = 403;
ctx.body = {
message: err.message,
};
}
});
const server = http.createServer();