Check monetizejs token

This commit is contained in:
benweet 2014-08-19 00:30:45 +01:00
parent 2339543fd4
commit 7e0444524a
3 changed files with 93 additions and 73 deletions

View File

@ -8,12 +8,10 @@ app.engine('html', require('ejs').renderFile);
// Force HTTPS on stackedit.io
app.all('*', function(req, res, next) {
if (req.headers.host == 'stackedit.io' && req.headers['x-forwarded-proto'] != 'https') {
res.redirect('https://stackedit.io' + req.url);
return res.redirect('https://stackedit.io' + req.url);
}
else {
/\.(eot|ttf|woff)$/.test(req.url) && res.header('Access-Control-Allow-Origin', '*');
next();
}
});
// Use gzip compression

View File

@ -1,5 +1,8 @@
var spawn = require('child_process').spawn;
var fs = require('fs');
var path = require('path');
var os = require('os');
var request = require('request');
function waitForJavaScript() {
if(window.MathJax) {
@ -37,6 +40,34 @@ var authorizedPageSizes = [
];
module.exports = function(req, res, next) {
function onError(err) {
next(err);
}
function onUnknownError() {
res.statusCode = 400;
res.end('Unknown error');
}
function onUnauthorizedError() {
res.statusCode = 401;
res.end('Unauthorized');
}
function onTimeout() {
res.statusCode = 408;
res.end('Request timeout');
}
request({
uri: 'https://monetizejs.com/api/payments',
qs: {
access_token: req.query.token
},
json: true
}, function (err, paymentsRes, payments) {
var authorized = payments && payments.app == 'ESTHdCYOi18iLhhO' && (
(payments.chargeOption && payments.chargeOption.alias == 'once') ||
(payments.subscriptionOption && payments.subscriptionOption.alias == 'yearly'));
if(err || paymentsRes.statusCode != 200 || !authorized) {
return onUnauthorizedError();
}
var options, params = [];
try {
options = JSON.parse(req.query.options);
@ -72,8 +103,8 @@ module.exports = function(req, res, next) {
// Page size
params.push('--page-size', authorizedPageSizes.indexOf(options.pageSize) === -1 ? 'A4' : options.pageSize);
// wkhtmltopdf can't access /dev/stdout on Amazon EC2 for some reason
var filePath = '/tmp/' + Date.now() + '.pdf';
// Use a temp file as wkhtmltopdf can't access /dev/stdout on Amazon EC2 for some reason
var filePath = path.join(os.tmpDir(), Date.now() + '.pdf');
var binPath = process.env.WKHTMLTOPDF_PATH || 'wkhtmltopdf';
params.push('--run-script', waitForJavaScript.toString() + 'waitForJavaScript()');
params.push('--window-status', 'done');
@ -84,17 +115,6 @@ module.exports = function(req, res, next) {
'ignore'
]
});
function onError(err) {
next(err);
}
function onUnknownError() {
res.statusCode = 400;
res.end('Unknown error');
}
function onTimeout() {
res.statusCode = 408;
res.end('Request timeout');
}
var timeoutId = setTimeout(function() {
timeoutId = undefined;
wkhtmltopdf.kill();
@ -120,4 +140,5 @@ module.exports = function(req, res, next) {
readStream.on('error', onUnknownError);
});
req.pipe(wkhtmltopdf.stdin);
});
};

View File

@ -11,8 +11,9 @@
"doc": "doc"
},
"dependencies": {
"express": "3.x",
"ejs": "~0.8.4"
"express": "~3.16.6",
"ejs": "~0.8.4",
"request": "~2.40.0"
},
"devDependencies": {
"grunt-contrib-requirejs": "~0.4.3",