1// Copyright (c) 2019 Maxime “pep” Buquet <pep@bouah.net>
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7use crate::date::DateTime;
8use crate::util::helpers::Base64;
9use crate::pubsub::PubSubPayload;
10
11// TODO: Merge this container with the PubKey struct
12generate_element!(
13 /// Data contained in the PubKey element
14 PubKeyData, "data", OX,
15 text: (
16 /// Base64 data
17 data: Base64<Vec<u8>>
18 )
19);
20
21generate_element!(
22 /// Pubkey element to be used in PubSub publish payloads.
23 PubKey, "pubkey", OX,
24 attributes: [
25 /// Last updated date
26 date: Option<DateTime> = "date"
27 ],
28 children: [
29 /// Public key as base64 data
30 data: Required<PubKeyData> = ("data", OX) => PubKeyData
31 ]
32);
33
34impl PubSubPayload for PubKey {}
35
36generate_element!(
37 /// Public key metadata
38 PubKeyMeta, "pubkey-metadata", OX,
39 attributes: [
40 /// OpenPGP v4 fingerprint
41 v4fingerprint: Required<String> = "v4-fingerprint",
42 /// Time the key was published or updated
43 date: Required<DateTime> = "date",
44 ]
45);
46
47generate_element!(
48 /// List of public key metadata
49 PubKeysMeta, "public-key-list", OX,
50 children: [
51 /// Public keys
52 pubkeys: Vec<PubKeyMeta> = ("pubkey-metadata", OX) => PubKeyMeta
53 ]
54);
55
56impl PubSubPayload for PubKeysMeta {}
57
58#[cfg(test)]
59mod tests {
60 use super::*;
61 use crate::ns;
62 use std::str::FromStr;
63 use crate::pubsub::{NodeName, Item, pubsub::{Item as PubSubItem, Publish}};
64
65 #[test]
66 fn pubsub_publish_pubkey_data() {
67 let pubkey = PubKey {
68 date: None,
69 data: PubKeyData {
70 data: (&"Foo").as_bytes().to_vec(),
71 }
72 };
73 println!("Foo1: {:?}", pubkey);
74
75 let pubsub = Publish {
76 node: NodeName(format!("{}:{}", ns::OX_PUBKEYS, "some-fingerprint")),
77 items: vec![
78 PubSubItem(Item::new(None, None, Some(pubkey))),
79 ],
80 };
81 println!("Foo2: {:?}", pubsub);
82 }
83
84 #[test]
85 fn pubsub_publish_pubkey_meta() {
86 let pubkeymeta = PubKeysMeta {
87 pubkeys: vec![
88 PubKeyMeta {
89 v4fingerprint: "some-fingerprint".to_owned(),
90 date: DateTime::from_str("2019-03-30T18:30:25Z").unwrap(),
91 },
92 ],
93 };
94 println!("Foo1: {:?}", pubkeymeta);
95
96 let pubsub = Publish {
97 node: NodeName("foo".to_owned()),
98 items: vec![
99 PubSubItem(Item::new(None, None, Some(pubkeymeta))),
100 ],
101 };
102 println!("Foo2: {:?}", pubsub);
103 }
104}