Put comment before email when generating UIDs (#892)

This commit is contained in:
Si Feng 2019-05-01 04:12:52 -07:00 committed by Daniel Huigens
parent cc4a9119a3
commit d2c38693f5
2 changed files with 58 additions and 4 deletions

View File

@ -652,11 +652,23 @@ export default {
* Format user id for internal use.
*/
formatUserId: function(id) {
// name and email address can be empty but must be of the correct type
if ((id.name && !util.isString(id.name)) || (id.email && !util.isEmailAddress(id.email))) {
// name, email address and comment can be empty but must be of the correct type
if ((id.name && !util.isString(id.name)) ||
(id.email && !util.isEmailAddress(id.email)) ||
(id.comment && !util.isString(id.comment))) {
throw new Error('Invalid user id format');
}
return new rfc2822.Address(id.name, id.email, id.comment).format();
const components = [];
if (id.name) {
components.push(id.name);
}
if (id.comment) {
components.push(`(${id.comment})`);
}
if (id.email) {
components.push(`<${id.email}>`);
}
return components.join(' ');
},
/**

View File

@ -1737,7 +1737,49 @@ function versionSpecificTests() {
return openpgp.generateKey(opt).then(function(key) {
key = key.key;
expect(key.users.length).to.equal(1);
expect(key.users[0].userId.userid).to.equal('test <a@b.com> (test comment)');
expect(key.users[0].userId.userid).to.equal('test (test comment) <a@b.com>');
expect(key.users[0].userId.name).to.equal(userId.name);
expect(key.users[0].userId.email).to.equal(userId.email);
expect(key.users[0].userId.comment).to.equal(userId.comment);
});
});
it('Generate key - single userid (all missing)', function() {
const userId = { name: '', email: '', comment: '' };
const opt = {numBits: 512, userIds: userId, passphrase: '123'};
if (openpgp.util.getWebCryptoAll()) { opt.numBits = 2048; } // webkit webcrypto accepts minimum 2048 bit keys
return openpgp.generateKey(opt).then(function(key) {
key = key.key;
expect(key.users.length).to.equal(1);
expect(key.users[0].userId.userid).to.equal('');
expect(key.users[0].userId.name).to.equal(userId.name);
expect(key.users[0].userId.email).to.equal(userId.email);
expect(key.users[0].userId.comment).to.equal(userId.comment);
});
});
it('Generate key - single userid (missing email)', function() {
const userId = { name: 'test', email: '', comment: 'test comment' };
const opt = {numBits: 512, userIds: userId, passphrase: '123'};
if (openpgp.util.getWebCryptoAll()) { opt.numBits = 2048; } // webkit webcrypto accepts minimum 2048 bit keys
return openpgp.generateKey(opt).then(function(key) {
key = key.key;
expect(key.users.length).to.equal(1);
expect(key.users[0].userId.userid).to.equal('test (test comment)');
expect(key.users[0].userId.name).to.equal(userId.name);
expect(key.users[0].userId.email).to.equal(userId.email);
expect(key.users[0].userId.comment).to.equal(userId.comment);
});
});
it('Generate key - single userid (missing comment)', function() {
const userId = { name: 'test', email: 'a@b.com', comment: '' };
const opt = {numBits: 512, userIds: userId, passphrase: '123'};
if (openpgp.util.getWebCryptoAll()) { opt.numBits = 2048; } // webkit webcrypto accepts minimum 2048 bit keys
return openpgp.generateKey(opt).then(function(key) {
key = key.key;
expect(key.users.length).to.equal(1);
expect(key.users[0].userId.userid).to.equal('test <a@b.com>');
expect(key.users[0].userId.name).to.equal(userId.name);
expect(key.users[0].userId.email).to.equal(userId.email);
expect(key.users[0].userId.comment).to.equal(userId.comment);