Skip to content

Commit

Permalink
(Authentication) Implementation of authentication via SASL on connect
Browse files Browse the repository at this point in the history
  • Loading branch information
pocmo committed Jun 10, 2011
1 parent 4739d29 commit 2f19eb8
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 4 deletions.
49 changes: 47 additions & 2 deletions application/src/org/jibble/pircbot/PircBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ General Public License (GPL) and the www.jibble.org Commercial License.
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.
Expand Down Expand Up @@ -200,6 +201,33 @@ public final synchronized void connect(String hostname, int port, String passwor
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());

Expand Down Expand Up @@ -257,9 +285,8 @@ public final synchronized void reconnect() throws IOException, IrcException, Nic
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 <[email protected]>
*/
Expand All @@ -268,6 +295,20 @@ public void setUseSSL(boolean useSSL)
_useSSL = useSSL;
}

/**
* Set credentials for SASL authentication.
*
* @param username
* @param password
*
* @author Sebastian Kaspari <[email protected]>
*/
public void setSaslCredentials(String username, String password)
{
this.saslUsername = username;
this.saslPassword = password;
}


/**
* This method disconnects from the server cleanly by calling the
Expand Down Expand Up @@ -3106,6 +3147,10 @@ else if (userMode == VOICE_REMOVE) {
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>>();
Expand Down
7 changes: 7 additions & 0 deletions application/src/org/yaaic/irc/IRCService.java
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,13 @@ public void run() {
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 {
Expand Down
4 changes: 2 additions & 2 deletions application/src/org/yaaic/model/Authentication.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class Authentication
*/
public boolean hasNickservCredentials()
{
return this.nickservPassword != null;
return nickservPassword != null && nickservPassword.length() > 0;
}

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

/**
Expand Down

0 comments on commit 2f19eb8

Please sign in to comment.