summaryrefslogtreecommitdiffstats
path: root/openpgp/src/tsk.rs
diff options
context:
space:
mode:
authorNeal H. Walfield <neal@pep.foundation>2019-01-29 10:22:46 +0100
committerNeal H. Walfield <neal@pep.foundation>2019-01-29 10:22:46 +0100
commitbff390b189ac92012a3c25e8c361013c78698fd3 (patch)
tree4460d76b3d396c48d7974ba49cad2ed08f08abf9 /openpgp/src/tsk.rs
parentee276a2dc55fd55f22494e9e70187f060fb2f151 (diff)
openpgp: TPKBuilder::autocrypt should not default to an empty UID
- TPKBuilder::autocrypt created a TPK with a single User ID, as required by the Autocrypt specification. Since no User ID was passed, it used the empty string. - An empty User ID is a bit surprising, and it is unclear if it is even a reasonable default (GnuPG rejects it). But, even if the programmer is aware of this, adding a new user ID does not replace the empty User ID, and removing the empty User ID is a pain. - Change the API to better match typical usage: have the constructor take the User ID. - Nevertheless, preserve the flexibility by making the User ID optional to allow the caller to add a User ID later. In this case, a non-autocrypt compliant TPK with no User ID is created instead of an empty User ID. - Closes #146.
Diffstat (limited to 'openpgp/src/tsk.rs')
-rw-r--r--openpgp/src/tsk.rs56
1 files changed, 47 insertions, 9 deletions
diff --git a/openpgp/src/tsk.rs b/openpgp/src/tsk.rs
index fff2a7ed..57a6ff2d 100644
--- a/openpgp/src/tsk.rs
+++ b/openpgp/src/tsk.rs
@@ -77,17 +77,12 @@ impl TSK {
/// Generates a new key OpenPGP key. The key will be capable of encryption
/// and signing. If no user id is given the primary self signature will be
/// a direct key signature.
- pub fn new<'a, O: Into<Option<Cow<'a,str>>>>(primary_uid: O)
- -> Result<(TSK, Signature)> {
+ pub fn new<'a, O>(primary_uid: O) -> Result<(TSK, Signature)>
+ where O: Into<Option<Cow<'a, str>>>
+ {
use tpk::TPKBuilder;
- let mut key = TPKBuilder::autocrypt(None);
-
- match primary_uid.into() {
- Some(uid) => { key = key.add_userid(uid); }
- None => {}
- }
-
+ let key = TPKBuilder::autocrypt(None, primary_uid);
let (tpk, revocation) = key.generate()?;
Ok((TSK::from_tpk(tpk), revocation))
}
@@ -212,6 +207,7 @@ impl Serialize for TSK {
#[cfg(test)]
mod tests {
+ use super::*;
use tpk::TPKBuilder;
#[test]
@@ -251,4 +247,46 @@ mod tests {
tpk2.userids().next().unwrap().userid()).unwrap(),
true);
}
+
+ #[test]
+ fn user_ids() {
+ let (tpk, _) = TPKBuilder::default()
+ .add_userid("test1@example.com")
+ .add_userid("test2@example.com")
+ .generate().unwrap();
+
+ let userids = tpk
+ .userids()
+ .map(|binding| binding.userid().userid())
+ .collect::<Vec<_>>();
+ assert_eq!(userids.len(), 2);
+ assert!((userids[0] == b"test1@example.com"
+ && userids[1] == b"test2@example.com")
+ || (userids[0] == b"test2@example.com"
+ && userids[1] == b"test1@example.com"),
+ "User ids: {:?}", userids);
+
+
+ let (tpk, _) = TPKBuilder::autocrypt(None, Some("Foo".into()))
+ .generate()
+ .unwrap();
+
+ let userids = tpk
+ .userids()
+ .map(|binding| binding.userid().userid())
+ .collect::<Vec<_>>();
+ assert_eq!(userids.len(), 1);
+ assert_eq!(userids[0], b"Foo");
+
+
+ let (tsk, _) = TSK::new(Some("test@example.com".into())).unwrap();
+ let tpk = tsk.into_tpk();
+ let userids = tpk
+ .userids()
+ .map(|binding| binding.userid().userid())
+ .collect::<Vec<_>>();
+ assert_eq!(userids.len(), 1);
+ assert_eq!(userids[0], b"test@example.com",
+ "User ids: {:?}", userids);
+ }
}