Merge branch 'release/v1.6.10' of hardi/opt-cc into master

pull/28/head
hardi 2018-02-04 16:38:57 +01:00 committed by HardiReady
commit ea2c3e34ba
17 changed files with 1653 additions and 328 deletions

View File

@ -6,6 +6,10 @@ module.exports = {
db: 'cc', db: 'cc',
}, },
prod: {
env: 'production'
},
dev: { dev: {
env: 'dev' env: 'dev'
}, },

View File

@ -6,8 +6,14 @@ const {exec} = require('child_process');
const UserModel = require('../models/user'); const UserModel = require('../models/user');
const signatureTool = require('../tools/signature-tool'); const signatureTool = require('../tools/signature-tool');
const debug = require('debug');
const error = debug('cc:cronjob:err');
const logger = debug('cc:cronjob');
logger.log = console.log.bind(console);
const createAllSignatures = () => { const createAllSignatures = () => {
console.log('\x1b[35m%s\x1b[0m', new Date().toLocaleString() logger('\x1b[35m%s\x1b[0m', new Date().toLocaleString()
+ ': cron job started - UPDATE SIGNATURES'); + ': cron job started - UPDATE SIGNATURES');
// mock response // mock response
@ -25,31 +31,31 @@ const createAllSignatures = () => {
if (!err || (err && err.message.startsWith('Fraction not defined'))) { if (!err || (err && err.message.startsWith('Fraction not defined'))) {
callback() callback()
} else { } else {
console.error('\x1b[41m%s\x1b[0m', new Date().toLocaleString() error('\x1b[41m%s\x1b[0m', new Date().toLocaleString()
+ ': Error in execution - UPDATE SIGNATURES: ' + err) + ': Error in execution - UPDATE SIGNATURES: ' + err.message)
} }
}; };
signatureTool(user._id, res, next) signatureTool(user._id, res, next)
}, () => { }, () => {
if (err) { if (err) {
console.error('\x1b[41m%s\x1b[0m', new Date().toLocaleString() error('\x1b[41m%s\x1b[0m', new Date().toLocaleString()
+ ': Error in execution - UPDATE SIGNATURES: ' + err) + ': Error in execution - UPDATE SIGNATURES: ' + err.message)
} }
console.log('\x1b[35m%s\x1b[0m', new Date().toLocaleString() logger('\x1b[35m%s\x1b[0m', new Date().toLocaleString()
+ ': finished successful - UPDATE SIGNATURES'); + ': finished successful - UPDATE SIGNATURES');
}) })
}); });
}; };
const createBackup = () => { const createBackup = () => {
console.log('\x1b[35m%s\x1b[0m', new Date().toLocaleString() logger('\x1b[35m%s\x1b[0m', new Date().toLocaleString()
+ ': cron job started - CREATE BACKUP'); + ': cron job started - CREATE BACKUP');
exec(__dirname + '/../../backup/backup.sh', (error, stdout, stderr) => { exec(__dirname + '/../../backup/backup.sh', (err, stdout, stderr) => {
if (error) { if (err) {
console.log(error); error(err.message);
} }
console.log('\x1b[32m%s\x1b[0m',stderr); logger('\x1b[32m%s\x1b[0m',stderr);
console.log('\x1b[35m%s\x1b[0m', new Date().toLocaleString() logger('\x1b[35m%s\x1b[0m', new Date().toLocaleString()
+ ': cron job finished - CREATE BACKUP'); + ': cron job finished - CREATE BACKUP');
}) })
}; };

View File

