From e2695aecc745941ef58853470b0677c948240c2d Mon Sep 17 00:00:00 2001 From: Tankred Hase Date: Sun, 29 May 2016 18:59:14 +0200 Subject: [PATCH] Key upload and sending verification email works --- package.json | 1 + src/app.js | 11 +++++++++++ src/route/hkp.js | 1 + src/route/rest.js | 1 + src/service/public-key.js | 4 ++-- src/service/user-id.js | 13 ++++++++++--- test/integration/app-test.js | 13 ++++++++++--- test/integration/email-test.js | 11 +++++++---- 8 files changed, 43 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index 7d26e98..d54fabf 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ "koa": "^1.2.0", "koa-router": "^5.4.0", "mongodb": "^2.1.20", + "node-uuid": "^1.4.7", "nodemailer": "^2.4.2", "npmlog": "^2.0.4", "openpgp": "^2.3.0" diff --git a/src/app.js b/src/app.js index 714ec80..49665bd 100644 --- a/src/app.js +++ b/src/app.js @@ -101,6 +101,17 @@ function injectDependencies() { password: process.env.MONGO_PASS || credentials.mongo.pass }); 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); publicKey = new PublicKey(openpgp, mongo, email, userId); hkp = new HKP(publicKey); diff --git a/src/route/hkp.js b/src/route/hkp.js index b953df2..9a440b8 100644 --- a/src/route/hkp.js +++ b/src/route/hkp.js @@ -46,6 +46,7 @@ class HKP { } let origin = util.getOrigin(ctx); yield this._publicKey.put({ publicKeyArmored, origin }); + ctx.status = 201; } /** diff --git a/src/route/rest.js b/src/route/rest.js index 5b9555f..a2eaae9 100644 --- a/src/route/rest.js +++ b/src/route/rest.js @@ -47,6 +47,7 @@ class REST { } let origin = util.getOrigin(ctx); yield this._publicKey({ publicKeyArmored, primaryEmail, origin }); + ctx.status = 201; } *verify(ctx) { diff --git a/src/service/public-key.js b/src/service/public-key.js index 367cc06..df2c49d 100644 --- a/src/service/public-key.js +++ b/src/service/public-key.js @@ -62,7 +62,7 @@ class PublicKey { // check for existing verfied key by id or email addresses let verified = yield this._userid.getVerfied(params); 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 yield this.remove({ keyid:params.keyid }); @@ -74,7 +74,7 @@ class PublicKey { // persist new user ids let userIds = yield this._userid.batch(params); // 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 }); } /** diff --git a/src/service/user-id.js b/src/service/user-id.js index 365d428..48f5913 100644 --- a/src/service/user-id.js +++ b/src/service/user-id.js @@ -17,6 +17,8 @@ 'use strict'; +const uuid = require('node-uuid'); + /** * Database documents have the format: * { @@ -51,11 +53,16 @@ class UserId { * @yield {Array} A list of user ids with generated nonces */ *batch(options) { - options.userIds.forEach(u => u.keyid = options.keyid); // set keyid on docs - let r = yield this._mongo.batch(options.userIds, DB_TYPE); - if (r.insertedCount !== options.userIds.length) { + let userIds = options.userIds, keyid = options.keyid; + userIds.forEach(u => { + 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'); } + return userIds; } /** diff --git a/test/integration/app-test.js b/test/integration/app-test.js index 8738383..5b7c514 100644 --- a/test/integration/app-test.js +++ b/test/integration/app-test.js @@ -37,12 +37,19 @@ describe.skip('Koa App (HTTP Server) Integration Tests', function() { describe('HKP api', 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()) .get('/pks/lookup?op=get&search=0xDBC0B3D92B1B86E9') .expect(200) .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() { @@ -55,12 +62,12 @@ describe.skip('Koa App (HTTP Server) Integration Tests', function() { .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()) .post('/pks/add') .type('form') .send('keytext=' + encodeURIComponent(pgpKey1)) - .expect(200) + .expect(201) .end(done); }); }); diff --git a/test/integration/email-test.js b/test/integration/email-test.js index 10a39b9..d7aefce 100644 --- a/test/integration/email-test.js +++ b/test/integration/email-test.js @@ -25,12 +25,15 @@ describe('Email Integration Tests', function() { } email = new Email(nodemailer); email.init({ - host: credentials.smtp.host, + host: process.env.SMTP_HOST || credentials.smtp.host, auth: { - user: credentials.smtp.user, - pass: credentials.smtp.pass + user: process.env.SMTP_USER || credentials.smtp.user, + 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 + } }); });