1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-05-04 22:17:59 +08:00
Drasko DRASKOVIC 55b28f90f0 Add JWT data in the signature
This commit adds Mainflux metadata embedded in JWT.

Signed-off-by: Drasko DRASKOVIC <drasko.draskovic@gmail.com>
2015-12-06 14:57:40 +01:00

101 lines
2.3 KiB
JavaScript

var mongojs = require('mongojs');
var devicesDb = require('../database').collection('devices');
var jwt = require('jsonwebtoken');
var config = require('../../config/config');
var log = require('../logger');
var os = require('os');
/** createDevice() */
exports.createDevice = function(req, res, next) {
console.log("req.headers['x-auth-token'] = ", req.headers['x-auth-token']);
/** Save the device and check for errors */
devicesDb.insert(req.body, function(err, device) {
if (err)
return next(err);
var signaturePayload = {
version: config.version
}
console.log(signaturePayload);
var token = jwt.sign(signaturePayload, config.tokenSecret, {
subject: 'Device Auth Token',
issuer: req.headers.host,
audience: device._id.toString()
});
res.json({
status: 200,
message: 'Device created',
token: token
});
});
return next();
}
/** getAllDevices() */
exports.getAllDevices = function(req, res, next) {
console.log("req.headers['x-auth-token'] = ", req.headers['x-auth-token']);
log.info('hi');
devicesDb.find(req.body, function(err, devices) {
if (err)
return next(err);
res.json(devices);
return next();
});
}
/** getDevice() */
exports.getDevice = function(req, res, next) {
console.log(req.params.device_id);
devicesDb.findOne({_id: mongojs.ObjectId(req.params.device_id)}, function(err, device) {
if (err)
return next(err);
res.json(device);
return next();
});
}
/** updateDevice() */
exports.updateDevice = function(req, res, next) {
/** Use our device model to find the device we want */
console.log(req.body);
devicesDb.update({
_id: mongojs.ObjectId(req.params.device_id)
},
{$set: req.body},
function(err, device) {
if (err)
return next(err);
res.send('OK');
return next();
});
}
/** deleteDevice() */
exports.deleteDevice = function(req, res, next) {
devicesDb.remove({
_id: mongojs.ObjectId(req.params.device_id)
}, function(err, device) {
if (err)
return next(err);
res.send('OK');
return next();
});
}