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 acc = DB._byId[aid];
var domain = {
name: opts.domain || opts.name
name: (opts.domain || opts.name)
, hostname: opts.hostname
, os: opts.os
, createdAt: new Date().toISOString()
, wildcard: opts.wildcard
};
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('.');
}).reverse();
parts.shift();
@ -167,18 +167,18 @@ DB.domains._add = function (acc, opts) {
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";
throw err;
}
if (DB._byDomain[name]) {
if (acc !== DB._byDomain[name].account) {
throw new Error("domain '" + name + "' exists");
if (DB._byDomain[domain.name]) {
if (acc !== DB._byDomain[domain.name].account) {
throw new Error("domain '" + domain.name + "' exists");
}
// happily ignore non-change
return;
}
DB._byDomain[name] = {
DB._byDomain[domain.name] = {
account: acc
, domain: domain
};
@ -754,9 +754,9 @@ module.exports.pairPin = function (opts) {
acc = { email: true, domains: [], ports: [], nodes: [ emailNode ] };
}
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 })
, 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 })
]).then(function () {
var authzData = {
@ -1104,6 +1104,8 @@ function pairCode(req, res) {
}).then(function (tokenData) {
res.send(tokenData);
}, function (err) {
console.error('[error] pairCode:');
console.error(err);
res.send({ error: { message: err.toString() } });
//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) {
console.log("[pair_state] check i =", i, req.params.id);
if (auth._claimed) {
console.log("[pair_state] complete", req.params.id);
res.send({
status: 'complete'
});
} else if (auth._offered) {
console.log("[pair_state] ready", req.params.id);
res.send({
status: 'ready', access_token: auth.authz
, grant: { domains: auth.domains || [], ports: auth.ports || [] }
});
} else if (false === auth._offered) {
console.log("[pair_state] failed", req.params.id);
res.send({ status: 'failed', error: { message: "device pairing failed" } });
} 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);
res.statusCode = 200;
res.setHeader('Location', stateUrl);
res.setHeader('Link', '<' + stateUrl + '>;rel="next"');
res.send({ status: 'pending' });
} else {
console.log("[pair_state] try again i =", i, req.params.id);
setTimeout(check, 250, i + 1);
}
}