2017-08-31 16:43:36 +00:00
|
|
|
const storage = require('../storage');
|
|
|
|
const crypto = require('crypto');
|
|
|
|
|
|
|
|
module.exports = async function(req, res) {
|
|
|
|
const id = req.params.id;
|
|
|
|
|
|
|
|
try {
|
|
|
|
const auth = req.header('Authorization').split(' ')[1];
|
|
|
|
const meta = await storage.metadata(id);
|
|
|
|
const hmac = crypto.createHmac('sha256', Buffer.from(meta.auth, 'base64'));
|
|
|
|
hmac.update(Buffer.from(meta.nonce, 'base64'));
|
|
|
|
const verifyHash = hmac.digest();
|
|
|
|
if (!verifyHash.equals(Buffer.from(auth, 'base64'))) {
|
2017-11-30 21:41:09 +00:00
|
|
|
res.set('WWW-Authenticate', `send-v1 ${meta.nonce}`);
|
2017-08-31 16:43:36 +00:00
|
|
|
return res.sendStatus(401);
|
|
|
|
}
|
2017-11-30 21:41:09 +00:00
|
|
|
const nonce = crypto.randomBytes(16).toString('base64');
|
|
|
|
storage.setField(id, 'nonce', nonce);
|
|
|
|
res.set('WWW-Authenticate', `send-v1 ${nonce}`);
|
|
|
|
|
2017-08-31 16:43:36 +00:00
|
|
|
const size = await storage.length(id);
|
|
|
|
const ttl = await storage.ttl(id);
|
|
|
|
res.send({
|
|
|
|
metadata: meta.metadata,
|
2018-01-30 20:15:09 +00:00
|
|
|
finalDownload: +meta.dl + 1 === +meta.dlimit,
|
2017-08-31 16:43:36 +00:00
|
|
|
size,
|
|
|
|
ttl
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
res.sendStatus(404);
|
|
|
|
}
|
|
|
|
};
|