1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-26 22:12:05 +01:00

Formatting, Javadoc and other minor fixes.

git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@10843 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
Matt Tucker 2008-10-24 03:51:21 +00:00 committed by matt
parent 85c5f18a09
commit c6d72f8b01

View file

@ -35,8 +35,6 @@ import org.jivesoftware.smack.sasl.SASLGSSAPIMechanism;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import javax.security.auth.callback.CallbackHandler; import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.UnsupportedCallbackException;
import java.util.*; import java.util.*;
/** /**
@ -129,27 +127,27 @@ public class SASLAuthentication implements UserAuthentication {
} }
/** Registers a new SASL mechanism in the specified preference position. The client will try /**
* Registers a new SASL mechanism in the specified preference position. The client will try
* to authenticate using the most prefered SASL mechanism that is also supported by the server. * to authenticate using the most prefered SASL mechanism that is also supported by the server.
* The SASL mechanism must be registered via {@link #registerSASLMechanism(String, Class) * The SASL mechanism must be registered via {@link #registerSASLMechanism(String, Class)}
* *
* @param name common name of the SASL mechanism. E.g.: PLAIN, DIGEST-MD5 or KERBEROS_V4. * @param name common name of the SASL mechanism. E.g.: PLAIN, DIGEST-MD5 or KERBEROS_V4.
*/ */
public static void supportSASLMechanism(String name) { public static void supportSASLMechanism(String name) {
mechanismsPreferences.add(0, name); mechanismsPreferences.add(0, name);
} }
/** Registers a new SASL mechanism in the specified preference position. The client will try /**
* Registers a new SASL mechanism in the specified preference position. The client will try
* to authenticate using the most prefered SASL mechanism that is also supported by the server. * to authenticate using the most prefered SASL mechanism that is also supported by the server.
* Use the <tt>index</tt> parameter to set the level of preference of the new SASL mechanism. * Use the <tt>index</tt> parameter to set the level of preference of the new SASL mechanism.
* A value of 0 means that the mechanism is the most prefered one. The SASL mechanism must be * A value of 0 means that the mechanism is the most prefered one. The SASL mechanism must be
* registered via {@link #registerSASLMechanism(String, Class) * registered via {@link #registerSASLMechanism(String, Class)}
* *
* @param index preference position amongst all the implemented SASL mechanism. Starts with 0. * @param name common name of the SASL mechanism. E.g.: PLAIN, DIGEST-MD5 or KERBEROS_V4.
* @param name common name of the SASL mechanism. E.g.: PLAIN, DIGEST-MD5 or KERBEROS_V4. * @param index preference position amongst all the implemented SASL mechanism. Starts with 0.
*/ */
public static void supportSASLMechanism(String name, int index) { public static void supportSASLMechanism(String name, int index) {
mechanismsPreferences.add(index, name); mechanismsPreferences.add(index, name);
} }
@ -219,20 +217,20 @@ public class SASLAuthentication implements UserAuthentication {
public String authenticate(String username, String resource, CallbackHandler cbh) public String authenticate(String username, String resource, CallbackHandler cbh)
throws XMPPException { throws XMPPException {
// Locate the SASLMechanism to use // Locate the SASLMechanism to use
Class selected = null; String selectedMechanism = null;
for (String mechanism : mechanismsPreferences) { for (String mechanism : mechanismsPreferences) {
if (implementedMechanisms.containsKey(mechanism) && if (implementedMechanisms.containsKey(mechanism) &&
serverMechanisms.contains(mechanism)) { serverMechanisms.contains(mechanism)) {
selected = implementedMechanisms.get(mechanism); selectedMechanism = mechanism;
break; break;
} }
} }
if (selected != null) { if (selectedMechanism != null) {
// A SASL mechanism was found. Authenticate using the selected mechanism and then // A SASL mechanism was found. Authenticate using the selected mechanism and then
// proceed to bind a resource // proceed to bind a resource
try { try {
Constructor constructor = selected Class mechanismClass = implementedMechanisms.get(selectedMechanism);
.getConstructor(new Class[]{SASLAuthentication.class}); Constructor constructor = mechanismClass.getConstructor(SASLAuthentication.class);
currentMechanism = (SASLMechanism) constructor.newInstance(this); currentMechanism = (SASLMechanism) constructor.newInstance(this);
// Trigger SASL authentication with the selected mechanism. We use // Trigger SASL authentication with the selected mechanism. We use
// connection.getHost() since GSAPI requires the FQDN of the server, which // connection.getHost() since GSAPI requires the FQDN of the server, which
@ -244,7 +242,9 @@ public class SASLAuthentication implements UserAuthentication {
if (!saslNegotiated && !saslFailed) { if (!saslNegotiated && !saslFailed) {
try { try {
wait(30000); wait(30000);
} catch (InterruptedException e) { }
catch (InterruptedException e) {
// Ignore
} }
} }
} }
@ -252,7 +252,8 @@ public class SASLAuthentication implements UserAuthentication {
if (saslFailed) { if (saslFailed) {
// SASL authentication failed and the server may have closed the connection // SASL authentication failed and the server may have closed the connection
// so throw an exception // so throw an exception
throw new XMPPException("SASL authentication failed"); throw new XMPPException("SASL authentication failed using mechanism " +
selectedMechanism);
} }
if (saslNegotiated) { if (saslNegotiated) {
@ -268,7 +269,8 @@ public class SASLAuthentication implements UserAuthentication {
catch (Exception e) { catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
} else { }
else {
throw new XMPPException("SASL Authentication failed. No known authentication mechanisims."); throw new XMPPException("SASL Authentication failed. No known authentication mechanisims.");
} }
throw new XMPPException("SASL authentication failed"); throw new XMPPException("SASL authentication failed");
@ -291,20 +293,20 @@ public class SASLAuthentication implements UserAuthentication {
public String authenticate(String username, String password, String resource) public String authenticate(String username, String password, String resource)
throws XMPPException { throws XMPPException {
// Locate the SASLMechanism to use // Locate the SASLMechanism to use
Class selected = null; String selectedMechanism = null;
for (String mechanism : mechanismsPreferences) { for (String mechanism : mechanismsPreferences) {
if (implementedMechanisms.containsKey(mechanism) && if (implementedMechanisms.containsKey(mechanism) &&
serverMechanisms.contains(mechanism)) { serverMechanisms.contains(mechanism)) {
selected = implementedMechanisms.get(mechanism); selectedMechanism = mechanism;
break; break;
} }
} }
if (selected != null) { if (selectedMechanism != null) {
// A SASL mechanism was found. Authenticate using the selected mechanism and then // A SASL mechanism was found. Authenticate using the selected mechanism and then
// proceed to bind a resource // proceed to bind a resource
try { try {
Constructor constructor = selected Class mechanismClass = implementedMechanisms.get(selectedMechanism);
.getConstructor(new Class[]{SASLAuthentication.class}); Constructor constructor = mechanismClass.getConstructor(SASLAuthentication.class);
currentMechanism = (SASLMechanism) constructor.newInstance(this); currentMechanism = (SASLMechanism) constructor.newInstance(this);
// Trigger SASL authentication with the selected mechanism. We use // Trigger SASL authentication with the selected mechanism. We use
// connection.getHost() since GSAPI requires the FQDN of the server, which // connection.getHost() since GSAPI requires the FQDN of the server, which
@ -316,7 +318,9 @@ public class SASLAuthentication implements UserAuthentication {
if (!saslNegotiated && !saslFailed) { if (!saslNegotiated && !saslFailed) {
try { try {
wait(30000); wait(30000);
} catch (InterruptedException e) { }
catch (InterruptedException e) {
// Ignore
} }
} }
} }
@ -324,13 +328,15 @@ public class SASLAuthentication implements UserAuthentication {
if (saslFailed) { if (saslFailed) {
// SASL authentication failed and the server may have closed the connection // SASL authentication failed and the server may have closed the connection
// so throw an exception // so throw an exception
throw new XMPPException("SASL authentication failed"); throw new XMPPException("SASL authentication failed using mechanism " +
selectedMechanism);
} }
if (saslNegotiated) { if (saslNegotiated) {
// Bind a resource for this connection and // Bind a resource for this connection and
return bindResourceAndEstablishSession(resource); return bindResourceAndEstablishSession(resource);
} else { }
else {
// SASL authentication failed so try a Non-SASL authentication // SASL authentication failed so try a Non-SASL authentication
return new NonSASLAuthentication(connection) return new NonSASLAuthentication(connection)
.authenticate(username, password, resource); .authenticate(username, password, resource);
@ -345,7 +351,8 @@ public class SASLAuthentication implements UserAuthentication {
return new NonSASLAuthentication(connection) return new NonSASLAuthentication(connection)
.authenticate(username, password, resource); .authenticate(username, password, resource);
} }
} else { }
else {
// No SASL method was found so try a Non-SASL authentication // No SASL method was found so try a Non-SASL authentication
return new NonSASLAuthentication(connection).authenticate(username, password, resource); return new NonSASLAuthentication(connection).authenticate(username, password, resource);
} }
@ -372,7 +379,9 @@ public class SASLAuthentication implements UserAuthentication {
if (!saslNegotiated && !saslFailed) { if (!saslNegotiated && !saslFailed) {
try { try {
wait(5000); wait(5000);
} catch (InterruptedException e) { }
catch (InterruptedException e) {
// Ignore
} }
} }
} }
@ -401,7 +410,9 @@ public class SASLAuthentication implements UserAuthentication {
if (!resourceBinded) { if (!resourceBinded) {
try { try {
wait(30000); wait(30000);
} catch (InterruptedException e) { }
catch (InterruptedException e) {
// Ignore
} }
} }
} }