2016-05-26 07:58:50 +00:00
|
|
|
/**
|
|
|
|
* 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');
|
2016-05-27 17:57:48 +00:00
|
|
|
const config = require('config');
|
2016-05-26 07:58:50 +00:00
|
|
|
const router = require('koa-router')();
|
2016-05-27 17:57:48 +00:00
|
|
|
const openpgp = require('openpgp');
|
|
|
|
const nodemailer = require('nodemailer');
|
2016-05-26 07:58:50 +00:00
|
|
|
const Mongo = require('./dao/mongo');
|
2016-06-01 06:59:25 +00:00
|
|
|
const Email = require('./email/email');
|
2016-05-28 13:17:46 +00:00
|
|
|
const UserId = require('./service/user-id');
|
|
|
|
const PublicKey = require('./service/public-key');
|
|
|
|
const HKP = require('./route/hkp');
|
|
|
|
const REST = require('./route/rest');
|
2016-05-26 07:58:50 +00:00
|
|
|
|
2016-05-27 17:57:48 +00:00
|
|
|
let mongo, email, userId, publicKey, hkp, rest;
|
2016-05-26 07:58:50 +00:00
|
|
|
|
|
|
|
//
|
2016-05-26 11:45:32 +00:00
|
|
|
// Configure koa HTTP server
|
2016-05-26 07:58:50 +00:00
|
|
|
//
|
|
|
|
|
2016-05-26 11:45:32 +00:00
|
|
|
// HKP routes
|
|
|
|
router.post('/pks/add', function *() { // no query params
|
|
|
|
yield hkp.add(this);
|
|
|
|
});
|
|
|
|
router.get('/pks/lookup', function *() { // ?op=get&search=0x1234567890123456
|
2016-05-26 07:58:50 +00:00
|
|
|
yield hkp.lookup(this);
|
|
|
|
});
|
2016-05-26 11:45:32 +00:00
|
|
|
|
|
|
|
// REST api routes
|
2016-05-27 17:57:48 +00:00
|
|
|
router.post('/api/v1/key', function *() { // { publicKeyArmored, primaryEmail } hint the primary email address
|
2016-05-26 11:45:32 +00:00
|
|
|
yield rest.create(this);
|
|
|
|
});
|
2016-05-28 08:49:26 +00:00
|
|
|
router.get('/api/v1/key', function *() { // ?keyid=keyid OR ?email=email
|
2016-05-26 11:45:32 +00:00
|
|
|
yield rest.read(this);
|
|
|
|
});
|
2016-05-28 08:49:26 +00:00
|
|
|
router.del('/api/v1/key', function *() { // ?keyid=keyid OR ?email=email
|
2016-05-26 11:45:32 +00:00
|
|
|
yield rest.remove(this);
|
|
|
|
});
|
|
|
|
|
|
|
|
// links for verification and sharing
|
2016-05-28 08:49:26 +00:00
|
|
|
router.get('/api/v1/verify', function *() { // ?keyid=keyid&nonce=nonce
|
2016-05-26 11:45:32 +00:00
|
|
|
yield rest.verify(this);
|
|
|
|
});
|
2016-05-28 08:49:26 +00:00
|
|
|
router.get('/api/v1/verifyRemove', function *() { // ?keyid=keyid&nonce=nonce
|
2016-05-26 11:45:32 +00:00
|
|
|
yield rest.verifyRemove(this);
|
|
|
|
});
|
|
|
|
router.get('/:email', function *() { // shorthand link for sharing
|
2016-05-27 17:57:48 +00:00
|
|
|
yield rest.share(this);
|
2016-05-26 11:45:32 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Set HTTP response headers
|
|
|
|
app.use(function *(next) {
|
|
|
|
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('Pragma', 'no-cache');
|
|
|
|
this.set('Connection', 'keep-alive');
|
|
|
|
yield next;
|
2016-05-26 07:58:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
app.use(router.routes());
|
|
|
|
app.use(router.allowedMethods());
|
2016-05-27 17:57:48 +00:00
|
|
|
|
|
|
|
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);
|
2016-05-27 17:57:48 +00:00
|
|
|
} else {
|
2016-05-28 13:37:29 +00:00
|
|
|
log.error('app', 'Unknown error', error, ctx);
|
2016-05-27 17:57:48 +00:00
|
|
|
}
|
|
|
|
});
|
2016-05-26 07:58:50 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Module initialization
|
|
|
|
//
|
|
|
|
|
|
|
|
function injectDependencies() {
|
|
|
|
let credentials = readCredentials();
|
|
|
|
mongo = new Mongo({
|
2016-05-29 14:47:45 +00:00
|
|
|
uri: process.env.MONGO_URI || credentials.mongo.uri,
|
|
|
|
user: process.env.MONGO_USER || credentials.mongo.user,
|
|
|
|
password: process.env.MONGO_PASS || credentials.mongo.pass
|
2016-05-26 07:58:50 +00:00
|
|
|
});
|
2016-05-27 17:57:48 +00:00
|
|
|
email = new Email(nodemailer);
|
2016-05-29 16:59:14 +00:00
|
|
|
email.init({
|
|
|
|
host: process.env.SMTP_HOST || credentials.smtp.host,
|
|
|
|
auth: {
|
|
|
|
user: process.env.SMTP_USER || credentials.smtp.user,
|
|
|
|
pass: process.env.SMTP_PASS || credentials.smtp.pass
|
|
|
|
},
|
|
|
|
sender: {
|
|
|
|
name: process.env.SENDER_NAME || credentials.sender.name,
|
|
|
|
email: process.env.SENDER_EMAIL || credentials.sender.email
|
|
|
|
}
|
|
|
|
});
|
2016-05-27 17:57:48 +00:00
|
|
|
userId = new UserId(mongo);
|
|
|
|
publicKey = new PublicKey(openpgp, mongo, email, userId);
|
2016-05-26 07:58:50 +00:00
|
|
|
hkp = new HKP(publicKey);
|
2016-05-30 14:06:52 +00:00
|
|
|
rest = new REST(publicKey, userId);
|
2016-05-26 07:58:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function readCredentials() {
|
|
|
|
try {
|
2016-05-27 17:57:48 +00:00
|
|
|
return require('../credentials.json');
|
2016-05-26 07:58:50 +00:00
|
|
|
} catch(e) {
|
2016-05-28 13:37:29 +00:00
|
|
|
log.info('app', 'No credentials.json found ... using environment vars.');
|
2016-05-26 07:58:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Start app ... connect to the database and start listening
|
|
|
|
//
|
|
|
|
|
2016-05-27 17:57:48 +00:00
|
|
|
if (!global.testing) { // don't automatically start server in tests
|
|
|
|
co(function *() {
|
|
|
|
let app = yield init();
|
|
|
|
app.listen(config.server.port);
|
2016-05-29 14:47:45 +00:00
|
|
|
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));
|
2016-05-27 17:57:48 +00:00
|
|
|
}
|
2016-05-26 07:58:50 +00:00
|
|
|
|
2016-05-27 17:57:48 +00:00
|
|
|
function *init() {
|
2016-05-28 21:31:25 +00:00
|
|
|
log.level = config.log.level; // set log level depending on process.env.NODE_ENV
|
2016-05-26 07:58:50 +00:00
|
|
|
injectDependencies();
|
2016-05-29 14:47:45 +00:00
|
|
|
log.info('app', 'Connecting to MongoDB ...');
|
2016-05-26 07:58:50 +00:00
|
|
|
yield mongo.connect();
|
2016-05-27 17:57:48 +00:00
|
|
|
return app;
|
|
|
|
}
|
2016-05-26 07:58:50 +00:00
|
|
|
|
2016-05-27 17:57:48 +00:00
|
|
|
module.exports = init;
|