diff options
| author | Robert Gemmell <robbie@apache.org> | 2011-06-09 10:38:20 +0000 |
|---|---|---|
| committer | Robert Gemmell <robbie@apache.org> | 2011-06-09 10:38:20 +0000 |
| commit | b83ba1c0bc269d2b9dddd73dfe8a90d4d3723e58 (patch) | |
| tree | 8d674ad91fbf4b4dff1a7610c76f63a1cc6a6259 /qpid/java/broker/src/main | |
| parent | 46af35ba4c7867098a1c5e1cea9bd24275cbad40 (diff) | |
| download | qpid-python-b83ba1c0bc269d2b9dddd73dfe8a90d4d3723e58.tar.gz | |
QPID-3296: update RMIPasswordAuthenticator to authenticate users via AuthenticationManager instead of a PrincipalDatabase
Applied patch from Keith Wall <keith.wall@gmail.com>
git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk@1133781 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'qpid/java/broker/src/main')
11 files changed, 256 insertions, 108 deletions
diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/ServerConfiguration.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/ServerConfiguration.java index 297f7abdb8..f152865a27 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/ServerConfiguration.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/configuration/ServerConfiguration.java @@ -214,6 +214,13 @@ public class ServerConfiguration extends ConfigurationPlugin implements SignalHa + (_configFile == null ? "" : " Configuration file : " + _configFile); throw new ConfigurationException(message); } + + if (getListValue("security.jmx.principal-database").size() > 0) + { + String message = "Validation error : security/jmx/principal-database is no longer a supported element within the configuration xml." + + (_configFile == null ? "" : " Configuration file : " + _configFile); + throw new ConfigurationException(message); + } } /* @@ -533,11 +540,6 @@ public class ServerConfiguration extends ConfigurationPlugin implements SignalHa return getListValue(name); } - public List<String> getManagementPrincipalDBs() - { - return getListValue("security.jmx.principal-database"); - } - public int getFrameSize() { return getIntValue("advanced.framesize", DEFAULT_FRAME_SIZE); @@ -568,11 +570,6 @@ public class ServerConfiguration extends ConfigurationPlugin implements SignalHa return getBooleanValue("security.msg-auth"); } - public String getJMXPrincipalDatabase() - { - return getStringValue("security.jmx.principal-database"); - } - public String getManagementKeyStorePath() { return getStringValue("management.ssl.keyStorePath"); diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/handler/ConnectionSecureOkMethodHandler.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/handler/ConnectionSecureOkMethodHandler.java index d4b79134a2..79de0678f0 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/handler/ConnectionSecureOkMethodHandler.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/handler/ConnectionSecureOkMethodHandler.java @@ -20,6 +20,7 @@ */ package org.apache.qpid.server.handler; + import javax.security.sasl.SaslException; import javax.security.sasl.SaslServer; @@ -68,7 +69,7 @@ public class ConnectionSecureOkMethodHandler implements StateAwareMethodListener } MethodRegistry methodRegistry = session.getMethodRegistry(); AuthenticationResult authResult = authMgr.authenticate(ss, body.getResponse()); - switch (authResult.status) + switch (authResult.getStatus()) { case ERROR: Exception cause = authResult.getCause(); @@ -96,13 +97,14 @@ public class ConnectionSecureOkMethodHandler implements StateAwareMethodListener ConnectionStartOkMethodHandler.getConfiguredFrameSize(), ApplicationRegistry.getInstance().getConfiguration().getHeartBeatDelay()); session.writeFrame(tuneBody.generateFrame(0)); - session.setAuthorizedID(new UsernamePrincipal(ss.getAuthorizationID())); + final UsernamePrincipal principal = UsernamePrincipal.getUsernamePrincipalFromSubject(authResult.getSubject()); + session.setAuthorizedID(principal); disposeSaslServer(session); break; case CONTINUE: stateManager.changeState(AMQState.CONNECTION_NOT_AUTH); - ConnectionSecureBody secureBody = methodRegistry.createConnectionSecureBody(authResult.challenge); + ConnectionSecureBody secureBody = methodRegistry.createConnectionSecureBody(authResult.getChallenge()); session.writeFrame(secureBody.generateFrame(0)); } } diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/handler/ConnectionStartOkMethodHandler.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/handler/ConnectionStartOkMethodHandler.java index 4442f969c4..544bd62ed8 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/handler/ConnectionStartOkMethodHandler.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/handler/ConnectionStartOkMethodHandler.java @@ -88,7 +88,7 @@ public class ConnectionStartOkMethodHandler implements StateAwareMethodListener< MethodRegistry methodRegistry = session.getMethodRegistry(); - switch (authResult.status) + switch (authResult.getStatus()) { case ERROR: Exception cause = authResult.getCause(); @@ -121,7 +121,7 @@ public class ConnectionStartOkMethodHandler implements StateAwareMethodListener< case CONTINUE: stateManager.changeState(AMQState.CONNECTION_NOT_AUTH); - ConnectionSecureBody secureBody = methodRegistry.createConnectionSecureBody(authResult.challenge); + ConnectionSecureBody secureBody = methodRegistry.createConnectionSecureBody(authResult.getChallenge()); session.writeFrame(secureBody.generateFrame(0)); } } diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/management/JMXManagedObjectRegistry.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/management/JMXManagedObjectRegistry.java index 0334a856c1..6a34ff4a26 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/management/JMXManagedObjectRegistry.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/management/JMXManagedObjectRegistry.java @@ -20,32 +20,6 @@ */ package org.apache.qpid.server.management; -import org.apache.commons.configuration.ConfigurationException; -import org.apache.log4j.Logger; -import org.apache.qpid.AMQException; -import org.apache.qpid.server.registry.ApplicationRegistry; -import org.apache.qpid.server.registry.IApplicationRegistry; -import org.apache.qpid.server.security.auth.database.PrincipalDatabase; -import org.apache.qpid.server.security.auth.rmi.RMIPasswordAuthenticator; -import org.apache.qpid.server.logging.actors.CurrentActor; -import org.apache.qpid.server.logging.messages.ManagementConsoleMessages; - -import javax.management.JMException; -import javax.management.MBeanServer; -import javax.management.MBeanServerFactory; -import javax.management.ObjectName; -import javax.management.NotificationListener; -import javax.management.NotificationFilterSupport; -import javax.management.remote.JMXConnectorServer; -import javax.management.remote.JMXServiceURL; -import javax.management.remote.MBeanServerForwarder; -import javax.management.remote.JMXConnectionNotification; -import javax.management.remote.rmi.RMIConnectorServer; -import javax.management.remote.rmi.RMIJRMPServerImpl; -import javax.management.remote.rmi.RMIServerImpl; -import javax.rmi.ssl.SslRMIClientSocketFactory; -import javax.rmi.ssl.SslRMIServerSocketFactory; - import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; @@ -64,7 +38,31 @@ import java.rmi.server.RMIClientSocketFactory; import java.rmi.server.RMIServerSocketFactory; import java.rmi.server.UnicastRemoteObject; import java.util.HashMap; -import java.util.Map; + +import javax.management.JMException; +import javax.management.MBeanServer; +import javax.management.MBeanServerFactory; +import javax.management.NotificationFilterSupport; +import javax.management.NotificationListener; +import javax.management.ObjectName; +import javax.management.remote.JMXConnectionNotification; +import javax.management.remote.JMXConnectorServer; +import javax.management.remote.JMXServiceURL; +import javax.management.remote.MBeanServerForwarder; +import javax.management.remote.rmi.RMIConnectorServer; +import javax.management.remote.rmi.RMIJRMPServerImpl; +import javax.management.remote.rmi.RMIServerImpl; +import javax.rmi.ssl.SslRMIClientSocketFactory; +import javax.rmi.ssl.SslRMIServerSocketFactory; + +import org.apache.commons.configuration.ConfigurationException; +import org.apache.log4j.Logger; +import org.apache.qpid.AMQException; +import org.apache.qpid.server.logging.actors.CurrentActor; +import org.apache.qpid.server.logging.messages.ManagementConsoleMessages; +import org.apache.qpid.server.registry.ApplicationRegistry; +import org.apache.qpid.server.registry.IApplicationRegistry; +import org.apache.qpid.server.security.auth.rmi.RMIPasswordAuthenticator; /** * This class starts up an MBeanserver. If out of the box agent has been enabled then there are no @@ -113,12 +111,6 @@ public class JMXManagedObjectRegistry implements ManagedObjectRegistry IApplicationRegistry appRegistry = ApplicationRegistry.getInstance(); int port = appRegistry.getConfiguration().getJMXManagementPort(); - //retrieve the Principal Database assigned to JMX authentication duties - String jmxDatabaseName = appRegistry.getConfiguration().getJMXPrincipalDatabase(); - Map<String, PrincipalDatabase> map = appRegistry.getDatabaseManager().getDatabases(); - PrincipalDatabase db = map.get(jmxDatabaseName); - - HashMap<String,Object> env = new HashMap<String,Object>(); //Socket factories for the RMIConnectorServer, either default or SLL depending on configuration RMIClientSocketFactory csf; @@ -200,7 +192,8 @@ public class JMXManagedObjectRegistry implements ManagedObjectRegistry //add a JMXAuthenticator implementation the env map to authenticate the RMI based JMX connector server RMIPasswordAuthenticator rmipa = new RMIPasswordAuthenticator(); - rmipa.setPrincipalDatabase(db); + rmipa.setAuthenticationManager(appRegistry.getAuthenticationManager()); + HashMap<String,Object> env = new HashMap<String,Object>(); env.put(JMXConnectorServer.AUTHENTICATOR, rmipa); /* diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/management/MBeanInvocationHandlerImpl.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/management/MBeanInvocationHandlerImpl.java index 380f51e308..ce6bd3ee33 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/management/MBeanInvocationHandlerImpl.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/management/MBeanInvocationHandlerImpl.java @@ -27,7 +27,6 @@ import java.lang.reflect.Proxy; import java.security.AccessControlContext; import java.security.AccessController; import java.security.Principal; -import java.util.Properties; import java.util.Set; import javax.management.Attribute; @@ -65,7 +64,6 @@ public class MBeanInvocationHandlerImpl implements InvocationHandler, Notificati public final static String READONLY = "readonly"; private final static String DELEGATE = "JMImplementation:type=MBeanServerDelegate"; private MBeanServer _mbs; - private static Properties _userRoles = new Properties(); private static ManagementActor _logActor; public static MBeanServerForwarder newProxyInstance() diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/security/auth/AuthenticationResult.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/security/auth/AuthenticationResult.java index 62967ef7eb..8c2d60a660 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/security/auth/AuthenticationResult.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/security/auth/AuthenticationResult.java @@ -20,42 +20,93 @@ */ package org.apache.qpid.server.security.auth; +import javax.security.auth.Subject; + +/** + * Encapsulates the result of an attempt to authenticate. + * <p> + * The authentication status describes the overall outcome. + * <p> + * <ol> + * <li>If authentication status is SUCCESS, the subject will be populated. + * </li> + * <li>If authentication status is CONTINUE, the authentication has failed because the user + * supplied incorrect credentials (etc). If the authentication requires it, the next challenge + * is made available. + * </li> + * <li>If authentication status is ERROR , the authentication decision could not be made due + * to a failure (such as an external system), the {@link AuthenticationResult#getCause()} + * will provide the underlying exception. + * </li> + * </ol> + * + */ public class AuthenticationResult { public enum AuthenticationStatus { - SUCCESS, CONTINUE, ERROR + /** Authentication successful */ + SUCCESS, + /** Authentication not successful due to credentials problem etc */ + CONTINUE, + /** Problem prevented the authentication from being made e.g. failure of an external system */ + ERROR } - public AuthenticationStatus status; - public byte[] challenge; - - private Exception cause; + public final AuthenticationStatus _status; + public final byte[] _challenge; + private final Exception _cause; + private final Subject _subject; - public AuthenticationResult(AuthenticationStatus status) + public AuthenticationResult(final AuthenticationStatus status) { this(null, status, null); } - public AuthenticationResult(byte[] challenge, AuthenticationStatus status) + public AuthenticationResult(final byte[] challenge, final AuthenticationStatus status) { this(challenge, status, null); } - public AuthenticationResult(AuthenticationStatus error, Exception cause) + public AuthenticationResult(final AuthenticationStatus error, final Exception cause) { this(null, error, cause); } - public AuthenticationResult(byte[] challenge, AuthenticationStatus status, Exception cause) + public AuthenticationResult(final byte[] challenge, final AuthenticationStatus status, final Exception cause) + { + this._status = status; + this._challenge = challenge; + this._cause = cause; + this._subject = null; + } + + public AuthenticationResult(final Subject subject) { - this.status = status; - this.challenge = challenge; - this.cause = cause; + this._status = AuthenticationStatus.SUCCESS; + this._challenge = null; + this._cause = null; + this._subject = subject; } public Exception getCause() { - return cause; + return _cause; + } + + public AuthenticationStatus getStatus() + { + return _status; + } + + public byte[] getChallenge() + { + return _challenge; } + + public Subject getSubject() + { + return _subject; + } + } diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/security/auth/database/ConfigurationFilePrincipalDatabaseManager.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/security/auth/database/ConfigurationFilePrincipalDatabaseManager.java index e9276e1b0e..a22c66c73d 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/security/auth/database/ConfigurationFilePrincipalDatabaseManager.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/security/auth/database/ConfigurationFilePrincipalDatabaseManager.java @@ -23,6 +23,7 @@ package org.apache.qpid.server.security.auth.database; import java.io.FileNotFoundException; import java.io.IOException; import java.lang.reflect.Method; +import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -170,18 +171,13 @@ public class ConfigurationFilePrincipalDatabaseManager implements PrincipalDatab { AMQUserManagementMBean _mbean = new AMQUserManagementMBean(); - List<String> principalDBs = config.getManagementPrincipalDBs(); - if (principalDBs.isEmpty()) - { - throw new ConfigurationException("No principal-database specified for jmx security"); - } - String databaseName = principalDBs.get(0); - PrincipalDatabase database = getDatabases().get(databaseName); - if (database == null) + final Collection<PrincipalDatabase> dbs = getDatabases().values(); + if (dbs.size() == 0) { - throw new ConfigurationException("Principal-database '" + databaseName + "' not found"); + throw new ConfigurationException("Principal-database not found"); } + final PrincipalDatabase database = dbs.iterator().next(); _mbean.setPrincipalDatabase(database); _mbean.register(); diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/security/auth/manager/AuthenticationManager.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/security/auth/manager/AuthenticationManager.java index 39e1e07c57..c1ef4c8ff5 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/security/auth/manager/AuthenticationManager.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/security/auth/manager/AuthenticationManager.java @@ -32,9 +32,43 @@ import org.apache.qpid.server.security.auth.AuthenticationResult; */ public interface AuthenticationManager extends Closeable { + + /** + * Gets the SASL mechanisms known to this manager. + * + * @return SASL mechanism names, space separated. + */ String getMechanisms(); + /** + * Creates a SASL server for the specified mechanism name for the given + * fully qualified domain name. + * + * @param mechanism mechanism name + * @param localFQDN domain name + * + * @return SASL server + * @throws SaslException + */ SaslServer createSaslServer(String mechanism, String localFQDN) throws SaslException; + /** + * Authenticates a user using SASL negotiation. + * + * @param server SASL server + * @param response SASL response to process + * + * @return authentication result + */ AuthenticationResult authenticate(SaslServer server, byte[] response); + + /** + * Authenticates a user using their username and password. + * + * @param username username + * @param password password + * + * @return authentication result + */ + AuthenticationResult authenticate(String username, String password); } diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/security/auth/manager/PrincipalDatabaseAuthenticationManager.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/security/auth/manager/PrincipalDatabaseAuthenticationManager.java index d10ad2c170..d36bbc4f46 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/security/auth/manager/PrincipalDatabaseAuthenticationManager.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/security/auth/manager/PrincipalDatabaseAuthenticationManager.java @@ -23,12 +23,16 @@ package org.apache.qpid.server.security.auth.manager; import org.apache.log4j.Logger; import org.apache.qpid.server.registry.ApplicationRegistry; import org.apache.qpid.server.security.auth.manager.AuthenticationManager; +import org.apache.qpid.server.security.auth.AuthenticationResult.AuthenticationStatus; import org.apache.qpid.server.security.auth.database.PrincipalDatabase; import org.apache.qpid.server.security.auth.sasl.JCAProvider; import org.apache.qpid.server.security.auth.sasl.AuthenticationProviderInitialiser; +import org.apache.qpid.server.security.auth.sasl.UsernamePrincipal; import org.apache.qpid.server.security.auth.AuthenticationResult; +import javax.security.auth.Subject; import javax.security.auth.callback.CallbackHandler; +import javax.security.auth.login.AccountNotFoundException; import javax.security.sasl.SaslServerFactory; import javax.security.sasl.SaslServer; import javax.security.sasl.SaslException; @@ -163,7 +167,9 @@ public class PrincipalDatabaseAuthenticationManager implements AuthenticationMan if (server.isComplete()) { - return new AuthenticationResult(challenge, AuthenticationResult.AuthenticationStatus.SUCCESS); + final Subject subject = new Subject(); + subject.getPrincipals().add(new UsernamePrincipal(server.getAuthorizationID())); + return new AuthenticationResult(subject); } else { @@ -181,4 +187,31 @@ public class PrincipalDatabaseAuthenticationManager implements AuthenticationMan _mechanisms = null; Security.removeProvider(PROVIDER_NAME); } + + /** + * @see org.apache.qpid.server.security.auth.manager.AuthenticationManager#authenticate(String, String) + */ + @Override + public AuthenticationResult authenticate(final String username, final String password) + { + final PrincipalDatabase db = ApplicationRegistry.getInstance().getDatabaseManager().getDatabases().values().iterator().next(); + + try + { + if (db.verifyPassword(username, password.toCharArray())) + { + final Subject subject = new Subject(); + subject.getPrincipals().add(new UsernamePrincipal(username)); + return new AuthenticationResult(subject); + } + else + { + return new AuthenticationResult(AuthenticationStatus.CONTINUE); + } + } + catch (AccountNotFoundException e) + { + return new AuthenticationResult(AuthenticationStatus.CONTINUE); + } + } } diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/security/auth/rmi/RMIPasswordAuthenticator.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/security/auth/rmi/RMIPasswordAuthenticator.java index 0cbbccb3b8..b7985ad972 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/security/auth/rmi/RMIPasswordAuthenticator.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/security/auth/rmi/RMIPasswordAuthenticator.java @@ -20,14 +20,13 @@ */ package org.apache.qpid.server.security.auth.rmi; -import java.util.Collections; - import javax.management.remote.JMXAuthenticator; import javax.management.remote.JMXPrincipal; import javax.security.auth.Subject; -import javax.security.auth.login.AccountNotFoundException; -import org.apache.qpid.server.security.auth.database.PrincipalDatabase; +import org.apache.qpid.server.security.auth.AuthenticationResult; +import org.apache.qpid.server.security.auth.AuthenticationResult.AuthenticationStatus; +import org.apache.qpid.server.security.auth.manager.AuthenticationManager; public class RMIPasswordAuthenticator implements JMXAuthenticator { @@ -39,15 +38,15 @@ public class RMIPasswordAuthenticator implements JMXAuthenticator static final String CREDENTIALS_REQUIRED = "User details are required. " + "Please ensure you are using an up to date management console to connect."; - private PrincipalDatabase _db = null; + private AuthenticationManager _authenticationManager = null; public RMIPasswordAuthenticator() { } - - public void setPrincipalDatabase(PrincipalDatabase pd) + + public void setAuthenticationManager(final AuthenticationManager authenticationManager) { - this._db = pd; + _authenticationManager = authenticationManager; } public Subject authenticate(Object credentials) throws SecurityException @@ -65,50 +64,39 @@ public class RMIPasswordAuthenticator implements JMXAuthenticator } } - // Verify that required number of credential's. + // Verify that required number of credentials. final String[] userCredentials = (String[]) credentials; if (userCredentials.length != 2) { throw new SecurityException(SHOULD_HAVE_2_ELEMENTS); } - String username = (String) userCredentials[0]; - String password = (String) userCredentials[1]; + final String username = (String) userCredentials[0]; + final String password = (String) userCredentials[1]; - // Verify that all required credential's are actually present. + // Verify that all required credentials are actually present. if (username == null || password == null) { throw new SecurityException(SHOULD_BE_NON_NULL); } - // Verify that a PD has been set. - if (_db == null) + // Verify that an AuthenticationManager has been set. + if (_authenticationManager == null) { throw new SecurityException(UNABLE_TO_LOOKUP); } - - boolean authenticated = false; + final AuthenticationResult result = _authenticationManager.authenticate(username, password); - // Perform authentication - try + if (AuthenticationStatus.ERROR.equals(result.getStatus())) { - if (_db.verifyPassword(username, password.toCharArray())) - { - authenticated = true; - } - } - catch (AccountNotFoundException e) - { - throw new SecurityException(INVALID_CREDENTIALS); // XXX + throw new SecurityException("Authentication manager failed", result.getCause()); } - - if (authenticated) + else if (AuthenticationStatus.SUCCESS.equals(result.getStatus())) { - //credential's check out, return the appropriate JAAS Subject - return new Subject(true, - Collections.singleton(new JMXPrincipal(username)), - Collections.EMPTY_SET, - Collections.EMPTY_SET); + final Subject subject = result.getSubject(); + subject.getPrincipals().add(new JMXPrincipal(username)); + subject.setReadOnly(); + return subject; } else { diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/security/auth/sasl/UsernamePrincipal.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/security/auth/sasl/UsernamePrincipal.java index d7c8383690..b4ee13fe6b 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/security/auth/sasl/UsernamePrincipal.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/security/auth/sasl/UsernamePrincipal.java @@ -21,14 +21,21 @@ package org.apache.qpid.server.security.auth.sasl; import java.security.Principal; +import java.util.Set; + +import javax.security.auth.Subject; /** A principal that is just a wrapper for a simple username. */ public class UsernamePrincipal implements Principal { - private String _name; + private final String _name; public UsernamePrincipal(String name) { + if (name == null) + { + throw new IllegalArgumentException("name cannot be null"); + } _name = name; } @@ -41,4 +48,53 @@ public class UsernamePrincipal implements Principal { return _name; } + + /** + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() + { + final int prime = 31; + return prime * _name.hashCode(); + } + + /** + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(Object obj) + { + if (this == obj) + { + return true; + } + else + { + if (obj instanceof UsernamePrincipal) + { + UsernamePrincipal other = (UsernamePrincipal) obj; + return _name.equals(other._name); + } + else + { + return false; + } + } + } + + public static UsernamePrincipal getUsernamePrincipalFromSubject(final Subject authSubject) + { + if (authSubject == null) + { + throw new IllegalArgumentException("No authenticated subject."); + } + + final Set<UsernamePrincipal> principals = authSubject.getPrincipals(UsernamePrincipal.class); + if (principals.size() != 1) + { + throw new IllegalArgumentException("Can't find single UsernamePrincipal in authenticated subject"); + } + return principals.iterator().next(); + } } |
