Key upload and sending verification email works

This commit is contained in:
Tankred Hase 2016-05-29 18:59:14 +02:00
parent 3a6842c296
commit e2695aecc7
8 changed files with 43 additions and 12 deletions

View File

@ -21,6 +21,7 @@
"koa": "^1.2.0", "koa": "^1.2.0",
"koa-router": "^5.4.0", "koa-router": "^5.4.0",
"mongodb": "^2.1.20", "mongodb": "^2.1.20",
"node-uuid": "^1.4.7",
"nodemailer": "^2.4.2", "nodemailer": "^2.4.2",
"npmlog": "^2.0.4", "npmlog": "^2.0.4",
"openpgp": "^2.3.0" "openpgp": "^2.3.0"

View File

@ -101,6 +101,17 @@ function injectDependencies() {
password: process.env.MONGO_PASS || credentials.mongo.pass password: process.env.MONGO_PASS || credentials.mongo.pass
}); });
email = new Email(nodemailer); email = new Email(nodemailer);
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
}
});
userId = new UserId(mongo); userId = new UserId(mongo);
publicKey = new PublicKey(openpgp, mongo, email, userId); publicKey = new PublicKey(openpgp, mongo, email, userId);
hkp = new HKP(publicKey); hkp = new HKP(publicKey);

View File

@ -46,6 +46,7 @@ class HKP {
} }
let origin = util.getOrigin(ctx); let origin = util.getOrigin(ctx);
yield this._publicKey.put({ publicKeyArmored, origin }); yield this._publicKey.put({ publicKeyArmored, origin });
ctx.status = 201;
} }
/** /**

View File

@ -47,6 +47,7 @@ class REST {
} }
let origin = util.getOrigin(ctx); let origin = util.getOrigin(ctx);
yield this._publicKey({ publicKeyArmored, primaryEmail, origin }); yield this._publicKey({ publicKeyArmored, primaryEmail, origin });
ctx.status = 201;
} }
*verify(ctx) { *verify(ctx) {

View File

@ -62,7 +62,7 @@ class PublicKey {
// check for existing verfied key by id or email addresses // check for existing verfied key by id or email addresses
let verified = yield this._userid.getVerfied(params); let verified = yield this._userid.getVerfied(params);
if (verified) { if (verified) {
util.throw(304, 'Key for this user already exists: ' + verified.stringify()); util.throw(304, 'Key for this user already exists: ' + JSON.stringify(verified));
} }
// delete old/unverified key and user ids with the same key id // delete old/unverified key and user ids with the same key id
yield this.remove({ keyid:params.keyid }); yield this.remove({ keyid:params.keyid });
@ -74,7 +74,7 @@ class PublicKey {
// persist new user ids // persist new user ids
let userIds = yield this._userid.batch(params); let userIds = yield this._userid.batch(params);
// send mails to verify user ids (send only one if primary email is provided) // send mails to verify user ids (send only one if primary email is provided)
yield this._email.sendVerification({ userIds, primaryEmail, origin }); yield this._email.sendVerifyKey({ userIds, primaryEmail, origin });
} }
/** /**

View File

@ -17,6 +17,8 @@
'use strict'; 'use strict';
const uuid = require('node-uuid');
/** /**
* Database documents have the format: * Database documents have the format:
* { * {
@ -51,11 +53,16 @@ class UserId {
* @yield {Array} A list of user ids with generated nonces * @yield {Array} A list of user ids with generated nonces
*/ */
*batch(options) { *batch(options) {
options.userIds.forEach(u => u.keyid = options.keyid); // set keyid on docs let userIds = options.userIds, keyid = options.keyid;
let r = yield this._mongo.batch(options.userIds, DB_TYPE); userIds.forEach(u => {
if (r.insertedCount !== options.userIds.length) { u.keyid = keyid; // set keyid on docs
u.nonce = uuid.v4(); // generate nonce for verification
});
let r = yield this._mongo.batch(userIds, DB_TYPE);
if (r.insertedCount !== userIds.length) {
throw new Error('Failed to persist user ids'); throw new Error('Failed to persist user ids');
} }
return userIds;
} }
/** /**

View File

@ -37,12 +37,19 @@ describe.skip('Koa App (HTTP Server) Integration Tests', function() {
describe('HKP api', function() { describe('HKP api', function() {
describe('GET /pks/add', function() { describe('GET /pks/add', function() {
it('should return 200 for a valid request', function (done) { it.skip('should return 200 for a valid request', function (done) {
request(app.listen()) request(app.listen())
.get('/pks/lookup?op=get&search=0xDBC0B3D92B1B86E9') .get('/pks/lookup?op=get&search=0xDBC0B3D92B1B86E9')
.expect(200) .expect(200)
.end(done); .end(done);
}); });
it('should return 404 if not found', function (done) {
request(app.listen())
.get('/pks/lookup?op=get&search=0xDBC0B3D92A1B86E9')
.expect(404)
.end(done);
});
}); });
describe('POST /pks/add', function() { describe('POST /pks/add', function() {
@ -55,12 +62,12 @@ describe.skip('Koa App (HTTP Server) Integration Tests', function() {
.end(done); .end(done);
}); });
it('should return 200 for a valid PGP key', function (done) { it('should return 201 for a valid PGP key', function (done) {
request(app.listen()) request(app.listen())
.post('/pks/add') .post('/pks/add')
.type('form') .type('form')
.send('keytext=' + encodeURIComponent(pgpKey1)) .send('keytext=' + encodeURIComponent(pgpKey1))
.expect(200) .expect(201)
.end(done); .end(done);
}); });
}); });

View File

@ -25,12 +25,15 @@ describe('Email Integration Tests', function() {
} }
email = new Email(nodemailer); email = new Email(nodemailer);
email.init({ email.init({
host: credentials.smtp.host, host: process.env.SMTP_HOST || credentials.smtp.host,
auth: { auth: {
user: credentials.smtp.user, user: process.env.SMTP_USER || credentials.smtp.user,
pass: credentials.smtp.pass pass: process.env.SMTP_PASS || credentials.smtp.pass
}, },
sender: credentials.sender sender: {
name: process.env.SENDER_NAME || credentials.sender.name,
email: process.env.SENDER_EMAIL || credentials.sender.email
}
}); });
}); });