You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by an...@apache.org on 2013/10/30 10:59:29 UTC
svn commit: r1537027 [2/2] - in /jackrabbit/trunk/jackrabbit-core/src:
main/java/org/apache/jackrabbit/core/
main/java/org/apache/jackrabbit/core/security/authentication/
main/java/org/apache/jackrabbit/core/security/authentication/token/
main/resource...
Modified: jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/security/authentication/token/TokenBasedAuthenticationTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/security/authentication/token/TokenBasedAuthenticationTest.java?rev=1537027&r1=1537026&r2=1537027&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/security/authentication/token/TokenBasedAuthenticationTest.java (original)
+++ jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/security/authentication/token/TokenBasedAuthenticationTest.java Wed Oct 30 09:59:28 2013
@@ -16,9 +16,7 @@
*/
package org.apache.jackrabbit.core.security.authentication.token;
-import org.apache.jackrabbit.api.security.authentication.token.TokenCredentials;
-import org.apache.jackrabbit.test.AbstractJCRTest;
-
+import java.util.UUID;
import javax.jcr.Credentials;
import javax.jcr.ItemNotFoundException;
import javax.jcr.Node;
@@ -27,45 +25,82 @@ import javax.jcr.SimpleCredentials;
import javax.jcr.lock.LockException;
import javax.jcr.nodetype.ConstraintViolationException;
import javax.jcr.version.VersionException;
-import java.util.Calendar;
-import java.util.Date;
+
+import org.apache.jackrabbit.api.security.authentication.token.TokenCredentials;
+import org.apache.jackrabbit.api.security.user.User;
+import org.apache.jackrabbit.core.SessionImpl;
+import org.apache.jackrabbit.test.AbstractJCRTest;
/**
- * <code>TokenBasedAuthenticationTest</code>...
+ * <code>TokenBasedAuthenticationOakTest</code>...
*/
public class TokenBasedAuthenticationTest extends AbstractJCRTest {
- Node tokenNode;
+ private SessionImpl adminSession;
+ private User testUser;
+
+ private String token;
+ private Node tokenNode;
+ private TokenCredentials tokenCreds;
+
+ private String expiredToken;
+ private Node expiredNode;
+ private TokenCredentials expiredCreds;
- TokenBasedAuthentication nullTokenAuth;
- TokenBasedAuthentication validTokenAuth;
- TokenCredentials tokenCreds;
- Credentials simpleCreds = new SimpleCredentials("uid", "pw".toCharArray());
- Credentials creds = new Credentials() {};
+ private TokenBasedAuthentication nullTokenAuth;
+ private TokenBasedAuthentication validTokenAuth;
+
+ private Credentials simpleCreds = new SimpleCredentials("uid", "pw".toCharArray());
+ private Credentials creds = new Credentials() {};
@Override
protected void setUp() throws Exception {
super.setUp();
- tokenNode = testRootNode.addNode(nodeName1, "nt:unstructured");
- tokenNode.setProperty(TokenBasedAuthentication.TOKEN_ATTRIBUTE +".exp", new Date().getTime()+TokenBasedAuthentication.TOKEN_EXPIRATION);
- superuser.save();
+ adminSession = (SessionImpl) getHelper().getSuperuserSession("security");
+ testUser = adminSession.getUserManager().createUser(UUID.randomUUID().toString(), "pw");
+ adminSession.save();
+
+ SimpleCredentials sc = new SimpleCredentials(testUser.getID(), "pw".toCharArray());
+ sc.setAttribute(TokenBasedAuthentication.TOKEN_ATTRIBUTE, "");
+ sc.setAttribute(TokenBasedAuthentication.TOKEN_ATTRIBUTE+".any", "correct");
+ sc.setAttribute("informative", "value");
- String token = tokenNode.getIdentifier();
+ TokenProvider tp = new TokenProvider(adminSession, TokenBasedAuthentication.TOKEN_EXPIRATION);
+ TokenInfo ti = tp.createToken(testUser, sc);
+ tokenCreds = ti.getCredentials();
+ token = tokenCreds.getToken();
+ tokenNode = TokenProvider.getTokenNode(token, adminSession);
+
+ tp = new TokenProvider(adminSession, 1);
+ TokenInfo expired = tp.createToken(testUser, sc);
+ expiredCreds = expired.getCredentials();
+ expiredToken = expiredCreds.getToken();
+ expiredNode = TokenProvider.getTokenNode(expiredToken, adminSession);
- nullTokenAuth = new TokenBasedAuthentication(null, -1, superuser);
- validTokenAuth = new TokenBasedAuthentication(token, 7200, superuser);
+ nullTokenAuth = new TokenBasedAuthentication(null, -1, adminSession);
+ validTokenAuth = new TokenBasedAuthentication(token, 7200, adminSession);
- tokenCreds = new TokenCredentials(token);
}
- private TokenBasedAuthentication expiredToken() throws RepositoryException, LockException, ConstraintViolationException, VersionException {
- Calendar cal = Calendar.getInstance();
- cal.setTimeInMillis(new Date().getTime()-100);
- tokenNode.setProperty(".token.exp", cal);
- superuser.save();
- return new TokenBasedAuthentication(tokenNode.getIdentifier(), TokenBasedAuthentication.TOKEN_EXPIRATION, superuser);
+ @Override
+ protected void tearDown() throws Exception {
+ try {
+ testUser.remove();
+ adminSession.save();
+ adminSession.logout();
+ } finally {
+ super.tearDown();
+ }
+ }
+
+ private TokenBasedAuthentication createAuthenticationForExpiredToken() throws RepositoryException, LockException, ConstraintViolationException, VersionException {
+ return new TokenBasedAuthentication(expiredToken, TokenBasedAuthentication.TOKEN_EXPIRATION, adminSession);
+ }
+
+ private TokenBasedAuthentication createAuthentication() throws RepositoryException {
+ return new TokenBasedAuthentication(token, TokenBasedAuthentication.TOKEN_EXPIRATION, adminSession);
}
public void testCanHandle() throws RepositoryException {
@@ -77,23 +112,23 @@ public class TokenBasedAuthenticationTes
assertFalse(validTokenAuth.canHandle(creds));
assertFalse(nullTokenAuth.canHandle(creds));
+ }
- TokenBasedAuthentication expiredToken = expiredToken();
- assertTrue(expiredToken.canHandle(tokenCreds));
+ public void testCanHandleExpiredToken() throws RepositoryException {
+ TokenBasedAuthentication expiredToken = createAuthenticationForExpiredToken();
+ assertTrue(expiredToken.canHandle(expiredCreds));
}
public void testExpiry() throws RepositoryException {
- assertTrue(validTokenAuth.authenticate(tokenCreds));
-
- TokenBasedAuthentication expiredToken = expiredToken();
- assertFalse(expiredToken.authenticate(tokenCreds));
+ TokenBasedAuthentication expiredToken = createAuthenticationForExpiredToken();
+ assertFalse(expiredToken.authenticate(expiredCreds));
}
public void testRemoval() throws RepositoryException {
- String identifier = tokenNode.getIdentifier();
+ String identifier = expiredNode.getIdentifier();
- TokenBasedAuthentication expiredToken = expiredToken();
- assertFalse(expiredToken.authenticate(tokenCreds));
+ TokenBasedAuthentication expiredToken = createAuthenticationForExpiredToken();
+ assertFalse(expiredToken.authenticate(expiredCreds));
try {
superuser.getNodeByIdentifier(identifier);
@@ -120,57 +155,48 @@ public class TokenBasedAuthenticationTes
}
public void testAttributes() throws RepositoryException {
- tokenNode.setProperty(TokenBasedAuthentication.TOKEN_ATTRIBUTE +".any", "correct");
- superuser.save();
- TokenBasedAuthentication auth = new TokenBasedAuthentication(tokenNode.getIdentifier(), TokenBasedAuthentication.TOKEN_EXPIRATION, superuser);
-
- assertFalse(auth.authenticate(tokenCreds));
-
- tokenCreds.setAttribute(TokenBasedAuthentication.TOKEN_ATTRIBUTE +".any", "wrong");
- assertFalse(auth.authenticate(tokenCreds));
+ TokenBasedAuthentication auth = createAuthentication();
+ assertFalse(auth.authenticate(new TokenCredentials(token)));
- tokenCreds.setAttribute(TokenBasedAuthentication.TOKEN_ATTRIBUTE +".any", "correct");
- assertTrue(auth.authenticate(tokenCreds));
-
- // add informative property
- tokenNode.setProperty("noMatchRequired", "abc");
- superuser.save();
- auth = new TokenBasedAuthentication(tokenNode.getIdentifier(), TokenBasedAuthentication.TOKEN_EXPIRATION, superuser);
+ TokenCredentials tc = new TokenCredentials(token);
+ tc.setAttribute(TokenBasedAuthentication.TOKEN_ATTRIBUTE +".any", "wrong");
+ assertFalse(auth.authenticate(tc));
+ tc = new TokenCredentials(token);
+ tc.setAttribute(TokenBasedAuthentication.TOKEN_ATTRIBUTE +".any", "correct");
assertTrue(auth.authenticate(tokenCreds));
}
public void testUpdateAttributes() throws RepositoryException {
- tokenNode.setProperty(TokenBasedAuthentication.TOKEN_ATTRIBUTE +".any", "correct");
- tokenNode.setProperty("informative","value");
- superuser.save();
-
// token credentials must be updated to contain the additional attribute
// present on the token node.
- TokenBasedAuthentication auth = new TokenBasedAuthentication(tokenNode.getIdentifier(), TokenBasedAuthentication.TOKEN_EXPIRATION, superuser);
- tokenCreds.setAttribute(TokenBasedAuthentication.TOKEN_ATTRIBUTE +".any", "correct");
- assertTrue(auth.authenticate(tokenCreds));
- assertEquals("value", tokenCreds.getAttribute("informative"));
+ TokenBasedAuthentication auth = createAuthentication();
+
+ TokenCredentials tc = new TokenCredentials(token);
+ tc.setAttribute(TokenBasedAuthentication.TOKEN_ATTRIBUTE +".any", "correct");
+
+ assertTrue(auth.authenticate(tc));
+ assertEquals("value", tc.getAttribute("informative"));
// additional informative property present on credentials upon subsequent
// authentication -> the node must not be updated
- auth = new TokenBasedAuthentication(tokenNode.getIdentifier(), TokenBasedAuthentication.TOKEN_EXPIRATION, superuser);
- tokenCreds.setAttribute("informative2", "value2");
- assertTrue(auth.authenticate(tokenCreds));
+ auth = createAuthentication();
+ tc.setAttribute("informative2", "value2");
+ assertTrue(auth.authenticate(tc));
assertFalse(tokenNode.hasProperty("informative2"));
// modified informative property present on credentials upon subsequent
// authentication -> the node must not be updated
- auth = new TokenBasedAuthentication(tokenNode.getIdentifier(), TokenBasedAuthentication.TOKEN_EXPIRATION, superuser);
- tokenCreds.setAttribute("informative", "otherValue");
- assertTrue(auth.authenticate(tokenCreds));
+ auth = createAuthentication();
+ tc.setAttribute("informative", "otherValue");
+ assertTrue(auth.authenticate(tc));
assertTrue(tokenNode.hasProperty("informative"));
assertEquals("value", tokenNode.getProperty("informative").getString());
// additional mandatory property on the credentials upon subsequent
// authentication -> must be ignored
- auth = new TokenBasedAuthentication(tokenNode.getIdentifier(), TokenBasedAuthentication.TOKEN_EXPIRATION, superuser);
- tokenCreds.setAttribute(TokenBasedAuthentication.TOKEN_ATTRIBUTE +".toIgnore", "ignore");
+ auth = createAuthentication();
+ tc.setAttribute(TokenBasedAuthentication.TOKEN_ATTRIBUTE +".toIgnore", "ignore");
assertTrue(auth.authenticate(tokenCreds));
assertFalse(tokenNode.hasProperty(TokenBasedAuthentication.TOKEN_ATTRIBUTE +".toIgnore"));
}