Author: stillalex
Date: Thu Sep 21 16:22:34 2017
New Revision: 1809179

URL: http://svn.apache.org/viewvc?rev=1809179&view=rev
Log:
OAK-6662 Extend CredentialsSupport pluggability


Added:
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/security/authentication/credentials/CompositeCredentialsSupport.java
   (with props)
    
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/spi/security/authentication/credentials/CompositeCredentialsSupportTest.java
   (with props)
Modified:
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/authentication/token/TokenConfigurationImpl.java
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/security/authentication/credentials/CredentialsSupport.java
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/security/authentication/credentials/SimpleCredentialsSupport.java
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/security/authentication/credentials/package-info.java
    
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/authentication/token/TestCredentialsSupport.java
    
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/authentication/token/TokenConfigurationImplOSGiTest.java
    
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/authentication/token/TokenLoginModuleCredentialsSupportTest.java

Modified: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/authentication/token/TokenConfigurationImpl.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/authentication/token/TokenConfigurationImpl.java?rev=1809179&r1=1809178&r2=1809179&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/authentication/token/TokenConfigurationImpl.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/authentication/token/TokenConfigurationImpl.java
 Thu Sep 21 16:22:34 2017
@@ -22,9 +22,14 @@ import java.security.Principal;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+
 import javax.annotation.Nonnull;
 
 import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableSet;
+
 import org.apache.felix.scr.annotations.Activate;
 import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Properties;
@@ -32,6 +37,7 @@ import org.apache.felix.scr.annotations.
 import org.apache.felix.scr.annotations.Reference;
 import org.apache.felix.scr.annotations.ReferenceCardinality;
 import org.apache.felix.scr.annotations.ReferencePolicy;
+import org.apache.felix.scr.annotations.References;
 import org.apache.felix.scr.annotations.Service;
 import org.apache.jackrabbit.oak.api.Root;
 import org.apache.jackrabbit.oak.spi.commit.MoveTracker;
@@ -40,6 +46,7 @@ import org.apache.jackrabbit.oak.spi.sec
 import org.apache.jackrabbit.oak.spi.security.ConfigurationParameters;
 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;
 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;
@@ -80,12 +87,17 @@ import org.apache.jackrabbit.oak.spi.sec
                 propertyPrivate = true,
                 value = 
"org.apache.jackrabbit.oak.security.authentication.token.TokenConfigurationImpl")
 })
