use of org.jivesoftware.smackx.ox.element.PublicKeysListElement in project Smack by igniterealtime.
the class OpenPgpPubSubUtil method publishPublicKey.
/**
* Publish the users OpenPGP public key to the public key node if necessary.
* Also announce the key to other users by updating the metadata node.
*
* @see <a href="https://xmpp.org/extensions/xep-0373.html#announcing-pubkey">XEP-0373 ยง4.1</a>
*
* @param pepManager The PEP manager.
* @param pubkeyElement {@link PubkeyElement} containing the public key
* @param fingerprint fingerprint of the public key
*
* @throws InterruptedException if the thread gets interrupted.
* @throws PubSubException.NotALeafNodeException if either the metadata node or the public key node is not a
* {@link LeafNode}.
* @throws XMPPException.XMPPErrorException in case of an XMPP protocol error.
* @throws SmackException.NotConnectedException if we are not connected.
* @throws SmackException.NoResponseException if the server doesn't respond.
*/
public static void publishPublicKey(PepManager pepManager, PubkeyElement pubkeyElement, OpenPgpV4Fingerprint fingerprint) throws InterruptedException, PubSubException.NotALeafNodeException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, SmackException.NoResponseException {
String keyNodeName = PEP_NODE_PUBLIC_KEY(fingerprint);
PubSubManager pm = pepManager.getPepPubSubManager();
// Check if key available at data node
// If not, publish key to data node
LeafNode keyNode = pm.getOrCreateLeafNode(keyNodeName);
changeAccessModelIfNecessary(keyNode, AccessModel.open);
List<Item> items = keyNode.getItems(1);
if (items.isEmpty()) {
LOGGER.log(Level.FINE, "Node " + keyNodeName + " is empty. Publish.");
keyNode.publish(new PayloadItem<>(pubkeyElement));
} else {
LOGGER.log(Level.FINE, "Node " + keyNodeName + " already contains key. Skip.");
}
// Fetch IDs from metadata node
LeafNode metadataNode = pm.getOrCreateLeafNode(PEP_NODE_PUBLIC_KEYS);
changeAccessModelIfNecessary(metadataNode, AccessModel.open);
List<PayloadItem<PublicKeysListElement>> metadataItems = metadataNode.getItems(1);
PublicKeysListElement.Builder builder = PublicKeysListElement.builder();
if (!metadataItems.isEmpty() && metadataItems.get(0).getPayload() != null) {
// Add old entries back to list.
PublicKeysListElement publishedList = metadataItems.get(0).getPayload();
for (PublicKeysListElement.PubkeyMetadataElement meta : publishedList.getMetadata().values()) {
builder.addMetadata(meta);
}
}
builder.addMetadata(new PublicKeysListElement.PubkeyMetadataElement(fingerprint, new Date()));
// Publish IDs to metadata node
metadataNode.publish(new PayloadItem<>(builder.build()));
}
use of org.jivesoftware.smackx.ox.element.PublicKeysListElement in project Smack by igniterealtime.
the class OpenPgpContact method updateKeys.
/**
* Update the contacts keys by consulting the users PubSub nodes.
* This method fetches the users metadata node and then tries to fetch any announced keys.
*
* @param connection our {@link XMPPConnection}.
*
* @throws InterruptedException In case the thread gets interrupted.
* @throws SmackException.NotConnectedException in case the connection is not connected.
* @throws SmackException.NoResponseException in case the server doesn't respond.
* @throws XMPPException.XMPPErrorException in case of an XMPP protocol error.
* @throws PubSubException.NotALeafNodeException in case the metadata node is not a {@link LeafNode}.
* @throws PubSubException.NotAPubSubNodeException in case the metadata node is not a PubSub node.
* @throws IOException IO is brittle.
*/
public void updateKeys(XMPPConnection connection) throws InterruptedException, SmackException.NotConnectedException, SmackException.NoResponseException, XMPPException.XMPPErrorException, PubSubException.NotALeafNodeException, PubSubException.NotAPubSubNodeException, IOException {
PublicKeysListElement metadata = OpenPgpPubSubUtil.fetchPubkeysList(connection, getJid());
if (metadata == null) {
return;
}
updateKeys(connection, metadata);
}
use of org.jivesoftware.smackx.ox.element.PublicKeysListElement in project Smack by igniterealtime.
the class PublicKeysListElementProvider method parse.
@Override
public PublicKeysListElement parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, ParseException {
PublicKeysListElement.Builder builder = PublicKeysListElement.builder();
while (true) {
XmlPullParser.TagEvent tag = parser.nextTag();
String name;
switch(tag) {
case START_ELEMENT:
name = parser.getName();
if (PublicKeysListElement.PubkeyMetadataElement.ELEMENT.equals(name)) {
String finger = parser.getAttributeValue(null, PublicKeysListElement.PubkeyMetadataElement.ATTR_V4_FINGERPRINT);
String dt = parser.getAttributeValue(null, PublicKeysListElement.PubkeyMetadataElement.ATTR_DATE);
OpenPgpV4Fingerprint fingerprint = new OpenPgpV4Fingerprint(finger);
Date date = ParserUtils.getDateFromXep82String(dt);
builder.addMetadata(new PublicKeysListElement.PubkeyMetadataElement(fingerprint, date));
}
break;
case END_ELEMENT:
name = parser.getName();
if (name.equals(PublicKeysListElement.ELEMENT)) {
return builder.build();
}
break;
default:
// Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
break;
}
}
}
use of org.jivesoftware.smackx.ox.element.PublicKeysListElement in project Smack by igniterealtime.
the class PublicKeysListElementTest method providerTest.
@Test
public void providerTest() throws Exception {
String expected = "<public-keys-list xmlns='urn:xmpp:openpgp:0'>" + "<pubkey-metadata " + "v4-fingerprint='1357B01865B2503C18453D208CAC2A9678548E35' " + "date='2018-03-01T15:26:12.000+00:00'" + "/>" + "<pubkey-metadata " + "v4-fingerprint='67819B343B2AB70DED9320872C6464AF2A8E4C02' " + "date='1953-05-16T12:00:00.000+00:00'" + "/>" + "</public-keys-list>";
Date date1 = XmppDateTime.parseDate("2018-03-01T15:26:12.000+00:00");
Date date2 = XmppDateTime.parseDate("1953-05-16T12:00:00.000+00:00");
PublicKeysListElement.PubkeyMetadataElement child1 = new PublicKeysListElement.PubkeyMetadataElement(new OpenPgpV4Fingerprint("1357B01865B2503C18453D208CAC2A9678548E35"), date1);
PublicKeysListElement.PubkeyMetadataElement child2 = new PublicKeysListElement.PubkeyMetadataElement(new OpenPgpV4Fingerprint("67819B343B2AB70DED9320872C6464AF2A8E4C02"), date2);
PublicKeysListElement element = PublicKeysListElement.builder().addMetadata(child1).addMetadata(child2).build();
assertXmlSimilar(expected, element.toXML().toString());
XmlPullParser parser = TestUtils.getParser(expected);
PublicKeysListElement parsed = PublicKeysListElementProvider.TEST_INSTANCE.parse(parser);
assertEquals(element.getMetadata(), parsed.getMetadata());
}
Aggregations