From 2195bce3032cf6a445ffd8005ec8880e623ea5e9 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Sat, 16 May 2020 08:55:13 +0200 Subject: [PATCH] =?UTF-8?q?don=E2=80=99t=20allow=20escaped=20usernames=20i?= =?UTF-8?q?n=20magic=20create?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../conversations/ui/MagicCreateActivity.java | 10 +- .../java/eu/siacs/conversations/xmpp/Jid.java | 150 ++---------------- 2 files changed, 19 insertions(+), 141 deletions(-) diff --git a/src/conversations/java/eu/siacs/conversations/ui/MagicCreateActivity.java b/src/conversations/java/eu/siacs/conversations/ui/MagicCreateActivity.java index 0d627a116..a20d416bc 100644 --- a/src/conversations/java/eu/siacs/conversations/ui/MagicCreateActivity.java +++ b/src/conversations/java/eu/siacs/conversations/ui/MagicCreateActivity.java @@ -81,13 +81,13 @@ public class MagicCreateActivity extends XmppActivity implements TextWatcher { final boolean fixedUsername; if (this.domain != null && this.username != null) { fixedUsername = true; - jid = Jid.ofLocalAndDomain(this.username, this.domain); + jid = Jid.ofLocalAndDomainEscaped(this.username, this.domain); } else if (this.domain != null) { fixedUsername = false; - jid = Jid.ofLocalAndDomain(username, this.domain); + jid = Jid.ofLocalAndDomainEscaped(username, this.domain); } else { fixedUsername = false; - jid = Jid.ofLocalAndDomain(username, Config.MAGIC_CREATE_DOMAIN); + jid = Jid.ofLocalAndDomainEscaped(username, Config.MAGIC_CREATE_DOMAIN); } if (!jid.getEscapedLocal().equals(jid.getLocal()) || (this.username == null && username.length() < 3)) { binding.username.setError(getString(R.string.invalid_username)); @@ -151,9 +151,9 @@ public class MagicCreateActivity extends XmppActivity implements TextWatcher { binding.fullJid.setVisibility(View.VISIBLE); final Jid jid; if (this.domain == null) { - jid = Jid.ofLocalAndDomain(username, Config.MAGIC_CREATE_DOMAIN); + jid = Jid.ofLocalAndDomainEscaped(username, Config.MAGIC_CREATE_DOMAIN); } else { - jid = Jid.ofLocalAndDomain(username, this.domain); + jid = Jid.ofLocalAndDomainEscaped(username, this.domain); } binding.fullJid.setText(getString(R.string.your_full_jid_will_be, jid.toEscapedString())); } catch (IllegalArgumentException e) { diff --git a/src/main/java/eu/siacs/conversations/xmpp/Jid.java b/src/main/java/eu/siacs/conversations/xmpp/Jid.java index 2f4a31d9c..622b3a67c 100644 --- a/src/main/java/eu/siacs/conversations/xmpp/Jid.java +++ b/src/main/java/eu/siacs/conversations/xmpp/Jid.java @@ -45,14 +45,7 @@ public interface Jid extends Comparable, Serializable, CharSequence { } } - /** - * Creates a bare JID with only the domain part, e.g. capulet.com - * - * @param domain The domain. - * @return The JID. - * @throws NullPointerException If the domain is null. - * @throws IllegalArgumentException If the domain or local part are not valid. - */ + static Jid ofDomain(CharSequence domain) { try { return new WrappedJid(JidCreate.domainBareFrom(domain)); @@ -61,15 +54,6 @@ public interface Jid extends Comparable, Serializable, CharSequence { } } - /** - * Creates a bare JID with a local and domain part, e.g. juliet@capulet.com - * - * @param local The local part. - * @param domain The domain. - * @return The JID. - * @throws NullPointerException If the domain is null. - * @throws IllegalArgumentException If the domain or local part are not valid. - */ static Jid ofLocalAndDomain(CharSequence local, CharSequence domain) { try { return new WrappedJid( @@ -83,17 +67,19 @@ public interface Jid extends Comparable, Serializable, CharSequence { } } - /** - * Creates a JID from an unescaped string. The format must be - *

[ localpart "@" ] domainpart [ "/" resourcepart ]

- * The input string will be escaped. - * - * @param jid The JID. - * @return The JID. - * @throws NullPointerException If the jid is null. - * @throws IllegalArgumentException If the jid could not be parsed or is not valid. - * @see XEP-0106: JID Escaping - */ + static Jid ofLocalAndDomainEscaped(CharSequence local, CharSequence domain) { + try { + return new WrappedJid( + JidCreate.bareFrom( + Localpart.from(local.toString()), + Domainpart.from(domain.toString()) + ) + ); + } catch (XmppStringprepException e) { + throw new IllegalArgumentException(e); + } + } + static Jid of(CharSequence jid) { if (jid instanceof Jid) { return (Jid) jid; @@ -105,17 +91,6 @@ public interface Jid extends Comparable, Serializable, CharSequence { } } - /** - * Creates a JID from a escaped JID string. The format must be - *

[ localpart "@" ] domainpart [ "/" resourcepart ]

- * This method should be used, when parsing JIDs from the XMPP stream. - * - * @param jid The JID. - * @return The JID. - * @throws NullPointerException If the jid is null. - * @throws IllegalArgumentException If the jid could not be parsed or is not valid. - * @see XEP-0106: JID Escaping - */ static Jid ofEscaped(CharSequence jid) { try { return new WrappedJid(JidCreate.from(jid)); @@ -125,120 +100,23 @@ public interface Jid extends Comparable, Serializable, CharSequence { } } - /** - * Checks if the JID is a full JID. - *
- *

The term "full JID" refers to an XMPP address of the form <localpart@domainpart/resourcepart> (for a particular authorized client or device associated with an account) or of the form <domainpart/resourcepart> (for a particular resource or script associated with a server).

- *
- * - * @return True, if the JID is a full JID; otherwise false. - */ boolean isFullJid(); - /** - * Checks if the JID is a bare JID. - *
- *

The term "bare JID" refers to an XMPP address of the form <localpart@domainpart> (for an account at a server) or of the form <domainpart> (for a server).

- *
- * - * @return True, if the JID is a bare JID; otherwise false. - */ boolean isBareJid(); - /** - * Checks if the JID is a domain JID, i.e. if it has no local part. - * - * @return True, if the JID is a domain JID, i.e. if it has no local part. - */ boolean isDomainJid(); - /** - * Gets the bare JID representation of this JID, i.e. removes the resource part. - *
- *

The term "bare JID" refers to an XMPP address of the form <localpart@domainpart> (for an account at a server) or of the form <domainpart> (for a server).

- *
- * - * @return The bare JID. - * @see #withResource(CharSequence) - */ Jid asBareJid(); - /** - * Creates a new full JID with a resource and the same local and domain part of the current JID. - * - * @param resource The resource. - * @return The full JID with a resource. - * @throws IllegalArgumentException If the resource is not a valid resource part. - * @see #asBareJid() - */ Jid withResource(CharSequence resource); - - /** - * Gets the local part of the JID, also known as the name or node. - *
- *

3.3. Localpart

- *

The localpart of a JID is an optional identifier placed before the - * domainpart and separated from the latter by the '@' character. - * Typically, a localpart uniquely identifies the entity requesting and - * using network access provided by a server (i.e., a local account), - * although it can also represent other kinds of entities (e.g., a - * chatroom associated with a multi-user chat service [XEP-0045]). The - * entity represented by an XMPP localpart is addressed within the - * context of a specific domain (i.e., <localpart@domainpart>).

- *
- * - * @return The local part or null. - * @see #getEscapedLocal() - */ String getLocal(); - /** - * Gets the escaped local part of the JID. - * - * @return The escaped local part or null. - * @see #getLocal() - * @since 0.8.0 - */ String getEscapedLocal(); - /** - * Gets the domain part. - *
- *

3.2. Domainpart

- *

The domainpart is the primary identifier and is the only REQUIRED - * element of a JID (a mere domainpart is a valid JID). Typically, - * a domainpart identifies the "home" server to which clients connect - * for XML routing and data management functionality.

- *
- * - * @return The domain part. - */ String getDomain(); - /** - * Gets the resource part. - *
- *

3.4. Resourcepart

- *

The resourcepart of a JID is an optional identifier placed after the - * domainpart and separated from the latter by the '/' character. A - * resourcepart can modify either a <localpart@domainpart> address or a - * mere <domainpart> address. Typically, a resourcepart uniquely - * identifies a specific connection (e.g., a device or location) or - * object (e.g., an occupant in a multi-user chatroom [XEP-0045]) - * belonging to the entity associated with an XMPP localpart at a domain - * (i.e., <localpart@domainpart/resourcepart>).

- *
- * - * @return The resource part or null. - */ String getResource(); - /** - * Returns the JID in escaped form as described in XEP-0106: JID Escaping. - * - * @return The escaped JID. - * @see #toString() - */ String toEscapedString(); }