-public class TokenConfigurationImpl extends ConfigurationBase implements 
TokenConfiguration {
-
+@References({
     @Reference(
-            cardinality = ReferenceCardinality.OPTIONAL_UNARY,
+            name = "credentialsSupport",
+            referenceInterface = CredentialsSupport.class,
+            cardinality = ReferenceCardinality.OPTIONAL_MULTIPLE,
             policy = ReferencePolicy.DYNAMIC)
-    private CredentialsSupport credentialsSupport = 
SimpleCredentialsSupport.getInstance();
+})
+public class TokenConfigurationImpl extends ConfigurationBase implements 
TokenConfiguration {
+
+    private final Map<String, CredentialsSupport> credentialsSupport = new 
ConcurrentHashMap<>(
+            ImmutableMap.of(SimpleCredentialsSupport.class.getName(), 
SimpleCredentialsSupport.getInstance()));
 
     @SuppressWarnings("UnusedDeclaration")
     public TokenConfigurationImpl() {
@@ -103,17 +115,12 @@ public class TokenConfigurationImpl exte
         setParameters(ConfigurationParameters.of(properties));
     }
 
-    @SuppressWarnings("UnusedDeclaration")
     public void bindCredentialsSupport(CredentialsSupport credentialsSupport) {
-        this.credentialsSupport = credentialsSupport;
+        this.credentialsSupport.put(credentialsSupport.getClass().getName(), 
credentialsSupport);
     }
 
-    @SuppressWarnings("UnusedDeclaration")
     public void unbindCredentialsSupport(CredentialsSupport 
credentialsSupport) {
-        if (credentialsSupport == this.credentialsSupport) {
-            // reset to default implementation
-            this.credentialsSupport = SimpleCredentialsSupport.getInstance();
-        }
+        
this.credentialsSupport.remove(credentialsSupport.getClass().getName());
     }
 
     //----------------------------------------------< SecurityConfiguration 
>---
@@ -141,6 +148,14 @@ public class TokenConfigurationImpl exte
     @Override
     public TokenProvider getTokenProvider(Root root) {
         UserConfiguration uc = 
getSecurityProvider().getConfiguration(UserConfiguration.class);
-        return new TokenProviderImpl(root, getParameters(), uc, 
credentialsSupport);
+        return new TokenProviderImpl(root, getParameters(), uc, 
newCredentialsSupport());
+    }
+
+    private CredentialsSupport newCredentialsSupport() {
+        if (!credentialsSupport.isEmpty()) {
+            return CompositeCredentialsSupport.newInstance(() -> 
ImmutableSet.copyOf(credentialsSupport.values()));
+        } else {
+            return SimpleCredentialsSupport.getInstance();
+        }
     }
 }
\ No newline at end of file

Added: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/security/authentication/credentials/CompositeCredentialsSupport.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/security/authentication/credentials/CompositeCredentialsSupport.java?rev=1809179&view=auto
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/security/authentication/credentials/CompositeCredentialsSupport.java
 (added)
+++ 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/security/authentication/credentials/CompositeCredentialsSupport.java
 Thu Sep 21 16:22:34 2017
@@ -0,0 +1,107 @@
+/*
+ * 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.spi.security.authentication.credentials;
+
+import java.util.Collection;
+import java.util.Map;
+import java.util.Set;
+import java.util.function.Supplier;
+
+import javax.annotation.CheckForNull;
+import javax.annotation.Nonnull;
+import javax.jcr.Credentials;
+
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableSet;
+import static com.google.common.collect.Sets.newHashSet;
+import static com.google.common.collect.Maps.newHashMap;
+
+/**
+ * Composite implementation of the
+ * {@link 
org.apache.jackrabbit.oak.spi.security.authentication.credentials.CredentialsSupport}
+ * interface that handles multiple providers.
+ */
+public final class CompositeCredentialsSupport implements CredentialsSupport {
+
+    @Nonnull
+    private final Supplier<Collection<CredentialsSupport>> credentialSupplier;
+
+    private CompositeCredentialsSupport(@Nonnull 
Supplier<Collection<CredentialsSupport>> credentialSupplier) {
+        this.credentialSupplier = credentialSupplier;
+    }
+
+    public static CredentialsSupport newInstance(@Nonnull 
Supplier<Collection<CredentialsSupport>> credentialSupplier) {
+        return new CompositeCredentialsSupport(credentialSupplier);
+    }
+
+    @Override
+    @Nonnull
+    public Set<Class> getCredentialClasses() {
+        Collection<CredentialsSupport> all = this.credentialSupplier.get();
+        if (all.isEmpty()) {
+            return ImmutableSet.of();
+        } else if (all.size() == 1) {
+            return all.iterator().next().getCredentialClasses();
+        } else {
+            Set<Class> classes = newHashSet();
+            for (CredentialsSupport c : all) {
+                classes.addAll(c.getCredentialClasses());
+            }
+            return classes;
+        }
+    }
+
+    @Override
+    @CheckForNull
+    public String getUserId(@Nonnull Credentials credentials) {
+        Collection<CredentialsSupport> all = this.credentialSupplier.get();
+        for (CredentialsSupport c : all) {
+            String userId = c.getUserId(credentials);
+            if (userId != null) {
+                return userId;
+            }
+        }
+        return null;
+    }
+
+    @Override
+    @Nonnull
+    public Map<String, ?> getAttributes(@Nonnull Credentials credentials) {
+        Collection<CredentialsSupport> all = this.credentialSupplier.get();
+        if (all.isEmpty()) {
+            return ImmutableMap.of();
+        } else if (all.size() == 1) {
+            return all.iterator().next().getAttributes(credentials);
+        } else {
+            Map<String, Object> attrs = newHashMap();
+            for (CredentialsSupport c : all) {
+                attrs.putAll(c.getAttributes(credentials));
+            }
+            return attrs;
+        }
+    }
+
+    @Override
+    public boolean setAttributes(@Nonnull Credentials credentials, @Nonnull 
Map<String, ?> attributes) {
+        boolean set = false;
+        Collection<CredentialsSupport> all = this.credentialSupplier.get();
+        for (CredentialsSupport c : all) {
+            set = c.setAttributes(credentials, attributes) || set;
+        }
+        return set;
+    }
+}
\ No newline at end of file

Propchange: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/security/authentication/credentials/CompositeCredentialsSupport.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/security/authentication/credentials/CredentialsSupport.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/security/authentication/credentials/CredentialsSupport.java?rev=1809179&r1=1809178&r2=1809179&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/security/authentication/credentials/CredentialsSupport.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/security/authentication/credentials/CredentialsSupport.java
 Thu Sep 21 16:22:34 2017
@@ -33,7 +33,7 @@ public interface CredentialsSupport {
 
     /**
      * Returns all {@link Credentials credentials} classes supported by this
-     * implemenation.
+     * implementation.
      *
      * @return the supported {@link Credentials credentials} classes.
      */

Modified: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/security/authentication/credentials/SimpleCredentialsSupport.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/security/authentication/credentials/SimpleCredentialsSupport.java?rev=1809179&r1=1809178&r2=1809179&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/security/authentication/credentials/SimpleCredentialsSupport.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/security/authentication/credentials/SimpleCredentialsSupport.java
 Thu Sep 21 16:22:34 2017
@@ -29,8 +29,9 @@ import com.google.common.collect.Immutab
 import com.google.common.collect.Maps;
 
 /**
- * Implementation of the {@code SupportedCredentials} interface that handles
- * {@link javax.jcr.SimpleCredentials}.
+ * Implementation of the
+ * {@link 
org.apache.jackrabbit.oak.spi.security.authentication.credentials.CredentialsSupport}
+ * interface that handles {@link javax.jcr.SimpleCredentials}.
  */
 public final class SimpleCredentialsSupport implements CredentialsSupport {
 

Modified: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/security/authentication/credentials/package-info.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/security/authentication/credentials/package-info.java?rev=1809179&r1=1809178&r2=1809179&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/security/authentication/credentials/package-info.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/security/authentication/credentials/package-info.java
 Thu Sep 21 16:22:34 2017
@@ -14,7 +14,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-@Version("2.0.0")
+@Version("2.1.0")
 package org.apache.jackrabbit.oak.spi.security.authentication.credentials;
 
 import org.osgi.annotation.versioning.Version;
\ No newline at end of file

Modified: 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/authentication/token/TestCredentialsSupport.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/authentication/token/TestCredentialsSupport.java?rev=1809179&r1=1809178&r2=1809179&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/authentication/token/TestCredentialsSupport.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/authentication/token/TestCredentialsSupport.java
 Thu Sep 21 16:22:34 2017
@@ -23,7 +23,10 @@ import javax.annotation.CheckForNull;
 import javax.annotation.Nonnull;
 import javax.jcr.Credentials;
 
+import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Maps;
+
 import 
org.apache.jackrabbit.oak.spi.security.authentication.credentials.CredentialsSupport;
 import 
org.apache.jackrabbit.oak.spi.security.authentication.token.TokenConstants;
 
@@ -66,7 +69,7 @@ public class TestCredentialsSupport impl
         if (credentials instanceof Creds) {
             return ((Creds) credentials).attributes;
         } else {
-            throw new IllegalArgumentException();
+            return ImmutableMap.of();
         }
     }
 
@@ -76,16 +79,16 @@ public class TestCredentialsSupport impl
             ((Creds) credentials).attributes.putAll(attributes);
             return true;
         } else {
-            throw new IllegalArgumentException();
+            return false;
         }
     }
 
     static final class Creds implements Credentials {
 
-        private final Map attributes;
+        private final Map<String, Object> attributes;
 
         Creds() {
-            attributes = new HashMap();
+            attributes = Maps.newHashMap();
             attributes.put(TokenConstants.TOKEN_ATTRIBUTE, "");
         }
     }