@ -12,8 +12,10 @@ const apiAuthentication = (req, res, next) => {
// decode token // decode token
if (token) { if (token) {
const secret = process.env.NODE_ENV === config.prod.env ? process.env.JWS_SECRET : 'dev-secret';
// verifies secret and checks exp // verifies secret and checks exp
jwt.verify(token, config.secret, (err, decoded) => { jwt.verify(token, secret, (err, decoded) => {
if (err) { if (err) {
return res.status(403).json({success: false, message: 'Failed to authenticate token.'}); return res.status(403).json({success: false, message: 'Failed to authenticate token.'});
} else { } else {

View File

@ -0,0 +1,20 @@
"use strict";
// HTTP status codes by name
const codes = require('../routes/http-codes');
/**
* check if id has valid UUID format
*/
const idValidator = (req, res, next) => {
const reqId = req.params.id;
if (!reqId.match(/^[0-9a-fA-F]{24}$/)) {
const err = new Error("Invalid request id format");
err.status = codes.notfound;
return next(err);
}
next();
};
exports.idValidator = idValidator;

1824
api/package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -6,8 +6,8 @@
"author": "Florian Hartwich <hardi@noarch.de>", "author": "Florian Hartwich <hardi@noarch.de>",
"private": true, "private": true,
"scripts": { "scripts": {
"start": "NODE_ENV=production node server.js", "start": "DEBUG='cc:*' NODE_ENV=production node server.js",
"dev": "NODE_ENV=dev nodemon server.js", "dev": "DEBUG='cc:*' NODE_ENV=dev nodemon server.js",
"test": "mocha --require ./test/config/spec_helper.js", "test": "mocha --require ./test/config/spec_helper.js",
"e2e": "NODE_ENV=test node server.js" "e2e": "NODE_ENV=test node server.js"
}, },
@ -17,25 +17,26 @@
"body-parser": "~1.13.2", "body-parser": "~1.13.2",
"cors": "^2.8.4", "cors": "^2.8.4",
"cron": "^1.3.0", "cron": "^1.3.0",
"debug": "~2.2.0", "debug": "^3.1.0",
"express": "^4.16.1", "express": "^4.16.2",
"imagemin": "^5.2.2", "imagemin": "^5.2.2",
"imagemin-pngquant": "^5.0.0", "imagemin-pngquant": "^5.0.0",
"jimp": "^0.2.27", "jimp": "^0.2.27",
"jsonwebtoken": "^7.4.3", "jsonwebtoken": "^7.4.3",
"lodash": "^4.17.4", "lodash": "^4.17.4",
"mkdirp": "^0.5.1", "mkdirp": "^0.5.1",
"mongoose": "^4.12.0", "mongoose": "^5.0.3",
"morgan": "~1.6.1", "morgan": "~1.6.1",
"multer": "^1.3.0", "multer": "^1.3.0",
"node-sha1": "^1.0.1", "node-sha1": "^1.0.1",
"q": "^1.5.0", "q": "^1.5.0",
"serve-favicon": "~2.3.0" "serve-favicon": "~2.3.0",
"supports-color": "^5.1.0"
}, },
"devDependencies": { "devDependencies": {
"chai": "^3.5.0", "chai": "^3.5.0",
"chai-http": "^3.0.0", "chai-http": "^3.0.0",
"mocha": "^3.5.3", "mocha": "^3.5.3",
"nodemon": "^1.12.1" "nodemon": "^1.14.12"
} }
} }

View File

@ -8,12 +8,11 @@ const Q = require('q');
const _ = require('lodash'); const _ = require('lodash');
const logger = require('debug')('cc:authenticate'); const logger = require('debug')('cc:authenticate');
const apiAuthenticationMiddleware = require('../middleware/auth-middleware');
const checkAdmin = require('../middleware/permission-check').checkAdmin;
// HTTP status codes by name // HTTP status codes by name
const codes = require('./http-codes'); const codes = require('./http-codes');
const config = require('../config/config');
const routerHandling = require('../middleware/router-handling'); const routerHandling = require('../middleware/router-handling');
const AppUserModel = require('../models/app-user'); const AppUserModel = require('../models/app-user');
@ -55,7 +54,10 @@ let authCheck = (username, password, res) => {
} }
if (user && user.activated && bcrypt.compareSync(password, user.password)) { if (user && user.activated && bcrypt.compareSync(password, user.password)) {
// authentication successful // authentication successful
var secret = process.env.JWS_SECRET; const secret = process.env.NODE_ENV === config.prod.env ? process.env.JWS_SECRET : 'dev-secret';
console.log(secret)
deferred.resolve({ deferred.resolve({
_id: user._id, _id: user._id,
username: user.username, username: user.username,

View File

@ -7,10 +7,12 @@ const logger = require('debug')('cc:campaigns');
// HTTP status codes by name // HTTP status codes by name
const codes = require('./http-codes'); const codes = require('./http-codes');
const routerHandling = require('../middleware/router-handling');
const apiAuthenticationMiddleware = require('../middleware/auth-middleware'); const apiAuthenticationMiddleware = require('../middleware/auth-middleware');
const checkMT = require('../middleware/permission-check').checkMT; const checkMT = require('../middleware/permission-check').checkMT;
const routerHandling = require('../middleware/router-handling');
const idValidator = require('../middleware/validators').idValidator;
// Mongoose Model using mongoDB // Mongoose Model using mongoDB
const CampaignModel = require('../models/campaign'); const CampaignModel = require('../models/campaign');
const WarModel = require('../models/war'); const WarModel = require('../models/war');
@ -41,7 +43,7 @@ campaigns.route('/')
); );
campaigns.route('/:id') campaigns.route('/:id')
.get((req, res, next) => { .get(idValidator, (req, res, next) => {
CampaignModel.findById(req.params.id, (err, item) => { CampaignModel.findById(req.params.id, (err, item) => {
if (err) { if (err) {
err.status = codes.servererror; err.status = codes.servererror;

View File

@ -13,7 +13,9 @@ const codes = require('./http-codes');
const apiAuthenticationMiddleware = require('../middleware/auth-middleware'); const apiAuthenticationMiddleware = require('../middleware/auth-middleware');
const checkHl = require('../middleware/permission-check').checkHl; const checkHl = require('../middleware/permission-check').checkHl;
const routerHandling = require('../middleware/router-handling'); const routerHandling = require('../middleware/router-handling');
const idValidator = require('../middleware/validators').idValidator;
// Mongoose Model using mongoDB // Mongoose Model using mongoDB
const DecorationModel = require('../models/decoration'); const DecorationModel = require('../models/decoration');
@ -71,7 +73,7 @@ decoration.route('/')
); );
decoration.route('/:id') decoration.route('/:id')
.get((req, res, next) => { .get(idValidator, (req, res, next) => {
DecorationModel.findById(req.params.id, (err, item) => { DecorationModel.findById(req.params.id, (err, item) => {
if (err) { if (err) {
err.status = codes.servererror; err.status = codes.servererror;

View File

@ -13,16 +13,15 @@ const codes = require('./http-codes');
const apiAuthenticationMiddleware = require('../middleware/auth-middleware'); const apiAuthenticationMiddleware = require('../middleware/auth-middleware');
const checkHl = require('../middleware/permission-check').checkHl; const checkHl = require('../middleware/permission-check').checkHl;
const routerHandling = require('../middleware/router-handling'); const routerHandling = require('../middleware/router-handling');
const idValidator = require('../middleware/validators').idValidator;
// Mongoose Model using mongoDB // Mongoose Model using mongoDB
const RankModel = require('../models/rank'); const RankModel = require('../models/rank');
const ranks = express.Router(); const ranks = express.Router();
// add middleware for bonus tasks 4 and 5 to find filter and offset/limit params for GET / and GET /:id
// routes ********************** // routes **********************
ranks.route('/') ranks.route('/')
.get((req, res, next) => { .get((req, res, next) => {
@ -74,7 +73,7 @@ ranks.route('/')
ranks.route('/:id') ranks.route('/:id')
.get((req, res, next) => { .get(idValidator, (req, res, next) => {
RankModel.findById(req.params.id, (err, item) => { RankModel.findById(req.params.id, (err, item) => {
if (err) { if (err) {
err.status = codes.servererror; err.status = codes.servererror;

View File

@ -16,6 +16,7 @@ const signatures = express.Router();
// routes ********************** // routes **********************
signatures.route('/:id') signatures.route('/:id')
// does not use idValidator since it works by username
.get((req, res, next) => { .get((req, res, next) => {
// decode UTF8-escape sequences (special characters) // decode UTF8-escape sequences (special characters)
const uri = decodeURIComponent(req.params.id); const uri = decodeURIComponent(req.params.id);

View File

@ -13,7 +13,9 @@ const codes = require('./http-codes');
const apiAuthenticationMiddleware = require('../middleware/auth-middleware'); const apiAuthenticationMiddleware = require('../middleware/auth-middleware');
const checkHl = require('../middleware/permission-check').checkHl; const checkHl = require('../middleware/permission-check').checkHl;
const routerHandling = require('../middleware/router-handling'); const routerHandling = require('../middleware/router-handling');
const idValidator = require('../middleware/validators').idValidator;
// Mongoose Model using mongoDB // Mongoose Model using mongoDB
const SquadModel = require('../models/squad'); const SquadModel = require('../models/squad');
@ -74,7 +76,7 @@ squads.route('/')
); );
squads.route('/:id') squads.route('/:id')
.get((req, res, next) => { .get(idValidator, (req, res, next) => {
SquadModel.findById(req.params.id, (err, item) => { SquadModel.findById(req.params.id, (err, item) => {
if (err) { if (err) {
err.status = codes.servererror; err.status = codes.servererror;

View File

@ -10,9 +10,11 @@ const codes = require('./http-codes');
const apiAuthenticationMiddleware = require('../middleware/auth-middleware'); const apiAuthenticationMiddleware = require('../middleware/auth-middleware');
const checkHl = require('../middleware/permission-check').checkHl; const checkHl = require('../middleware/permission-check').checkHl;
const offsetlimitMiddleware = require('../middleware/limitoffset-middleware-mongo'); const offsetlimitMiddleware = require('../middleware/limitoffset-middleware-mongo');
const filterHandlerCreator = require('../middleware/filter-handler-mongo'); const filterHandlerCreator = require('../middleware/filter-handler-mongo');
const routerHandling = require('../middleware/router-handling'); const routerHandling = require('../middleware/router-handling');
const idValidator = require('../middleware/validators').idValidator;
// Mongoose Model using mongoDB // Mongoose Model using mongoDB
const UserModel = require('../models/user'); const UserModel = require('../models/user');
@ -86,7 +88,7 @@ users.route('/')
users.route('/:id') users.route('/:id')
.get((req, res, next) => { .get(idValidator, (req, res, next) => {
UserModel.findById(req.params.id).populate('squadId').exec((err, user) => { UserModel.findById(req.params.id).populate('squadId').exec((err, user) => {
if (err) { if (err) {
err.status = codes.servererror; err.status = codes.servererror;

View File

@ -12,10 +12,14 @@ const logger = require('debug')('cc:wars');
// HTTP status codes by name // HTTP status codes by name
const codes = require('./http-codes'); const codes = require('./http-codes');
// access check
const apiAuthenticationMiddleware = require('../middleware/auth-middleware'); const apiAuthenticationMiddleware = require('../middleware/auth-middleware');
const checkMT = require('../middleware/permission-check').checkMT; const checkMT = require('../middleware/permission-check').checkMT;
const routerHandling = require('../middleware/router-handling');
const routerHandling = require('../middleware/router-handling');
const idValidator = require('../middleware/validators').idValidator;
// log paser tool
const parseWarLog = require('../tools/log-parse-tool'); const parseWarLog = require('../tools/log-parse-tool');
// Mongoose Model using mongoDB // Mongoose Model using mongoDB
@ -139,7 +143,7 @@ wars.route('/')
); );
wars.route('/:id') wars.route('/:id')
.get((req, res, next) => { .get(idValidator, (req, res, next) => {
WarModel.findById(req.params.id, (err, item) => { WarModel.findById(req.params.id, (err, item) => {
if (err) { if (err) {
err.status = codes.servererror; err.status = codes.servererror;

View File

@ -5,7 +5,12 @@ const path = require('path');
const favicon = require('serve-favicon'); const favicon = require('serve-favicon');
const bodyParser = require('body-parser'); const bodyParser = require('body-parser');
const requestLogger = require('morgan'); const requestLogger = require('morgan');
const debug = require('debug')('cc:server');
const debug = require('debug');
const error = debug('cc:server:err');
const logger = debug('cc:server');
logger.log = console.log.bind(console);
const cors = require('cors') const cors = require('cors')
const mongoose = require('mongoose'); const mongoose = require('mongoose');
@ -93,44 +98,29 @@ app.use(function (req, res) {
res.sendFile("public/index.html", {root: __dirname + '/..'}); res.sendFile("public/index.html", {root: __dirname + '/..'});
}); });
// (from express-generator boilerplate standard code)
// Errorhandling and requests without proper URLs ************************
// catch 404 and forward to error handler
app.use((req, res, next) => {
debug('Catching unmatched request to answer with 404');
let err = new Error('Not Found');
err.status = 404;
next(err);
});
// register error handlers // register error handlers
errorResponseWare(app); errorResponseWare(app);
// Start the server // Start the server
if (process.env.NODE_ENV !== config.test.env) { if (process.env.NODE_ENV !== config.test.env) {
const mongoosePromise = mongoose.connect(config.database.uri + config.database.db, { const mongoosePromise = mongoose.connect(config.database.uri + config.database.db);
useMongoClient: true
});
mongoosePromise.then((db) => { mongoosePromise.then((db) => {
app.listen(config.port, (err) => { app.listen(config.port, (err) => {
if (err !== undefined) { if (err !== undefined) {
debug('Error on startup, ', err); error('Error on startup, ', err);
} }
else { else {
debug('Listening on port ' + config.port); logger('Listening on port ' + config.port);
signatureCronJob.start(); signatureCronJob.start();
backupCronJob.start(); backupCronJob.start();
} }
}); });
}) })
} else { } else {
const mongoosePromise = mongoose.connect(config.database.uri + config.test.db, { const mongoosePromise = mongoose.connect(config.database.uri + config.test.db);
useMongoClient: true
});
mongoosePromise.then((db) => { mongoosePromise.then((db) => {
app.listen(config.test.port); app.listen(config.test.port);
console.log('Listening on port ' + config.test.port); logger('Listening on port ' + config.test.port);
}) })
} }

View File

@ -23,7 +23,7 @@ do
# provide date for restore process, if data import is needed # provide date for restore process, if data import is needed
if [ -z "$1" ] if [ -z "$1" ]
then then
echo "table ${i}" (>&2 echo -e "$(date "+%Y-%m-%dT%T.%3N%z")\tTable: ${i}")
mongoexport --db cc --collection $i --out ${DATE}/collections/${i}.json; mongoexport --db cc --collection $i --out ${DATE}/collections/${i}.json;
else else
mongoimport --db cc --collection $i --drop --file ${1}/collections/${i}.json mongoimport --db cc --collection $i --drop --file ${1}/collections/${i}.json

View File

@ -1,6 +1,6 @@
{ {
"name": "opt-cc", "name": "opt-cc",
"version": "1.6.9", "version": "1.6.10",
"author": "Florian Hartwich <hardi@noarch.de>", "author": "Florian Hartwich <hardi@noarch.de>",
"private": true, "private": true,
"scripts": { "scripts": {