2016-05-26 11:45:32 +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';
|
|
|
|
|
2016-05-27 17:57:48 +00:00
|
|
|
const parse = require('co-body');
|
2016-05-28 13:17:46 +00:00
|
|
|
const util = require('../service/util');
|
2016-05-27 17:57:48 +00:00
|
|
|
|
2016-05-26 11:45:32 +00:00
|
|
|
/**
|
|
|
|
* The REST api to provide additional functionality on top of HKP
|
|
|
|
*/
|
|
|
|
class REST {
|
|
|
|
|
2016-05-27 17:57:48 +00:00
|
|
|
/**
|
|
|
|
* Create an instance of the REST server
|
|
|
|
* @param {Object} publicKey An instance of the public key controller
|
|
|
|
*/
|
2016-05-26 11:45:32 +00:00
|
|
|
constructor(publicKey) {
|
|
|
|
this._publicKey = publicKey;
|
|
|
|
}
|
|
|
|
|
2016-05-27 17:57:48 +00:00
|
|
|
/**
|
|
|
|
* Public key upload via http POST
|
|
|
|
* @param {Object} ctx The koa request/response context
|
|
|
|
*/
|
2016-05-26 11:45:32 +00:00
|
|
|
*create(ctx) {
|
2016-05-29 14:47:45 +00:00
|
|
|
let body = yield parse.json(ctx, { limit: '1mb' });
|
|
|
|
let primaryEmail = body.primaryEmail;
|
|
|
|
let publicKeyArmored = body.publicKeyArmored;
|
|
|
|
if ((primaryEmail && !util.validateAddress(primaryEmail)) ||
|
|
|
|
!util.validatePublicKey(publicKeyArmored)) {
|
2016-05-27 17:57:48 +00:00
|
|
|
ctx.throw(400, 'Invalid request!');
|
|
|
|
}
|
2016-05-29 14:47:45 +00:00
|
|
|
let origin = util.getOrigin(ctx);
|
2016-05-30 13:36:32 +00:00
|
|
|
yield this._publicKey.put({ publicKeyArmored, primaryEmail, origin });
|
2016-05-29 16:59:14 +00:00
|
|
|
ctx.status = 201;
|
2016-05-26 11:45:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*verify(ctx) {
|
|
|
|
ctx.throw(501, 'Not implemented!');
|
|
|
|
yield;
|
|
|
|
}
|
|
|
|
|
2016-05-27 17:57:48 +00:00
|
|
|
/**
|
|
|
|
* Public key fetch via http GET
|
|
|
|
* @param {Object} ctx The koa request/response context
|
|
|
|
*/
|
2016-05-26 11:45:32 +00:00
|
|
|
*read(ctx) {
|
2016-05-27 17:57:48 +00:00
|
|
|
let q = { keyid:ctx.query.keyid, email:ctx.query.email };
|
|
|
|
if (!util.validateKeyId(q.keyid) && !util.validateAddress(q.email)) {
|
|
|
|
ctx.throw(400, 'Invalid request!');
|
|
|
|
}
|
|
|
|
ctx.body = yield this._publicKey.get(q);
|
2016-05-26 11:45:32 +00:00
|
|
|
}
|
|
|
|
|
2016-05-27 17:57:48 +00:00
|
|
|
/**
|
|
|
|
* Public key fetch via http GET (shorthand link for sharing)
|
|
|
|
* @param {Object} ctx The koa request/response context
|
|
|
|
*/
|
|
|
|
*share(ctx) {
|
|
|
|
let q = { email:ctx.params.email };
|
|
|
|
if (!util.validateAddress(q.email)) {
|
|
|
|
ctx.throw(400, 'Invalid request!');
|
|
|
|
}
|
|
|
|
ctx.body = (yield this._publicKey.get(q)).publicKeyArmored;
|
|
|
|
}
|
|
|
|
|
2016-05-26 11:45:32 +00:00
|
|
|
*remove(ctx) {
|
|
|
|
ctx.throw(501, 'Not implemented!');
|
|
|
|
yield;
|
|
|
|
}
|
|
|
|
|
|
|
|
*verifyRemove(ctx) {
|
|
|
|
ctx.throw(501, 'Not implemented!');
|
|
|
|
yield;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = REST;
|