Modified: 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/authentication/token/TokenConfigurationImplOSGiTest.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/authentication/token/TokenConfigurationImplOSGiTest.java?rev=1809179&r1=1809178&r2=1809179&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/authentication/token/TokenConfigurationImplOSGiTest.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/authentication/token/TokenConfigurationImplOSGiTest.java
 Thu Sep 21 16:22:34 2017
@@ -76,7 +76,7 @@ public class TokenConfigurationImplOSGiT
 
         TokenProvider tp = tokenConfiguration.getTokenProvider(root);
 
-        assertFalse(tp.doCreateToken(sc));
+        assertTrue(tp.doCreateToken(sc));
         assertTrue(tp.doCreateToken(new TestCredentialsSupport.Creds()));
     }
 
@@ -86,7 +86,7 @@ public class TokenConfigurationImplOSGiT
         ServiceRegistration registration = 
context.bundleContext().registerService(CredentialsSupport.class.getName(), 
testSupport, new Hashtable());
 
         TokenProvider tp = tokenConfiguration.getTokenProvider(root);
-        assertFalse(tp.doCreateToken(sc));
+        assertTrue(tp.doCreateToken(sc));
         assertTrue(tp.doCreateToken(new TestCredentialsSupport.Creds()));
 
         registration.unregister();

