You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by an...@apache.org on 2022/06/22 12:10:41 UTC

[jackrabbit-oak] branch trunk updated: OAK-9812 : TokenConfigurationImpl does not define Context

This is an automated email from the ASF dual-hosted git repository.

angela pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/jackrabbit-oak.git


The following commit(s) were added to refs/heads/trunk by this push:
     new bb330831d7 OAK-9812 : TokenConfigurationImpl does not define Context
bb330831d7 is described below

commit bb330831d778584d0e6d7bfe20939432e75d89cf
Author: angela <an...@adobe.com>
AuthorDate: Wed Jun 22 14:10:33 2022 +0200

    OAK-9812 : TokenConfigurationImpl does not define Context
---
 .../token/TokenConfigurationImpl.java              |  17 +-
 .../authentication/token/TokenContext.java         |  74 +++++++++
 .../token/TokenConfigurationImplTest.java          |   9 +-
 .../authentication/token/TokenContextTest.java     | 184 +++++++++++++++++++++
 4 files changed, 277 insertions(+), 7 deletions(-)

diff --git a/oak-core/src/main/java/org/apache/jackrabbit/oak/security/authentication/token/TokenConfigurationImpl.java b/oak-core/src/main/java/org/apache/jackrabbit/oak/security/authentication/token/TokenConfigurationImpl.java
index 5f47a95997..adc30fbccc 100644
--- a/oak-core/src/main/java/org/apache/jackrabbit/oak/security/authentication/token/TokenConfigurationImpl.java
+++ b/oak-core/src/main/java/org/apache/jackrabbit/oak/security/authentication/token/TokenConfigurationImpl.java
@@ -16,11 +16,6 @@
  */
 package org.apache.jackrabbit.oak.security.authentication.token;
 
-import java.security.Principal;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.concurrent.ConcurrentHashMap;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableSet;
@@ -29,6 +24,7 @@ import org.apache.jackrabbit.oak.spi.commit.MoveTracker;
 import org.apache.jackrabbit.oak.spi.commit.ValidatorProvider;
 import org.apache.jackrabbit.oak.spi.security.ConfigurationBase;
 import org.apache.jackrabbit.oak.spi.security.ConfigurationParameters;
+import org.apache.jackrabbit.oak.spi.security.Context;
 import org.apache.jackrabbit.oak.spi.security.SecurityConfiguration;
 import org.apache.jackrabbit.oak.spi.security.SecurityProvider;
 import org.apache.jackrabbit.oak.spi.security.authentication.credentials.CompositeCredentialsSupport;
@@ -48,6 +44,12 @@ import org.osgi.service.metatype.annotations.AttributeDefinition;
 import org.osgi.service.metatype.annotations.Designate;
 import org.osgi.service.metatype.annotations.ObjectClassDefinition;
 
