diff --git a/lib/extensions/index.js b/lib/extensions/index.js index 3b3efe1..90b2043 100644 --- a/lib/extensions/index.js +++ b/lib/extensions/index.js @@ -45,6 +45,7 @@ function sendMail(state, auth) { text = text.replace(new RegExp('{{' + key + '}}', 'g'), val); html = html.replace(new RegExp('{{' + key + '}}', 'g'), val); }); + return requestAsync({ url: state.config.mailer.url , method: 'POST' @@ -74,10 +75,12 @@ module.exports.pairRequest = function (opts) { var auth = opts.auth; var jwt = require('jsonwebtoken'); - console.log("[DEBUG] gonna send email"); auth.id = crypto.randomBytes(12).toString('hex'); auth.secret = crypto.randomBytes(12).toString('hex'); //var id = crypto.randomBytes(16).toString('base64').replace(/\+/g,'-').replace(/\//g,'_').replace(/=/g,''); + + console.log("[DEBUG] !!state", !!state); + console.log("[DEBUG] !!auth", !!auth); return sendMail(state, auth).then(function () { var now = Date.now(); var authnToken = { @@ -118,7 +121,8 @@ module.exports.pairPin = function (opts) { throw new Error("I can't even right now - bad device pair pin"); } - delete _auths[auth.id]; + auth._paired = true; + //delete _auths[auth.id]; var hri = require('human-readable-ids').hri; var hrname = hri.random() + '.' + state.config.sharedDomain; var authzToken = { @@ -205,17 +209,37 @@ var staticApp = express(); var nowww = require('nowww')(); var CORS = require('connect-cors'); var bodyParser = require('body-parser'); +var urls = { + pairState: '/api/telebit.cloud/pair_state/:id' +}; staticApp.use('/', express.static(path.join(__dirname, 'admin'))); app.use('/api', CORS({})); +app.use('/api', function (req, res, next) { + next(); + req.on('data', function (chunk) { + console.log('chunk', chunk.toString()); + }); + req.on('end', function () { + console.log('end'); + }); +}); app.use('/api', bodyParser.json()); // From Device app.post('/api/telebit.cloud/pair_request', function (req, res) { var auth = req.body; - module.exports.authenticate({ state: req._state, auth: auth }).then(function (tokenData) { + console.log('[ext] pair_request (request)', req.headers); + console.log('[ext] pair_request (request)', req.body); + module.exports.pairRequest({ state: req._state, auth: auth }).then(function (tokenData) { + console.log('[ext] pair_request (response)', tokenData); // res.send({ success: true, message: "pair request sent" }); + var stateUrl = 'https://' + req._state.config.apiDomain + urls.pairState.replace(/:id/g, tokenData.id); + res.statusCode = 201; + res.setHeader('Location', stateUrl); + res.setHeader('Link', '<' + stateUrl + '>;rel="next"'); res.send(tokenData); }, function (err) { - res.send({ error: err }); + console.error(err); + res.send({ error: { code: err.code, message: err.toString() } }); }); }); // From Browser @@ -228,13 +252,28 @@ app.post('/api/telebit.cloud/pair_code', function (req, res) { }); }); // From Device (polling) -app.get('/api/telebit.cloud/pair_state', function (req, res) { +app.get(urls.pairState, function (req, res) { // check if pair is complete // respond immediately if so // wait for a little bit otherwise // respond if/when it completes // or respond after time if it does not complete - res.send({ error: { message: "not implemented" } }); + var auth = _auths[req.params.id]; + if (!auth) { + res.send({ status: 'invalid' }); + return; + } + + if (true === auth.paired) { + res.send({ + status: 'ready', access_token: _auths[req.params.id].jwt + , grant: { domains: auth.domains || [], ports: auth.ports || [] } + }); + } else if (false === _auths[req.params.id].paired) { + res.send({ status: 'failed', error: { message: "device pairing failed" } }); + } else { + res.send({ status: 'pending' }); + } }); // From Browser app.get('/api/telebit.cloud/magic/:magic/:pin?', function (req, res) { @@ -263,7 +302,7 @@ module.exports.webadmin = function (state, req, res) { staticApp(req, res); return; } - if ('api.' + state.config.webminDomain === host) { + if ((state.config.apiDomain || ('api.' + state.config.webminDomain )) === host) { console.log("DEBUG going to api"); req._state = state; app(req, res);