ground work to support multiple auth mechanisms

This commit is contained in:
Daniel Gultsch 2014-03-30 21:21:55 +02:00
parent e8290d52b1
commit ad3e23fa7c
1 changed files with 26 additions and 2 deletions

View File

@ -212,6 +212,9 @@ public class XmppConnection implements Runnable {
} else if (nextTag.isStart("failure")) { } else if (nextTag.isStart("failure")) {
tagReader.readElement(nextTag); tagReader.readElement(nextTag);
changeStatus(Account.STATUS_UNAUTHORIZED); 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")) { } else if (nextTag.isStart("enabled")) {
this.stanzasSent = 0; this.stanzasSent = 0;
Element enabled = tagReader.readElement(nextTag); 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(), String saslString = CryptoHelper.saslPlain(account.getUsername(),
account.getPassword()); account.getPassword());
Element auth = new Element("auth"); Element auth = new Element("auth");
@ -456,6 +459,13 @@ public class XmppConnection implements Runnable {
tagWriter.writeElement(auth); 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) private void processStreamFeatures(Tag currentTag)
throws XmlPullParserException, IOException { throws XmlPullParserException, IOException {
this.streamFeatures = tagReader.readElement(currentTag); this.streamFeatures = tagReader.readElement(currentTag);
@ -469,7 +479,13 @@ public class XmppConnection implements Runnable {
disconnect(true); disconnect(true);
} else if (this.streamFeatures.hasChild("mechanisms") } else if (this.streamFeatures.hasChild("mechanisms")
&& shouldAuthenticate) { && shouldAuthenticate) {
sendSaslAuth(); List<String> 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) { } else if (this.streamFeatures.hasChild("sm") && streamId != null) {
Log.d(LOGTAG,"found old stream id. trying to remuse"); Log.d(LOGTAG,"found old stream id. trying to remuse");
ResumePacket resume = new ResumePacket(this.streamId,stanzasReceived); ResumePacket resume = new ResumePacket(this.streamId,stanzasReceived);
@ -485,6 +501,14 @@ public class XmppConnection implements Runnable {
} }
} }
private List<String> extractMechanisms(Element stream) {
ArrayList<String> mechanisms = new ArrayList<String>(stream.getChildren().size());
for(Element child : stream.getChildren()) {
mechanisms.add(child.getContent());
}
return mechanisms;
}
private void sendRegistryRequest() { private void sendRegistryRequest() {
IqPacket register = new IqPacket(IqPacket.TYPE_GET); IqPacket register = new IqPacket(IqPacket.TYPE_GET);
register.query("jabber:iq:register"); register.query("jabber:iq:register");