Author: stillalex
Date: Tue Jul 24 13:18:24 2018
New Revision: 1836553

URL: http://svn.apache.org/viewvc?rev=1836553&view=rev
Log:
OAK-6662 Extend CredentialsSupport pluggability
 - tweaked the backport some more


Removed:
    
jackrabbit/oak/branches/1.6/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/security/authentication/credentials/AbstractCredentials.java
    
jackrabbit/oak/branches/1.6/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/security/authentication/credentials/CompositeCredentialsSupport.java
    
jackrabbit/oak/branches/1.6/oak-core/src/test/java/org/apache/jackrabbit/oak/spi/security/authentication/credentials/CompositeCredentialsSupportTest.java
Modified:
    
jackrabbit/oak/branches/1.6/oak-core/src/main/java/org/apache/jackrabbit/oak/security/authentication/token/TokenConfigurationImpl.java
    
jackrabbit/oak/branches/1.6/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/security/authentication/credentials/package-info.java

Modified: 
jackrabbit/oak/branches/1.6/oak-core/src/main/java/org/apache/jackrabbit/oak/security/authentication/token/TokenConfigurationImpl.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/branches/1.6/oak-core/src/main/java/org/apache/jackrabbit/oak/security/authentication/token/TokenConfigurationImpl.java?rev=1836553&r1=1836552&r2=1836553&view=diff
==============================================================================
--- 
jackrabbit/oak/branches/1.6/oak-core/src/main/java/org/apache/jackrabbit/oak/security/authentication/token/TokenConfigurationImpl.java
 (original)
+++ 
jackrabbit/oak/branches/1.6/oak-core/src/main/java/org/apache/jackrabbit/oak/security/authentication/token/TokenConfigurationImpl.java
 Tue Jul 24 13:18:24 2018
@@ -16,6 +16,9 @@
  */
 package org.apache.jackrabbit.oak.security.authentication.token;
 
+import static com.google.common.collect.Maps.newHashMap;
+import static com.google.common.collect.Sets.newHashSet;
+
 import java.security.Principal;
 import java.util.Collection;
 import java.util.List;
@@ -23,7 +26,9 @@ import java.util.Map;
 import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 
+import javax.annotation.CheckForNull;
 import javax.annotation.Nonnull;
+import javax.jcr.Credentials;
 
 import com.google.common.base.Supplier;
 import com.google.common.collect.ImmutableList;
@@ -46,7 +51,6 @@ 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;
@@ -160,4 +164,81 @@ public class TokenConfigurationImpl exte
             return SimpleCredentialsSupport.getInstance();
         }
     }
+
+    /**
+     * Composite implementation of the
+     * {@link 
org.apache.jackrabbit.oak.spi.security.authentication.credentials.CredentialsSupport}
+     * interface that handles multiple providers.
+     */
+    private static 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

Modified: 
jackrabbit/oak/branches/1.6/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/security/authentication/credentials/package-info.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/branches/1.6/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/security/authentication/credentials/package-info.java?rev=1836553&r1=1836552&r2=1836553&view=diff
==============================================================================
--- 
jackrabbit/oak/branches/1.6/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/security/authentication/credentials/package-info.java
 (original)
+++ 
jackrabbit/oak/branches/1.6/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/security/authentication/credentials/package-info.java
 Tue Jul 24 13:18:24 2018
@@ -14,7 +14,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-@Version("2.1.0")
+@Version("2.0.0")
 package org.apache.jackrabbit.oak.spi.security.authentication.credentials;
 
 import aQute.bnd.annotation.Version;
\ No newline at end of file


Reply via email to