From dcb66eef592bf3959a3aaafae0802e0b35500e2d Mon Sep 17 00:00:00 2001 From: Aditya Borikar Date: Fri, 19 Jun 2020 05:12:03 +0530 Subject: [PATCH 1/4] Add support for HTTP lookup method through xep-0156 --- documentation/extensions/index.md | 1 + .../altconnections/HttpLookupMethod.java | 176 ++++++++++++++++++ .../smack/altconnections/package-info.java | 22 +++ .../altconnections/HttpLookupMethodTest.java | 57 ++++++ 4 files changed, 256 insertions(+) create mode 100644 smack-core/src/main/java/org/jivesoftware/smack/altconnections/HttpLookupMethod.java create mode 100644 smack-core/src/main/java/org/jivesoftware/smack/altconnections/package-info.java create mode 100644 smack-core/src/test/java/org/jivesoftware/smack/altconnections/HttpLookupMethodTest.java diff --git a/documentation/extensions/index.md b/documentation/extensions/index.md index 0223d1554..78a2bbc57 100644 --- a/documentation/extensions/index.md +++ b/documentation/extensions/index.md @@ -14,6 +14,7 @@ Currently supported XEPs of Smack (all sub-projects) | Name | XEP | Version | Description | |---------------------------------------------|--------------------------------------------------------|-----------|----------------------------------------------------------------------------------------------------------| +| Discovering Alternative XMPP Connection Methods | [XEP-0156](https://xmpp.org/extensions/xep-0156.html) | 1.3.0 | Defines ways to discover alternative connection methods. | | Nonzas | [XEP-0360](https://xmpp.org/extensions/xep-0360.html) | n/a | Defines the term "Nonza", describing every top level stream element that is not a Stanza. | Currently supported XEPs of smack-tcp diff --git a/smack-core/src/main/java/org/jivesoftware/smack/altconnections/HttpLookupMethod.java b/smack-core/src/main/java/org/jivesoftware/smack/altconnections/HttpLookupMethod.java new file mode 100644 index 000000000..75a4cf263 --- /dev/null +++ b/smack-core/src/main/java/org/jivesoftware/smack/altconnections/HttpLookupMethod.java @@ -0,0 +1,176 @@ +/** + * + * Copyright 2020 Aditya Borikar. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smack.altconnections; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.List; + +import org.jivesoftware.smack.util.PacketParserUtils; +import org.jivesoftware.smack.xml.XmlPullParser; +import org.jivesoftware.smack.xml.XmlPullParserException; + +import org.jxmpp.jid.DomainBareJid; + +/** + * The HTTP lookup method uses web host metadata to list the URIs of alternative connection methods. + * + *

In order to obtain host-meta XRD element from the host in the form of an InputStream, + * use {@link #getXrdStream(DomainBareJid)} method. To obtain endpoints for Bosh or Websocket + * connection endpoints from host, use {@link LinkRelation#BOSH} and {@link LinkRelation#WEBSOCKET} + * respectively with the {@link #lookup(DomainBareJid, LinkRelation)} method. In case one is looking + * for endpoints described by other than BOSH or Websocket LinkRelation, use the more flexible + * {@link #lookup(DomainBareJid, String)} method.

+ * Example:
+ *
+ * {@code
+ * DomainBareJid xmppServerAddress = JidCreate.domainBareFrom("example.org");
+ * List endpoints = HttpLookupMethod.lookup(xmppServiceAddress, LinkRelation.WEBSOCKET);
+ * }
+ * 
+ * @see + * HTTP Lookup Method from XEP-0156. + * + */ +public final class HttpLookupMethod { + private static final String XRD_NAMESPACE = "http://docs.oasis-open.org/ns/xri/xrd-1.0"; + + /** + * Specifies a link relation for the selected type of connection. + */ + public enum LinkRelation { + /** + * Selects link relation attribute as "urn:xmpp:alt-connections:xbosh". + */ + BOSH("urn:xmpp:alt-connections:xbosh"), + /** + * Selects link relation attribute as "urn:xmpp:alt-connections:websocket". + */ + WEBSOCKET("urn:xmpp:alt-connections:websocket"); + private final String attribute; + LinkRelation(String relAttribute) { + this.attribute = relAttribute; + } + } + + /** + * Get remote endpoints for the given LinkRelation from host. + * + * @param xmppServiceAddress address of host + * @param relation LinkRelation as a string specifying type of connection + * @return list of endpoints + * @throws IOException exception due to input/output operations + * @throws XmlPullParserException exception encountered during XML parsing + * @throws URISyntaxException exception to indicate that a string could not be parsed as a URI reference + */ + public static List lookup(DomainBareJid xmppServiceAddress, String relation) throws IOException, XmlPullParserException, URISyntaxException { + try (InputStream inputStream = getXrdStream(xmppServiceAddress)) { + XmlPullParser xmlPullParser = PacketParserUtils.getParserFor(inputStream); + List endpoints = parseXrdLinkReferencesFor(xmlPullParser, relation); + return endpoints; + } + } + + /** + * Get remote endpoints for the given LinkRelation from host. + * + * @param xmppServiceAddress address of host + * @param relation {@link LinkRelation} specifying type of connection + * @return list of endpoints + * @throws IOException exception due to input/output operations + * @throws XmlPullParserException exception encountered during XML parsing + * @throws URISyntaxException exception to indicate that a string could not be parsed as a URI reference + */ + public static List lookup(DomainBareJid xmppServiceAddress, LinkRelation relation) throws IOException, XmlPullParserException, URISyntaxException { + return lookup(xmppServiceAddress, relation.attribute); + } + + /** + * Constructs a HTTP connection with the host specified by the DomainBareJid + * and retrieves XRD element in the form of an InputStream. The method will + * throw a {@link FileNotFoundException} if host-meta isn't published. + * + * @param xmppServiceAddress address of host + * @return InputStream containing XRD element + * @throws IOException exception due to input/output operations + */ + public static InputStream getXrdStream(DomainBareJid xmppServiceAddress) throws IOException { + final String metadataUrl = "https://" + xmppServiceAddress + "/.well-known/host-meta"; + final URL putUrl = new URL(metadataUrl); + final URLConnection urlConnection = putUrl.openConnection(); + return urlConnection.getInputStream(); + } + + /** + * Get remote endpoints for the provided LinkRelation from provided XmlPullParser. + * + * @param parser XmlPullParser that contains LinkRelations + * @param relation type of endpoints specified by the given LinkRelation + * @return list of endpoints + * @throws IOException exception due to input/output operations + * @throws XmlPullParserException exception encountered during XML parsing + * @throws URISyntaxException exception to indicate that a string could not be parsed as a URI reference + */ + public static List parseXrdLinkReferencesFor(XmlPullParser parser, String relation) throws IOException, XmlPullParserException, URISyntaxException { + List uriList = new ArrayList<>(); + int initialDepth = parser.getDepth(); + + loop: while (true) { + XmlPullParser.TagEvent tag = parser.nextTag(); + switch (tag) { + case START_ELEMENT: + String name = parser.getName(); + String namespace = parser.getNamespace(); + String rel = parser.getAttributeValue("rel"); + + if (!namespace.equals(XRD_NAMESPACE) || !name.equals("Link") || !rel.equals(relation)) { + continue loop; + } + String endpointUri = parser.getAttributeValue("href"); + URI uri = new URI(endpointUri); + uriList.add(uri); + break; + case END_ELEMENT: + if (parser.getDepth() == initialDepth) { + break loop; + } + break; + } + } + return uriList; + } + + /** + * Get remote endpoints for the provided LinkRelation from provided XmlPullParser. + * + * @param parser XmlPullParser that contains LinkRelations + * @param relation type of endpoints specified by the given LinkRelation + * @return list of endpoints + * @throws IOException exception due to input/output operations + * @throws XmlPullParserException exception encountered during XML parsing + * @throws URISyntaxException exception to indicate that a string could not be parsed as a URI reference + */ + public static List parseXrdLinkReferencesFor(XmlPullParser parser, LinkRelation relation) throws IOException, XmlPullParserException, URISyntaxException { + return parseXrdLinkReferencesFor(parser, relation.attribute); + } +} diff --git a/smack-core/src/main/java/org/jivesoftware/smack/altconnections/package-info.java b/smack-core/src/main/java/org/jivesoftware/smack/altconnections/package-info.java new file mode 100644 index 000000000..5d9955232 --- /dev/null +++ b/smack-core/src/main/java/org/jivesoftware/smack/altconnections/package-info.java @@ -0,0 +1,22 @@ +/** + * + * Copyright 2020 Aditya Borikar. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/** + * Smack's API for XEP-0156: Discovering Alternative XMPP Connection Methods. + *
+ * XEP is partially supported as HTTP lookup is supported but DNS lookup isn't. + */ +package org.jivesoftware.smack.altconnections; diff --git a/smack-core/src/test/java/org/jivesoftware/smack/altconnections/HttpLookupMethodTest.java b/smack-core/src/test/java/org/jivesoftware/smack/altconnections/HttpLookupMethodTest.java new file mode 100644 index 000000000..efa503ac1 --- /dev/null +++ b/smack-core/src/test/java/org/jivesoftware/smack/altconnections/HttpLookupMethodTest.java @@ -0,0 +1,57 @@ +/** + * + * Copyright 2020 Aditya Borikar. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smack.altconnections; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.ArrayList; +import java.util.List; + +import org.jivesoftware.smack.altconnections.HttpLookupMethod.LinkRelation; +import org.jivesoftware.smack.util.PacketParserUtils; +import org.jivesoftware.smack.xml.XmlPullParserException; + +import org.junit.jupiter.api.Test; +import org.jxmpp.stringprep.XmppStringprepException; + +public class HttpLookupMethodTest { + private static final String XRD_XML = "" + + "" + + "" + + "" + + ""; + + @Test + public void parseXrdLinkReferencesForWebsockets() throws XmppStringprepException, IOException, XmlPullParserException, URISyntaxException { + List endpoints = new ArrayList<>(); + endpoints.add(new URI("wss://xmpp.igniterealtime.org:7483/ws/")); + endpoints.add(new URI("ws://xmpp.igniterealtime.org:7070/ws/")); + List expectedEndpoints = HttpLookupMethod.parseXrdLinkReferencesFor(PacketParserUtils.getParserFor(XRD_XML), LinkRelation.WEBSOCKET); + assertEquals(expectedEndpoints, endpoints); + } + + @Test + public void parseXrdLinkReferencesForBosh() throws URISyntaxException, IOException, XmlPullParserException { + List endpoints = new ArrayList<>(); + endpoints.add(new URI("https://igniterealtime.org:443/http-bind/")); + List expectedEndpoints = HttpLookupMethod.parseXrdLinkReferencesFor(PacketParserUtils.getParserFor(XRD_XML), LinkRelation.BOSH); + assertEquals(expectedEndpoints, endpoints); + } +} From 0eeb89409a0de7714b05655801978f9acea5d438 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Sun, 28 Jun 2020 17:42:01 +0200 Subject: [PATCH 2/4] [gradle] Add fix for javadoc search --- build.gradle | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/build.gradle b/build.gradle index 2e1270a4c..b962c9105 100644 --- a/build.gradle +++ b/build.gradle @@ -259,6 +259,13 @@ allprojects { if (JavaVersion.current().isJava9Compatible()) { tasks.withType(Javadoc) { options.addStringOption('-release', javaMajor) + + // Fix for javadoc search. If not set, the search result would direct to + // javadoc/undefined/org/jivesoftware/smack/altconnections/HttpLookupMethod.html + // instead of + // javadoc/org/jivesoftware/smack/altconnections/HttpLookupMethod.html + // https://stackoverflow.com/a/53732633/194894 + options.addBooleanOption("-no-module-directories", true) } tasks.withType(JavaCompile) { options.compilerArgs.addAll([ From 8850a2950c7c1158710fa3d6436ad6d41db6f425 Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Sat, 4 Jul 2020 20:02:33 +0200 Subject: [PATCH 3/4] Remove unnecessary lines from OX backup restore function The manual construction of the public key ring as done in the removed lines is already performed in the BCUtil method called a line above. --- .../java/org/jivesoftware/smackx/ox/OpenPgpManager.java | 7 ------- 1 file changed, 7 deletions(-) diff --git a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/OpenPgpManager.java b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/OpenPgpManager.java index ce49dbde6..cf070bdf1 100644 --- a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/OpenPgpManager.java +++ b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/OpenPgpManager.java @@ -482,13 +482,6 @@ public final class OpenPgpManager extends Manager { provider.getStore().importSecretKey(getJidOrThrow(), secretKeys); provider.getStore().importPublicKey(getJidOrThrow(), BCUtil.publicKeyRingFromSecretKeyRing(secretKeys)); - ByteArrayOutputStream buffer = new ByteArrayOutputStream(2048); - for (PGPSecretKey sk : secretKeys) { - PGPPublicKey pk = sk.getPublicKey(); - if (pk != null) pk.encode(buffer); - } - PGPPublicKeyRing publicKeys = new PGPPublicKeyRing(buffer.toByteArray(), new BcKeyFingerprintCalculator()); - provider.getStore().importPublicKey(getJidOrThrow(), publicKeys); return new OpenPgpV4Fingerprint(secretKeys); } From 1c7ffb57202ceb64521e5770fc9bb3d16642149c Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Sat, 4 Jul 2020 20:02:50 +0200 Subject: [PATCH 4/4] Trust key from backup upon restore --- .../main/java/org/jivesoftware/smackx/ox/OpenPgpManager.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/OpenPgpManager.java b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/OpenPgpManager.java index cf070bdf1..7cf036c2e 100644 --- a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/OpenPgpManager.java +++ b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/OpenPgpManager.java @@ -479,9 +479,11 @@ public final class OpenPgpManager extends Manager { String backupCode = codeCallback.askForBackupCode(); PGPSecretKeyRing secretKeys = SecretKeyBackupHelper.restoreSecretKeyBackup(backup, backupCode); + OpenPgpV4Fingerprint fingerprint = new OpenPgpV4Fingerprint(secretKeys); provider.getStore().importSecretKey(getJidOrThrow(), secretKeys); provider.getStore().importPublicKey(getJidOrThrow(), BCUtil.publicKeyRingFromSecretKeyRing(secretKeys)); + getOpenPgpSelf().trust(fingerprint); return new OpenPgpV4Fingerprint(secretKeys); }