diff --git a/authentication/source/java/ch/systemsx/cisd/authentication/DefaultSessionManager.java b/authentication/source/java/ch/systemsx/cisd/authentication/DefaultSessionManager.java index 46f1bc949c4153b4057bb262a042505e3fd6da2b..ee6f5d29146348ae7b84c92749a1bf18088b62b5 100644 --- a/authentication/source/java/ch/systemsx/cisd/authentication/DefaultSessionManager.java +++ b/authentication/source/java/ch/systemsx/cisd/authentication/DefaultSessionManager.java @@ -342,18 +342,10 @@ public class DefaultSessionManager<T extends BasicSession> implements ISessionMa checkIfNotBlank(password, "password"); try { - final String applicationToken = authenticationService.authenticateApplication(); - if (applicationToken == null) - { - operationLog.error("User '" + user - + "' failed to authenticate: application not authenticated."); - return null; - } String sessionToken = null; final long now = System.currentTimeMillis(); final Principal principalOrNull = - authenticationService.tryGetAndAuthenticateUser(applicationToken, user, - password); + authenticationService.tryGetAndAuthenticateUser(null, user, password); final boolean isAuthenticated = Principal.isAuthenticated(principalOrNull); if (isAuthenticated) { diff --git a/authentication/source/java/ch/systemsx/cisd/authentication/DummyAuthenticationService.java b/authentication/source/java/ch/systemsx/cisd/authentication/DummyAuthenticationService.java index ba3ffe74cac9fb5363674ca44fabf75b2cbf007a..ab1162e407079f5e56a54fcb35b296d6485983fc 100644 --- a/authentication/source/java/ch/systemsx/cisd/authentication/DummyAuthenticationService.java +++ b/authentication/source/java/ch/systemsx/cisd/authentication/DummyAuthenticationService.java @@ -45,6 +45,14 @@ public final class DummyAuthenticationService implements IAuthenticationService return StringUtils.EMPTY; } + /** + * Always returns <code>true</code>, meaning that the login was successful. + */ + public final boolean authenticateUser(final String user, final String password) + { + return true; + } + /** * Always returns <code>true</code>, meaning that the login was successful. */ @@ -54,7 +62,7 @@ public final class DummyAuthenticationService implements IAuthenticationService return true; } - public final Principal getPrincipal(final String applicationToken, final String user) + public final Principal getPrincipal(final String user) { // Generate a random first and last name combination final String firstName; @@ -66,36 +74,67 @@ public final class DummyAuthenticationService implements IAuthenticationService return new Principal(user, firstName, lastName, "franz-josef.elmer@systemsx.ch", false); } - public Principal tryGetAndAuthenticateUser(String applicationToken, String user, - String passwordOrNull) + public final Principal getPrincipal(final String applicationToken, final String user) + { + return getPrincipal(user); + } + + public Principal tryGetAndAuthenticateUser(String user, String passwordOrNull) { - final Principal principal = getPrincipal(applicationToken, user); + final Principal principal = getPrincipal(user); principal.setAuthenticated(true); return principal; } - public Principal tryGetAndAuthenticateUserByEmail(String applicationToken, String email, String passwordOrNull) + public Principal tryGetAndAuthenticateUser(String applicationToken, String user, + String passwordOrNull) { - final Principal principal = getPrincipal(applicationToken, email); + return tryGetAndAuthenticateUser(user, passwordOrNull); + } + + public Principal tryGetAndAuthenticateUserByEmail(String email, String passwordOrNull) + { + final Principal principal = getPrincipal(email); principal.setAuthenticated(true); return principal; } + public Principal tryGetAndAuthenticateUserByEmail(String applicationToken, String email, + String passwordOrNull) + { + return tryGetAndAuthenticateUserByEmail(email, passwordOrNull); + } + public boolean isRemote() { return false; } + public List<Principal> listPrincipalsByEmail(String emailQuery) + { + throw new UnsupportedOperationException(); + } + public List<Principal> listPrincipalsByEmail(String applicationToken, String emailQuery) { throw new UnsupportedOperationException(); } + public List<Principal> listPrincipalsByLastName(String lastNameQuery) + { + throw new UnsupportedOperationException(); + } + public List<Principal> listPrincipalsByLastName(String applicationToken, String lastNameQuery) { throw new UnsupportedOperationException(); } + public List<Principal> listPrincipalsByUserId(String userIdQuery) + { + throw new UnsupportedOperationException(); + } + public List<Principal> listPrincipalsByUserId(String applicationToken, String userIdQuery) { throw new UnsupportedOperationException(); diff --git a/authentication/source/java/ch/systemsx/cisd/authentication/IAuthenticationService.java b/authentication/source/java/ch/systemsx/cisd/authentication/IAuthenticationService.java index 4c1bc8873b1cdc7a46480d77a0717a0ebf2cca5d..0b7ac6b39c76403dffe6cc54839ec2f754b0d709 100644 --- a/authentication/source/java/ch/systemsx/cisd/authentication/IAuthenticationService.java +++ b/authentication/source/java/ch/systemsx/cisd/authentication/IAuthenticationService.java @@ -27,39 +27,17 @@ import ch.systemsx.cisd.common.utilities.ISelfTestable; */ public interface IAuthenticationService extends ISelfTestable { - - /** - * Attempts authentication of the application with credentials passed in the constructor and - * returns the application token. Implementations should log what is going on, whether the - * application could register itself successfully or not. - * <p> - * The returned application token can then be used to authenticate an user (via - * {@link #authenticateUser(String, String, String)}) or to retrieve additional details about an - * user (via {@link #getPrincipal(String, String)}) - * </p> - * - * @return the application token if the application has been successfully authenticated, - * <code>null</code> otherwise. - */ - public String authenticateApplication(); - /** * Attempts authentication for the given user credentials. - * <p> - * Note that the application must be authenticated (meaning that <var>applicationToken</var> is - * not <code>null</code>) to perform this lookup. - * </p> * * @return <code>true</code> if the <var>user</var> has been successfully authenticated. */ - public boolean authenticateUser(String applicationToken, String user, String password); + public boolean authenticateUser(String user, String password); /** * Returns the user details for the given <var>userId</var>, optionally trying to authenticating * the user with the given <var>passwordOrNull</var>. * - * @param applicationToken The token to authenticate the application towards the authentication - * system. * @param user The user id to get the details for. * @param passwordOrNull The password to use for the authentication request. If * <code>null</code>, the user will not be authenticated. @@ -68,26 +46,23 @@ public interface IAuthenticationService extends ISelfTestable * {@link Principal#isAuthenticated(Principal)} whether the authentication request has * been successful. */ - public Principal tryGetAndAuthenticateUser(String applicationToken, String user, - String passwordOrNull); + public Principal tryGetAndAuthenticateUser(String user, String passwordOrNull); /** * For a given user name returns additional details encapsulated in returned * <code>Principal</code>. - * <p> - * Note that the application must be authenticated (meaning that <var>applicationToken</var> is - * not <code>null</code>) to perform this lookup. - * </p> * * @return The <code>Principal</code> object for the given <var>user</var>. * @throws IllegalArgumentException If the <var>user</var> cannot be found. */ - public Principal getPrincipal(String applicationToken, String user) - throws IllegalArgumentException; + public Principal getPrincipal(String user) throws IllegalArgumentException; /** * Returns <code>true</code> if this authentication service supports listing of principals by * user id. + * <p> + * Note that this does not refer to the methods that return only one principal like + * {@link #getPrincipal(String)} or {@link #tryGetAndAuthenticateUser(String, String)}. */ public boolean supportsListingByUserId(); @@ -100,12 +75,15 @@ public interface IAuthenticationService extends ISelfTestable * @throws UnsupportedOperationException if this authentication service does not support this * operation. */ - public List<Principal> listPrincipalsByUserId(String applicationToken, String userIdQuery) + public List<Principal> listPrincipalsByUserId(String userIdQuery) throws IllegalArgumentException; /** * Returns <code>true</code> if this authentication service supports listing of principals by * email address. + * <p> + * Note that this also refers to the method + * {@link #tryGetAndAuthenticateUserByEmail(String, String)}. */ public boolean supportsListingByEmail(); @@ -116,8 +94,6 @@ public interface IAuthenticationService extends ISelfTestable * <b>Note: if multiple users with this email address exist in the authentication repository, * the first one regarding an arbitrary (repository determined) order will be returned.</b> * - * @param applicationToken The token to authenticate the application towards the authentication - * system. * @param email The email of the user to get the details for. * @param passwordOrNull The password to use for the authentication request. If * <code>null</code>, the user will not be authenticated. @@ -129,8 +105,7 @@ public interface IAuthenticationService extends ISelfTestable * operation. * @throws IllegalArgumentException If the <var>applicationToken</var> is invalid. */ - public Principal tryGetAndAuthenticateUserByEmail(String applicationToken, String email, - String passwordOrNull); + public Principal tryGetAndAuthenticateUserByEmail(String email, String passwordOrNull); /** * Returns a list of all users that match the <var>emailQuery</var>. @@ -141,8 +116,7 @@ public interface IAuthenticationService extends ISelfTestable * operation. * @throws IllegalArgumentException If the <var>applicationToken</var> is invalid. */ - public List<Principal> listPrincipalsByEmail(String applicationToken, String emailQuery) - throws IllegalArgumentException; + public List<Principal> listPrincipalsByEmail(String emailQuery) throws IllegalArgumentException; /** * Returns <code>true</code> if this authentication service supports listing of principals by @@ -159,7 +133,108 @@ public interface IAuthenticationService extends ISelfTestable * operation. * @throws IllegalArgumentException If the <var>applicationToken</var> is invalid. */ - public List<Principal> listPrincipalsByLastName(String applicationToken, String lastNameQuery) + public List<Principal> listPrincipalsByLastName(String lastNameQuery) + throws IllegalArgumentException; + + // + // Deprecated methods + // + + /** + * Dummy operation, kept for backward compatibility. Don't use. + */ + public String authenticateApplication(); + + /** + * Attempts authentication for the given user credentials. + * + * @return <code>true</code> if the <var>user</var> has been successfully authenticated. + */ + public boolean authenticateUser(String dummyToken, String user, String password); + + /** + * Returns the user details for the given <var>userId</var>, optionally trying to authenticating + * the user with the given <var>passwordOrNull</var>. + * + * @param dummyToken Some string that is ignored. Can be <code>null</code>. + * @param user The user id to get the details for. + * @param passwordOrNull The password to use for the authentication request. If + * <code>null</code>, the user will not be authenticated. + * @return The Principal object, if a user with this <var>userId</var> exist, <code>null</code> + * otherwise. You can check with {@link Principal#isAuthenticated()} or + * {@link Principal#isAuthenticated(Principal)} whether the authentication request has + * been successful. + */ + public Principal tryGetAndAuthenticateUser(String dummyToken, String user, String passwordOrNull); + + /** + * For a given user name returns additional details encapsulated in returned + * <code>Principal</code>. + * + * @return The <code>Principal</code> object for the given <var>user</var>. + * @throws IllegalArgumentException If the <var>user</var> cannot be found. + */ + public Principal getPrincipal(String dummyToken, String user) throws IllegalArgumentException; + + /** + * Returns a list of all users that match the <var>userIdQuery</var>. + * + * @param dummyToken Some string that is ignored. Can be <code>null</code>. + * @param userIdQuery The query for user ids to list. As user ids are unique, it can only ever + * return more than one user if it contains one or more wildcard characters ( + * <code>*</code>). + * @throws UnsupportedOperationException if this authentication service does not support this + * operation. + */ + public List<Principal> listPrincipalsByUserId(String dummyToken, String userIdQuery) + throws IllegalArgumentException; + + /** + * Returns the user details for the given <var>email</var>, optionally trying to authenticating + * the user with the given <var>passwordOrNull</var>. + * <p> + * <b>Note: if multiple users with this email address exist in the authentication repository, + * the first one regarding an arbitrary (repository determined) order will be returned.</b> + * + * @param dummyToken Some string that is ignored. Can be <code>null</code>. + * @param email The email of the user to get the details for. + * @param passwordOrNull The password to use for the authentication request. If + * <code>null</code>, the user will not be authenticated. + * @return The Principal object, if a user with this <var>email</var> exist, <code>null</code> + * otherwise. You can check with {@link Principal#isAuthenticated()} or + * {@link Principal#isAuthenticated(Principal)} whether the authentication request has + * been successful. + * @throws UnsupportedOperationException if this authentication service does not support this + * operation. + * @throws IllegalArgumentException If the <var>applicationToken</var> is invalid. + */ + public Principal tryGetAndAuthenticateUserByEmail(String dummyToken, String email, + String passwordOrNull); + + /** + * Returns a list of all users that match the <var>emailQuery</var>. + * + * @param dummyToken Some string that is ignored. Can be <code>null</code>. + * @param emailQuery The query for email addresses to list. May contain one or more wildcard + * characters (<code>*</code>). + * @throws UnsupportedOperationException if this authentication service does not support this + * operation. + * @throws IllegalArgumentException If the <var>applicationToken</var> is invalid. + */ + public List<Principal> listPrincipalsByEmail(String dummyToken, String emailQuery) + throws IllegalArgumentException; + + /** + * Returns a list of all users that match the <var>lastNameQuery</var>. + * + * @param dummyToken Some string that is ignored. Can be <code>null</code>. + * @param lastNameQuery The query for last names to list. May contain one or more wildcard + * characters (<code>*</code>). + * @throws UnsupportedOperationException if this authentication service does not support this + * operation. + * @throws IllegalArgumentException If the <var>applicationToken</var> is invalid. + */ + public List<Principal> listPrincipalsByLastName(String dummyToken, String lastNameQuery) throws IllegalArgumentException; } \ No newline at end of file diff --git a/authentication/source/java/ch/systemsx/cisd/authentication/NullAuthenticationService.java b/authentication/source/java/ch/systemsx/cisd/authentication/NullAuthenticationService.java index ad7dcf25e8553e804eda79a41825e91da779e20e..ad80dce5f1d14e61ce3e52422d242a78af62b7f0 100644 --- a/authentication/source/java/ch/systemsx/cisd/authentication/NullAuthenticationService.java +++ b/authentication/source/java/ch/systemsx/cisd/authentication/NullAuthenticationService.java @@ -94,4 +94,41 @@ public class NullAuthenticationService implements IAuthenticationService return false; } + public boolean authenticateUser(String user, String password) + { + return false; + } + + public Principal getPrincipal(String user) throws IllegalArgumentException + { + return null; + } + + public List<Principal> listPrincipalsByEmail(String emailQuery) throws IllegalArgumentException + { + return null; + } + + public List<Principal> listPrincipalsByLastName(String lastNameQuery) + throws IllegalArgumentException + { + return null; + } + + public List<Principal> listPrincipalsByUserId(String userIdQuery) + throws IllegalArgumentException + { + return null; + } + + public Principal tryGetAndAuthenticateUser(String user, String passwordOrNull) + { + return null; + } + + public Principal tryGetAndAuthenticateUserByEmail(String email, String passwordOrNull) + { + return null; + } + } diff --git a/authentication/source/java/ch/systemsx/cisd/authentication/crowd/CrowdAuthenticationService.java b/authentication/source/java/ch/systemsx/cisd/authentication/crowd/CrowdAuthenticationService.java index 1748512854d6cdecfc32a7ff005f794c776e20d7..3158e128eb409cddc99bca475fa4f754ff786c1e 100644 --- a/authentication/source/java/ch/systemsx/cisd/authentication/crowd/CrowdAuthenticationService.java +++ b/authentication/source/java/ch/systemsx/cisd/authentication/crowd/CrowdAuthenticationService.java @@ -21,6 +21,7 @@ import java.io.StringReader; import java.text.MessageFormat; import java.util.List; import java.util.Map; +import java.util.concurrent.atomic.AtomicReference; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -57,12 +58,17 @@ import ch.systemsx.cisd.common.logging.LogFactory; */ public class CrowdAuthenticationService implements IAuthenticationService { + private static final String DUMMY_TOKEN_STR = "DUMMY-TOKEN"; + private static final String EMAIL_PROPERTY_KEY = "mail"; private static final String LAST_NAME_PROPERTY_KEY = "sn"; private static final String FIRST_NAME_PROPERTY_KEY = "givenName"; + private static final String ERROR_MSG_WITH_INVALID_APPLICATION_TOKEN = + "The application.name or application.password in the crowd.properties file does not match the password in Crowd."; + private static final Logger operationLog = LogFactory.getLogger(LogCategory.OPERATION, CrowdAuthenticationService.class); @@ -169,6 +175,8 @@ public class CrowdAuthenticationService implements IAuthenticationService private final IRequestExecutor requestExecutor; + private final AtomicReference<String> applicationTokenHolder = new AtomicReference<String>(); + public CrowdAuthenticationService(final String host, final String port, final String application, final String applicationPassword) { @@ -224,11 +232,15 @@ public class CrowdAuthenticationService implements IAuthenticationService { try { - final String response = execute(AUTHENTICATE_APPL, application, applicationPassword); - if (pickElementContent(response, CrowdSoapElements.TOKEN) == null) + final String xmlResponse = execute(AUTHENTICATE_APPL, application, applicationPassword); + final String applicationToken = + StringEscapeUtils.unescapeXml(pickElementContent(xmlResponse, + CrowdSoapElements.TOKEN)); + applicationTokenHolder.set(applicationToken); + if (applicationToken == null) { throw new EnvironmentFailureException("Application '" + application - + "' couldn't be authenticated: " + response); + + "' couldn't be authenticated: " + xmlResponse); } } catch (final EnvironmentFailureException ex) { @@ -249,66 +261,154 @@ public class CrowdAuthenticationService implements IAuthenticationService public final String authenticateApplication() { - final String applicationToken = - StringEscapeUtils.unescapeXml(execute(CrowdSoapElements.TOKEN, AUTHENTICATE_APPL, - application, applicationPassword)); - if (applicationToken == null) - { - operationLog.error("CROWD: application '" + application + "' failed to authenticate."); - } else + return DUMMY_TOKEN_STR; + } + + public final boolean authenticateUser(final String dummyToken, final String user, + final String password) + { + return authenticateUser(user, password); + } + + public final boolean authenticateUser(final String user, final String password) + { + assert user != null; + + String userToken = null; + while (true) { - if (operationLog.isDebugEnabled()) + final String xmlResponse = + execute(AUTHENTICATE_USER, application, getApplicationToken(false), user, + password); + userToken = extractUserToken(xmlResponse, user); + if (userToken == null) { - operationLog.debug("CROWD: application '" + application - + "' successfully authenticated."); + if (isApplicationNotAuthenticated(xmlResponse)) + { + if (getApplicationToken(true) == null) + { + // We couldn't authenticate the application. + break; + } + } else + { + // The application is authenticated but the user credentials are not right. + break; + } + } else + { + // Everything is fine. + break; } } - return applicationToken; + logAuthentication(user, userToken != null); + return userToken != null; } - public final boolean authenticateUser(final String applicationToken, final String user, - final String password) + private void logAuthentication(final String user, final boolean authenticated) { - assert applicationToken != null; - assert user != null; - - final String userToken = - StringEscapeUtils.unescapeXml(execute(CrowdSoapElements.OUT, AUTHENTICATE_USER, - application, applicationToken, user, password)); if (operationLog.isInfoEnabled()) { final String msg = "CROWD: authentication of user '" + user + "', application '" + application + "': "; - operationLog.info(msg + (userToken == null ? "FAILED." : "SUCCESS.")); + operationLog.info(msg + (authenticated ? "SUCCESS." : "FAILED.")); } - return userToken != null; } - public Principal tryGetAndAuthenticateUser(String applicationToken, String user, - String passwordOrNull) + private String getApplicationToken(boolean forceNewToken) { - String xmlResponse = null; - try + String applicationToken = applicationTokenHolder.get(); + if (applicationToken == null || forceNewToken) { - xmlResponse = execute(FIND_PRINCIPAL_BY_NAME, application, applicationToken, user); - final Map<String, String> parseXmlResponse = parseXmlResponse(xmlResponse); - Principal principal = null; - if (parseXmlResponse.size() >= 1) + final String xmlResponse = execute(AUTHENTICATE_APPL, application, applicationPassword); + applicationToken = + StringEscapeUtils.unescapeXml(pickElementContent(xmlResponse, + CrowdSoapElements.TOKEN)); + if (applicationToken == null) { - principal = createPrincipal(user, parseXmlResponse); + operationLog.error("CROWD: application '" + application + + "' failed to authenticate."); } else { if (operationLog.isDebugEnabled()) { - operationLog - .debug("No SOAPAttribute element could be found in the SOAP XML response."); + operationLog.debug("CROWD: application '" + application + + "' successfully authenticated."); } } - if (principal != null && passwordOrNull != null) + applicationTokenHolder.set(applicationToken); + } + return applicationToken; + } + + private boolean isApplicationNotAuthenticated(final String xmlResponse) + { + return xmlResponse.indexOf(ERROR_MSG_WITH_INVALID_APPLICATION_TOKEN) >= 0; + } + + private final String extractUserToken(String xmlResponse, String user) + { + final String userToken = + StringEscapeUtils + .unescapeXml(pickElementContent(xmlResponse, CrowdSoapElements.OUT)); + return userToken; + + } + + public Principal tryGetAndAuthenticateUser(String dummyToken, String user, String passwordOrNull) + { + return tryGetAndAuthenticateUser(user, passwordOrNull); + } + + public Principal tryGetAndAuthenticateUser(String user, String passwordOrNull) + { + String xmlResponse = null; + try + { + Principal principal = null; + while (true) { - principal - .setAuthenticated(authenticateUser(applicationToken, user, passwordOrNull)); + xmlResponse = + execute(FIND_PRINCIPAL_BY_NAME, application, getApplicationToken(false), + user); + final Map<String, String> parseXmlResponse = parseXmlResponse(xmlResponse); + if (parseXmlResponse.size() >= 1) + { + principal = createPrincipal(user, parseXmlResponse); + } else + { + if (isApplicationNotAuthenticated(xmlResponse)) + { + if (getApplicationToken(true) == null) + { + // We couldn't authenticate the application. + break; + } + } else + { + // The application is authenticated, but the principal does not exist. + if (operationLog.isDebugEnabled()) + { + operationLog + .debug("No SOAPAttribute element could be found in the SOAP XML response."); + } + break; + } + } + if (principal != null && passwordOrNull != null) + { + principal.setAuthenticated(authenticateUser(getApplicationToken(false), user, + passwordOrNull)); + } + if (principal != null) + { + break; + } + } + if (passwordOrNull != null) + { + logAuthentication(user, Principal.isAuthenticated(principal)); } return principal; } catch (final Exception ex) // SAXException, IOException @@ -321,7 +421,12 @@ public class CrowdAuthenticationService implements IAuthenticationService public final Principal getPrincipal(final String applicationToken, final String user) { - final Principal principalOrNull = tryGetAndAuthenticateUser(applicationToken, user, null); + return getPrincipal(user); + } + + public final Principal getPrincipal(final String user) + { + final Principal principalOrNull = tryGetAndAuthenticateUser(user, null); if (principalOrNull == null) { throw new IllegalArgumentException("Cannot find user '" + user + "'."); @@ -359,18 +464,10 @@ public class CrowdAuthenticationService implements IAuthenticationService } /** - * Constructs the POST message, does the HTTP request and picks the given - * <code>responseElement</code> in the server's response. + * Constructs the POST message and does the HTTP request. * * @return The <var>responseElement</var> in the server's response. */ - private final String execute(final String responseElement, final MessageFormat template, - final String... args) - { - final String response = execute(template, args); - return pickElementContent(response, responseElement); - } - private final String execute(final MessageFormat template, final String... args) { final Object[] decodedArguments = new Object[args.length]; @@ -384,7 +481,7 @@ public class CrowdAuthenticationService implements IAuthenticationService /** * Tries to find given <code>element</code> in <code>xmlString</code>. * <p> - * Note that this is a special-perpose method not suitable for putting it into general utility + * Note that this is a special-purpose method not suitable for putting it into general utility * classes. For example it does not find empty elements. * * @return The requested element, or <code>null</code> if it could not be found. @@ -443,12 +540,28 @@ public class CrowdAuthenticationService implements IAuthenticationService return index; } + public List<Principal> listPrincipalsByEmail(String emailQuery) + { + throw new UnsupportedOperationException(); + } + public List<Principal> listPrincipalsByEmail(String applicationToken, String emailQuery) { throw new UnsupportedOperationException(); } - public Principal tryGetAndAuthenticateUserByEmail(String applicationToken, String email, String passwordOrNull) + public Principal tryGetAndAuthenticateUserByEmail(String email, String passwordOrNull) + { + throw new UnsupportedOperationException(); + } + + public Principal tryGetAndAuthenticateUserByEmail(String applicationToken, String email, + String passwordOrNull) + { + throw new UnsupportedOperationException(); + } + + public List<Principal> listPrincipalsByLastName(String lastNameQuery) { throw new UnsupportedOperationException(); } @@ -458,6 +571,11 @@ public class CrowdAuthenticationService implements IAuthenticationService throw new UnsupportedOperationException(); } + public List<Principal> listPrincipalsByUserId(String userIdQuery) + { + throw new UnsupportedOperationException(); + } + public List<Principal> listPrincipalsByUserId(String applicationToken, String userIdQuery) { throw new UnsupportedOperationException(); diff --git a/authentication/source/java/ch/systemsx/cisd/authentication/file/FileAuthenticationService.java b/authentication/source/java/ch/systemsx/cisd/authentication/file/FileAuthenticationService.java index a173e1f87bafb193a4f446a799e2ec04c9babe60..25edb73319fc8ff165e8c738e7b6ccc7767890ed 100644 --- a/authentication/source/java/ch/systemsx/cisd/authentication/file/FileAuthenticationService.java +++ b/authentication/source/java/ch/systemsx/cisd/authentication/file/FileAuthenticationService.java @@ -19,14 +19,10 @@ package ch.systemsx.cisd.authentication.file; import java.io.File; import java.util.List; -import org.apache.log4j.Logger; - import ch.systemsx.cisd.authentication.IAuthenticationService; import ch.systemsx.cisd.authentication.Principal; import ch.systemsx.cisd.common.exceptions.ConfigurationFailureException; import ch.systemsx.cisd.common.exceptions.EnvironmentFailureException; -import ch.systemsx.cisd.common.logging.LogCategory; -import ch.systemsx.cisd.common.logging.LogFactory; /** * An implementation of {@link IAuthenticationService} that gets the authentication information from @@ -46,11 +42,7 @@ import ch.systemsx.cisd.common.logging.LogFactory; public class FileAuthenticationService implements IAuthenticationService { - private static final String TOKEN_FAILURE_MSG_TEMPLATE = - "Wrong application token provided, expected '%s', got '%s'"; - - private static final Logger operationLog = - LogFactory.getLogger(LogCategory.OPERATION, FileAuthenticationService.class); + private static final String DUMMY_TOKEN_STR = "DUMMY-TOKEN"; private final IUserStore userStore; @@ -71,39 +63,33 @@ public class FileAuthenticationService implements IAuthenticationService this.userStore = userStore; } - private String getToken() - { - return userStore.getId(); - } - /** * Returns the id of the password store, which we consider to be the token. */ public String authenticateApplication() { - return getToken(); + return DUMMY_TOKEN_STR; } - public boolean authenticateUser(String applicationToken, String user, String password) + public boolean authenticateUser(String dummyToken, String user, String password) + { + return userStore.isPasswordCorrect(user, password); + } + + public boolean authenticateUser(String user, String password) { - final String token = getToken(); - if (token.equals(applicationToken) == false) - { - operationLog.warn(String.format(TOKEN_FAILURE_MSG_TEMPLATE, token, applicationToken)); - return false; - } return userStore.isPasswordCorrect(user, password); } - public Principal tryGetAndAuthenticateUser(String applicationToken, String user, + public Principal tryGetAndAuthenticateUser(String dummyToken, String user, + String passwordOrNull) + { + return tryGetAndAuthenticateUser(user, passwordOrNull); + } + + public Principal tryGetAndAuthenticateUser(String user, String passwordOrNull) { - final String token = getToken(); - if (token.equals(applicationToken) == false) - { - operationLog.warn(String.format(TOKEN_FAILURE_MSG_TEMPLATE, token, applicationToken)); - return null; - } final UserEntry userOrNull = userStore.tryGetUser(user); if (userOrNull != null) { @@ -111,7 +97,7 @@ public class FileAuthenticationService implements IAuthenticationService if (passwordOrNull != null) { principal - .setAuthenticated(authenticateUser(applicationToken, user, passwordOrNull)); + .setAuthenticated(authenticateUser(user, passwordOrNull)); } return principal; } else @@ -122,7 +108,12 @@ public class FileAuthenticationService implements IAuthenticationService public Principal getPrincipal(String applicationToken, String user) { - final Principal principalOrNull = tryGetAndAuthenticateUser(applicationToken, user, null); + return getPrincipal(user); + } + + public Principal getPrincipal(String user) + { + final Principal principalOrNull = tryGetAndAuthenticateUser(user, null); if (principalOrNull == null) { throw new IllegalArgumentException("Cannot find user '" + user + "'."); @@ -135,17 +126,37 @@ public class FileAuthenticationService implements IAuthenticationService throw new UnsupportedOperationException(); } + public Principal tryGetAndAuthenticateUserByEmail(String email, String passwordOrNull) + { + throw new UnsupportedOperationException(); + } + public List<Principal> listPrincipalsByEmail(String applicationToken, String emailQuery) { throw new UnsupportedOperationException(); } + public List<Principal> listPrincipalsByEmail(String emailQuery) + { + throw new UnsupportedOperationException(); + } + public List<Principal> listPrincipalsByLastName(String applicationToken, String lastNameQuery) { throw new UnsupportedOperationException(); } - public List<Principal> listPrincipalsByUserId(String applicationToken, String userIdQuery) + public List<Principal> listPrincipalsByLastName(String lastNameQuery) + { + throw new UnsupportedOperationException(); + } + + public List<Principal> listPrincipalsByUserId(String dummyToken, String userIdQuery) + { + throw new UnsupportedOperationException(); + } + + public List<Principal> listPrincipalsByUserId(String userIdQuery) { throw new UnsupportedOperationException(); } diff --git a/authentication/source/java/ch/systemsx/cisd/authentication/ldap/LDAPAuthenticationService.java b/authentication/source/java/ch/systemsx/cisd/authentication/ldap/LDAPAuthenticationService.java index 75f95dbe0de8e447a791a07c7d19e3bd18f31068..d6c94b1e9f671615b5349f4471f41162a126b941 100644 --- a/authentication/source/java/ch/systemsx/cisd/authentication/ldap/LDAPAuthenticationService.java +++ b/authentication/source/java/ch/systemsx/cisd/authentication/ldap/LDAPAuthenticationService.java @@ -45,18 +45,34 @@ public class LDAPAuthenticationService implements IAuthenticationService } public boolean authenticateUser(String applicationToken, String user, String password) + { + return authenticateUser(user, password); + } + + public boolean authenticateUser(String user, String password) { return query.authenticateUser(user, password); } public Principal tryGetAndAuthenticateUser(String applicationToken, String user, String passwordOrNull) + { + return tryGetAndAuthenticateUser(user, passwordOrNull); + } + + public Principal tryGetAndAuthenticateUser(String user, String passwordOrNull) { return query.tryGetAndAuthenticatePrincipal(user, passwordOrNull); } public Principal getPrincipal(String applicationToken, String user) throws IllegalArgumentException + { + return getPrincipal(user); + } + + public Principal getPrincipal(String user) + throws IllegalArgumentException { final Principal principalOrNull = query.tryGetPrincipal(user); if (principalOrNull == null) @@ -67,21 +83,43 @@ public class LDAPAuthenticationService implements IAuthenticationService } public List<Principal> listPrincipalsByEmail(String applicationToken, String emailQuery) + { + return listPrincipalsByEmail(emailQuery); + } + + public List<Principal> listPrincipalsByEmail(String emailQuery) { return query.listPrincipalsByEmail(emailQuery); } - public Principal tryGetAndAuthenticateUserByEmail(String applicationToken, String email, String passwordOrNull) + public Principal tryGetAndAuthenticateUserByEmail(String applicationToken, String email, + String passwordOrNull) + { + return tryGetAndAuthenticateUserByEmail(email, passwordOrNull); + } + + public Principal tryGetAndAuthenticateUserByEmail(String email, + String passwordOrNull) { return query.tryGetAndAuthenticatePrincipalByEmail(email, passwordOrNull); } public List<Principal> listPrincipalsByLastName(String applicationToken, String lastNameQuery) + { + return listPrincipalsByLastName(lastNameQuery); + } + + public List<Principal> listPrincipalsByLastName(String lastNameQuery) { return query.listPrincipalsByLastName(lastNameQuery); } public List<Principal> listPrincipalsByUserId(String applicationToken, String userIdQuery) + { + return listPrincipalsByUserId(userIdQuery); + } + + public List<Principal> listPrincipalsByUserId(String userIdQuery) { return query.listPrincipalsByUserId(userIdQuery); } diff --git a/authentication/source/java/ch/systemsx/cisd/authentication/stacked/StackedAuthenticationService.java b/authentication/source/java/ch/systemsx/cisd/authentication/stacked/StackedAuthenticationService.java index 3c93b2053527f991520593094b6f6f4e483eecab..8fb7792594e81679836f5ccde1d1687502b94e78 100644 --- a/authentication/source/java/ch/systemsx/cisd/authentication/stacked/StackedAuthenticationService.java +++ b/authentication/source/java/ch/systemsx/cisd/authentication/stacked/StackedAuthenticationService.java @@ -36,8 +36,6 @@ public class StackedAuthenticationService implements IAuthenticationService private final List<IAuthenticationService> delegates; - private final List<String> tokens; - private final boolean remote; private final boolean supportsListingByUserId; @@ -49,7 +47,6 @@ public class StackedAuthenticationService implements IAuthenticationService public StackedAuthenticationService(List<IAuthenticationService> authenticationServices) { this.delegates = authenticationServices; - this.tokens = new ArrayList<String>(delegates.size()); boolean foundRemote = false; boolean foundSupportsListingByUserId = false; boolean foundSupportsListingByEmail = false; @@ -69,31 +66,28 @@ public class StackedAuthenticationService implements IAuthenticationService public String authenticateApplication() { - tokens.clear(); - for (IAuthenticationService service : delegates) - { - final String token = service.authenticateApplication(); - if (token == null) - { - tokens.clear(); - return null; - } - tokens.add(token); - } return DUMMY_TOKEN_STR; } - public boolean authenticateUser(String applicationToken, String user, String password) + public boolean authenticateUser(String dummyToken, String user, String password) + { + return authenticateUser(user, password); + } + + public boolean authenticateUser(String user, String password) { - final Principal principalOrNull = - tryGetAndAuthenticateUser(applicationToken, user, password); + final Principal principalOrNull = tryGetAndAuthenticateUser(user, password); return Principal.isAuthenticated(principalOrNull); } - public Principal getPrincipal(String applicationToken, String user) - throws IllegalArgumentException + public Principal getPrincipal(String dummyToken, String user) throws IllegalArgumentException { - final Principal principalOrNull = tryGetAndAuthenticateUser(applicationToken, user, null); + return getPrincipal(user); + } + + public Principal getPrincipal(String user) throws IllegalArgumentException + { + final Principal principalOrNull = tryGetAndAuthenticateUser(DUMMY_TOKEN_STR, user, null); if (principalOrNull == null) { throw new IllegalArgumentException("Cannot find user '" + user + "'."); @@ -101,101 +95,110 @@ public class StackedAuthenticationService implements IAuthenticationService return principalOrNull; } - public Principal tryGetAndAuthenticateUser(String applicationToken, String user, - String passwordOrNull) + public Principal tryGetAndAuthenticateUser(String dummyToken, String user, String passwordOrNull) + { + return tryGetAndAuthenticateUser(user, passwordOrNull); + } + + public Principal tryGetAndAuthenticateUser(String user, String passwordOrNull) { - checkAuthenticatedApplication(); - int i = 0; for (IAuthenticationService service : delegates) { - final String token = tokens.get(i); final Principal principal = - service.tryGetAndAuthenticateUser(token, user, passwordOrNull); + service.tryGetAndAuthenticateUser(DUMMY_TOKEN_STR, user, passwordOrNull); if (principal != null) { return principal; } - ++i; } return null; } - public Principal tryGetAndAuthenticateUserByEmail(String applicationToken, String email, String passwordOrNull) + public Principal tryGetAndAuthenticateUserByEmail(String applicationToken, String email, + String passwordOrNull) + { + return tryGetAndAuthenticateUserByEmail(email, passwordOrNull); + } + + public Principal tryGetAndAuthenticateUserByEmail(String email, + String passwordOrNull) { - checkAuthenticatedApplication(); - int i = 0; for (IAuthenticationService service : delegates) { - final String token = tokens.get(i); - final Principal principal = service.tryGetAndAuthenticateUserByEmail(token, email, passwordOrNull); + final Principal principal = + service + .tryGetAndAuthenticateUserByEmail(DUMMY_TOKEN_STR, email, + passwordOrNull); if (principal != null) { return principal; } - ++i; } return null; } public List<Principal> listPrincipalsByEmail(String applicationToken, String emailQuery) + { + return listPrincipalsByEmail(emailQuery); + } + + public List<Principal> listPrincipalsByEmail(String emailQuery) { if (supportsListingByEmail == false) { throw new UnsupportedOperationException(); } - checkAuthenticatedApplication(); final List<Principal> principals = new ArrayList<Principal>(); - int i = 0; for (IAuthenticationService service : delegates) { - final String token = tokens.get(i); if (service.supportsListingByEmail()) { - principals.addAll(service.listPrincipalsByEmail(token, emailQuery)); + principals.addAll(service.listPrincipalsByEmail(DUMMY_TOKEN_STR, emailQuery)); } - ++i; } return principals; } public List<Principal> listPrincipalsByLastName(String applicationToken, String lastNameQuery) + { + return listPrincipalsByLastName(lastNameQuery); + } + + public List<Principal> listPrincipalsByLastName(String lastNameQuery) { if (supportsListingByLastName == false) { throw new UnsupportedOperationException(); } - checkAuthenticatedApplication(); final List<Principal> principals = new ArrayList<Principal>(); - int i = 0; for (IAuthenticationService service : delegates) { - final String token = tokens.get(i); if (service.supportsListingByLastName()) { - principals.addAll(service.listPrincipalsByLastName(token, lastNameQuery)); + principals.addAll(service.listPrincipalsByLastName(DUMMY_TOKEN_STR, lastNameQuery)); } - ++i; } return principals; } public List<Principal> listPrincipalsByUserId(String applicationToken, String userIdQuery) + { + return listPrincipalsByUserId(userIdQuery); + } + + public List<Principal> listPrincipalsByUserId(String userIdQuery) { if (supportsListingByUserId == false) { throw new UnsupportedOperationException(); } - checkAuthenticatedApplication(); final List<Principal> principals = new ArrayList<Principal>(); - int i = 0; for (IAuthenticationService service : delegates) { - final String token = tokens.get(i); if (service.supportsListingByUserId()) { - principals.addAll(service.listPrincipalsByUserId(token, userIdQuery)); + principals.addAll(service.listPrincipalsByUserId(DUMMY_TOKEN_STR, userIdQuery)); } - ++i; } return principals; } @@ -228,12 +231,4 @@ public class StackedAuthenticationService implements IAuthenticationService return remote; } - private void checkAuthenticatedApplication() - { - if (tokens.isEmpty()) - { - throw new IllegalArgumentException("Application not authenticated."); - } - } - } diff --git a/authentication/sourceTest/java/ch/systemsx/cisd/authentication/DefaultSessionManagerTest.java b/authentication/sourceTest/java/ch/systemsx/cisd/authentication/DefaultSessionManagerTest.java index a95e9b496107f336259595fd3c38fc97c8c6b599..b13b4d786a145a69d67aa89b2e267e384d46d5a5 100644 --- a/authentication/sourceTest/java/ch/systemsx/cisd/authentication/DefaultSessionManagerTest.java +++ b/authentication/sourceTest/java/ch/systemsx/cisd/authentication/DefaultSessionManagerTest.java @@ -130,18 +130,14 @@ public class DefaultSessionManagerTest @Test public void testSuccessfulAuthentication() { - final String applicationToken = "ole"; final String user = "bla"; prepareRemoteHostSessionFactoryAndPrefixGenerator(user); context.checking(new Expectations() { { - one(authenticationService).tryGetAndAuthenticateUser(applicationToken, user, + one(authenticationService).tryGetAndAuthenticateUser(null, user, "blub"); will(returnValue(principal)); - - one(authenticationService).authenticateApplication(); - will(returnValue(applicationToken)); } }); @@ -162,17 +158,13 @@ public class DefaultSessionManagerTest @Test public void testFailedAuthentication() { - final String applicationToken = "ole"; final String user = "bla"; context.checking(new Expectations() { { - one(authenticationService).tryGetAndAuthenticateUser("ole", user, "blub"); + one(authenticationService).tryGetAndAuthenticateUser(null, user, "blub"); will(returnValue(null)); - one(authenticationService).authenticateApplication(); - will(returnValue(applicationToken)); - allowing(remoteHostProvider).getRemoteHost(); will(returnValue(REMOTE_HOST)); @@ -216,7 +208,6 @@ public class DefaultSessionManagerTest @Test public void testExpirationOfSession() { - final String applicationToken = "ole"; final String user = "bla"; prepareRemoteHostSessionFactoryAndPrefixGenerator(user); context.checking(new Expectations() @@ -224,12 +215,9 @@ public class DefaultSessionManagerTest { one(authenticationService).check(); - one(authenticationService).tryGetAndAuthenticateUser(applicationToken, user, + one(authenticationService).tryGetAndAuthenticateUser(null, user, "blub"); will(returnValue(principal)); - - one(authenticationService).authenticateApplication(); - will(returnValue(applicationToken)); } }); @@ -267,19 +255,15 @@ public class DefaultSessionManagerTest @Test public void testSessionRemoval() { - final String applicationToken = "ole"; final String user = "bla"; final String password = "blub"; prepareRemoteHostSessionFactoryAndPrefixGenerator(user); context.checking(new Expectations() { { - one(authenticationService).tryGetAndAuthenticateUser(applicationToken, user, + one(authenticationService).tryGetAndAuthenticateUser(null, user, password); will(returnValue(principal)); - - one(authenticationService).authenticateApplication(); - will(returnValue(applicationToken)); } }); diff --git a/authentication/sourceTest/java/ch/systemsx/cisd/authentication/crowd/CrowdAuthenticationServiceTest.java b/authentication/sourceTest/java/ch/systemsx/cisd/authentication/crowd/CrowdAuthenticationServiceTest.java index 8739763a730126c791c30355118374c24fe70fec..2a53c397b47f2b2a740cad113d9810ee19091a1c 100644 --- a/authentication/sourceTest/java/ch/systemsx/cisd/authentication/crowd/CrowdAuthenticationServiceTest.java +++ b/authentication/sourceTest/java/ch/systemsx/cisd/authentication/crowd/CrowdAuthenticationServiceTest.java @@ -51,7 +51,7 @@ public class CrowdAuthenticationServiceTest private static final String APPLICATION_PASSWORD_ESCAPED = "<password>"; - private static final String APPLICATION_TOKEN = "application<&>token"; + private static final String APPLICATION_TOKEN = "DUMMY-TOKEN"; private static final String APPLICATION_TOKEN_ESACPED = "application<&>token"; @@ -91,66 +91,24 @@ public class CrowdAuthenticationServiceTest } @Test - public void testSuccessfullApplicationAuthentication() + public void testSuccessfullUserAuthentication() { context.checking(new Expectations() { { - final Object[] parameters = new Object[] + Object[] parameters = new Object[] { APPLICATION_ESCAPED, APPLICATION_PASSWORD_ESCAPED }; - final String message = + String message = CrowdAuthenticationService.AUTHENTICATE_APPL.format(parameters); one(executor).execute(URL, message); will(returnValue(createXMLElement(CrowdSoapElements.TOKEN, APPLICATION_TOKEN_ESACPED))); - } - - }); - final String result = authenticationService.authenticateApplication(); - assertEquals(APPLICATION_TOKEN, result); - assertEquals(createDebugLogEntry("CROWD: application '" + APPLICATION - + "' successfully authenticated."), logRecorder.getLogContent()); - - context.assertIsSatisfied(); - } - - @Test - public void testFailedApplicationAuthentication() - { - context.checking(new Expectations() - { - { - final Object[] parameters = new Object[] - { APPLICATION_ESCAPED, APPLICATION_PASSWORD_ESCAPED }; - final String message = - CrowdAuthenticationService.AUTHENTICATE_APPL.format(parameters); - one(executor).execute(URL, message); - will(returnValue("error")); - } - }); - final String result = authenticationService.authenticateApplication(); - assertEquals(null, result); - assertEquals(createDebugLogEntry("Element '" + CrowdSoapElements.TOKEN - + "' could not be found in 'error'.") - + OSUtilities.LINE_SEPARATOR - + createErrorLogEntry("CROWD: application '" + APPLICATION - + "' failed to authenticate."), logRecorder.getLogContent()); - context.assertIsSatisfied(); - } - - @Test - public void testSuccessfullUserAuthentication() - { - context.checking(new Expectations() - { - { - final Object[] parameters = + parameters = new Object[] { APPLICATION_ESCAPED, APPLICATION_TOKEN_ESACPED, USER_ESCAPED, USER_PASSWORD_ESCAPED }; - final String message = - CrowdAuthenticationService.AUTHENTICATE_USER.format(parameters); + message = CrowdAuthenticationService.AUTHENTICATE_USER.format(parameters); one(executor).execute(URL, message); will(returnValue(createXMLElement("n:" + CrowdSoapElements.OUT, APPLICATION_TOKEN_ESACPED))); @@ -159,8 +117,11 @@ public class CrowdAuthenticationServiceTest final boolean result = authenticationService.authenticateUser(APPLICATION_TOKEN, USER, USER_PASSWORD); assertEquals(true, result); - assertEquals(createInfoLogEntry("CROWD: authentication of user '" + USER - + "', application '" + APPLICATION + "': SUCCESS."), logRecorder.getLogContent()); + assertEquals(createDebugLogEntry("CROWD: application '" + APPLICATION + + "' successfully authenticated.") + + OSUtilities.LINE_SEPARATOR + + createInfoLogEntry("CROWD: authentication of user '" + USER + "', application '" + + APPLICATION + "': SUCCESS."), logRecorder.getLogContent()); context.assertIsSatisfied(); } @@ -171,12 +132,19 @@ public class CrowdAuthenticationServiceTest context.checking(new Expectations() { { - final Object[] parameters = + Object[] parameters = new Object[] + { APPLICATION_ESCAPED, APPLICATION_PASSWORD_ESCAPED }; + String message = + CrowdAuthenticationService.AUTHENTICATE_APPL.format(parameters); + one(executor).execute(URL, message); + will(returnValue(createXMLElement(CrowdSoapElements.TOKEN, + APPLICATION_TOKEN_ESACPED))); + + parameters = new Object[] { APPLICATION_ESCAPED, APPLICATION_TOKEN_ESACPED, USER_ESCAPED, USER_PASSWORD_ESCAPED }; - final String message = - CrowdAuthenticationService.AUTHENTICATE_USER.format(parameters); + message = CrowdAuthenticationService.AUTHENTICATE_USER.format(parameters); one(executor).execute(URL, message); will(returnValue("error")); } @@ -184,8 +152,11 @@ public class CrowdAuthenticationServiceTest final boolean result = authenticationService.authenticateUser(APPLICATION_TOKEN, USER, USER_PASSWORD); assertEquals(false, result); - assertEquals(createDebugLogEntry("Element '" + CrowdSoapElements.OUT - + "' could not be found in 'error'.") + assertEquals(createDebugLogEntry("CROWD: application '" + APPLICATION + + "' successfully authenticated.") + + OSUtilities.LINE_SEPARATOR + + createDebugLogEntry("Element '" + CrowdSoapElements.OUT + + "' could not be found in 'error'.") + OSUtilities.LINE_SEPARATOR + createInfoLogEntry("CROWD: authentication of user '" + USER + "', application '" + APPLICATION + "': FAILED."), logRecorder.getLogContent()); @@ -199,10 +170,17 @@ public class CrowdAuthenticationServiceTest context.checking(new Expectations() { { - final Object[] parameters = new Object[] + Object[] parameters = new Object[] + { APPLICATION_ESCAPED, APPLICATION_PASSWORD_ESCAPED }; + String message = + CrowdAuthenticationService.AUTHENTICATE_APPL.format(parameters); + one(executor).execute(URL, message); + will(returnValue(createXMLElement(CrowdSoapElements.TOKEN, + APPLICATION_TOKEN_ESACPED))); + + parameters = new Object[] { APPLICATION_ESCAPED, APPLICATION_TOKEN_ESACPED, USER_ESCAPED }; - final String message = - CrowdAuthenticationService.FIND_PRINCIPAL_BY_NAME.format(parameters); + message = CrowdAuthenticationService.FIND_PRINCIPAL_BY_NAME.format(parameters); one(executor).execute(URL, message); String element = createSOAPAttribute("sn", "Stepka"); element += createSOAPAttribute("invalidPasswordAttempts", "0"); @@ -229,7 +207,8 @@ public class CrowdAuthenticationServiceTest assertEquals("false", result.getProperty("requiresPasswordChange")); assertEquals("1169440408520", result.getProperty("lastAuthenticated")); assertEquals("1168995491407", result.getProperty("passwordLastChanged")); - assertEquals("", logRecorder.getLogContent()); + assertEquals(createDebugLogEntry("CROWD: application '" + APPLICATION + + "' successfully authenticated."), logRecorder.getLogContent()); context.assertIsSatisfied(); } @@ -239,10 +218,17 @@ public class CrowdAuthenticationServiceTest context.checking(new Expectations() { { - final Object[] parameters = new Object[] + Object[] parameters = new Object[] + { APPLICATION_ESCAPED, APPLICATION_PASSWORD_ESCAPED }; + String message = + CrowdAuthenticationService.AUTHENTICATE_APPL.format(parameters); + one(executor).execute(URL, message); + will(returnValue(createXMLElement(CrowdSoapElements.TOKEN, + APPLICATION_TOKEN_ESACPED))); + + parameters = new Object[] { APPLICATION_ESCAPED, APPLICATION_TOKEN_ESACPED, USER_ESCAPED }; - final String message = - CrowdAuthenticationService.FIND_PRINCIPAL_BY_NAME.format(parameters); + message = CrowdAuthenticationService.FIND_PRINCIPAL_BY_NAME.format(parameters); one(executor).execute(URL, message); will(returnValue("<a></a>")); } @@ -257,7 +243,10 @@ public class CrowdAuthenticationServiceTest } assertEquals( - createDebugLogEntry("No SOAPAttribute element could be found in the SOAP XML response."), + createDebugLogEntry("CROWD: application '" + APPLICATION + + "' successfully authenticated.") + + OSUtilities.LINE_SEPARATOR + + createDebugLogEntry("No SOAPAttribute element could be found in the SOAP XML response."), logRecorder.getLogContent()); context.assertIsSatisfied(); @@ -273,11 +262,6 @@ public class CrowdAuthenticationServiceTest return createLogEntry("INFO ", message); } - private String createErrorLogEntry(final String message) - { - return createLogEntry("ERROR", message); - } - private String createLogEntry(final String level, final String message) { return level + " OPERATION." + authenticationService.getClass().getSimpleName() + " - " diff --git a/authentication/sourceTest/java/ch/systemsx/cisd/authentication/crowd/RealCrowdAuthenticationTest.java b/authentication/sourceTest/java/ch/systemsx/cisd/authentication/crowd/RealCrowdAuthenticationTest.java index e9ed31a56208596dbfe1509ef80eb69ba441e499..f36b4a53ff9cee68ecaf0b8415fc9ffaff75625b 100644 --- a/authentication/sourceTest/java/ch/systemsx/cisd/authentication/crowd/RealCrowdAuthenticationTest.java +++ b/authentication/sourceTest/java/ch/systemsx/cisd/authentication/crowd/RealCrowdAuthenticationTest.java @@ -45,6 +45,8 @@ import ch.systemsx.cisd.authentication.Principal; public class RealCrowdAuthenticationTest { + private static final String DUMMY_TOKEN = "DUMMY"; + private static final String PORT_OF_AUTHENTICATION_SERVICE = null; // FIX! private static final String HOST_NAME_OF_AUTHENTICATION_SERVICE = null; // FIX! @@ -64,11 +66,8 @@ public class RealCrowdAuthenticationTest new CrowdAuthenticationService(HOST_NAME_OF_AUTHENTICATION_SERVICE, PORT_OF_AUTHENTICATION_SERVICE, NAME_OF_TEST_APPLICATION, PASSWORD_OF_TEST_APPLICATION); - final String token = as.authenticateApplication(); - assertNotNull(token); - assertTrue(as.authenticateUser(token, NAME_OF_TEST_USER, PASSWORD_OF_TEST_USER)); - final Principal p = as.getPrincipal(token, NAME_OF_TEST_USER); - assertNotNull(token); + assertTrue(as.authenticateUser(DUMMY_TOKEN, NAME_OF_TEST_USER, PASSWORD_OF_TEST_USER)); + final Principal p = as.getPrincipal(DUMMY_TOKEN, NAME_OF_TEST_USER); assertEquals(NAME_OF_TEST_USER, p.getUserId()); System.out.println("firstName=" + p.getFirstName()); System.out.println("lastName=" + p.getLastName()); @@ -78,4 +77,18 @@ public class RealCrowdAuthenticationTest System.out.println(key + " : " + p.getProperty(key)); } } + + @Test(groups = "broken") + public void testCrowdTestAccountWithTryGetAndAuthenticateUser() + { + final IAuthenticationService as = + new CrowdAuthenticationService(HOST_NAME_OF_AUTHENTICATION_SERVICE, + PORT_OF_AUTHENTICATION_SERVICE, NAME_OF_TEST_APPLICATION, + PASSWORD_OF_TEST_APPLICATION); + final Principal principal = + as.tryGetAndAuthenticateUser(DUMMY_TOKEN, NAME_OF_TEST_USER, PASSWORD_OF_TEST_USER); + assertNotNull(principal); + assertEquals(NAME_OF_TEST_USER, principal.getUserId()); + assertTrue(principal.isAuthenticated()); + } } diff --git a/authentication/sourceTest/java/ch/systemsx/cisd/authentication/file/FileAuthenticationServiceTest.java b/authentication/sourceTest/java/ch/systemsx/cisd/authentication/file/FileAuthenticationServiceTest.java index 252fb82ba0241acba179389bb88c891829e8db3d..cf365373799dd4d0370cdf47d5a2f70f65c22fa3 100644 --- a/authentication/sourceTest/java/ch/systemsx/cisd/authentication/file/FileAuthenticationServiceTest.java +++ b/authentication/sourceTest/java/ch/systemsx/cisd/authentication/file/FileAuthenticationServiceTest.java @@ -52,95 +52,68 @@ public class FileAuthenticationServiceTest context.assertIsSatisfied(); } - @Test - public void testAuthenticateApplication() - { - final String token = "Some Token"; - context.checking(new Expectations() - { - { - one(userStore).getId(); - will(returnValue(token)); - } - }); - assertEquals(token, authService.authenticateApplication()); - context.assertIsSatisfied(); - } - @Test public void testAuthenticateUserSuccess() { - final String token = "Some Token"; final String user = "User"; final String password = "passw0rd"; context.checking(new Expectations() { { - one(userStore).getId(); - will(returnValue(token)); one(userStore).isPasswordCorrect(user, password); will(returnValue(true)); } }); - assertTrue(authService.authenticateUser(token, user, password)); + assertTrue(authService.authenticateUser("doesntmatter", user, password)); context.assertIsSatisfied(); } @Test public void testAuthenticateUserFailure() { - final String token = "Some Token"; final String user = "User"; final String password = "passw0rd"; context.checking(new Expectations() { { - one(userStore).getId(); - will(returnValue(token)); one(userStore).isPasswordCorrect(user, password); will(returnValue(false)); } }); - assertFalse(authService.authenticateUser(token, user, password)); + assertFalse(authService.authenticateUser("doesntmatter", user, password)); context.assertIsSatisfied(); } @Test public void testGetPrincipalSuccess() { - final String token = "Some Token"; final String uid = "uid"; final UserEntry user = new UserEntry(uid, "email", "first", "last", "pwd"); context.checking(new Expectations() { { - one(userStore).getId(); - will(returnValue(token)); one(userStore).tryGetUser(uid); will(returnValue(user)); } }); - assertEquals(user.asPrincipal(), authService.getPrincipal(token, uid)); + assertEquals(user.asPrincipal(), authService.getPrincipal("doesntmatter", uid)); context.assertIsSatisfied(); } @Test public void testGetPrincipalFailure() { - final String token = "Some Token"; final String uid = "uid"; context.checking(new Expectations() { { - one(userStore).getId(); - will(returnValue(token)); one(userStore).tryGetUser(uid); will(returnValue(null)); } }); try { - authService.getPrincipal(token, uid); + authService.getPrincipal("doesntmatter", uid); fail("Unknown user went undetected."); } catch (IllegalArgumentException ex) { diff --git a/authentication/sourceTest/java/ch/systemsx/cisd/authentication/stacked/StackedAuthenticationServiceTest.java b/authentication/sourceTest/java/ch/systemsx/cisd/authentication/stacked/StackedAuthenticationServiceTest.java index 076fbd00bab096d130e07f0de396d2062cca37d2..74bf7ccce16726c5e2f8adbf9cf7cf4fa0e713f3 100644 --- a/authentication/sourceTest/java/ch/systemsx/cisd/authentication/stacked/StackedAuthenticationServiceTest.java +++ b/authentication/sourceTest/java/ch/systemsx/cisd/authentication/stacked/StackedAuthenticationServiceTest.java @@ -37,6 +37,8 @@ import ch.systemsx.cisd.authentication.Principal; */ public class StackedAuthenticationServiceTest { + private static final String DUMMY_TOKEN_STR = "DUMMY-TOKEN"; + private Mockery context; private IAuthenticationService authService1; @@ -192,77 +194,19 @@ public class StackedAuthenticationServiceTest context.assertIsSatisfied(); } - @Test - public void testAuthenticateApplicationOK() - { - final String token1 = "token1"; - final String token2 = "token2"; - - context.checking(new Expectations() - { - { - one(authService1).authenticateApplication(); - will(returnValue(token1)); - one(authService2).authenticateApplication(); - will(returnValue(token2)); - } - }); - assertNotNull(stackedAuthService.authenticateApplication()); - context.assertIsSatisfied(); - } - - @Test - public void testAuthenticateApplicationServiceOneFails() - { - context.checking(new Expectations() - { - { - one(authService1).authenticateApplication(); - will(returnValue(null)); - } - }); - assertNull(stackedAuthService.authenticateApplication()); - context.assertIsSatisfied(); - } - - @Test - public void testAuthenticateApplicationServiceTwoFails() - { - final String token1 = "token1"; - - context.checking(new Expectations() - { - { - one(authService1).authenticateApplication(); - will(returnValue(token1)); - one(authService2).authenticateApplication(); - will(returnValue(null)); - } - }); - assertNull(stackedAuthService.authenticateApplication()); - context.assertIsSatisfied(); - } - @Test public void testAuthenticateUserFalse() { - final String token1 = "token1"; - final String token2 = "token2"; final String user = "user"; final String password = "password"; context.checking(new Expectations() { { - one(authService1).authenticateApplication(); - will(returnValue(token1)); - one(authService2).authenticateApplication(); - will(returnValue(token2)); - one(authService1).tryGetAndAuthenticateUser(token1, user, password); - one(authService2).tryGetAndAuthenticateUser(token2, user, password); + one(authService1).tryGetAndAuthenticateUser(DUMMY_TOKEN_STR, user, password); + one(authService2).tryGetAndAuthenticateUser(DUMMY_TOKEN_STR, user, password); } }); - assertNotNull(stackedAuthService.authenticateApplication()); assertFalse(stackedAuthService.authenticateUser("doesntmatter", user, password)); context.assertIsSatisfied(); } @@ -270,8 +214,6 @@ public class StackedAuthenticationServiceTest @Test public void testAuthenticateUserFirstServiceTrue() { - final String token1 = "token1"; - final String token2 = "token2"; final String user = "user"; final String password = "password"; final Principal principal = new Principal(user, "", "", "", true); @@ -279,15 +221,10 @@ public class StackedAuthenticationServiceTest context.checking(new Expectations() { { - one(authService1).authenticateApplication(); - will(returnValue(token1)); - one(authService2).authenticateApplication(); - will(returnValue(token2)); - one(authService1).tryGetAndAuthenticateUser(token1, user, password); + one(authService1).tryGetAndAuthenticateUser(DUMMY_TOKEN_STR, user, password); will(returnValue(principal)); } }); - assertNotNull(stackedAuthService.authenticateApplication()); assertTrue(stackedAuthService.authenticateUser("doesntmatter", user, password)); context.assertIsSatisfied(); } @@ -295,8 +232,6 @@ public class StackedAuthenticationServiceTest @Test public void testAuthenticateUserSecondServiceTrue() { - final String token1 = "token1"; - final String token2 = "token2"; final String user = "user"; final String password = "password"; final Principal principal = new Principal(user, "", "", "", true); @@ -304,16 +239,11 @@ public class StackedAuthenticationServiceTest context.checking(new Expectations() { { - one(authService1).authenticateApplication(); - will(returnValue(token1)); - one(authService2).authenticateApplication(); - will(returnValue(token2)); - one(authService1).tryGetAndAuthenticateUser(token1, user, password); - one(authService2).tryGetAndAuthenticateUser(token2, user, password); + one(authService1).tryGetAndAuthenticateUser(DUMMY_TOKEN_STR, user, password); + one(authService2).tryGetAndAuthenticateUser(DUMMY_TOKEN_STR, user, password); will(returnValue(principal)); } }); - assertNotNull(stackedAuthService.authenticateApplication()); assertTrue(stackedAuthService.authenticateUser("doesntmatter", user, password)); context.assertIsSatisfied(); } @@ -321,8 +251,6 @@ public class StackedAuthenticationServiceTest @Test public void testGetPrincipalFirstService() { - final String token1 = "token1"; - final String token2 = "token2"; final String user = "user"; final String firstName = "first name"; final String lastName = "last name"; @@ -332,15 +260,10 @@ public class StackedAuthenticationServiceTest context.checking(new Expectations() { { - one(authService1).authenticateApplication(); - will(returnValue(token1)); - one(authService2).authenticateApplication(); - will(returnValue(token2)); - one(authService1).tryGetAndAuthenticateUser(token1, user, null); + one(authService1).tryGetAndAuthenticateUser(DUMMY_TOKEN_STR, user, null); will(returnValue(principal)); } }); - assertNotNull(stackedAuthService.authenticateApplication()); assertEquals(principal, stackedAuthService.getPrincipal("doesntmatter", user)); context.assertIsSatisfied(); } @@ -348,8 +271,6 @@ public class StackedAuthenticationServiceTest @Test public void testGetPrincipalSecondService() { - final String token1 = "token1"; - final String token2 = "token2"; final String user = "user"; final String firstName = "first name"; final String lastName = "last name"; @@ -359,47 +280,27 @@ public class StackedAuthenticationServiceTest context.checking(new Expectations() { { - one(authService1).authenticateApplication(); - will(returnValue(token1)); - one(authService2).authenticateApplication(); - will(returnValue(token2)); - one(authService1).tryGetAndAuthenticateUser(token1, user, null); - one(authService2).tryGetAndAuthenticateUser(token2, user, null); + one(authService1).tryGetAndAuthenticateUser(DUMMY_TOKEN_STR, user, null); + one(authService2).tryGetAndAuthenticateUser(DUMMY_TOKEN_STR, user, null); will(returnValue(principal)); } }); - assertNotNull(stackedAuthService.authenticateApplication()); assertEquals(principal, stackedAuthService.getPrincipal("doesntmatter", user)); context.assertIsSatisfied(); } - @Test(expectedExceptions = IllegalArgumentException.class) - public void testGetPrincipalApplicationNotAuthenticated() - { - final String user = "user"; - - stackedAuthService.getPrincipal("doesntmatter", user); - } - @Test(expectedExceptions = IllegalArgumentException.class) public void testGetPrincipalNoService() { - final String token1 = "token1"; - final String token2 = "token2"; final String user = "user"; context.checking(new Expectations() { { - one(authService1).authenticateApplication(); - will(returnValue(token1)); - one(authService2).authenticateApplication(); - will(returnValue(token2)); - one(authService1).tryGetAndAuthenticateUser(token1, user, null); - one(authService2).tryGetAndAuthenticateUser(token2, user, null); + one(authService1).tryGetAndAuthenticateUser(DUMMY_TOKEN_STR, user, null); + one(authService2).tryGetAndAuthenticateUser(DUMMY_TOKEN_STR, user, null); } }); - assertNotNull(stackedAuthService.authenticateApplication()); stackedAuthService.getPrincipal("doesntmatter", user); } @@ -409,8 +310,6 @@ public class StackedAuthenticationServiceTest context = new Mockery(); authService1 = context.mock(IAuthenticationService.class, "auth service 1"); authService2 = context.mock(IAuthenticationService.class, "auth service 2"); - final String token1 = "token1"; - final String token2 = "token2"; final String emailQuery = "some email with *"; final Principal principal1 = new Principal("user1", "first name 1", "last name 1", "email 1", false); @@ -429,18 +328,13 @@ public class StackedAuthenticationServiceTest one(authService2).supportsListingByUserId(); exactly(2).of(authService2).supportsListingByEmail(); one(authService2).supportsListingByLastName(); - one(authService1).authenticateApplication(); - will(returnValue(token1)); - one(authService2).authenticateApplication(); - will(returnValue(token2)); - one(authService1).listPrincipalsByEmail(token1, emailQuery); + one(authService1).listPrincipalsByEmail(DUMMY_TOKEN_STR, emailQuery); will(returnValue(Arrays.asList(principal1, principal2))); } }); stackedAuthService = new StackedAuthenticationService(Arrays.asList(authService1, authService2)); - assertNotNull(stackedAuthService.authenticateApplication()); final List<Principal> result = stackedAuthService.listPrincipalsByEmail("doesntmatter", emailQuery); assertEquals(2, result.size()); @@ -455,8 +349,6 @@ public class StackedAuthenticationServiceTest context = new Mockery(); authService1 = context.mock(IAuthenticationService.class, "auth service 1"); authService2 = context.mock(IAuthenticationService.class, "auth service 2"); - final String token1 = "token1"; - final String token2 = "token2"; final String emailQuery = "some email with *"; final Principal principal = new Principal("user3", "first name 3", "last name 3", "email 3", false); @@ -473,18 +365,13 @@ public class StackedAuthenticationServiceTest exactly(2).of(authService2).supportsListingByEmail(); will(returnValue(true)); one(authService2).supportsListingByLastName(); - one(authService1).authenticateApplication(); - will(returnValue(token1)); - one(authService2).authenticateApplication(); - will(returnValue(token2)); - one(authService2).listPrincipalsByEmail(token2, emailQuery); + one(authService2).listPrincipalsByEmail(DUMMY_TOKEN_STR, emailQuery); will(returnValue(Arrays.asList(principal))); } }); stackedAuthService = new StackedAuthenticationService(Arrays.asList(authService1, authService2)); - assertNotNull(stackedAuthService.authenticateApplication()); final List<Principal> result = stackedAuthService.listPrincipalsByEmail("doesntmatter", emailQuery); assertEquals(1, result.size()); @@ -498,8 +385,6 @@ public class StackedAuthenticationServiceTest context = new Mockery(); authService1 = context.mock(IAuthenticationService.class, "auth service 1"); authService2 = context.mock(IAuthenticationService.class, "auth service 2"); - final String token1 = "token1"; - final String token2 = "token2"; final String emailQuery = "some email with *"; final Principal principal1 = new Principal("user1", "first name 1", "last name 1", "email 1", false); @@ -521,20 +406,15 @@ public class StackedAuthenticationServiceTest exactly(2).of(authService2).supportsListingByEmail(); will(returnValue(true)); one(authService2).supportsListingByLastName(); - one(authService1).authenticateApplication(); - will(returnValue(token1)); - one(authService2).authenticateApplication(); - will(returnValue(token2)); - one(authService1).listPrincipalsByEmail(token1, emailQuery); + one(authService1).listPrincipalsByEmail(DUMMY_TOKEN_STR, emailQuery); will(returnValue(Arrays.asList(principal1, principal2))); - one(authService2).listPrincipalsByEmail(token2, emailQuery); + one(authService2).listPrincipalsByEmail(DUMMY_TOKEN_STR, emailQuery); will(returnValue(Arrays.asList(principal3))); } }); stackedAuthService = new StackedAuthenticationService(Arrays.asList(authService1, authService2)); - assertNotNull(stackedAuthService.authenticateApplication()); final List<Principal> result = stackedAuthService.listPrincipalsByEmail("doesntmatter", emailQuery); assertEquals(3, result.size()); @@ -550,8 +430,6 @@ public class StackedAuthenticationServiceTest context = new Mockery(); authService1 = context.mock(IAuthenticationService.class, "auth service 1"); authService2 = context.mock(IAuthenticationService.class, "auth service 2"); - final String token1 = "token1"; - final String token2 = "token2"; final String userIdQuery = "some user id with *"; final Principal principal1 = new Principal("user1", "first name 1", "last name 1", "email 1", false); @@ -570,18 +448,13 @@ public class StackedAuthenticationServiceTest exactly(2).of(authService2).supportsListingByUserId(); one(authService2).supportsListingByEmail(); one(authService2).supportsListingByLastName(); - one(authService1).authenticateApplication(); - will(returnValue(token1)); - one(authService2).authenticateApplication(); - will(returnValue(token2)); - one(authService1).listPrincipalsByUserId(token1, userIdQuery); + one(authService1).listPrincipalsByUserId(DUMMY_TOKEN_STR, userIdQuery); will(returnValue(Arrays.asList(principal1, principal2))); } }); stackedAuthService = new StackedAuthenticationService(Arrays.asList(authService1, authService2)); - assertNotNull(stackedAuthService.authenticateApplication()); final List<Principal> result = stackedAuthService.listPrincipalsByUserId("doesntmatter", userIdQuery); assertEquals(2, result.size()); @@ -596,8 +469,6 @@ public class StackedAuthenticationServiceTest context = new Mockery(); authService1 = context.mock(IAuthenticationService.class, "auth service 1"); authService2 = context.mock(IAuthenticationService.class, "auth service 2"); - final String token1 = "token1"; - final String token2 = "token2"; final String userIdQuery = "some user id with *"; final Principal principal = new Principal("user3", "first name 3", "last name 3", "email 3", false); @@ -614,18 +485,13 @@ public class StackedAuthenticationServiceTest will(returnValue(true)); one(authService2).supportsListingByEmail(); one(authService2).supportsListingByLastName(); - one(authService1).authenticateApplication(); - will(returnValue(token1)); - one(authService2).authenticateApplication(); - will(returnValue(token2)); - one(authService2).listPrincipalsByUserId(token2, userIdQuery); + one(authService2).listPrincipalsByUserId(DUMMY_TOKEN_STR, userIdQuery); will(returnValue(Arrays.asList(principal))); } }); stackedAuthService = new StackedAuthenticationService(Arrays.asList(authService1, authService2)); - assertNotNull(stackedAuthService.authenticateApplication()); final List<Principal> result = stackedAuthService.listPrincipalsByUserId("doesntmatter", userIdQuery); assertEquals(1, result.size()); @@ -639,8 +505,6 @@ public class StackedAuthenticationServiceTest context = new Mockery(); authService1 = context.mock(IAuthenticationService.class, "auth service 1"); authService2 = context.mock(IAuthenticationService.class, "auth service 2"); - final String token1 = "token1"; - final String token2 = "token2"; final String userIdQuery = "some user id with *"; final Principal principal1 = new Principal("user1", "first name 1", "last name 1", "email 1", false); @@ -662,20 +526,15 @@ public class StackedAuthenticationServiceTest will(returnValue(true)); one(authService2).supportsListingByEmail(); one(authService2).supportsListingByLastName(); - one(authService1).authenticateApplication(); - will(returnValue(token1)); - one(authService2).authenticateApplication(); - will(returnValue(token2)); - one(authService1).listPrincipalsByUserId(token1, userIdQuery); + one(authService1).listPrincipalsByUserId(DUMMY_TOKEN_STR, userIdQuery); will(returnValue(Arrays.asList(principal1, principal2))); - one(authService2).listPrincipalsByUserId(token2, userIdQuery); + one(authService2).listPrincipalsByUserId(DUMMY_TOKEN_STR, userIdQuery); will(returnValue(Arrays.asList(principal3))); } }); stackedAuthService = new StackedAuthenticationService(Arrays.asList(authService1, authService2)); - assertNotNull(stackedAuthService.authenticateApplication()); final List<Principal> result = stackedAuthService.listPrincipalsByUserId("doesntmatter", userIdQuery); assertEquals(3, result.size()); @@ -691,8 +550,6 @@ public class StackedAuthenticationServiceTest context = new Mockery(); authService1 = context.mock(IAuthenticationService.class, "auth service 1"); authService2 = context.mock(IAuthenticationService.class, "auth service 2"); - final String token1 = "token1"; - final String token2 = "token2"; final String lastNameQuery = "some user id with *"; final Principal principal1 = new Principal("user1", "first name 1", "last name 1", "email 1", false); @@ -711,18 +568,13 @@ public class StackedAuthenticationServiceTest one(authService2).supportsListingByUserId(); one(authService2).supportsListingByEmail(); exactly(2).of(authService2).supportsListingByLastName(); - one(authService1).authenticateApplication(); - will(returnValue(token1)); - one(authService2).authenticateApplication(); - will(returnValue(token2)); - one(authService1).listPrincipalsByLastName(token1, lastNameQuery); + one(authService1).listPrincipalsByLastName(DUMMY_TOKEN_STR, lastNameQuery); will(returnValue(Arrays.asList(principal1, principal2))); } }); stackedAuthService = new StackedAuthenticationService(Arrays.asList(authService1, authService2)); - assertNotNull(stackedAuthService.authenticateApplication()); final List<Principal> result = stackedAuthService.listPrincipalsByLastName("doesntmatter", lastNameQuery); assertEquals(2, result.size()); @@ -737,8 +589,6 @@ public class StackedAuthenticationServiceTest context = new Mockery(); authService1 = context.mock(IAuthenticationService.class, "auth service 1"); authService2 = context.mock(IAuthenticationService.class, "auth service 2"); - final String token1 = "token1"; - final String token2 = "token2"; final String lastNameQuery = "some user id with *"; final Principal principal = new Principal("user3", "first name 3", "last name 3", "email 3", false); @@ -755,18 +605,13 @@ public class StackedAuthenticationServiceTest one(authService2).supportsListingByEmail(); exactly(2).of(authService2).supportsListingByLastName(); will(returnValue(true)); - one(authService1).authenticateApplication(); - will(returnValue(token1)); - one(authService2).authenticateApplication(); - will(returnValue(token2)); - one(authService2).listPrincipalsByLastName(token2, lastNameQuery); + one(authService2).listPrincipalsByLastName(DUMMY_TOKEN_STR, lastNameQuery); will(returnValue(Arrays.asList(principal))); } }); stackedAuthService = new StackedAuthenticationService(Arrays.asList(authService1, authService2)); - assertNotNull(stackedAuthService.authenticateApplication()); final List<Principal> result = stackedAuthService.listPrincipalsByLastName("doesntmatter", lastNameQuery); assertEquals(1, result.size()); @@ -780,8 +625,6 @@ public class StackedAuthenticationServiceTest context = new Mockery(); authService1 = context.mock(IAuthenticationService.class, "auth service 1"); authService2 = context.mock(IAuthenticationService.class, "auth service 2"); - final String token1 = "token1"; - final String token2 = "token2"; final String lastNameQuery = "some user id with *"; final Principal principal1 = new Principal("user1", "first name 1", "last name 1", "email 1", false); @@ -803,20 +646,15 @@ public class StackedAuthenticationServiceTest one(authService2).supportsListingByEmail(); exactly(2).of(authService2).supportsListingByLastName(); will(returnValue(true)); - one(authService1).authenticateApplication(); - will(returnValue(token1)); - one(authService2).authenticateApplication(); - will(returnValue(token2)); - one(authService1).listPrincipalsByLastName(token1, lastNameQuery); + one(authService1).listPrincipalsByLastName(DUMMY_TOKEN_STR, lastNameQuery); will(returnValue(Arrays.asList(principal1, principal2))); - one(authService2).listPrincipalsByLastName(token2, lastNameQuery); + one(authService2).listPrincipalsByLastName(DUMMY_TOKEN_STR, lastNameQuery); will(returnValue(Arrays.asList(principal3))); } }); stackedAuthService = new StackedAuthenticationService(Arrays.asList(authService1, authService2)); - assertNotNull(stackedAuthService.authenticateApplication()); final List<Principal> result = stackedAuthService.listPrincipalsByLastName("doesntmatter", lastNameQuery); assertEquals(3, result.size());