Commit 24b38ada authored by Gaston Dombiak's avatar Gaston Dombiak Committed by gaston

Optimizations to the login and authentication process. JM-203


git-svn-id: http://svn.igniterealtime.org/svn/repos/messenger/trunk@1116 b35dd754-fafc-0310-a699-88a17e54d16e
parent b3d143c8
......@@ -407,9 +407,11 @@ public class SessionManager extends BasicModule {
private void broadcastPresenceToOtherResource(ClientSession session)
throws UserNotFoundException, UnauthorizedException {
Presence presence = null;
Collection<ClientSession> availableSession;
SessionMap sessionMap = sessions.get(session.getUsername());
if (sessionMap != null) {
for (ClientSession userSession : sessionMap.getAvailableSessions()) {
availableSession = new ArrayList<ClientSession>(sessionMap.getAvailableSessions());
for (ClientSession userSession : availableSession) {
if (userSession != session) {
// Send the presence of an existing session to the session that has just changed
// the presence
......@@ -563,10 +565,8 @@ public class SessionManager extends BasicModule {
return session;
}
public boolean isActiveRoute(JID route) {
public boolean isActiveRoute(String username, String resource) {
boolean hasRoute = false;
String resource = route.getResource();
String username = route.getNode();
if (username == null || "".equals(username)) {
if (resource != null) {
......@@ -607,24 +607,47 @@ public class SessionManager extends BasicModule {
* @return the <code>Session</code> associated with the JID.
*/
public ClientSession getSession(JID from) {
// Return null if the JID is null or belongs to a foreign server. If the server is
// Return null if the JID is null
if (from == null) {
return null;
}
return getSession(from.getNode(), from.getDomain(), from.getResource());
}
/**
* Returns the session responsible for this JID data.
*
* @param username the username of the JID.
* @param domain the username of the JID.
* @param resource the username of the JID.
* @return the <code>Session</code> associated with the JID data.
*/
public ClientSession getSession(String username, String domain, String resource) {
// Return null if the JID's data belongs to a foreign server. If the server is
// shutting down then serverName will be null so answer null too in this case.
if (from == null || serverName == null || !serverName.equals(from.getDomain())) {
if (serverName == null || !serverName.equals(domain)) {
return null;
}
ClientSession session = null;
// Build a JID represention based on the given JID data
StringBuilder buf = new StringBuilder();
if (username != null) {
buf.append(username).append("@");
}
buf.append(domain);
if (resource != null) {
buf.append("/").append(resource);
}
// Initially Check preAuthenticated Sessions
session = preAuthenticatedSessions.get(from.toString());
session = preAuthenticatedSessions.get(buf.toString());
if(session != null){
return (ClientSession)session;
}
String resource = from.getResource();
if (resource == null) {
return null;
}
String username = from.getNode();
if (username == null || "".equals(username)) {
session = anonymousSessions.get(resource);
}
......
......@@ -13,9 +13,6 @@ package org.jivesoftware.messenger.auth;
import org.jivesoftware.database.DbConnectionManager;
import org.jivesoftware.util.Log;
import org.jivesoftware.messenger.user.UserNotFoundException;
import org.jivesoftware.stringprep.Stringprep;
import org.jivesoftware.stringprep.StringprepException;
import java.sql.Connection;
import java.sql.PreparedStatement;
......@@ -42,12 +39,7 @@ public class DefaultAuthProvider implements AuthProvider {
if (username == null || password == null) {
throw new UnauthorizedException();
}
try {
username = Stringprep.nodeprep(username);
}
catch (StringprepException se) {
throw new UnauthorizedException("Illegal username: " + se.getMessage());
}
username = username.trim().toLowerCase();
Connection con = null;
PreparedStatement pstmt = null;
try {
......@@ -80,12 +72,7 @@ public class DefaultAuthProvider implements AuthProvider {
if (username == null || token == null || digest == null) {
throw new UnauthorizedException();
}
try {
username = Stringprep.nodeprep(username);
}
catch (StringprepException se) {
throw new UnauthorizedException("Illegal username: " + se.getMessage());
}
username = username.trim().toLowerCase();
Connection con = null;
PreparedStatement pstmt = null;
try {
......
......@@ -22,6 +22,8 @@ import org.jivesoftware.messenger.user.UserManager;
import org.jivesoftware.messenger.user.UserNotFoundException;
import org.jivesoftware.util.LocaleUtils;
import org.jivesoftware.util.Log;
import org.jivesoftware.stringprep.Stringprep;
import org.jivesoftware.stringprep.StringprepException;
import org.xmpp.packet.IQ;
import org.xmpp.packet.JID;
import org.xmpp.packet.PacketError;
......@@ -150,14 +152,16 @@ public class IQAuthHandler extends IQHandler implements IQAuthInfo {
ClientSession session, String digest) throws UnauthorizedException,
UserNotFoundException
{
JID jid = localServer.createJID(username, iq.elementTextTrim("resource"));
String resource = iq.elementTextTrim("resource");
resource = resource != null ? resource.toLowerCase() : null;
// If a session already exists with the requested JID, then check to see
// if we should kick it off or refuse the new connection
if (sessionManager.isActiveRoute(jid)) {
if (sessionManager.isActiveRoute(username, resource)) {
ClientSession oldSession = null;
try {
oldSession = sessionManager.getSession(jid);
String domain = localServer.getServerInfo().getName();
oldSession = sessionManager.getSession(username, domain, resource);
oldSession.incrementConflictCount();
int conflictLimit = sessionManager.getConflictKickLimit();
if (conflictLimit != SessionManager.NEVER_KICK && oldSession.getConflictCount() > conflictLimit) {
......@@ -189,7 +193,7 @@ public class IQAuthHandler extends IQHandler implements IQAuthInfo {
throw new UnauthorizedException();
}
else {
session.setAuthToken(token, userManager, jid.getResource());
session.setAuthToken(token, userManager, resource);
packet.setFrom(session.getAddress());
response = IQ.createResultIQ(packet);
}
......
......@@ -132,12 +132,7 @@ public class UserManager {
*/
public User getUser(String username) throws UserNotFoundException {
// Make sure that the username is valid.
try {
username = Stringprep.nodeprep(username);
}
catch (StringprepException se) {
throw new UserNotFoundException("Invalid username: " + username, se);
}
username = username.trim().toLowerCase();
User user = (User) userCache.get(username);
if (user == null) {
synchronized(username.intern()) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment