Fix bug where keyId was undefined in verifyRemove link

This commit is contained in:
Tankred Hase 2016-06-10 13:17:28 +02:00
parent cfb4b9bab0
commit 9be7feab04
3 changed files with 13 additions and 9 deletions

View File

@ -70,8 +70,8 @@ class Email {
params: { params: {
name: userId.name, name: userId.name,
baseUrl: util.url(origin), baseUrl: util.url(origin),
keyId: encodeURIComponent(keyId), keyId: keyId,
nonce: encodeURIComponent(userId.nonce) nonce: userId.nonce
} }
}; };
return yield this._sendHelper(message); return yield this._sendHelper(message);

View File

@ -221,11 +221,12 @@ class PublicKey {
*/ */
*requestRemove(options) { *requestRemove(options) {
let keyId = options.keyId, email = options.email, origin = options.origin; let keyId = options.keyId, email = options.email, origin = options.origin;
let userIds = yield this._flagForRemove(keyId, email); let key = yield this._flagForRemove(keyId, email);
if (!userIds.length) { if (!key) {
util.throw(404, 'User id not found'); util.throw(404, 'User id not found');
} }
for (let userId of userIds) { keyId = key.keyId; // get keyId in case request was by email
for (let userId of key.userIds) {
yield this._email.send({ template:tpl.verifyRemove, userId, keyId, origin }); yield this._email.send({ template:tpl.verifyRemove, userId, keyId, origin });
} }
} }
@ -241,14 +242,14 @@ class PublicKey {
let query = email ? { 'userIds.email':email } : { keyId }; let query = email ? { 'userIds.email':email } : { keyId };
let key = yield this._mongo.get(query, DB_TYPE); let key = yield this._mongo.get(query, DB_TYPE);
if (!key) { if (!key) {
return []; return;
} }
if (email) { if (email) {
let nonce = util.random(); let nonce = util.random();
yield this._mongo.update(query, { 'userIds.$.nonce':nonce }, DB_TYPE); yield this._mongo.update(query, { 'userIds.$.nonce':nonce }, DB_TYPE);
let uid = key.userIds.find(u => u.email === email); let uid = key.userIds.find(u => u.email === email);
uid.nonce = nonce; uid.nonce = nonce;
return [uid]; return { userIds:[uid], keyId:key.keyId };
} }
if (keyId) { if (keyId) {
for (let uid of key.userIds) { for (let uid of key.userIds) {
@ -256,7 +257,7 @@ class PublicKey {
yield this._mongo.update({ 'userIds.email':uid.email }, { 'userIds.$.nonce':nonce }, DB_TYPE); yield this._mongo.update({ 'userIds.email':uid.email }, { 'userIds.$.nonce':nonce }, DB_TYPE);
uid.nonce = nonce; uid.nonce = nonce;
} }
return key.userIds; return key;
} }
} }

View File

@ -35,7 +35,7 @@ describe('Public Key Integration Tests', function() {
return recipient.to.address === primaryEmail; return recipient.to.address === primaryEmail;
}), sinon.match(params => { }), sinon.match(params => {
emailParams = params; emailParams = params;
return !!params.nonce; return params.nonce !== undefined && params.keyId !== undefined;
})); }));
sinon.stub(nodemailer, 'createTransport').returns({ sinon.stub(nodemailer, 'createTransport').returns({
templateSender: () => { return sendEmailStub; } templateSender: () => { return sendEmailStub; }
@ -253,18 +253,21 @@ describe('Public Key Integration Tests', function() {
yield publicKey.verify(emailParams); yield publicKey.verify(emailParams);
emailParams = null; emailParams = null;
yield publicKey.requestRemove({ keyId, origin }); yield publicKey.requestRemove({ keyId, origin });
expect(emailParams.keyId).to.exist;
expect(emailParams.nonce).to.exist; expect(emailParams.nonce).to.exist;
}); });
it('should work for unverified key', function *() { it('should work for unverified key', function *() {
emailParams = null; emailParams = null;
yield publicKey.requestRemove({ keyId, origin }); yield publicKey.requestRemove({ keyId, origin });
expect(emailParams.keyId).to.exist;
expect(emailParams.nonce).to.exist; expect(emailParams.nonce).to.exist;
}); });
it('should work by email address', function *() { it('should work by email address', function *() {
emailParams = null; emailParams = null;
yield publicKey.requestRemove({ email:primaryEmail, origin }); yield publicKey.requestRemove({ email:primaryEmail, origin });
expect(emailParams.keyId).to.exist;
expect(emailParams.nonce).to.exist; expect(emailParams.nonce).to.exist;
}); });