From ad3e23fa7c4bb2521c860f218dfa0b950285790d Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Sun, 30 Mar 2014 21:21:55 +0200 Subject: [PATCH] ground work to support multiple auth mechanisms --- .../conversations/xmpp/XmppConnection.java | 28 +++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/eu/siacs/conversations/xmpp/XmppConnection.java b/src/eu/siacs/conversations/xmpp/XmppConnection.java index a32256ff4..36bd4be22 100644 --- a/src/eu/siacs/conversations/xmpp/XmppConnection.java +++ b/src/eu/siacs/conversations/xmpp/XmppConnection.java @@ -212,6 +212,9 @@ public class XmppConnection implements Runnable { } else if (nextTag.isStart("failure")) { tagReader.readElement(nextTag); changeStatus(Account.STATUS_UNAUTHORIZED); + } else if (nextTag.isStart("challenge")) { + String challange = tagReader.readElement(nextTag).getContent(); + Log.d(LOGTAG,"a challange arrived! "+challange); } else if (nextTag.isStart("enabled")) { this.stanzasSent = 0; Element enabled = tagReader.readElement(nextTag); @@ -446,7 +449,7 @@ public class XmppConnection implements Runnable { } } - private void sendSaslAuth() throws IOException, XmlPullParserException { + private void sendSaslAuthPlain() throws IOException { String saslString = CryptoHelper.saslPlain(account.getUsername(), account.getPassword()); Element auth = new Element("auth"); @@ -455,6 +458,13 @@ public class XmppConnection implements Runnable { auth.setContent(saslString); tagWriter.writeElement(auth); } + + private void sendSaslAuthDigestMd5() throws IOException { + Element auth = new Element("auth"); + auth.setAttribute("xmlns", "urn:ietf:params:xml:ns:xmpp-sasl"); + auth.setAttribute("mechanism", "DIGEST-MD5"); + tagWriter.writeElement(auth); + } private void processStreamFeatures(Tag currentTag) throws XmlPullParserException, IOException { @@ -469,7 +479,13 @@ public class XmppConnection implements Runnable { disconnect(true); } else if (this.streamFeatures.hasChild("mechanisms") && shouldAuthenticate) { - sendSaslAuth(); + List mechanisms = extractMechanisms( streamFeatures.findChild("mechanisms")); + Log.d(LOGTAG,account.getJid()+": "+mechanisms.toString()); + if (mechanisms.contains("PLAIN")) { + sendSaslAuthPlain(); + } else if (mechanisms.contains("DIGEST-MD5")) { + sendSaslAuthDigestMd5(); + } } else if (this.streamFeatures.hasChild("sm") && streamId != null) { Log.d(LOGTAG,"found old stream id. trying to remuse"); ResumePacket resume = new ResumePacket(this.streamId,stanzasReceived); @@ -485,6 +501,14 @@ public class XmppConnection implements Runnable { } } + private List extractMechanisms(Element stream) { + ArrayList mechanisms = new ArrayList(stream.getChildren().size()); + for(Element child : stream.getChildren()) { + mechanisms.add(child.getContent()); + } + return mechanisms; + } + private void sendRegistryRequest() { IqPacket register = new IqPacket(IqPacket.TYPE_GET); register.query("jabber:iq:register");