1
0
mirror of https://github.com/moparisthebest/Yaaic synced 2024-11-22 17:02:21 -05:00

(Authentication) Implementation of authentication via SASL on connect

This commit is contained in:
Sebastian Kaspari 2011-06-10 22:03:19 +02:00
parent 4739d292c5
commit 2f19eb8032
3 changed files with 56 additions and 4 deletions

View File

@ -39,6 +39,7 @@ import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.X509TrustManager;
import org.yaaic.ssl.NaiveTrustManager;
import org.yaaic.tools.Base64;
/**
* PircBot is a Java framework for writing IRC bots quickly and easily.
@ -200,6 +201,33 @@ public abstract class PircBot implements ReplyConstants {
OutputThread.sendRawLine(this, bwriter, "PASS " + password);
}
String nick = this.getName();
if (saslUsername != null) {
OutputThread.sendRawLine(this, bwriter, "CAP LS");
OutputThread.sendRawLine(this, bwriter, "CAP REQ : sasl multi-prefix");
OutputThread.sendRawLine(this, bwriter, "CAP END");
OutputThread.sendRawLine(this, bwriter, "AUTHENTICATE PLAIN");
String authString = saslUsername + "\0" + saslUsername + "\0" + saslPassword;
String authStringEncoded = Base64.encodeBytes(authString.getBytes());
while (authStringEncoded.length() >= 400) {
String toSend = authStringEncoded.substring(0, 400);
authString = authStringEncoded.substring(400);
OutputThread.sendRawLine(this, bwriter, "AUTHENTICATE " + toSend);
}
if (authStringEncoded.length() > 0) {
OutputThread.sendRawLine(this, bwriter, "AUTHENTICATE " + authStringEncoded);
} else {
OutputThread.sendRawLine(this, bwriter, "AUTHENTICATE +");
}
}
OutputThread.sendRawLine(this, bwriter, "NICK " + nick);
OutputThread.sendRawLine(this, bwriter, "USER " + this.getLogin() + " 8 * :" + this.getVersion());
@ -257,9 +285,8 @@ public abstract class PircBot implements ReplyConstants {
connect(getServer(), getPort(), getPassword());
}
/**
* Set wether SSL should be used to connect to the server
* Set wether SSL should be used to connect to the server.
*
* @author Sebastian Kaspari <sebastian@yaaic.org>
*/
@ -268,6 +295,20 @@ public abstract class PircBot implements ReplyConstants {
_useSSL = useSSL;
}
/**
* Set credentials for SASL authentication.
*
* @param username
* @param password
*
* @author Sebastian Kaspari <sebastian@yaaic.org>
*/
public void setSaslCredentials(String username, String password)
{
this.saslUsername = username;
this.saslPassword = password;
}
/**
* This method disconnects from the server cleanly by calling the
@ -3106,6 +3147,10 @@ public abstract class PircBot implements ReplyConstants {
private final Queue _outQueue = new Queue();
private long _messageDelay = 1000;
// SASL
private String saslUsername;
private String saslPassword;
// A Hashtable of channels that points to a selfreferential Hashtable of
// User objects (used to remember which users are in which channels).
private Hashtable<String, Hashtable<User, User>> _channels = new Hashtable<String, Hashtable<User, User>>();

View File

@ -399,6 +399,13 @@ public class IRCService extends Service
connection.setEncoding(server.getCharset());
}
if (server.getAuthentication().hasSaslCredentials()) {
connection.setSaslCredentials(
server.getAuthentication().getSaslUsername(),
server.getAuthentication().getSaslPassword()
);
}
if (server.getPassword() != "") {
connection.connect(server.getHost(), server.getPort(), server.getPassword());
} else {

View File

@ -38,7 +38,7 @@ public class Authentication
*/
public boolean hasNickservCredentials()
{
return this.nickservPassword != null;
return nickservPassword != null && nickservPassword.length() > 0;
}
/**
@ -48,7 +48,7 @@ public class Authentication
*/
public boolean hasSaslCredentials()
{
return this.saslUsername != null;
return saslUsername != null && saslUsername.length() > 0;
}
/**