smack.sasl.patch 2.07 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
Index: source/org/jivesoftware/smack/sasl/SASLDigestMD5Mechanism.java
===================================================================
--- source/org/jivesoftware/smack/sasl/SASLDigestMD5Mechanism.java	(revision 11361)
+++ source/org/jivesoftware/smack/sasl/SASLDigestMD5Mechanism.java	(working copy)
@@ -20,7 +20,9 @@
 package org.jivesoftware.smack.sasl;
 
 import org.jivesoftware.smack.SASLAuthentication;
+import org.jivesoftware.smack.util.Base64;
 
+import java.io.IOException;
 /**
  * Implementation of the SASL DIGEST-MD5 mechanism
  *
@@ -35,4 +37,43 @@
     protected String getName() {
         return "DIGEST-MD5";
     }
-}
+
+
+    /**
+     * The server is challenging the SASL mechanism for the stanza he just sent. Send a
+     * response to the server's challenge.
+     *
+     * @param challenge a base64 encoded string representing the challenge.
+     * @throws IOException if an exception sending the response occurs.
+     */
+    public void challengeReceived(String challenge) throws IOException {
+        // Build the challenge response stanza encoding the response text
+        StringBuilder stanza = new StringBuilder();
+
+        byte response[];
+        if(challenge != null) {
+            response = sc.evaluateChallenge(Base64.decode(challenge));
+        } else {
+            response = sc.evaluateChallenge(null);
+        }
+
+       String authenticationText = null;
+       if (response != null) {
+            authenticationText = Base64.encodeBytes(response,Base64.DONT_BREAK_LINES);
+            if(authenticationText.equals("")) {
+                authenticationText = "=";
+            }
+       }
+
+       if (authenticationText != null) {
+            stanza.append("<response xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">");
+            stanza.append(authenticationText);
+            stanza.append("</response>");
+       } else {
+            stanza.append("<response xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\" />");
+       }
+
+        // Send the authentication to the server
+        getSASLAuthentication().send(stanza.toString());
+    }
+ }