1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-12-23 21:17:58 +01:00

SMACK-264 Fix NPE in SASLMechanism

git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@11256 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
Günther Niess 2009-09-09 11:10:34 +00:00 committed by niess
parent 197aa187d9
commit 8dcc226bb5
2 changed files with 76 additions and 24 deletions

View file

@ -23,6 +23,7 @@ package org.jivesoftware.smack;
import org.jivesoftware.smack.filter.PacketIDFilter;
import org.jivesoftware.smack.packet.Bind;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.Session;
import org.jivesoftware.smack.sasl.*;
@ -528,9 +529,8 @@ public class SASLAuthentication implements UserAuthentication {
}
}
public void send(String stanza) throws IOException {
connection.writer.write(stanza);
connection.writer.flush();
public void send(Packet stanza) {
connection.sendPacket(stanza);
}
/**

View file

@ -22,6 +22,7 @@ package org.jivesoftware.smack.sasl;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.SASLAuthentication;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.util.Base64;
import java.io.IOException;
@ -110,24 +111,18 @@ public abstract class SASLMechanism implements CallbackHandler {
}
protected void authenticate() throws IOException, XMPPException {
StringBuilder stanza = new StringBuilder();
stanza.append("<auth mechanism=\"").append(getName());
stanza.append("\" xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">");
String authenticationText = null;
try {
if(sc.hasInitialResponse()) {
byte[] response = sc.evaluateChallenge(new byte[0]);
String authenticationText = Base64.encodeBytes(response,Base64.DONT_BREAK_LINES);
if(authenticationText != null && !authenticationText.equals("")) {
stanza.append(authenticationText);
}
authenticationText = Base64.encodeBytes(response,Base64.DONT_BREAK_LINES);
}
} catch (SaslException e) {
throw new XMPPException("SASL authentication failed", e);
}
stanza.append("</auth>");
// Send the authentication to the server
getSASLAuthentication().send(stanza.toString());
getSASLAuthentication().send(new AuthMechanism(getName(), authenticationText));
}
@ -139,27 +134,23 @@ public abstract class SASLMechanism implements CallbackHandler {
* @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);
response = sc.evaluateChallenge(new byte[0]);
}
String authenticationText = Base64.encodeBytes(response,Base64.DONT_BREAK_LINES);
if(authenticationText.equals("")) {
authenticationText = "=";
Packet responseStanza;
if (response == null) {
responseStanza = new Response();
}
else {
responseStanza = new Response(Base64.encodeBytes(response,Base64.DONT_BREAK_LINES));
}
stanza.append("<response xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">");
stanza.append(authenticationText);
stanza.append("</response>");
// Send the authentication to the server
getSASLAuthentication().send(stanza.toString());
getSASLAuthentication().send(responseStanza);
}
/**
@ -196,4 +187,65 @@ public abstract class SASLMechanism implements CallbackHandler {
}
}
}
/**
* Initiating SASL authentication by select a mechanism.
*/
public class AuthMechanism extends Packet {
final private String name;
final private String authenticationText;
public AuthMechanism(String name, String authenticationText) {
if (name == null) {
throw new NullPointerException("SASL mechanism name shouldn't be null.");
}
this.name = name;
this.authenticationText = authenticationText;
}
public String toXML() {
StringBuilder stanza = new StringBuilder();
stanza.append("<auth mechanism=\"").append(name);
stanza.append("\" xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">");
if (authenticationText != null &&
authenticationText.trim().length() > 0) {
stanza.append(authenticationText);
}
stanza.append("</auth>");
return stanza.toString();
}
}
/**
* A SASL response stanza.
*/
public class Response extends Packet {
final private String authenticationText;
public Response() {
authenticationText = null;
}
public Response(String authenticationText) {
if (authenticationText == null || authenticationText.trim().length() == 0) {
this.authenticationText = null;
}
else {
this.authenticationText = authenticationText;
}
}
public String toXML() {
StringBuilder stanza = new StringBuilder();
stanza.append("<response xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">");
if (authenticationText == null) {
stanza.append(authenticationText);
}
else {
stanza.append("=");
}
stanza.append("</response>");
return stanza.toString();
}
}
}