support xmpp uris with single 'omemo' parameter for fingerprint (w/o sid)

This commit is contained in:
Daniel Gultsch 2020-01-16 18:54:56 +01:00
parent e0c034c2c9
commit c8b5b96af9
4 changed files with 218 additions and 218 deletions

View File

@ -52,12 +52,12 @@ public class WelcomeActivity extends XmppActivity {
private boolean processXmppUri(final XmppUri xmppUri) { private boolean processXmppUri(final XmppUri xmppUri) {
if (xmppUri.isValidJid()) { if (xmppUri.isValidJid()) {
final String preauth = xmppUri.getParamater("preauth"); final String preauth = xmppUri.getParameter("preauth");
final Jid jid = xmppUri.getJid(); final Jid jid = xmppUri.getJid();
final Intent intent; final Intent intent;
if (xmppUri.isAction(XmppUri.ACTION_REGISTER)) { if (xmppUri.isAction(XmppUri.ACTION_REGISTER)) {
intent = SignupUtils.getTokenRegistrationIntent(this, jid, preauth); intent = SignupUtils.getTokenRegistrationIntent(this, jid, preauth);
} else if (xmppUri.isAction(XmppUri.ACTION_ROSTER) && "y".equals(xmppUri.getParamater("ibr"))) { } else if (xmppUri.isAction(XmppUri.ACTION_ROSTER) && "y".equals(xmppUri.getParameter("ibr"))) {
intent = SignupUtils.getTokenRegistrationIntent(this, Jid.ofDomain(jid.getDomain()), preauth); intent = SignupUtils.getTokenRegistrationIntent(this, Jid.ofDomain(jid.getDomain()), preauth);
intent.putExtra(StartConversationActivity.EXTRA_INVITE_URI, xmppUri.toString()); intent.putExtra(StartConversationActivity.EXTRA_INVITE_URI, xmppUri.toString());
} else { } else {

View File

@ -324,6 +324,7 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece
if (handleErrorMessage(account, original)) { if (handleErrorMessage(account, original)) {
return; return;
} }
Log.d(Config.LOGTAG,original.toString());
final MessagePacket packet; final MessagePacket packet;
Long timestamp = null; Long timestamp = null;
boolean isCarbon = false; boolean isCarbon = false;

View File

@ -88,7 +88,7 @@ public class UriHandlerActivity extends AppCompatActivity {
final List<Jid> accounts = DatabaseBackend.getInstance(this).getAccountJids(true); final List<Jid> accounts = DatabaseBackend.getInstance(this).getAccountJids(true);
if (SignupUtils.isSupportTokenRegistry() && xmppUri.isValidJid()) { if (SignupUtils.isSupportTokenRegistry() && xmppUri.isValidJid()) {
final String preauth = xmppUri.getParamater("preauth"); final String preauth = xmppUri.getParameter("preauth");
final Jid jid = xmppUri.getJid(); final Jid jid = xmppUri.getJid();
if (xmppUri.isAction(XmppUri.ACTION_REGISTER)) { if (xmppUri.isAction(XmppUri.ACTION_REGISTER)) {
if (jid.getEscapedLocal() != null && accounts.contains(jid.asBareJid())) { if (jid.getEscapedLocal() != null && accounts.contains(jid.asBareJid())) {
@ -99,7 +99,7 @@ public class UriHandlerActivity extends AppCompatActivity {
startActivity(intent); startActivity(intent);
return; return;
} }
if (xmppUri.isAction(XmppUri.ACTION_ROSTER) && "y".equals(xmppUri.getParamater("ibr"))) { if (xmppUri.isAction(XmppUri.ACTION_ROSTER) && "y".equals(xmppUri.getParameter("ibr"))) {
intent = SignupUtils.getTokenRegistrationIntent(this, Jid.ofDomain(jid.getDomain()), preauth); intent = SignupUtils.getTokenRegistrationIntent(this, Jid.ofDomain(jid.getDomain()), preauth);
intent.putExtra(StartConversationActivity.EXTRA_INVITE_URI, xmppUri.toString()); intent.putExtra(StartConversationActivity.EXTRA_INVITE_URI, xmppUri.toString());
startActivity(intent); startActivity(intent);

View File

@ -18,241 +18,240 @@ import rocks.xmpp.addr.Jid;
public class XmppUri { public class XmppUri {
protected Uri uri; public static final String ACTION_JOIN = "join";
protected String jid; public static final String ACTION_MESSAGE = "message";
private List<Fingerprint> fingerprints = new ArrayList<>(); public static final String ACTION_REGISTER = "register";
private Map<String,String> parameters = Collections.emptyMap(); public static final String ACTION_ROSTER = "roster";
private boolean safeSource = true; private static final String OMEMO_URI_PARAM = "omemo-sid-";
protected Uri uri;
protected String jid;
private List<Fingerprint> fingerprints = new ArrayList<>();
private Map<String, String> parameters = Collections.emptyMap();
private boolean safeSource = true;
private static final String OMEMO_URI_PARAM = "omemo-sid-"; public XmppUri(String uri) {
try {
parse(Uri.parse(uri));
} catch (IllegalArgumentException e) {
try {
jid = Jid.of(uri).asBareJid().toString();
} catch (IllegalArgumentException e2) {
jid = null;
}
}
}
public static final String ACTION_JOIN = "join"; public XmppUri(Uri uri) {
public static final String ACTION_MESSAGE = "message"; parse(uri);
public static final String ACTION_REGISTER = "register"; }
public static final String ACTION_ROSTER = "roster";
public XmppUri(String uri) { public XmppUri(Uri uri, boolean safeSource) {
try { this.safeSource = safeSource;
parse(Uri.parse(uri)); parse(uri);
} catch (IllegalArgumentException e) { }
try {
jid = Jid.of(uri).asBareJid().toString();
} catch (IllegalArgumentException e2) {
jid = null;
}
}
}
public XmppUri(Uri uri) { private static Map<String, String> parseParameters(final String query, final char seperator) {
parse(uri); final ImmutableMap.Builder<String, String> builder = new ImmutableMap.Builder<>();
} final String[] pairs = query == null ? new String[0] : query.split(String.valueOf(seperator));
for (String pair : pairs) {
final String[] parts = pair.split("=", 2);
if (parts.length == 0) {
continue;
}
final String key = parts[0].toLowerCase(Locale.US);
final String value;
if (parts.length == 2) {
String decoded;
try {
decoded = URLDecoder.decode(parts[1], "UTF-8");
} catch (UnsupportedEncodingException e) {
decoded = "";
}
value = decoded;
} else {
value = "";
}
builder.put(key, value);
}
return builder.build();
}
public XmppUri(Uri uri, boolean safeSource) { private static List<Fingerprint> parseFingerprints(Map<String, String> parameters) {
this.safeSource = safeSource; ImmutableList.Builder<Fingerprint> builder = new ImmutableList.Builder<>();
parse(uri); for (Map.Entry<String, String> parameter : parameters.entrySet()) {
} final String key = parameter.getKey();
final String value = parameter.getValue().toLowerCase(Locale.US);
if (key.startsWith(OMEMO_URI_PARAM)) {
try {
final int id = Integer.parseInt(key.substring(OMEMO_URI_PARAM.length()));
builder.add(new Fingerprint(FingerprintType.OMEMO, value, id));
} catch (Exception e) {
//ignoring invalid device id
}
} else if ("omemo".equals(key)) {
builder.add(new Fingerprint(FingerprintType.OMEMO, value, 0));
}
}
return builder.build();
}
public boolean isSafeSource() { public static String getFingerprintUri(final String base, final List<XmppUri.Fingerprint> fingerprints, char separator) {
return safeSource; final StringBuilder builder = new StringBuilder(base);
} builder.append('?');
for (int i = 0; i < fingerprints.size(); ++i) {
XmppUri.FingerprintType type = fingerprints.get(i).type;
if (type == XmppUri.FingerprintType.OMEMO) {
builder.append(XmppUri.OMEMO_URI_PARAM);
builder.append(fingerprints.get(i).deviceId);
}
builder.append('=');
builder.append(fingerprints.get(i).fingerprint);
if (i != fingerprints.size() - 1) {
builder.append(separator);
}
}
return builder.toString();
}
protected void parse(final Uri uri) { private static String lameUrlDecode(String url) {
if (uri == null) { return url.replace("%23", "#").replace("%25", "%");
return; }
}
this.uri = uri;
String scheme = uri.getScheme();
String host = uri.getHost();
List<String> segments = uri.getPathSegments();
if ("https".equalsIgnoreCase(scheme) && "conversations.im".equalsIgnoreCase(host)) {
if (segments.size() >= 2 && segments.get(1).contains("@")) {
// sample : https://conversations.im/i/foo@bar.com
try {
jid = Jid.of(lameUrlDecode(segments.get(1))).toString();
} catch (Exception e) {
jid = null;
}
} else if (segments.size() >= 3) {
// sample : https://conversations.im/i/foo/bar.com
jid = segments.get(1) + "@" + segments.get(2);
}
if (segments.size() > 1 && "j".equalsIgnoreCase(segments.get(0))) {
this.parameters = ImmutableMap.of(ACTION_JOIN, "");
}
final Map<String,String> parameters = parseParameters(uri.getQuery(), '&');
this.fingerprints = parseFingerprints(parameters);
} else if ("xmpp".equalsIgnoreCase(scheme)) {
// sample: xmpp:foo@bar.com
this.parameters = parseParameters(uri.getQuery(), ';');
if (uri.getAuthority() != null) {
jid = uri.getAuthority();
} else {
final String[] parts = uri.getSchemeSpecificPart().split("\\?");
if (parts.length > 0) {
jid = parts[0];
} else {
return;
}
}
this.fingerprints = parseFingerprints(parameters);
} else if ("imto".equalsIgnoreCase(scheme)) {
// sample: imto://xmpp/foo@bar.com
try {
jid = URLDecoder.decode(uri.getEncodedPath(), "UTF-8").split("/")[1].trim();
} catch (final UnsupportedEncodingException ignored) {
jid = null;
}
} else {
try {
jid = Jid.of(uri.toString()).asBareJid().toString();
} catch (final IllegalArgumentException ignored) {
jid = null;
}
}
}
public static String lameUrlEncode(String url) {
return url.replace("%", "%25").replace("#", "%23");
}
private static Map<String,String> parseParameters(final String query, final char seperator) { public boolean isSafeSource() {
final ImmutableMap.Builder<String,String> builder = new ImmutableMap.Builder<>(); return safeSource;
final String[] pairs = query == null ? new String[0] : query.split(String.valueOf(seperator)); }
for (String pair : pairs) {
final String[] parts = pair.split("=", 2);
if (parts.length == 0) {
continue;
}
final String key = parts[0].toLowerCase(Locale.US);
final String value;
if (parts.length == 2) {
String decoded;
try {
decoded = URLDecoder.decode(parts[1],"UTF-8");
} catch (UnsupportedEncodingException e) {
decoded = "";
}
value = decoded;
} else {
value = "";
}
builder.put(key, value);
}
return builder.build();
}
@Override protected void parse(final Uri uri) {
@NonNull if (uri == null) {
public String toString() { return;
if (uri != null) { }
return uri.toString(); this.uri = uri;
} String scheme = uri.getScheme();
return ""; String host = uri.getHost();
} List<String> segments = uri.getPathSegments();
if ("https".equalsIgnoreCase(scheme) && "conversations.im".equalsIgnoreCase(host)) {
if (segments.size() >= 2 && segments.get(1).contains("@")) {
// sample : https://conversations.im/i/foo@bar.com
try {
jid = Jid.of(lameUrlDecode(segments.get(1))).toString();
} catch (Exception e) {
jid = null;
}
} else if (segments.size() >= 3) {
// sample : https://conversations.im/i/foo/bar.com
jid = segments.get(1) + "@" + segments.get(2);
}
if (segments.size() > 1 && "j".equalsIgnoreCase(segments.get(0))) {
this.parameters = ImmutableMap.of(ACTION_JOIN, "");
}
final Map<String, String> parameters = parseParameters(uri.getQuery(), '&');
this.fingerprints = parseFingerprints(parameters);
} else if ("xmpp".equalsIgnoreCase(scheme)) {
// sample: xmpp:foo@bar.com
this.parameters = parseParameters(uri.getQuery(), ';');
if (uri.getAuthority() != null) {
jid = uri.getAuthority();
} else {
final String[] parts = uri.getSchemeSpecificPart().split("\\?");
if (parts.length > 0) {
jid = parts[0];
} else {
return;
}
}
this.fingerprints = parseFingerprints(parameters);
} else if ("imto".equalsIgnoreCase(scheme)) {
// sample: imto://xmpp/foo@bar.com
try {
jid = URLDecoder.decode(uri.getEncodedPath(), "UTF-8").split("/")[1].trim();
} catch (final UnsupportedEncodingException ignored) {
jid = null;
}
} else {
try {
jid = Jid.of(uri.toString()).asBareJid().toString();
} catch (final IllegalArgumentException ignored) {
jid = null;
}
}
}
private static List<Fingerprint> parseFingerprints(Map<String,String> parameters) { @Override
ImmutableList.Builder<Fingerprint> builder = new ImmutableList.Builder<>(); @NonNull
for (Map.Entry<String, String> parameter : parameters.entrySet()) { public String toString() {
final String key = parameter.getKey(); if (uri != null) {
final String value = parameter.getValue().toLowerCase(Locale.US); return uri.toString();
if (key.startsWith(OMEMO_URI_PARAM)) { }
try { return "";
final int id = Integer.parseInt(key.substring(OMEMO_URI_PARAM.length())); }
builder.add(new Fingerprint(FingerprintType.OMEMO, value, id));
} catch (Exception e) {
//ignoring invalid device id
}
}
}
return builder.build();
}
public boolean isAction(final String action) { public boolean isAction(final String action) {
return parameters.containsKey(action); return parameters.containsKey(action);
} }
public Jid getJid() { public Jid getJid() {
try { try {
return this.jid == null ? null : Jid.of(this.jid); return this.jid == null ? null : Jid.of(this.jid);
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
return null; return null;
} }
} }
public boolean isValidJid() { public boolean isValidJid() {
if (jid == null) { if (jid == null) {
return false; return false;
} }
try { try {
Jid.of(jid); Jid.of(jid);
return true; return true;
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
return false; return false;
} }
} }
public String getBody() { public String getBody() {
return parameters.get("body"); return parameters.get("body");
} }
public String getName() { public String getName() {
return parameters.get("name"); return parameters.get("name");
} }
public String getParamater(String key) { public String getParameter(String key) {
return this.parameters.get(key); return this.parameters.get(key);
} }
public List<Fingerprint> getFingerprints() { public List<Fingerprint> getFingerprints() {
return this.fingerprints; return this.fingerprints;
} }
public boolean hasFingerprints() { public boolean hasFingerprints() {
return fingerprints.size() > 0; return fingerprints.size() > 0;
} }
public enum FingerprintType { public enum FingerprintType {
OMEMO OMEMO
} }
public static String getFingerprintUri(String base, List<XmppUri.Fingerprint> fingerprints, char separator) { public static class Fingerprint {
StringBuilder builder = new StringBuilder(base); public final FingerprintType type;
builder.append('?'); public final String fingerprint;
for (int i = 0; i < fingerprints.size(); ++i) { final int deviceId;
XmppUri.FingerprintType type = fingerprints.get(i).type;
if (type == XmppUri.FingerprintType.OMEMO) {
builder.append(XmppUri.OMEMO_URI_PARAM);
builder.append(fingerprints.get(i).deviceId);
}
builder.append('=');
builder.append(fingerprints.get(i).fingerprint);
if (i != fingerprints.size() - 1) {
builder.append(separator);
}
}
return builder.toString();
}
public static class Fingerprint { public Fingerprint(FingerprintType type, String fingerprint, int deviceId) {
public final FingerprintType type; this.type = type;
public final String fingerprint; this.fingerprint = fingerprint;
final int deviceId; this.deviceId = deviceId;
}
public Fingerprint(FingerprintType type, String fingerprint, int deviceId) { @NonNull
this.type = type; @Override
this.fingerprint = fingerprint; public String toString() {
this.deviceId = deviceId; return type.toString() + ": " + fingerprint + (deviceId != 0 ? " " + deviceId : "");
} }
}
@NonNull
@Override
public String toString() {
return type.toString() + ": " + fingerprint + (deviceId != 0 ? " " + deviceId : "");
}
}
private static String lameUrlDecode(String url) {
return url.replace("%23", "#").replace("%25", "%");
}
public static String lameUrlEncode(String url) {
return url.replace("%", "%25").replace("#", "%23");
}
} }