Modified: 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/authentication/token/TokenLoginModuleCredentialsSupportTest.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/authentication/token/TokenLoginModuleCredentialsSupportTest.java?rev=1809179&r1=1809178&r2=1809179&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/authentication/token/TokenLoginModuleCredentialsSupportTest.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/authentication/token/TokenLoginModuleCredentialsSupportTest.java
 Thu Sep 21 16:22:34 2017
@@ -18,6 +18,8 @@ package org.apache.jackrabbit.oak.securi
 
 import java.util.Collections;
 import java.util.Map;
+
+import javax.jcr.SimpleCredentials;
 import javax.security.auth.login.AppConfigurationEntry;
 import javax.security.auth.login.Configuration;
 
@@ -42,6 +44,7 @@ public class TokenLoginModuleCredentials
     private TokenConfigurationImpl tc;
     private CredentialsSupport credentialsSupport;
 
+    @Override
     @Before
     public void before() throws Exception {
         super.before();
@@ -109,4 +112,24 @@ public class TokenLoginModuleCredentials
             cs.close();
         }
     }
+
+    @Test
+    public void testSimpleCredentials() throws Exception {
+        SimpleCredentials credentials = (SimpleCredentials) 
getAdminCredentials();
+        credentials.setAttribute(TokenConstants.TOKEN_ATTRIBUTE, "");
+
+        ContentSession cs = null;
+        try {
+            cs = login(credentials);
+            assertEquals(credentials.getUserID(), 
cs.getAuthInfo().getUserID());
+            String token = 
credentials.getAttribute(TokenConstants.TOKEN_ATTRIBUTE).toString();
+            assertFalse(token.isEmpty());
+            cs.close();
+        } finally {
+
+            if (cs != null) {
+                cs.close();
+            }
+        }
+    }
 }

Added: 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/spi/security/authentication/credentials/CompositeCredentialsSupportTest.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/spi/security/authentication/credentials/CompositeCredentialsSupportTest.java?rev=1809179&view=auto
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/spi/security/authentication/credentials/CompositeCredentialsSupportTest.java
 (added)
+++ 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/spi/security/authentication/credentials/CompositeCredentialsSupportTest.java
 Thu Sep 21 16:22:34 2017
