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 { ...@@ -407,9 +407,11 @@ public class SessionManager extends BasicModule {
private void broadcastPresenceToOtherResource(ClientSession session) private void broadcastPresenceToOtherResource(ClientSession session)
throws UserNotFoundException, UnauthorizedException { throws UserNotFoundException, UnauthorizedException {
Presence presence = null; Presence presence = null;
Collection<ClientSession> availableSession;
SessionMap sessionMap = sessions.get(session.getUsername()); SessionMap sessionMap = sessions.get(session.getUsername());
if (sessionMap != null) { if (sessionMap != null) {
for (ClientSession userSession : sessionMap.getAvailableSessions()) { availableSession = new ArrayList<ClientSession>(sessionMap.getAvailableSessions());
for (ClientSession userSession : availableSession) {
if (userSession != session) { if (userSession != session) {
// Send the presence of an existing session to the session that has just changed // Send the presence of an existing session to the session that has just changed
// the presence // the presence
...@@ -563,10 +565,8 @@ public class SessionManager extends BasicModule { ...@@ -563,10 +565,8 @@ public class SessionManager extends BasicModule {
return session; return session;
} }
public boolean isActiveRoute(JID route) { public boolean isActiveRoute(String username, String resource) {
boolean hasRoute = false; boolean hasRoute = false;
String resource = route.getResource();
String username = route.getNode();
if (username == null || "".equals(username)) { if (username == null || "".equals(username)) {
if (resource != null) { if (resource != null) {
...@@ -607,24 +607,47 @@ public class SessionManager extends BasicModule { ...@@ -607,24 +607,47 @@ public class SessionManager extends BasicModule {
* @return the <code>Session</code> associated with the JID. * @return the <code>Session</code> associated with the JID.
*/ */
public ClientSession getSession(JID from) { 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. // 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; return null;
} }
ClientSession session = 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 // Initially Check preAuthenticated Sessions
session = preAuthenticatedSessions.get(from.toString()); session = preAuthenticatedSessions.get(buf.toString());
if(session != null){ if(session != null){
return (ClientSession)session; return (ClientSession)session;
} }
String resource = from.getResource();
if (resource == null) { if (resource == null) {
return null; return null;
} }
String username = from.getNode();
if (username == null || "".equals(username)) { if (username == null || "".equals(username)) {
session = anonymousSessions.get(resource); session = anonymousSessions.get(resource);
} }
......
...@@ -13,9 +13,6 @@ package org.jivesoftware.messenger.auth; ...@@ -13,9 +13,6 @@ package org.jivesoftware.messenger.auth;
import org.jivesoftware.database.DbConnectionManager; import org.jivesoftware.database.DbConnectionManager;
import org.jivesoftware.util.Log; 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.Connection;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
...@@ -42,12 +39,7 @@ public class DefaultAuthProvider implements AuthProvider { ...@@ -42,12 +39,7 @@ public class DefaultAuthProvider implements AuthProvider {
if (username == null || password == null) { if (username == null || password == null) {
throw new UnauthorizedException(); throw new UnauthorizedException();
} }
try { username = username.trim().toLowerCase();
username = Stringprep.nodeprep(username);
}
catch (StringprepException se) {
throw new UnauthorizedException("Illegal username: " + se.getMessage());
}
Connection con = null; Connection con = null;
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
try { try {
...@@ -80,12 +72,7 @@ public class DefaultAuthProvider implements AuthProvider { ...@@ -80,12 +72,7 @@ public class DefaultAuthProvider implements AuthProvider {
if (username == null || token == null || digest == null) { if (username == null || token == null || digest == null) {
throw new UnauthorizedException(); throw new UnauthorizedException();
} }
try { username = username.trim().toLowerCase();
username = Stringprep.nodeprep(username);
}
catch (StringprepException se) {
throw new UnauthorizedException("Illegal username: " + se.getMessage());
}
Connection con = null; Connection con = null;
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
try { try {
......
...@@ -22,6 +22,8 @@ import org.jivesoftware.messenger.user.UserManager; ...@@ -22,6 +22,8 @@ import org.jivesoftware.messenger.user.UserManager;
import org.jivesoftware.messenger.user.UserNotFoundException; import org.jivesoftware.messenger.user.UserNotFoundException;
import org.jivesoftware.util.LocaleUtils; import org.jivesoftware.util.LocaleUtils;
import org.jivesoftware.util.Log; import org.jivesoftware.util.Log;
import org.jivesoftware.stringprep.Stringprep;
import org.jivesoftware.stringprep.StringprepException;
import org.xmpp.packet.IQ; import org.xmpp.packet.IQ;
import org.xmpp.packet.JID; import org.xmpp.packet.JID;
import org.xmpp.packet.PacketError; import org.xmpp.packet.PacketError;
...@@ -150,14 +152,16 @@ public class IQAuthHandler extends IQHandler implements IQAuthInfo { ...@@ -150,14 +152,16 @@ public class IQAuthHandler extends IQHandler implements IQAuthInfo {
ClientSession session, String digest) throws UnauthorizedException, ClientSession session, String digest) throws UnauthorizedException,
UserNotFoundException 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 a session already exists with the requested JID, then check to see
// if we should kick it off or refuse the new connection // if we should kick it off or refuse the new connection
if (sessionManager.isActiveRoute(jid)) { if (sessionManager.isActiveRoute(username, resource)) {
ClientSession oldSession = null; ClientSession oldSession = null;
try { try {
oldSession = sessionManager.getSession(jid); String domain = localServer.getServerInfo().getName();
oldSession = sessionManager.getSession(username, domain, resource);
oldSession.incrementConflictCount(); oldSession.incrementConflictCount();
int conflictLimit = sessionManager.getConflictKickLimit(); int conflictLimit = sessionManager.getConflictKickLimit();
if (conflictLimit != SessionManager.NEVER_KICK && oldSession.getConflictCount() > conflictLimit) { if (conflictLimit != SessionManager.NEVER_KICK && oldSession.getConflictCount() > conflictLimit) {
...@@ -189,7 +193,7 @@ public class IQAuthHandler extends IQHandler implements IQAuthInfo { ...@@ -189,7 +193,7 @@ public class IQAuthHandler extends IQHandler implements IQAuthInfo {
throw new UnauthorizedException(); throw new UnauthorizedException();
} }
else { else {
session.setAuthToken(token, userManager, jid.getResource()); session.setAuthToken(token, userManager, resource);
packet.setFrom(session.getAddress()); packet.setFrom(session.getAddress());
response = IQ.createResultIQ(packet); response = IQ.createResultIQ(packet);
} }
......
...@@ -132,12 +132,7 @@ public class UserManager { ...@@ -132,12 +132,7 @@ public class UserManager {
*/ */
public User getUser(String username) throws UserNotFoundException { public User getUser(String username) throws UserNotFoundException {
// Make sure that the username is valid. // Make sure that the username is valid.
try { username = username.trim().toLowerCase();
username = Stringprep.nodeprep(username);
}
catch (StringprepException se) {
throw new UserNotFoundException("Invalid username: " + username, se);
}
User user = (User) userCache.get(username); User user = (User) userCache.get(username);
if (user == null) { if (user == null) {
synchronized(username.intern()) { 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