obvious bugfixes

This commit is contained in:
AJ ONeal 2018-08-08 01:59:51 -06:00
parent 5b7f19e7a8
commit 60ee3720e0
1 changed files with 70 additions and 70 deletions

View File

@ -15,7 +15,7 @@ var crypto = require('crypto');
var escapeHtml = require('escape-html'); var escapeHtml = require('escape-html');
var jwt = require('jsonwebtoken'); var jwt = require('jsonwebtoken');
var requestAsync = util.promisify(require('@coolaj86/urequest')); var requestAsync = util.promisify(require('@coolaj86/urequest'));
var readFileAsync = util.promisify(fs.readFile); //var readFileAsync = util.promisify(fs.readFile);
var mkdirpAsync = util.promisify(require('mkdirp')); var mkdirpAsync = util.promisify(require('mkdirp'));
var TRUSTED_ISSUERS = [ 'oauth3.org' ]; var TRUSTED_ISSUERS = [ 'oauth3.org' ];
var DB = {}; var DB = {};
@ -46,14 +46,14 @@ DB._load = function () {
DB._grants[acc.id] = []; DB._grants[acc.id] = [];
} }
acc.domains.forEach(function (d) { acc.domains.forEach(function (d) {
DB._grants[d.name + '|id|' + acc.id] = true DB._grants[d.name + '|id|' + acc.id] = true;
if (!DB._grantsMap[acc.id][d.name]) { if (!DB._grantsMap[acc.id][d.name]) {
DB._grantsMap[acc.id][d.name] = d; DB._grantsMap[acc.id][d.name] = d;
DB._grants[acc.id].push(d); DB._grants[acc.id].push(d);
} }
}); });
acc.ports.forEach(function (p) { acc.ports.forEach(function (p) {
DB._grants[p.number + '|id|' + acc.id] = true DB._grants[p.number + '|id|' + acc.id] = true;
if (!DB._grantsMap[acc.id][p.number]) { if (!DB._grantsMap[acc.id][p.number]) {
DB._grantsMap[acc.id][p.number] = p; DB._grantsMap[acc.id][p.number] = p;
DB._grants[acc.id].push(p); DB._grants[acc.id].push(p);
@ -78,14 +78,14 @@ DB._load = function () {
DB._grants[node.name] = []; DB._grants[node.name] = [];
} }
acc.domains.forEach(function (d) { acc.domains.forEach(function (d) {
DB._grants[d.name + '|' + (node.scheme||node.type) + '|' + node.name] = true DB._grants[d.name + '|' + (node.scheme||node.type) + '|' + node.name] = true;
if (!DB._grantsMap[node.name][d.name]) { if (!DB._grantsMap[node.name][d.name]) {
DB._grantsMap[node.name][d.name] = d; DB._grantsMap[node.name][d.name] = d;
DB._grants[node.name].push(d); DB._grants[node.name].push(d);
} }
}); });
acc.ports.forEach(function (p) { acc.ports.forEach(function (p) {
DB._grants[p.number + '|' + (node.scheme||node.type) + '|' + node.name] = true DB._grants[p.number + '|' + (node.scheme||node.type) + '|' + node.name] = true;
if (!DB._grantsMap[node.name][p.number]) { if (!DB._grantsMap[node.name][p.number]) {
DB._grantsMap[node.name][p.number] = p; DB._grantsMap[node.name][p.number] = p;
DB._grants[node.name].push(p); DB._grants[node.name].push(p);
@ -107,7 +107,7 @@ DB._load = function () {
}); });
acc.ports.forEach(function (port) { acc.ports.forEach(function (port) {
if (DB._byPort[port.number]) { if (DB._byPort[port.number]) {
console.warn("duplicate port '" + domain.number + "'"); console.warn("duplicate port '" + port.number + "'");
console.warn("::existing account '" + acc.nodes.map(function (node) { return node.name; }) + "'"); console.warn("::existing account '" + acc.nodes.map(function (node) { return node.name; }) + "'");
console.warn("::new account '" + DB._byPort[port.number].account.nodes.map(function (node) { return node.name; }) + "'"); console.warn("::new account '" + DB._byPort[port.number].account.nodes.map(function (node) { return node.name; }) + "'");
} }
@ -156,7 +156,7 @@ DB.domains._add = function (acc, opts) {
, wildcard: opts.wildcard , wildcard: opts.wildcard
}; };
var pdomain; var pdomain;
var parts = name.split('.').map(function (el, i) { var parts = name.split('.').map(function (el, i, arr) {
return arr.slice(i).join('.'); return arr.slice(i).join('.');
}).reverse(); }).reverse();
parts.shift(); parts.shift();
@ -200,15 +200,15 @@ DB.ports._add = function (acc, opts) {
, os: opts.os , os: opts.os
, createdAt: new Date().toISOString() , createdAt: new Date().toISOString()
}; };
if (DB._byPort[number]) { if (DB._byPort[port.number]) {
// TODO verifications // TODO verifications
throw new Error("port '" + number + "' exists"); throw new Error("port '" + port.number + "' exists");
} }
DB._byPort[number] = { DB._byPort[port.number] = {
account: acc account: acc
, domain: domain , port: port
}; };
acc.domains.push(domain); acc.ports.push(port);
}); });
}; };
DB._save = function () { DB._save = function () {
@ -219,7 +219,7 @@ DB._savePromises = [];
DB._savePromise = PromiseA.resolve(); DB._savePromise = PromiseA.resolve();
DB.save = function () { DB.save = function () {
clearTimeout(DB._saveToken); clearTimeout(DB._saveToken);
return new Promise(function (resolve, reject) { return new PromiseA(function (resolve, reject) {
function doSave() { function doSave() {
DB._savePromise = DB._savePromise.then(function () { DB._savePromise = DB._savePromise.then(function () {
return DB._save().then(function (yep) { return DB._save().then(function (yep) {
@ -709,11 +709,11 @@ DB.getDomainAndPort = function (state) {
portCount += 1; portCount += 1;
var portnumber = (1024 + 1) + Math.round(Math.random() * 65535); var portnumber = (1024 + 1) + Math.round(Math.random() * 65535);
return DB.ports.available(portnumber).then(function (available) { return DB.ports.available(portnumber).then(function (available) {
if (!available) { return portDomain(); } if (!available) { return choosePort(); }
return portnumber; return portnumber;
}); });
} }
return Promise.all([ return PromiseA.all([
chooseDomain() chooseDomain()
, choosePort() , choosePort()
]).then(function (two) { ]).then(function (two) {
@ -745,65 +745,65 @@ module.exports.pairPin = function (opts) {
} }
console.log('[pairPin] generating offer'); console.log('[pairPin] generating offer');
return DB.getDomainAndPort(state); return DB.getDomainAndPort(state).then(function (grantable) {
}).then(function (grantable) { var emailNode = { scheme: 'mailto', type: 'email', name: auth.subject };
var emailNode = { scheme: 'mailto', type: 'email', name: auth.subject };
return DB.accounts.get(emailNode).then(function (_acc) { return DB.accounts.get(emailNode).then(function (_acc) {
var acc = _acc; var acc = _acc;
if (!acc) { if (!acc) {
acc = { email: true, domains: [], ports: [], nodes: [ emailNode ] }; acc = { email: true, domains: [], ports: [], nodes: [ emailNode ] };
}
return PromiseA.all([
DB.domains._add(acc, { domain: opts.domain, wildcard: true, hostname: auth.authnData.hostname,
os: auth.authnData.os_type, arch: auth.authnData.os_arch })
, DB.ports._add(acc, { port: opts.port, hostname: auth.authnData.hostname,
os: auth.authnData.os_type, arch: auth.authnData.os_arch })
]).then(function () {
var authzData = {
id: auth.id
, domains: [ grantable.domain ]
, ports: [ grantable.port ]
, aud: state.config.webminDomain
, iat: Math.round(Date.now() / 1000)
// of the client's computer
, hostname: auth.hostname
};
auth.authz = jwt.sign(authzData, state.secret);
auth.authzData = authzData;
authzData.jwt = auth.authz;
auth._offered = authzData;
if (auth.resolve) {
console.log('[pairPin] resolving');
auth.resolve(auth);
} else {
console.log('[pairPin] not resolvable');
} }
return PromiseA.all([
DB.domains._add(acc, { domain: opts.domain, wildcard: true, hostname: auth.authnData.hostname,
os: auth.authnData.os_type, arch: auth.authnData.os_arch })
, DB.ports._add(acc, { port: opts.port, hostname: auth.authnData.hostname,
os: auth.authnData.os_type, arch: auth.authnData.os_arch })
]).then(function () {
var authzData = {
id: auth.id
, domains: [ grantable.domain ]
, ports: [ grantable.port ]
, aud: state.config.webminDomain
, iat: Math.round(Date.now() / 1000)
// of the client's computer
, hostname: auth.hostname
};
auth.authz = jwt.sign(authzData, state.secret);
auth.authzData = authzData;
authzData.jwt = auth.authz;
auth._offered = authzData;
if (auth.resolve) {
console.log('[pairPin] resolving');
auth.resolve(auth);
} else {
console.log('[pairPin] not resolvable');
}
if (!_acc) { if (!_acc) {
return DB.accounts.add(acc).then(function () { return DB.accounts.add(acc).then(function () {
// intentionally not returned to the promise chain // intentionally not returned to the promise chain
DB.save().catch(function (err) { DB.save().catch(function (err) {
console.error('DB.save() failed:'); console.error('DB.save() failed:');
console.error(err); console.error(err);
});
return authzData;
}); });
} else {
return authzData; return authzData;
}); }
} else { });
return authzData; });
/*
var pathname = path.join(__dirname, 'emails', auth.subject + '.' + hrname + '.data');
fs.writeFile(pathname, JSON.stringify(authzData), function (err) {
if (err) {
console.error('[ERROR] in writing token details');
console.error(err);
} }
}); });
*/
}); });
/*
var pathname = path.join(__dirname, 'emails', auth.subject + '.' + hrname + '.data');
fs.writeFile(pathname, JSON.stringify(authzData), function (err) {
if (err) {
console.error('[ERROR] in writing token details');
console.error(err);
}
});
*/
}); });
}; };
@ -941,7 +941,7 @@ app.use('/api', bodyParser.json());
app.use('/api/telebit.cloud/account', oauth3Auth); app.use('/api/telebit.cloud/account', oauth3Auth);
Accounts._associateEmails = function (req) { Accounts._associateEmails = function (req) {
if (-1 === (req._state.config.trustedIssuers||TRUSTED_ISSUERS).indexOf(req.auth.data.iss)) { if (-1 === (req._state.config.trustedIssuers||TRUSTED_ISSUERS).indexOf(req.auth.data.iss)) {
// again, make sure that untrusted issuers do not get // again, make sure that untrusted issuers do not get
return null; return null;
} }
@ -952,9 +952,9 @@ Accounts._associateEmails = function (req) {
}).then(function (resp) { }).then(function (resp) {
var email; var email;
var err; var err;
(resp.data.nodes||[]).some(function (node) { (resp.data.nodes||[]).some(function (/*node*/) {
// TODO use verified email addresses // TODO use verified email addresses
return true return true;
}); });
// back-compat for current way email is stored // back-compat for current way email is stored
if (!email && /@/.test(resp.data.username)) { if (!email && /@/.test(resp.data.username)) {
@ -962,7 +962,7 @@ Accounts._associateEmails = function (req) {
} }
if (!email) { if (!email) {
err = new Error ("could not find a verified email address in profile settings"); err = new Error ("could not find a verified email address in profile settings");
err.code = "E_NO_EMAIL" err.code = "E_NO_EMAIL";
return PromiseA.reject(err); return PromiseA.reject(err);
} }