@@ -0,0 +1,166 @@
+/*
+ * 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.spi.security.authentication.credentials;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+import javax.jcr.Credentials;
+import javax.jcr.SimpleCredentials;
+
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableSet;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import static com.google.common.collect.Sets.newHashSet;
+
+public class CompositeCredentialsSupportTest {
+
+    private final TestCredentialsSupport tcs = new TestCredentialsSupport();
+
+    private final CredentialsSupport credentialsSupport = 
CompositeCredentialsSupport
+            .newInstance(() -> 
newHashSet(SimpleCredentialsSupport.getInstance(), tcs));
+
+    @Test
+    public void testGetCredentialClasses() {
+        Set<Class> supported = credentialsSupport.getCredentialClasses();
+        assertNotNull(supported);
+        assertEquals(2, supported.size());
+        assertTrue(supported.contains(TestCredentials.class));
+        assertTrue(supported.contains(SimpleCredentials.class));
+    }
+
+    @Test
+    public void testGetUserId() {
+        assertEquals("Test1CredentialsSupport", 
credentialsSupport.getUserId(new TestCredentials()));
+        assertNull(credentialsSupport.getUserId(new SimpleCredentials(null, 
new char[0])));
+        assertEquals("uid", credentialsSupport.getUserId(new 
SimpleCredentials("uid", new char[0])));
+        assertNull(credentialsSupport.getUserId(new Credentials() {
+        }));
+    }
+
+    @Test
+    public void testGetAttributes() {
+        Map<String, ?> attributes = credentialsSupport.getAttributes(new 
TestCredentials());
+        assertNotNull(attributes);
+        assertTrue(attributes.isEmpty());
+
+        SimpleCredentials sc = new SimpleCredentials("uid", new char[0]);
+        attributes = credentialsSupport.getAttributes(sc);
+        assertNotNull(attributes);
+        assertTrue(attributes.isEmpty());
+
+        Map<String, ?> expected = ImmutableMap.of("a", "a", "b", Boolean.TRUE, 
"c", new TestCredentials());
+        for (Map.Entry<String, ?> entry : expected.entrySet()) {
+            sc.setAttribute(entry.getKey(), entry.getValue());
+        }
+
+        attributes = credentialsSupport.getAttributes(sc);
+        assertNotNull(attributes);
+        assertEquals(3, attributes.size());
+        assertEquals(expected, attributes);
+    }
+
+    @Test
+    public void testSetAttributes() {
+        SimpleCredentials sc = new SimpleCredentials("uid", new char[0]);
+        TestCredentials tc = new TestCredentials();
+        Credentials dummy = new Credentials() {
+        };
+
+        Map<String, ?> attributesS = credentialsSupport.getAttributes(sc);
+        assertNotNull(attributesS);
+        assertTrue(attributesS.isEmpty());
+
+        Map<String, ?> attributesT = credentialsSupport.getAttributes(tc);
+        assertNotNull(attributesT);
+        assertTrue(attributesT.isEmpty());
+
+        Map<String, ?> attributesD = credentialsSupport.getAttributes(dummy);
+        assertNotNull(attributesD);
+        assertTrue(attributesD.isEmpty());
+
+        Map<String, ?> expectedS = ImmutableMap.of("a", "a", "b", 
Boolean.TRUE, "c", new TestCredentials());
+        assertTrue(credentialsSupport.setAttributes(sc, expectedS));
+
+        Map<String, ?> expectedT = ImmutableMap.of("test", 
"Test1CredentialsSupport");
+        assertTrue(credentialsSupport.setAttributes(tc, expectedT));
+
+        assertFalse(credentialsSupport.setAttributes(dummy, 
ImmutableMap.of("none", "none")));
+
+        attributesS = credentialsSupport.getAttributes(sc);
+        for (Map.Entry<String, ?> entry : expectedS.entrySet()) {
+            assertEquals(entry.getValue(), attributesS.get(entry.getKey()));
+        }
+        attributesT = credentialsSupport.getAttributes(tc);
+        for (Map.Entry<String, ?> entry : expectedT.entrySet()) {
+            assertEquals(entry.getValue(), attributesT.get(entry.getKey()));
+        }
+        attributesD = credentialsSupport.getAttributes(dummy);
+        assertNotNull(attributesD);
+        assertTrue(attributesD.isEmpty());
+    }
+
+    private static final class TestCredentials implements Credentials {
+    }
+
+    private static final class TestCredentialsSupport implements 
CredentialsSupport {
+
+        private final Map<String, Object> attributes = new HashMap<String, 
Object>();
+
+        @Override
+        public Set<Class> getCredentialClasses() {
+            return ImmutableSet.of(TestCredentials.class);
+        }
+
+        @Override
+        public String getUserId(Credentials credentials) {
+            if (credentials instanceof TestCredentials) {
+                return "Test1CredentialsSupport";
+            } else {
+                return null;
+            }
+        }
+
+        @Override
+        public Map<String, ?> getAttributes(Credentials credentials) {
+            if (credentials instanceof TestCredentials) {
+                return attributes;
+            } else {
+                return ImmutableMap.of();
+            }
+        }
+
+        @Override
+        public boolean setAttributes(Credentials credentials, Map<String, ?> 
attributes) {
+            if (credentials instanceof TestCredentials) {
+                this.attributes.putAll(attributes);
+                return true;
+            } else {
+                return false;
+            }
+        }
+    }
+
+}
\ No newline at end of file

Propchange: 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/spi/security/authentication/credentials/CompositeCredentialsSupportTest.java
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to