|
|
@@ -2,9 +2,11 @@
|
|
|
const whiskers = require('whiskers');
|
|
|
const fs = require('fs');
|
|
|
|
|
|
+const ApiKeyModel = require('../models/apiKey.js').ApiKeyModel;
|
|
|
const PasteContent = require('../models/pasteContent.js').PasteContent;
|
|
|
const mCrypto = require('../src/crypto.js');
|
|
|
const Security = require('../src/security.js');
|
|
|
+const CONFIG = require('../src/config.js');
|
|
|
|
|
|
async function renderRawPage(app, req, res, entity) {
|
|
|
if (entity.type === 'paste')
|
|
|
@@ -23,6 +25,7 @@ async function renderPublicPage(app, req, res, entity) {
|
|
|
return await app.routerUtils.staticServe(res, app.getData(entity.privId));
|
|
|
else if (entity.type === 'file') {
|
|
|
let data = JSON.parse(entity.data);
|
|
|
+ // FIXME viewer
|
|
|
return await app.routerUtils.staticDownload(res, app.getData(entity.privId), data.name, data.type);
|
|
|
}
|
|
|
else if (entity.type === 'short')
|
|
|
@@ -65,13 +68,21 @@ module.exports = { register: app => {
|
|
|
res.end(whiskers.render(require('../templates/pastit.js'), context));
|
|
|
});
|
|
|
app.router.post("/pastit", async (req, res) => {
|
|
|
- const content = req.body.content;
|
|
|
+ const content = "" + (req.body.content || req.post);
|
|
|
const privId = mCrypto.string(content);
|
|
|
- const captchaOk = await Security.captchaCheck(req.body['g-recaptcha-response'], req.headers['x-forwarded-for'] || req.socket.remoteAddress);
|
|
|
+
|
|
|
+ if (req.body['g-recaptcha-response']) {
|
|
|
+ const captchaOk = await Security.captchaCheck(req.body['g-recaptcha-response'], req.headers['x-forwarded-for'] || req.socket.remoteAddress);
|
|
|
+ if (!captchaOk)
|
|
|
+ return app.routerUtils.jsonResponse(res, { err: "Invalid captcha input", id: null });
|
|
|
+ } else if (req.body['apiKey']) {
|
|
|
+ if (!(await app.databaseHelper.findOne(ApiKeyModel, { apiKey: req.body['apiKey'] })))
|
|
|
+ return app.routerUtils.jsonResponse(res, { err: "Unauthorized access", id: null });
|
|
|
+ } else {
|
|
|
+ return app.routerUtils.jsonResponse(res, { err: "Unauthorized access", id: null });
|
|
|
+ }
|
|
|
let entity = await app.databaseHelper.findOne(PasteContent, { privId: privId });
|
|
|
|
|
|
- if (!captchaOk)
|
|
|
- return app.routerUtils.jsonResponse(res, { err: "Invalid captcha input", id: null });
|
|
|
if (!content || !content.length)
|
|
|
return app.routerUtils.jsonResponse(res, { err: "Empty input", id: null });
|
|
|
if (entity && !entity.expired) {
|
|
|
@@ -80,11 +91,15 @@ module.exports = { register: app => {
|
|
|
} else {
|
|
|
entity = entity || new PasteContent(privId, "paste");
|
|
|
entity.expired = false;
|
|
|
+ entity.apiKey = req.body['apiKey'] || null;
|
|
|
entity.renew();
|
|
|
fs.writeFileSync(app.getData(privId), content);
|
|
|
await app.databaseHelper.upsertOne(entity);
|
|
|
}
|
|
|
- app.routerUtils.jsonResponse(res, { err: null, id: entity.publicId });
|
|
|
+ if (req.body.apiKey)
|
|
|
+ res.end(CONFIG.url+"/x/" +entity.publicId+"\r\n");
|
|
|
+ else
|
|
|
+ app.routerUtils.jsonResponse(res, { err: null, id: entity.publicId });
|
|
|
});
|
|
|
|
|
|
// URL shortener tool
|
|
|
@@ -94,7 +109,7 @@ module.exports = { register: app => {
|
|
|
res.end(whiskers.render(require('../templates/short.js'), context));
|
|
|
});
|
|
|
app.router.post("/short", async (req, res) => {
|
|
|
- const link = req.body.content;
|
|
|
+ const link = "" + req.body.content;
|
|
|
const privId = mCrypto.string(await app.databaseHelper.count(PasteContent) + link);
|
|
|
const captchaOk = await Security.captchaCheck(req.body['g-recaptcha-response'], req.headers['x-forwarded-for'] || req.socket.remoteAddress);
|
|
|
|
|
|
@@ -129,5 +144,23 @@ module.exports = { register: app => {
|
|
|
await app.databaseHelper.insertOne(entity);
|
|
|
app.routerUtils.jsonResponse(res, { err: null, id: entity.privId });
|
|
|
});
|
|
|
+
|
|
|
+ // API page
|
|
|
+ app.router.get("/api", (req, res) => {
|
|
|
+ let context = app.routerUtils.commonRenderInfos();
|
|
|
+ context.page_title += " - API Usage";
|
|
|
+ res.end(whiskers.render(require('../templates/api.js'), context));
|
|
|
+ });
|
|
|
+ app.router.post("/api", async (req, res) => {
|
|
|
+ const ipAddress = req.headers['x-forwarded-for'] || req.socket.remoteAddress;
|
|
|
+ const captchaOk = await Security.captchaCheck(req.body['g-recaptcha-response'], ipAddress);
|
|
|
+
|
|
|
+ if (!captchaOk)
|
|
|
+ return app.routerUtils.jsonResponse(res, { err: "Invalid captcha input", id: null });
|
|
|
+ const privKey = mCrypto.string(Date.now() + "" + await app.databaseHelper.count(ApiKeyModel) + "SALT_INPUT_API_KEY" +ipAddress);
|
|
|
+ const model = new ApiKeyModel(privKey, ipAddress);
|
|
|
+ await app.databaseHelper.insertOne(model);
|
|
|
+ return app.routerUtils.jsonResponse(res, { err: null, id: privKey });
|
|
|
+ });
|
|
|
}};
|
|
|
|