[java-identity-provider COMMIT] in /trunk: idp-authn-api/src/main/java/net/shibboleth/idp/authn/principal/DefaultPrin...

noreply at shibboleth.net noreply at shibboleth.net
Thu Feb 19 22:16:48 EST 2015


Author: scantor
Date: Thu Feb 19 22:16:47 2015
New Revision: 7356

URL: http://svn.shibboleth.net/view/java-identity-provider?rev=7356&view=rev
Log:
IDP-602 - authentication context class default is unexpected 

Modified:
    trunk/idp-authn-api/src/main/java/net/shibboleth/idp/authn/principal/DefaultPrincipalDeterminationStrategy.java
    trunk/idp-conf/src/main/resources/conf/authn/general-authn.xml
    trunk/idp-conf/src/main/resources/system/flows/saml/saml1/common-beans.xml
    trunk/idp-conf/src/main/resources/system/flows/saml/saml2/common-beans.xml
    trunk/idp-saml-impl/src/main/java/net/shibboleth/idp/saml/saml1/profile/impl/AddAuthenticationStatementToAssertion.java
    trunk/idp-saml-impl/src/main/java/net/shibboleth/idp/saml/saml2/profile/impl/AddAuthnStatementToAssertion.java

Modified: trunk/idp-authn-api/src/main/java/net/shibboleth/idp/authn/principal/DefaultPrincipalDeterminationStrategy.java
URL: http://svn.shibboleth.net/view/java-identity-provider/trunk/idp-authn-api/src/main/java/net/shibboleth/idp/authn/principal/DefaultPrincipalDeterminationStrategy.java?rev=7356&r1=7355&r2=7356&view=diff
==============================================================================
--- trunk/idp-authn-api/src/main/java/net/shibboleth/idp/authn/principal/DefaultPrincipalDeterminationStrategy.java (original)
+++ trunk/idp-authn-api/src/main/java/net/shibboleth/idp/authn/principal/DefaultPrincipalDeterminationStrategy.java Thu Feb 19 22:16:47 2015
@@ -18,12 +18,18 @@
 package net.shibboleth.idp.authn.principal;
 
 import java.security.Principal;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.Map;
 import java.util.Set;
 
 import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
 
 import net.shibboleth.idp.authn.context.AuthenticationContext;
+import net.shibboleth.utilities.java.support.annotation.constraint.NonnullElements;
 import net.shibboleth.utilities.java.support.logic.Constraint;
 
 import org.opensaml.messaging.context.navigate.ChildContextLookup;
@@ -46,16 +52,19 @@
  * 
  * @param <T> the custom Principal type to locate
  */
-public class DefaultPrincipalDeterminationStrategy<T extends Principal> implements Function<ProfileRequestContext, T> {
-
-    /** Authentication context lookup strategy. */
-    @Nonnull private Function<ProfileRequestContext, AuthenticationContext> authnContextLookupStrategy;
+public class DefaultPrincipalDeterminationStrategy<T extends Principal> implements Function<ProfileRequestContext,T> {
 
     /** Type of Principal to return. */
     @Nonnull private final Class<T> principalType;
 
     /** Default Principal to return. */
     @Nonnull private final T defaultPrincipal;
+    
+    /** A map supplying weighted preference to particular Principals. */
+    @Nonnull @NonnullElements private Map<T,Integer> weightMap;
+
+    /** Authentication context lookup strategy. */
+    @Nonnull private Function<ProfileRequestContext,AuthenticationContext> authnContextLookupStrategy;
 
     /**
      * Constructor.
@@ -66,7 +75,28 @@
     public DefaultPrincipalDeterminationStrategy(@Nonnull final Class<T> type, @Nonnull final T principal) {
         principalType = Constraint.isNotNull(type, "Class type cannot be null");
         defaultPrincipal = Constraint.isNotNull(principal, "Default Principal cannot be null");
+        weightMap = Collections.emptyMap();
         authnContextLookupStrategy = new ChildContextLookup<>(AuthenticationContext.class, false);
+    }
+    
+    /**
+     * Set the map of Principals to weight values to impose a sort order on any matching Principals
+     * found in the authentication result.
+     * 
+     * @param map   map to set
+     */
+    public void setWeightMap(@Nonnull @NonnullElements final Map<T,Integer> map) {
+        if (map == null) {
+            weightMap = Collections.emptyMap();
+            return;
+        }
+        
+        weightMap = new HashMap<>(map.size());
+        for (final Map.Entry<T,Integer> entry : map.entrySet()) {
+            if (entry.getKey() != null && entry.getValue() != null) {
+                weightMap.put(entry.getKey(), entry.getValue());
+            }
+        }
     }
 
     /**
@@ -89,9 +119,36 @@
         final Set<T> principals = ac.getAuthenticationResult().getSupportedPrincipals(principalType);
         if (principals.isEmpty()) {
             return defaultPrincipal;
-        } else {
+        } else if (principals.size() == 1) {
             return principals.iterator().next();
         }
+            
+        Object[] principalArray = principals.toArray();
+        Arrays.sort(principalArray, new WeightedComparator());
+        return (T) principalArray[principalArray.length - 1];
     }
 
+    /**
+     * A {@link Comparator} that compares the mapped weights of the two operands, using a weight of zero
+     * for any unmapped values.
+     */
+    private class WeightedComparator implements Comparator {
+
+        /** {@inheritDoc} */
+        @Override
+        public int compare(Object o1, Object o2) {

[... 251 lines stripped ...]


More information about the commits mailing list