1
0
mirror of https://github.com/Foltik/Shimapan synced 2024-11-10 23:53:31 -05:00
shimapan/app/routes/keys.js

75 lines
1.9 KiB
JavaScript
Raw Normal View History

2017-10-18 13:31:08 -04:00
var express = require('express');
var router = express.Router();
var crypto = require('crypto');
var Key = require('../models/Key.js');
router.post('/create', function (req, res) {
if (!req.body.identifier || !req.body.scope) {
res.status(400).json({'message': 'Bad request.'});
return;
}
Key.count({'username': req.session.passport.user}, function (err, count) {
if (count >= 10) {
res.status(403).json({'message': 'Key limit reached.'});
return;
}
var scope;
try {
scope = JSON.parse(req.body.scope);
} catch (e) {
res.status(400).json({'message': e.name + ': ' + e.message});
return;
}
var id = req.sanitize(req.body.identifier);
if (id.length === 0) id = "err";
var entry = {
key: crypto.randomBytes(32).toString('hex'),
identifier: id,
scope: scope,
username: req.session.passport.user,
date: Date.now()
};
Key.create(entry, function (err) {
if (err) {
throw err;
} else {
res.status(200).json({
key: entry.key,
identifier: entry.identifier,
scope: entry.scope
});
}
})
})
});
router.get('/get', function (req, res, next) {
var query = {username: req.session.passport.user};
if (req.body.identifier)
query.identifier = req.body.identifier;
Key.find(query, function (err, keys) {
if (err) {
next(err);
} else {
res.status(200).json(keys);
}
})
});
2017-10-21 15:10:24 -04:00
router.post('/delete', function(req, res, next) {
2017-10-21 17:47:55 -04:00
Key.deleteOne({key: req.body.key}, function(err) {
if (err) next(err);
else res.status(200).json({'message': 'Successfully deleted.'});
});
2017-10-21 15:10:24 -04:00
});
2017-10-18 13:31:08 -04:00
module.exports = router;