Search in sources :

Example 1 with TokenInfo

use of org.apache.jackrabbit.oak.spi.security.authentication.token.TokenInfo in project jackrabbit-oak by apache.

the class TokenValidatorTest method testModifyExpirationDate.

@Test
public void testModifyExpirationDate() throws Exception {
    TokenInfo info = tokenProvider.createToken(userId, Collections.<String, Object>emptyMap());
    NodeUtil tokenTree = new NodeUtil(getTokenTree(info));
    tokenTree.setDate(TOKEN_ATTRIBUTE_EXPIRY, new Date().getTime());
    root.commit(CommitMarker.asCommitAttributes());
}
Also used : TokenInfo(org.apache.jackrabbit.oak.spi.security.authentication.token.TokenInfo) Date(java.util.Date) NodeUtil(org.apache.jackrabbit.oak.util.NodeUtil) Test(org.junit.Test)

Example 2 with TokenInfo

use of org.apache.jackrabbit.oak.spi.security.authentication.token.TokenInfo in project jackrabbit-oak by apache.

the class TokenValidatorTest method testChangeTokenParentPrimaryType.

@Test
public void testChangeTokenParentPrimaryType() throws Exception {
    TokenInfo info = tokenProvider.createToken(userId, Collections.<String, Object>emptyMap());
    try {
        Tree tokensTree = getTokenTree(info).getParent();
        tokensTree.setProperty(JcrConstants.JCR_PRIMARYTYPE, JcrConstants.NT_UNSTRUCTURED, Type.NAME);
        root.commit();
        fail("The primary type of the token parent must not be changed from rep:Unstructured to another type.");
    } catch (CommitFailedException e) {
        assertEquals(69, e.getCode());
    } finally {
        root.refresh();
    }
}
Also used : Tree(org.apache.jackrabbit.oak.api.Tree) TokenInfo(org.apache.jackrabbit.oak.spi.security.authentication.token.TokenInfo) CommitFailedException(org.apache.jackrabbit.oak.api.CommitFailedException) Test(org.junit.Test)

Example 3 with TokenInfo

use of org.apache.jackrabbit.oak.spi.security.authentication.token.TokenInfo in project jackrabbit-oak by apache.

the class TokenValidatorTest method testCreateTokenAtInvalidLocationBelowTestNode.

@Test
public void testCreateTokenAtInvalidLocationBelowTestNode() throws Exception {
    TokenInfo info = tokenProvider.createToken(userId, Collections.<String, Object>emptyMap());
    Tree tokenTree = getTokenTree(info);
    assertNotNull(tokenProvider.getTokenInfo(info.getToken()));
    NodeUtil node = new NodeUtil(root.getTree("/")).addChild("testNode", JcrConstants.NT_UNSTRUCTURED);
    try {
        createTokenTree(info, node, TOKEN_NT_NAME);
        tokenTree.remove();
        root.commit(CommitMarker.asCommitAttributes());
        fail("Creating a new token not  at '/testNode' must fail.");
    } catch (CommitFailedException e) {
        assertEquals(64, e.getCode());
    } finally {
        node.getTree().remove();
        root.commit(CommitMarker.asCommitAttributes());
    }
}
Also used : Tree(org.apache.jackrabbit.oak.api.Tree) TokenInfo(org.apache.jackrabbit.oak.spi.security.authentication.token.TokenInfo) CommitFailedException(org.apache.jackrabbit.oak.api.CommitFailedException) NodeUtil(org.apache.jackrabbit.oak.util.NodeUtil) Test(org.junit.Test)

Example 4 with TokenInfo

use of org.apache.jackrabbit.oak.spi.security.authentication.token.TokenInfo in project jackrabbit-oak by apache.

the class TokenValidatorTest method testCreateTokenAtInvalidLocationInsideUser.

