keyserver/src/app.js

141 lines
3.8 KiB
JavaScript
Raw Normal View History

/**
* Mailvelope - secure email with OpenPGP encryption for Webmail
* Copyright (C) 2016 Mailvelope GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License version 3
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
'use strict';
const co = require('co');
const app = require('koa')();
const log = require('npmlog');
const config = require('config');
const router = require('koa-router')();
2016-06-10 10:06:08 +00:00
const util = require('./service/util');
const Mongo = require('./dao/mongo');
const Email = require('./email/email');
2016-06-09 16:08:15 +00:00
const PGP = require('./service/pgp');
2016-05-28 13:17:46 +00:00
const PublicKey = require('./service/public-key');
const HKP = require('./route/hkp');
const REST = require('./route/rest');
2016-06-09 16:08:15 +00:00
const home = require('./route/home');
2016-06-09 16:08:15 +00:00
let mongo, email, pgp, publicKey, hkp, rest;
//
2016-05-26 11:45:32 +00:00
// Configure koa HTTP server
//
2016-05-26 11:45:32 +00:00
// HKP routes
2016-06-09 16:08:15 +00:00
router.post('/pks/add', function *() {
2016-05-26 11:45:32 +00:00
yield hkp.add(this);
});
2016-06-09 16:08:15 +00:00
router.get('/pks/lookup', function *() {
yield hkp.lookup(this);
});
2016-05-26 11:45:32 +00:00
// REST api routes
2016-06-09 16:08:15 +00:00
router.post('/api/v1/key', function *() {
2016-05-26 11:45:32 +00:00
yield rest.create(this);
});
2016-06-09 16:08:15 +00:00
router.get('/api/v1/key', function *() {
2016-05-26 11:45:32 +00:00
yield rest.read(this);
});
2016-06-09 16:08:15 +00:00
router.del('/api/v1/key', function *() {
2016-05-26 11:45:32 +00:00
yield rest.remove(this);
});
2016-06-09 16:08:15 +00:00
// links for verification, removal and sharing
router.get('/api/v1/verify', function *() {
2016-05-26 11:45:32 +00:00
yield rest.verify(this);
});
2016-06-09 16:08:15 +00:00
router.get('/api/v1/removeKey', function *() {
yield rest.remove(this);
});
router.get('/api/v1/verifyRemove', function *() {
2016-05-26 11:45:32 +00:00
yield rest.verifyRemove(this);
});
2016-06-09 16:08:15 +00:00
router.get('/user/:email', function *() {
yield rest.share(this);
2016-05-26 11:45:32 +00:00
});
2016-06-09 16:08:15 +00:00
// display homepage
router.get('/', home);
2016-05-26 11:45:32 +00:00
// Set HTTP response headers
app.use(function *(next) {
2016-06-07 18:43:09 +00:00
this.set('Strict-Transport-Security', 'max-age=16070400');
2016-05-26 11:45:32 +00:00
this.set('Access-Control-Allow-Origin', '*');
this.set('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
this.set('Access-Control-Allow-Headers', 'Content-Type');
this.set('Cache-Control', 'no-cache');
this.set('Connection', 'keep-alive');
yield next;
});
2016-06-07 18:43:09 +00:00
// Redirect all http traffic to https
app.use(function *(next) {
2016-06-10 10:06:08 +00:00
if (config.server.upgradeHTTP && util.checkHTTP(this)) {
2016-06-07 18:43:09 +00:00
this.redirect('https://' + this.hostname + this.url);
} else {
yield next;
}
});
app.use(router.routes());
app.use(router.allowedMethods());
app.on('error', (error, ctx) => {
if (error.status) {
2016-05-28 13:37:29 +00:00
log.verbose('app', 'Request faild: %s, %s', error.status, error.message);
} else {
2016-05-28 13:37:29 +00:00
log.error('app', 'Unknown error', error, ctx);
}
});
//
// Module initialization
//
function injectDependencies() {
2016-06-09 16:08:15 +00:00
mongo = new Mongo();
email = new Email();
pgp = new PGP();
publicKey = new PublicKey(pgp, mongo, email);
hkp = new HKP(publicKey);
2016-06-09 16:08:15 +00:00
rest = new REST(publicKey);
}
//
// Start app ... connect to the database and start listening
//
if (!global.testing) { // don't automatically start server in tests
co(function *() {
let app = yield init();
app.listen(config.server.port);
log.info('app', 'Ready to rock! Listening on http://localhost:' + config.server.port);
2016-05-28 13:37:29 +00:00
}).catch(err => log.error('app', 'Initialization failed!', err));
}
function *init() {
2016-05-28 21:31:25 +00:00
log.level = config.log.level; // set log level depending on process.env.NODE_ENV
injectDependencies();
2016-06-09 16:08:15 +00:00
email.init(config.email);
log.info('app', 'Connecting to MongoDB ...');
2016-06-09 16:08:15 +00:00
yield mongo.init(config.mongo);
return app;
}
module.exports = init;