tested working pair code

This commit is contained in:
AJ ONeal 2018-08-08 08:22:56 +00:00
parent f828839b4d
commit acf8522195
1 changed files with 11 additions and 15 deletions

View File

@ -149,14 +149,14 @@ DB.domains._add = function (acc, opts) {
var err; var err;
//var acc = DB._byId[aid]; //var acc = DB._byId[aid];
var domain = { var domain = {
name: opts.domain || opts.name name: (opts.domain || opts.name)
, hostname: opts.hostname , hostname: opts.hostname
, os: opts.os , os: opts.os
, createdAt: new Date().toISOString() , createdAt: new Date().toISOString()
, wildcard: opts.wildcard , wildcard: opts.wildcard
}; };
var pdomain; var pdomain;
var parts = opts.name.split('.').map(function (el, i, arr) { var parts = (opts.domain || domain.name).split('.').map(function (el, i, arr) {
return arr.slice(i).join('.'); return arr.slice(i).join('.');
}).reverse(); }).reverse();
parts.shift(); parts.shift();
@ -167,18 +167,18 @@ DB.domains._add = function (acc, opts) {
return true; return true;
} }
})) { })) {
err = new Error("'" + name + "' exists as '" + pdomain + "' and therefore requires an admin to review and approve"); err = new Error("'" + domain.name + "' exists as '" + pdomain + "' and therefore requires an admin to review and approve");
err.code = "E_REQ_ADMIN"; err.code = "E_REQ_ADMIN";
throw err; throw err;
} }
if (DB._byDomain[name]) { if (DB._byDomain[domain.name]) {
if (acc !== DB._byDomain[name].account) { if (acc !== DB._byDomain[domain.name].account) {
throw new Error("domain '" + name + "' exists"); throw new Error("domain '" + domain.name + "' exists");
} }
// happily ignore non-change // happily ignore non-change
return; return;
} }
DB._byDomain[name] = { DB._byDomain[domain.name] = {
account: acc account: acc
, domain: domain , domain: domain
}; };
@ -754,9 +754,9 @@ module.exports.pairPin = function (opts) {
acc = { email: true, domains: [], ports: [], nodes: [ emailNode ] }; acc = { email: true, domains: [], ports: [], nodes: [ emailNode ] };
} }
return PromiseA.all([ return PromiseA.all([
DB.domains._add(acc, { domain: opts.domain, wildcard: true, hostname: auth.authnData.hostname, DB.domains._add(acc, { domain: grantable.domain, wildcard: true, hostname: auth.authnData.hostname,
os: auth.authnData.os_type, arch: auth.authnData.os_arch }) os: auth.authnData.os_type, arch: auth.authnData.os_arch })
, DB.ports._add(acc, { port: opts.port, hostname: auth.authnData.hostname, , DB.ports._add(acc, { port: grantable.port, hostname: auth.authnData.hostname,
os: auth.authnData.os_type, arch: auth.authnData.os_arch }) os: auth.authnData.os_type, arch: auth.authnData.os_arch })
]).then(function () { ]).then(function () {
var authzData = { var authzData = {
@ -1104,6 +1104,8 @@ function pairCode(req, res) {
}).then(function (tokenData) { }).then(function (tokenData) {
res.send(tokenData); res.send(tokenData);
}, function (err) { }, function (err) {
console.error('[error] pairCode:');
console.error(err);
res.send({ error: { message: err.toString() } }); res.send({ error: { message: err.toString() } });
//res.send(tokenData || { error: { code: "E_TOKEN", message: "Invalid or expired magic link. (" + magic + ")" } }); //res.send(tokenData || { error: { code: "E_TOKEN", message: "Invalid or expired magic link. (" + magic + ")" } });
}); });
@ -1127,30 +1129,24 @@ app.get(urls.pairState, function (req, res) {
} }
function check(i) { function check(i) {
console.log("[pair_state] check i =", i, req.params.id);
if (auth._claimed) { if (auth._claimed) {
console.log("[pair_state] complete", req.params.id);
res.send({ res.send({
status: 'complete' status: 'complete'
}); });
} else if (auth._offered) { } else if (auth._offered) {
console.log("[pair_state] ready", req.params.id);
res.send({ res.send({
status: 'ready', access_token: auth.authz status: 'ready', access_token: auth.authz
, grant: { domains: auth.domains || [], ports: auth.ports || [] } , grant: { domains: auth.domains || [], ports: auth.ports || [] }
}); });
} else if (false === auth._offered) { } else if (false === auth._offered) {
console.log("[pair_state] failed", req.params.id);
res.send({ status: 'failed', error: { message: "device pairing failed" } }); res.send({ status: 'failed', error: { message: "device pairing failed" } });
} else if (i >= 7) { } else if (i >= 7) {
console.log("[pair_state] overdue i =", i, req.params.id);
var stateUrl = 'https://' + req._state.config.apiDomain + urls.pairState.replace(/:id/g, auth.id); var stateUrl = 'https://' + req._state.config.apiDomain + urls.pairState.replace(/:id/g, auth.id);
res.statusCode = 200; res.statusCode = 200;
res.setHeader('Location', stateUrl); res.setHeader('Location', stateUrl);
res.setHeader('Link', '<' + stateUrl + '>;rel="next"'); res.setHeader('Link', '<' + stateUrl + '>;rel="next"');
res.send({ status: 'pending' }); res.send({ status: 'pending' });
} else { } else {
console.log("[pair_state] try again i =", i, req.params.id);
setTimeout(check, 250, i + 1); setTimeout(check, 250, i + 1);
} }
} }