diff --git a/lib/extensions/index.js b/lib/extensions/index.js index 1ef9c88..2293696 100644 --- a/lib/extensions/index.js +++ b/lib/extensions/index.js @@ -15,7 +15,7 @@ var crypto = require('crypto'); var escapeHtml = require('escape-html'); var jwt = require('jsonwebtoken'); 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 TRUSTED_ISSUERS = [ 'oauth3.org' ]; var DB = {}; @@ -46,14 +46,14 @@ DB._load = function () { DB._grants[acc.id] = []; } 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]) { DB._grantsMap[acc.id][d.name] = d; DB._grants[acc.id].push(d); } }); 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]) { DB._grantsMap[acc.id][p.number] = p; DB._grants[acc.id].push(p); @@ -78,14 +78,14 @@ DB._load = function () { DB._grants[node.name] = []; } 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]) { DB._grantsMap[node.name][d.name] = d; DB._grants[node.name].push(d); } }); 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]) { DB._grantsMap[node.name][p.number] = p; DB._grants[node.name].push(p); @@ -107,7 +107,7 @@ DB._load = function () { }); acc.ports.forEach(function (port) { 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("::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 }; var pdomain; - var parts = name.split('.').map(function (el, i) { + var parts = name.split('.').map(function (el, i, arr) { return arr.slice(i).join('.'); }).reverse(); parts.shift(); @@ -200,15 +200,15 @@ DB.ports._add = function (acc, opts) { , os: opts.os , createdAt: new Date().toISOString() }; - if (DB._byPort[number]) { + if (DB._byPort[port.number]) { // TODO verifications - throw new Error("port '" + number + "' exists"); + throw new Error("port '" + port.number + "' exists"); } - DB._byPort[number] = { + DB._byPort[port.number] = { account: acc - , domain: domain + , port: port }; - acc.domains.push(domain); + acc.ports.push(port); }); }; DB._save = function () { @@ -219,7 +219,7 @@ DB._savePromises = []; DB._savePromise = PromiseA.resolve(); DB.save = function () { clearTimeout(DB._saveToken); - return new Promise(function (resolve, reject) { + return new PromiseA(function (resolve, reject) { function doSave() { DB._savePromise = DB._savePromise.then(function () { return DB._save().then(function (yep) { @@ -709,11 +709,11 @@ DB.getDomainAndPort = function (state) { portCount += 1; var portnumber = (1024 + 1) + Math.round(Math.random() * 65535); return DB.ports.available(portnumber).then(function (available) { - if (!available) { return portDomain(); } + if (!available) { return choosePort(); } return portnumber; }); } - return Promise.all([ + return PromiseA.all([ chooseDomain() , choosePort() ]).then(function (two) { @@ -745,65 +745,65 @@ module.exports.pairPin = function (opts) { } console.log('[pairPin] generating offer'); - return DB.getDomainAndPort(state); - }).then(function (grantable) { - var emailNode = { scheme: 'mailto', type: 'email', name: auth.subject }; + return DB.getDomainAndPort(state).then(function (grantable) { + var emailNode = { scheme: 'mailto', type: 'email', name: auth.subject }; - return DB.accounts.get(emailNode).then(function (_acc) { - var acc = _acc; - if (!acc) { - 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 DB.accounts.get(emailNode).then(function (_acc) { + var acc = _acc; + if (!acc) { + 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'); + } - if (!_acc) { - return DB.accounts.add(acc).then(function () { - // intentionally not returned to the promise chain - DB.save().catch(function (err) { - console.error('DB.save() failed:'); - console.error(err); + if (!_acc) { + return DB.accounts.add(acc).then(function () { + // intentionally not returned to the promise chain + DB.save().catch(function (err) { + console.error('DB.save() failed:'); + console.error(err); + }); + return authzData; }); + } else { 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); Accounts._associateEmails = function (req) { 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; } @@ -952,9 +952,9 @@ Accounts._associateEmails = function (req) { }).then(function (resp) { var email; var err; - (resp.data.nodes||[]).some(function (node) { + (resp.data.nodes||[]).some(function (/*node*/) { // TODO use verified email addresses - return true + return true; }); // back-compat for current way email is stored if (!email && /@/.test(resp.data.username)) { @@ -962,7 +962,7 @@ Accounts._associateEmails = function (req) { } if (!email) { 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); }