GUAC-1115: Move DN derivation into UserService.

This commit is contained in:
Michael Jumper
2015-10-23 15:22:31 -07:00
parent 947e7b1004
commit bf53b5515d
2 changed files with 44 additions and 20 deletions

View File

@@ -27,10 +27,10 @@ import com.google.inject.Provider;
import com.novell.ldap.LDAPConnection; import com.novell.ldap.LDAPConnection;
import com.novell.ldap.LDAPException; import com.novell.ldap.LDAPException;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.util.List;
import org.glyptodon.guacamole.auth.ldap.user.AuthenticatedUser; import org.glyptodon.guacamole.auth.ldap.user.AuthenticatedUser;
import org.glyptodon.guacamole.auth.ldap.user.UserContext; import org.glyptodon.guacamole.auth.ldap.user.UserContext;
import org.glyptodon.guacamole.GuacamoleException; import org.glyptodon.guacamole.GuacamoleException;
import org.glyptodon.guacamole.auth.ldap.user.UserService;
import org.glyptodon.guacamole.net.auth.Credentials; import org.glyptodon.guacamole.net.auth.Credentials;
import org.glyptodon.guacamole.net.auth.credentials.CredentialsInfo; import org.glyptodon.guacamole.net.auth.credentials.CredentialsInfo;
import org.glyptodon.guacamole.net.auth.credentials.GuacamoleInvalidCredentialsException; import org.glyptodon.guacamole.net.auth.credentials.GuacamoleInvalidCredentialsException;
@@ -50,18 +50,18 @@ public class AuthenticationProviderService {
*/ */
private final Logger logger = LoggerFactory.getLogger(AuthenticationProviderService.class); private final Logger logger = LoggerFactory.getLogger(AuthenticationProviderService.class);
/**
* Service for escaping parts of LDAP queries.
*/
@Inject
private EscapingService escapingService;
/** /**
* Service for retrieving LDAP server configuration information. * Service for retrieving LDAP server configuration information.
*/ */
@Inject @Inject
private ConfigurationService confService; private ConfigurationService confService;
/**
* Service for retrieving users and their corresponding LDAP DNs.
*/
@Inject
private UserService userService;
/** /**
* Provider for AuthenticatedUser objects. * Provider for AuthenticatedUser objects.
*/ */
@@ -93,20 +93,8 @@ public class AuthenticationProviderService {
private String getUserBindDN(String username) private String getUserBindDN(String username)
throws GuacamoleException { throws GuacamoleException {
// Pull username attributes from properties
List<String> usernameAttributes = confService.getUsernameAttributes();
// We need exactly one base DN to derive the user DN
if (usernameAttributes.size() != 1) {
logger.warn("Cannot directly derive user DN when multiple username attributes are specified");
return null;
}
// Derive user DN from base DN // Derive user DN from base DN
return return userService.deriveUserDN(username);
escapingService.escapeDN(usernameAttributes.get(0))
+ "=" + escapingService.escapeDN(username)
+ "," + confService.getUserBaseDN();
} }

View File

@@ -269,4 +269,40 @@ public class UserService {
} }
/**
* Determines the DN which corresponds to the user having the given
* username. The DN will either be derived directly from the user base DN,
* or queried from the LDAP server, depending on how LDAP authentication
* has been configured.
*
* @param username
* The username of the user whose corresponding DN should be returned.
*
* @return
* The DN which corresponds to the user having the given username.
*
* @throws GuacamoleException
* If required properties are missing, and thus the user DN cannot be
* determined.
*/
public String deriveUserDN(String username)
throws GuacamoleException {
// Pull username attributes from properties
List<String> usernameAttributes = confService.getUsernameAttributes();
// We need exactly one base DN to derive the user DN
if (usernameAttributes.size() != 1) {
logger.warn("Cannot directly derive user DN when multiple username attributes are specified");
return null;
}
// Derive user DN from base DN
return
escapingService.escapeDN(usernameAttributes.get(0))
+ "=" + escapingService.escapeDN(username)
+ "," + confService.getUserBaseDN();
}
} }