Author: alain_defrance
Date: 2010-10-06 06:50:51 -0400 (Wed, 06 Oct 2010)
New Revision: 4534
Added:
components/wci/branches/adf/wci/src/main/java/org/gatein/wci/security/Credentials.java
Removed:
components/wci/branches/adf/wci/src/main/java/org/gatein/wci/authentication/WCICredentials.java
Modified:
components/wci/branches/adf/test/core/src/main/java/org/gatein/wci/authentication/AuthenticationTestCase.java
components/wci/branches/adf/test/core/src/main/java/org/gatein/wci/spi/SPIAuthenticationTestCase.java
components/wci/branches/adf/wci/src/main/java/org/gatein/wci/authentication/GenericAuthentication.java
components/wci/branches/adf/wci/src/main/java/org/gatein/wci/authentication/GenericAuthenticationResult.java
components/wci/branches/adf/wci/src/main/java/org/gatein/wci/authentication/Ticket.java
components/wci/branches/adf/wci/src/main/java/org/gatein/wci/authentication/TicketService.java
components/wci/branches/adf/wci/src/main/java/org/gatein/wci/impl/DefaultServletContainer.java
components/wci/branches/adf/wci/src/main/java/org/gatein/wci/security/WCILoginController.java
components/wci/branches/adf/wci/src/main/java/org/gatein/wci/security/WCILoginModule.java
Log:
Tests added
Modified:
components/wci/branches/adf/test/core/src/main/java/org/gatein/wci/authentication/AuthenticationTestCase.java
===================================================================
---
components/wci/branches/adf/test/core/src/main/java/org/gatein/wci/authentication/AuthenticationTestCase.java 2010-10-06
10:38:34 UTC (rev 4533)
+++
components/wci/branches/adf/test/core/src/main/java/org/gatein/wci/authentication/AuthenticationTestCase.java 2010-10-06
10:50:51 UTC (rev 4534)
@@ -19,6 +19,7 @@
package org.gatein.wci.authentication;
+import org.gatein.wci.security.Credentials;
import org.jboss.unit.api.pojo.annotations.Test;
import static org.jboss.unit.api.Assert.*;
@@ -33,9 +34,9 @@
@Test
void testTicket() {
TicketService tService = GenericAuthentication.TICKET_SERVICE;
- WCICredentials credentials = new WCICredentials("foo", "bar");
+ Credentials credentials = new Credentials("foo", "bar");
String strTicket = tService.createTicket(credentials);
- WCICredentials credentialsFromTs = tService.validateToken(strTicket, false);
+ Credentials credentialsFromTs = tService.validateToken(strTicket, false);
assertEquals(credentials.getUsername(), credentialsFromTs.getUsername());
assertEquals(credentials.getPassword(), credentialsFromTs.getPassword());
assertNotNull(tService.validateToken(strTicket, true));
Modified:
components/wci/branches/adf/test/core/src/main/java/org/gatein/wci/spi/SPIAuthenticationTestCase.java
===================================================================
---
components/wci/branches/adf/test/core/src/main/java/org/gatein/wci/spi/SPIAuthenticationTestCase.java 2010-10-06
10:38:34 UTC (rev 4533)
+++
components/wci/branches/adf/test/core/src/main/java/org/gatein/wci/spi/SPIAuthenticationTestCase.java 2010-10-06
10:50:51 UTC (rev 4534)
@@ -30,7 +30,7 @@
import org.gatein.wci.authentication.GenericAuthentication;
import org.gatein.wci.authentication.GenericAuthenticationResult;
import org.gatein.wci.authentication.ProgrammaticAuthenticationResult;
-import org.gatein.wci.authentication.WCICredentials;
+import org.gatein.wci.security.Credentials;
import org.gatein.wci.impl.DefaultServletContainerFactory;
import org.jboss.unit.Failure;
import org.jboss.unit.driver.DriverCommand;
@@ -56,24 +56,30 @@
/** . */
private ServletContainer container;
+ /** . */
+ private final Value v = new Value();
+
+ /** . */
+ private AuthenticationResult result;
+
@Override
public DriverResponse service(TestServlet testServlet, WebRequest req, WebResponse
resp) throws ServletException, IOException
{
if (getRequestCount() == 0)
{
assertNull(req.getUserPrincipal());
- final Value v = new Value();
container = DefaultServletContainerFactory.getInstance().getServletContainer();
container.addAuthenticationListener(new TestListener(v));
assertEquals("", v.value);
- AuthenticationResult result = container.login(req, resp, username, password);
+ result = container.login(req, resp, username, password);
assertNotNull(result);
if (result instanceof GenericAuthenticationResult)
{
+ GenericAuthenticationResult gAuthentication = (GenericAuthenticationResult)
result;
// Test Ticket Service
- WCICredentials srcCredentials = new WCICredentials(username, password);
+ Credentials srcCredentials = new Credentials(username, password);
String ticket =
GenericAuthentication.TICKET_SERVICE.createTicket(srcCredentials);
- WCICredentials resultCredentials =
GenericAuthentication.TICKET_SERVICE.validateToken(ticket, false);
+ Credentials resultCredentials =
GenericAuthentication.TICKET_SERVICE.validateToken(ticket, false);
assertEquals(srcCredentials.getUsername(), resultCredentials.getUsername());
assertEquals(srcCredentials.getPassword(), resultCredentials.getPassword());
assertNotNull(GenericAuthentication.TICKET_SERVICE.validateToken(ticket,
true));
@@ -82,28 +88,53 @@
// Test Generic login
GenericAuthenticationResult gResult = (GenericAuthenticationResult) result;
String t = gResult.getTicket();
- WCICredentials credentials =
GenericAuthentication.TICKET_SERVICE.validateToken(t, true);
+ Credentials credentials =
GenericAuthentication.TICKET_SERVICE.validateToken(t, true);
assertNotNull(credentials);
+ assertEquals("", v.value);
+ gAuthentication.perform(req, resp);
// Test login Event
assertEquals("login", v.value);
+ assertTrue(resp.isCommitted());
+
+ }
+ else if (result instanceof ProgrammaticAuthenticationResult)
+ {
+ assertEquals("login", v.value);
+ assertNotNull(req.getUserPrincipal());
+ assertTrue(req.isUserInRole("test"));
+ }
+ //
+ String url = resp.renderURL("/", null, null);
+ return new InvokeGetResponse(url);
+ }
+ else if (getRequestCount() == 1)
+ {
+ if (result instanceof GenericAuthenticationResult)
+ {
// Test logout
+ assertNotNull(req.getSession(false));
+ assertEquals("login", v.value);
container.logout(req, resp);
assertNull(req.getSession(false));
// Test logout Event
assertEquals("logout", v.value);
-
}
else if (result instanceof ProgrammaticAuthenticationResult)
{
- assertNotNull(req.getUserPrincipal());
- assertTrue(req.isUserInRole("test"));
+ assertEquals("login", v.value);
+
+ container.logout(req, resp);
+
+ assertEquals("logout", v.value);
+ assertNull(req.getUserPrincipal());
}
+ return new EndTestResponse();
}
- return new EndTestResponse();
-
+
+ return new FailureResponse(Failure.createAssertionFailure(""));
}
@Override
Modified:
components/wci/branches/adf/wci/src/main/java/org/gatein/wci/authentication/GenericAuthentication.java
===================================================================
---
components/wci/branches/adf/wci/src/main/java/org/gatein/wci/authentication/GenericAuthentication.java 2010-10-06
10:38:34 UTC (rev 4533)
+++
components/wci/branches/adf/wci/src/main/java/org/gatein/wci/authentication/GenericAuthentication.java 2010-10-06
10:50:51 UTC (rev 4534)
@@ -19,6 +19,8 @@
package org.gatein.wci.authentication;
+import org.gatein.wci.security.Credentials;
+
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@@ -35,7 +37,7 @@
public AuthenticationResult login(String login, String password, HttpServletRequest
request, HttpServletResponse response)
{
- String ticket = TICKET_SERVICE.createTicket(new WCICredentials(login, password));
+ String ticket = TICKET_SERVICE.createTicket(new Credentials(login, password));
return new GenericAuthenticationResult(login, ticket);
}
Modified:
components/wci/branches/adf/wci/src/main/java/org/gatein/wci/authentication/GenericAuthenticationResult.java
===================================================================
---
components/wci/branches/adf/wci/src/main/java/org/gatein/wci/authentication/GenericAuthenticationResult.java 2010-10-06
10:38:34 UTC (rev 4533)
+++
components/wci/branches/adf/wci/src/main/java/org/gatein/wci/authentication/GenericAuthenticationResult.java 2010-10-06
10:50:51 UTC (rev 4534)
@@ -19,9 +19,14 @@
package org.gatein.wci.authentication;
+import org.gatein.wci.impl.DefaultServletContainer;
+import org.gatein.wci.impl.DefaultServletContainerFactory;
+import org.gatein.wci.security.Credentials;
+
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
+import java.lang.reflect.Method;
/**
* @author <a href="mailto:alain.defrance@exoplatform.com">Alain
Defrance</a>
@@ -42,9 +47,16 @@
public void perform(HttpServletRequest req, HttpServletResponse resp) throws
IOException
{
- req.getSession().removeAttribute(WCICredentials.CREDENTIALS);
+ req.getSession().removeAttribute(Credentials.CREDENTIALS);
String url = "j_security_check?j_username=" + username +
"&j_password=" + ticket;
url = resp.encodeRedirectURL(url);
resp.sendRedirect(url);
+ resp.flushBuffer();
+
+ Object o = DefaultServletContainerFactory.getInstance().getServletContainer();
+ if (o instanceof DefaultServletContainer)
+ {
+ ((DefaultServletContainer)o).fireEvent(DefaultServletContainer.EventType.LOGIN,
new AuthenticationEvent(req, resp, username, ticket));
+ }
}
}
Modified:
components/wci/branches/adf/wci/src/main/java/org/gatein/wci/authentication/Ticket.java
===================================================================
---
components/wci/branches/adf/wci/src/main/java/org/gatein/wci/authentication/Ticket.java 2010-10-06
10:38:34 UTC (rev 4533)
+++
components/wci/branches/adf/wci/src/main/java/org/gatein/wci/authentication/Ticket.java 2010-10-06
10:50:51 UTC (rev 4534)
@@ -19,6 +19,8 @@
package org.gatein.wci.authentication;
+import org.gatein.wci.security.Credentials;
+
/**
* @author <a href="mailto:alain.defrance@exoplatform.com">Alain
Defrance</a>
* @version $Revision$
@@ -29,9 +31,9 @@
private final long expirationTimeMillis;
/** . */
- private final WCICredentials payload;
+ private final Credentials payload;
- public Ticket(long expirationTimeMillis, WCICredentials payload)
+ public Ticket(long expirationTimeMillis, Credentials payload)
{
this.expirationTimeMillis = expirationTimeMillis;
this.payload = payload;
@@ -42,7 +44,7 @@
return expirationTimeMillis;
}
- public WCICredentials getPayload()
+ public Credentials getPayload()
{
return payload;
}
Modified:
components/wci/branches/adf/wci/src/main/java/org/gatein/wci/authentication/TicketService.java
===================================================================
---
components/wci/branches/adf/wci/src/main/java/org/gatein/wci/authentication/TicketService.java 2010-10-06
10:38:34 UTC (rev 4533)
+++
components/wci/branches/adf/wci/src/main/java/org/gatein/wci/authentication/TicketService.java 2010-10-06
10:50:51 UTC (rev 4534)
@@ -19,6 +19,8 @@
package org.gatein.wci.authentication;
+import org.gatein.wci.security.Credentials;
+
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;
@@ -35,7 +37,7 @@
protected final Random random = new Random();
- public String createTicket(WCICredentials credentials)
+ public String createTicket(Credentials credentials)
{
if (validityMillis < 0)
{
@@ -51,7 +53,7 @@
return tokenId;
}
- public WCICredentials validateToken(String stringKey, boolean remove)
+ public Credentials validateToken(String stringKey, boolean remove)
{
if (stringKey == null)
{
Deleted:
components/wci/branches/adf/wci/src/main/java/org/gatein/wci/authentication/WCICredentials.java
===================================================================
---
components/wci/branches/adf/wci/src/main/java/org/gatein/wci/authentication/WCICredentials.java 2010-10-06
10:38:34 UTC (rev 4533)
+++
components/wci/branches/adf/wci/src/main/java/org/gatein/wci/authentication/WCICredentials.java 2010-10-06
10:50:51 UTC (rev 4534)
@@ -1,79 +0,0 @@
-/*
-* Copyright (C) 2003-2009 eXo Platform SAS.
-*
-* This is free software; you can redistribute it and/or modify it
-* under the terms of the GNU Lesser General Public License as
-* published by the Free Software Foundation; either version 2.1 of
-* the License, or (at your option) any later version.
-*
-* This software is distributed in the hope that it will be useful,
-* but WITHOUT ANY WARRANTY; without even the implied warranty of
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-* Lesser General Public License for more details.
-*
-* You should have received a copy of the GNU Lesser General Public
-* License along with this software; if not, write to the Free
-* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
-* 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
-*/
-
-package org.gatein.wci.authentication;
-
-import java.io.Serializable;
-
-/**
- * @author <a href="mailto:alain.defrance@exoplatform.com">Alain
Defrance</a>
- * @version $Revision$
- */
-public class WCICredentials implements Serializable
-{
- /** . */
- private final String username;
-
- /** . */
- private final String password;
-
- /** . */
- public static final String CREDENTIALS = "credentials";
-
- /**
- * Construct a new instance.
- *
- * @param username the username value
- * @param password the password value
- * @throws NullPointerException if any argument is null
- */
- public WCICredentials(String username, String password) throws NullPointerException
- {
- if (username == null)
- {
- throw new IllegalArgumentException("Username is null");
- }
- if (password == null)
- {
- throw new IllegalArgumentException("Password is null");
- }
- this.username = username;
- this.password = password;
- }
-
- /**
- * Returns the username.
- *
- * @return the username
- */
- public String getUsername()
- {
- return username;
- }
-
- /**
- * Returns the password.
- *
- * @return the password
- */
- public String getPassword()
- {
- return password;
- }
-}
Modified:
components/wci/branches/adf/wci/src/main/java/org/gatein/wci/impl/DefaultServletContainer.java
===================================================================
---
components/wci/branches/adf/wci/src/main/java/org/gatein/wci/impl/DefaultServletContainer.java 2010-10-06
10:38:34 UTC (rev 4533)
+++
components/wci/branches/adf/wci/src/main/java/org/gatein/wci/impl/DefaultServletContainer.java 2010-10-06
10:50:51 UTC (rev 4534)
@@ -25,6 +25,8 @@
import org.gatein.wci.authentication.AuthenticationEvent;
import org.gatein.wci.authentication.AuthenticationListener;
import org.gatein.wci.authentication.AuthenticationResult;
+import org.gatein.wci.authentication.GenericAuthentication;
+import org.gatein.wci.authentication.GenericAuthenticationResult;
import org.gatein.wci.spi.ServletContainerContext;
import org.gatein.wci.spi.WebAppContext;
import org.gatein.wci.WebAppListener;
@@ -102,7 +104,10 @@
AuthenticationResult result = registration.context.login(request, response,
userName, password);
//
- fireEvent(EventType.LOGIN, new AuthenticationEvent(request, response, userName,
password));
+ if (!(result instanceof GenericAuthenticationResult))
+ {
+ fireEvent(EventType.LOGIN, new AuthenticationEvent(request, response, userName,
password));
+ }
return result;
}
@@ -261,7 +266,7 @@
return registration.context.include(targetServletContext, request, response,
callback, handback);
}
- public enum EventType {
+ public static enum EventType {
LOGIN, LOGOUT
}
Copied:
components/wci/branches/adf/wci/src/main/java/org/gatein/wci/security/Credentials.java
(from rev 4511,
components/wci/branches/adf/wci/src/main/java/org/gatein/wci/authentication/WCICredentials.java)
===================================================================
---
components/wci/branches/adf/wci/src/main/java/org/gatein/wci/security/Credentials.java
(rev 0)
+++
components/wci/branches/adf/wci/src/main/java/org/gatein/wci/security/Credentials.java 2010-10-06
10:50:51 UTC (rev 4534)
@@ -0,0 +1,79 @@
+/*
+* Copyright (C) 2003-2009 eXo Platform SAS.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+*/
+
+package org.gatein.wci.security;
+
+import java.io.Serializable;
+
+/**
+ * @author <a href="mailto:alain.defrance@exoplatform.com">Alain
Defrance</a>
+ * @version $Revision$
+ */
+public class Credentials implements Serializable
+{
+ /** . */
+ private final String username;
+
+ /** . */
+ private final String password;
+
+ /** . */
+ public static final String CREDENTIALS = "credentials";
+
+ /**
+ * Construct a new instance.
+ *
+ * @param username the username value
+ * @param password the password value
+ * @throws NullPointerException if any argument is null
+ */
+ public Credentials(String username, String password) throws NullPointerException
+ {
+ if (username == null)
+ {
+ throw new IllegalArgumentException("Username is null");
+ }
+ if (password == null)
+ {
+ throw new IllegalArgumentException("Password is null");
+ }
+ this.username = username;
+ this.password = password;
+ }
+
+ /**
+ * Returns the username.
+ *
+ * @return the username
+ */
+ public String getUsername()
+ {
+ return username;
+ }
+
+ /**
+ * Returns the password.
+ *
+ * @return the password
+ */
+ public String getPassword()
+ {
+ return password;
+ }
+}
Modified:
components/wci/branches/adf/wci/src/main/java/org/gatein/wci/security/WCILoginController.java
===================================================================
---
components/wci/branches/adf/wci/src/main/java/org/gatein/wci/security/WCILoginController.java 2010-10-06
10:38:34 UTC (rev 4533)
+++
components/wci/branches/adf/wci/src/main/java/org/gatein/wci/security/WCILoginController.java 2010-10-06
10:50:51 UTC (rev 4534)
@@ -21,7 +21,6 @@
import org.gatein.common.logging.Logger;
import org.gatein.common.logging.LoggerFactory;
-import org.gatein.wci.authentication.WCICredentials;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
@@ -59,8 +58,8 @@
//
log.debug("Found username and password and set credentials in http
session");
- WCICredentials credentials = new WCICredentials(username, password);
- req.getSession().setAttribute(WCICredentials.CREDENTIALS, credentials);
+ Credentials credentials = new Credentials(username, password);
+ req.getSession().setAttribute(Credentials.CREDENTIALS, credentials);
// Obtain initial URI
String uri = req.getParameter("initialURI");
Modified:
components/wci/branches/adf/wci/src/main/java/org/gatein/wci/security/WCILoginModule.java
===================================================================
---
components/wci/branches/adf/wci/src/main/java/org/gatein/wci/security/WCILoginModule.java 2010-10-06
10:38:34 UTC (rev 4533)
+++
components/wci/branches/adf/wci/src/main/java/org/gatein/wci/security/WCILoginModule.java 2010-10-06
10:50:51 UTC (rev 4534)
@@ -20,7 +20,6 @@
package org.gatein.wci.security;
import org.gatein.wci.authentication.GenericAuthentication;
-import org.gatein.wci.authentication.WCICredentials;
import javax.security.auth.Subject;
import javax.security.auth.callback.Callback;
@@ -63,7 +62,7 @@
callbackHandler.handle(callbacks);
String password = new String(((PasswordCallback)callbacks[1]).getPassword());
- WCICredentials credentials =
GenericAuthentication.TICKET_SERVICE.validateToken(password, true);
+ Credentials credentials =
GenericAuthentication.TICKET_SERVICE.validateToken(password, true);
sharedState.put("javax.security.auth.login.name",
credentials.getUsername());
sharedState.put("javax.security.auth.login.password",
credentials.getPassword());
}