@Test
public void testCreateTokenAtInvalidLocationInsideUser() throws Exception {
    TokenInfo info = tokenProvider.createToken(userId, Collections.<String, Object>emptyMap());
    Tree tokenTree = getTokenTree(info);
    assertNotNull(tokenProvider.getTokenInfo(info.getToken()));
    Tree userTree = root.getTree(getUserManager(root).getAuthorizable(userId).getPath());
    NodeUtil node = new NodeUtil(userTree).addChild("testNode", JcrConstants.NT_UNSTRUCTURED);
    try {
        createTokenTree(info, node, TOKEN_NT_NAME);
        tokenTree.remove();
        root.commit(CommitMarker.asCommitAttributes());
        fail("Creating a new token '" + node.getTree().getPath() + "' must fail.");
    } catch (CommitFailedException e) {
        assertEquals(65, e.getCode());
    } finally {
        node.getTree().remove();
        root.commit(CommitMarker.asCommitAttributes());
    }
}
Also used : Tree(org.apache.jackrabbit.oak.api.Tree) TokenInfo(org.apache.jackrabbit.oak.spi.security.authentication.token.TokenInfo) CommitFailedException(org.apache.jackrabbit.oak.api.CommitFailedException) NodeUtil(org.apache.jackrabbit.oak.util.NodeUtil) Test(org.junit.Test)

Example 5 with TokenInfo

use of org.apache.jackrabbit.oak.spi.security.authentication.token.TokenInfo in project jackrabbit-oak by apache.

the class TokenProviderImpl method createToken.

/**
     * Create a separate token node underneath a dedicated token store within
     * the user home node. That token node contains the hashed token, the
     * expiration time and additional mandatory attributes that will be verified
     * during login.
     *
     * @param credentials The current credentials.
     * @return A new {@code TokenInfo} or {@code null} if the token could not
     *         be created.
     */
@CheckForNull
@Override
public TokenInfo createToken(@Nonnull Credentials credentials) {
    Credentials creds = extractCredentials(credentials);
    String uid = (creds != null) ? credentialsSupport.getUserId(creds) : null;
    TokenInfo tokenInfo = null;
    if (uid != null) {
        Map<String, ?> attributes = credentialsSupport.getAttributes(creds);
        tokenInfo = createToken(uid, attributes);
        if (tokenInfo != null) {
            // also set the new token to the credentials.
            if (!credentialsSupport.setAttributes(creds, ImmutableMap.of(TOKEN_ATTRIBUTE, tokenInfo.getToken()))) {
                log.debug("Cannot set token attribute to " + creds);
            }
        }
    }
    return tokenInfo;
}
Also used : TokenInfo(org.apache.jackrabbit.oak.spi.security.authentication.token.TokenInfo) TokenCredentials(org.apache.jackrabbit.api.security.authentication.token.TokenCredentials) ImpersonationCredentials(org.apache.jackrabbit.oak.spi.security.authentication.ImpersonationCredentials) Credentials(javax.jcr.Credentials) CheckForNull(javax.annotation.CheckForNull)

Aggregations

TokenInfo (org.apache.jackrabbit.oak.spi.security.authentication.token.TokenInfo)51 Test (org.junit.Test)47 Tree (org.apache.jackrabbit.oak.api.Tree)15 TokenCredentials (org.apache.jackrabbit.api.security.authentication.token.TokenCredentials)14 NodeUtil (org.apache.jackrabbit.oak.util.NodeUtil)13 CommitFailedException (org.apache.jackrabbit.oak.api.CommitFailedException)10 AbstractSecurityTest (org.apache.jackrabbit.oak.AbstractSecurityTest)8 Date (java.util.Date)7 SimpleCredentials (javax.jcr.SimpleCredentials)5 Root (org.apache.jackrabbit.oak.api.Root)4 TokenProvider (org.apache.jackrabbit.oak.spi.security.authentication.token.TokenProvider)4 HashMap (java.util.HashMap)3 Credentials (javax.jcr.Credentials)3 LoginException (javax.security.auth.login.LoginException)3 TokenConfiguration (org.apache.jackrabbit.oak.spi.security.authentication.token.TokenConfiguration)3 ArrayList (java.util.ArrayList)2 ContentSession (org.apache.jackrabbit.oak.api.ContentSession)2 ImpersonationCredentials (org.apache.jackrabbit.oak.spi.security.authentication.ImpersonationCredentials)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1