Add client token authentication #92

Closed
siburny wants to merge 4 commits from token-authentication into master
3 changed files with 25 additions and 11 deletions
Showing only changes of commit 6a103f3e61 - Show all commits
+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, () => {
+8 -5
View File
@@ -26,15 +26,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;
+14 -6
View File
@@ -104,13 +104,21 @@ export default function(opt) {
return;
}
debug('making new client with id %s', reqId);
const info = await manager.newClient(reqId);
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;
const url = schema + '://' + info.id + '.' + ctx.request.host;
info.url = url;
ctx.body = info;
}
catch(err) {
debug('Error: %s with id %s', err.message, reqId);
ctx.status = 403;
ctx.body = {
message: err.message,
};
}
});
const server = http.createServer();