+import java.security.Principal;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+
 import static org.apache.jackrabbit.oak.spi.security.RegistrationConstants.OAK_SECURITY_NAME;
 
 /**
@@ -148,6 +150,11 @@ public class TokenConfigurationImpl extends ConfigurationBase implements TokenCo
         return ImmutableList.of(vp);
     }
 
+    @Override
+    public @NotNull Context getContext() {
+        return TokenContext.getInstance();
+    }
+
     //-------------------------------------------------< TokenConfiguration >---
     /**
      * Returns a new instance of {@link org.apache.jackrabbit.oak.spi.security.authentication.token.TokenProvider}.
diff --git a/oak-core/src/main/java/org/apache/jackrabbit/oak/security/authentication/token/TokenContext.java b/oak-core/src/main/java/org/apache/jackrabbit/oak/security/authentication/token/TokenContext.java
new file mode 100644
index 0000000000..4c1f76c869
--- /dev/null
+++ b/oak-core/src/main/java/org/apache/jackrabbit/oak/security/authentication/token/TokenContext.java
@@ -0,0 +1,74 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.oak.security.authentication.token;
+
+import org.apache.jackrabbit.oak.api.PropertyState;
+import org.apache.jackrabbit.oak.api.Tree;
+import org.apache.jackrabbit.oak.plugins.tree.TreeLocation;
+import org.apache.jackrabbit.oak.plugins.tree.TreeUtil;
+import org.apache.jackrabbit.oak.spi.security.Context;
+import org.apache.jackrabbit.oak.spi.security.authentication.token.TokenConstants;
+import org.jetbrains.annotations.NotNull;
+
+import static org.apache.jackrabbit.oak.spi.security.authentication.token.TokenConstants.TOKEN_NT_NAME;
+
+final class TokenContext implements Context {
+
+    private static final Context INSTANCE = new TokenContext();
+    
+    private TokenContext() {}
+
+    static Context getInstance() {
+        return INSTANCE;
+    }
+
+    @Override
+    public boolean definesProperty(@NotNull Tree parent, @NotNull PropertyState property) {
+        return isTokenNode(parent);
+    }
+
+    @Override
+    public boolean definesContextRoot(@NotNull Tree tree) {
+        return TokenConstants.TOKENS_NODE_NAME.equals(tree.getName());
+    }
+
+    @Override
+    public boolean definesTree(@NotNull Tree tree) {
+        return definesContextRoot(tree) || isTokenNode(tree);
+    }
+
+    @Override
+    public boolean definesLocation(@NotNull TreeLocation location) {
+        PropertyState ps = location.getProperty();
+        TreeLocation l = (ps != null) ? location.getParent() : location;
+        Tree t = l.getTree();
+        if (t == null) {
+            return false;
+        } else {
+            return (ps == null) ? definesTree(t) : definesProperty(t, ps);
+        }
+    }
+
+    @Override
+    public boolean definesInternal(@NotNull Tree tree) {
+        return false;
+    }
+    
+    private static boolean isTokenNode(@NotNull Tree tree) {
+        return TOKEN_NT_NAME.equals(TreeUtil.getPrimaryTypeName(tree));
+    }
+}
\ No newline at end of file
diff --git a/oak-core/src/test/java/org/apache/jackrabbit/oak/security/authentication/token/TokenConfigurationImplTest.java b/oak-core/src/test/java/org/apache/jackrabbit/oak/security/authentication/token/TokenConfigurationImplTest.java
index 44d07b17c7..c08f5ab886 100644
--- a/oak-core/src/test/java/org/apache/jackrabbit/oak/security/authentication/token/TokenConfigurationImplTest.java
+++ b/oak-core/src/test/java/org/apache/jackrabbit/oak/security/authentication/token/TokenConfigurationImplTest.java
@@ -22,6 +22,7 @@ import org.apache.jackrabbit.oak.AbstractSecurityTest;
 import org.apache.jackrabbit.oak.spi.commit.MoveTracker;
 import org.apache.jackrabbit.oak.spi.commit.ValidatorProvider;
 import org.apache.jackrabbit.oak.spi.security.ConfigurationParameters;
+import org.apache.jackrabbit.oak.spi.security.Context;
 import org.apache.jackrabbit.oak.spi.security.authentication.credentials.CredentialsSupport;
 import org.apache.jackrabbit.oak.spi.security.authentication.credentials.SimpleCredentialsSupport;
 import org.apache.jackrabbit.oak.spi.security.authentication.token.TokenConfiguration;
@@ -30,7 +31,6 @@ import org.junit.Test;
 
 import javax.jcr.Credentials;
 import javax.jcr.SimpleCredentials;
-import java.security.Principal;
 import java.util.Collections;
 import java.util.List;
 import java.util.Map;
@@ -160,6 +160,11 @@ public class TokenConfigurationImplTest extends AbstractSecurityTest {
 
         verify(cs, times(4)).getCredentialClasses();
         verify(cs, times(2)).getAttributes(creds);
-
+    }
+    
+    @Test
+    public void testGetContext() {
+        Context ctx = tc.getContext();
+        assertTrue(ctx instanceof TokenContext);
     }
 }
\ No newline at end of file
diff --git a/oak-core/src/test/java/org/apache/jackrabbit/oak/security/authentication/token/TokenContextTest.java b/oak-core/src/test/java/org/apache/jackrabbit/oak/security/authentication/token/TokenContextTest.java
new file mode 100644
index 0000000000..f963a84d16
--- /dev/null
+++ b/oak-core/src/test/java/org/apache/jackrabbit/oak/security/authentication/token/TokenContextTest.java
@@ -0,0 +1,184 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.oak.security.authentication.token;
+
+import org.apache.jackrabbit.JcrConstants;
+import org.apache.jackrabbit.oak.api.PropertyState;
+import org.apache.jackrabbit.oak.api.Tree;
+import org.apache.jackrabbit.oak.api.Type;
+import org.apache.jackrabbit.oak.plugins.memory.PropertyStates;
+import org.apache.jackrabbit.oak.plugins.tree.TreeLocation;
+import org.apache.jackrabbit.oak.spi.nodetype.NodeTypeConstants;
+import org.apache.jackrabbit.oak.spi.security.Context;
+import org.apache.jackrabbit.oak.spi.security.authentication.token.TokenConstants;
+import org.jetbrains.annotations.NotNull;
+import org.junit.Test;
+
+import static org.apache.jackrabbit.JcrConstants.JCR_PRIMARYTYPE;
+import static org.apache.jackrabbit.oak.spi.security.authentication.token.TokenConstants.TOKENS_NODE_NAME;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.reset;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoInteractions;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
+import static org.mockito.Mockito.when;
+
+public class TokenContextTest {
+
+    private final Context ctx = TokenContext.getInstance();
+
+    @Test
+    public void testDefinesProperty() {
+        PropertyState ps = mock(PropertyState.class);
+        Tree t = when(mock(Tree.class).getName()).thenReturn(TOKENS_NODE_NAME).getMock();
+
+        assertFalse(ctx.definesProperty(t, ps));
+
+        when(t.getName()).thenReturn("anyName");
+        PropertyState primaryType = PropertyStates.createProperty(JCR_PRIMARYTYPE, TokenConstants.TOKEN_NT_NAME, Type.NAME);
+        when(t.getProperty(JCR_PRIMARYTYPE)).thenReturn(primaryType);
+        
+        assertTrue(ctx.definesProperty(t, ps));
+
+        PropertyState primaryType2 = PropertyStates.createProperty(JCR_PRIMARYTYPE, JcrConstants.NT_UNSTRUCTURED, Type.NAME);
+        when(t.getProperty(JCR_PRIMARYTYPE)).thenReturn(primaryType2);
+
+        assertFalse(ctx.definesProperty(t, primaryType2));
+        
+        verifyNoInteractions(ps);
+        verify(t, never()).getName();
+        verify(t, times(3)).getProperty(JCR_PRIMARYTYPE);
+        verifyNoMoreInteractions(t);
+    }
+    
+    @Test
+    public void testDefinesContextRoot() {
+        Tree t = when(mock(Tree.class).getName()).thenReturn(TOKENS_NODE_NAME).getMock();
+        assertTrue(ctx.definesContextRoot(t));
+        
+        when(t.getName()).thenReturn("anyName", TokenConstants.TOKEN_ATTRIBUTE, TokenConstants.TOKEN_ATTRIBUTE_KEY);
+        assertFalse(ctx.definesContextRoot(t));
+        assertFalse(ctx.definesContextRoot(t));
+        assertFalse(ctx.definesContextRoot(t));
+        
+        verify(t, times(4)).getName();
+        verifyNoMoreInteractions(t);
+    }
+    
+    @Test
+    public void testDefinesTree() {
+        Tree t = when(mock(Tree.class).getName()).thenReturn(TOKENS_NODE_NAME).getMock();
+        assertTrue(ctx.definesTree(t));
+
+        when(t.getName()).thenReturn("anyName");
+        PropertyState ps = PropertyStates.createProperty(JCR_PRIMARYTYPE, TokenConstants.TOKEN_NT_NAME, Type.NAME);
+        when(t.getProperty(JCR_PRIMARYTYPE)).thenReturn(ps);
+
+        assertTrue(ctx.definesTree(t));
+
+        PropertyState ps2 = PropertyStates.createProperty(JCR_PRIMARYTYPE, JcrConstants.NT_UNSTRUCTURED, Type.NAME);
+        when(t.getProperty(JCR_PRIMARYTYPE)).thenReturn(ps2);
+
+        assertFalse(ctx.definesTree(t));
+
+        verify(t, times(3)).getName();
+        verify(t, times(2)).getProperty(anyString());
+        
+        verifyNoMoreInteractions(t);
+    }
+    
+    @Test
+    public void testDefinesLocation() {
+        PropertyState ps = mock(PropertyState.class);
+
+        TreeLocation tl = mock(TreeLocation.class);
+        when(tl.getName()).thenReturn(TOKENS_NODE_NAME);
+        
+        assertFalse(ctx.definesLocation(tl));
+        verifyTL(tl, false);
+
+        when(tl.getName()).thenReturn("any");
+        
+        assertFalse(ctx.definesLocation(tl));
+        verifyTL(tl, false);
+
+        Tree t = when(mock(Tree.class).getName()).thenReturn(TOKENS_NODE_NAME).getMock();
+        when(tl.getTree()).thenReturn(t);
+        
+        assertTrue(ctx.definesLocation(tl));
+        verifyTL(tl, false);
+        verify(t).getName();
+        verifyNoMoreInteractions(t);
+        reset(t);
+        
+        when(t.getName()).thenReturn("any").getMock();
+        PropertyState primary = PropertyStates.createProperty(JCR_PRIMARYTYPE, TokenConstants.TOKEN_NT_NAME, Type.NAME);
+        when(t.getProperty(JCR_PRIMARYTYPE)).thenReturn(primary);
+        when(tl.getTree()).thenReturn(t);
+        
+        assertTrue(ctx.definesLocation(tl));
+        verifyTL(tl, false);
+        verify(t).getName();
+        verify(t).getProperty(JCR_PRIMARYTYPE);
+        verifyNoMoreInteractions(t);
+        
+        when(tl.getProperty()).thenReturn(ps);
+        when(tl.getTree()).thenReturn(t);
+        when(tl.getParent()).thenReturn(tl);
+
+        assertTrue(ctx.definesLocation(tl));
+        verifyTL(tl, true);
+
+        PropertyState primary2 = PropertyStates.createProperty(JCR_PRIMARYTYPE, NodeTypeConstants.NT_OAK_UNSTRUCTURED, Type.NAME);
+        when(t.getProperty(JCR_PRIMARYTYPE)).thenReturn(primary2);
+
+        when(tl.getProperty()).thenReturn(ps);
+        when(tl.getTree()).thenReturn(t);
+        when(tl.getParent()).thenReturn(tl);
+        
+        assertFalse(ctx.definesLocation(tl));
+        verifyTL(tl, true);
+        
+        verifyNoInteractions(ps);
+    }
+    
+    private static void verifyTL(@NotNull TreeLocation tl, boolean isProperty) {
+        verify(tl).getProperty();
+        verify(tl).getTree();
+        if (isProperty) {
+            verify(tl).getParent();
+        }
+        verifyNoMoreInteractions(tl);
+        reset(tl);
+    }
+     
+    @Test
+    public void testDefinesInternal() {
+        Tree t = when(mock(Tree.class).getName()).thenReturn(TOKENS_NODE_NAME).getMock();
+        assertFalse(ctx.definesInternal(t));
+
+        when(t.getName()).thenReturn("anyName");
+        assertFalse(ctx.definesInternal(t));
+
+        verifyNoInteractions(t);
+    }
+}
\ No newline at end of file