cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jbernha...@apache.org
Subject [1/2] git commit: Adding ClaimUtils#filterValues method to filter out claim values.
Date Fri, 26 Sep 2014 17:49:07 GMT
Repository: cxf
Updated Branches:
  refs/heads/2.7.x-fixes 7e80606e8 -> 35d6c3d9f


Adding ClaimUtils#filterValues method to filter out claim values.

For example to filter for only roles with a common prefix.

Conflicts:
	services/sts/sts-core/src/main/java/org/apache/cxf/sts/claims/mapper/ClaimUtils.java


Project: http://git-wip-us.apache.org/repos/asf/cxf/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/18e1ac2c
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/18e1ac2c
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/18e1ac2c

Branch: refs/heads/2.7.x-fixes
Commit: 18e1ac2c5bc62593abc9189e7154e3256f86e118
Parents: 7e80606
Author: Jan Bernhardt <jbernhardt@talend.com>
Authored: Fri Sep 26 17:04:22 2014 +0200
Committer: Jan Bernhardt <jbernhardt@talend.com>
Committed: Fri Sep 26 19:36:59 2014 +0200

----------------------------------------------------------------------
 .../cxf/sts/claims/mapper/ClaimUtils.java       | 27 ++++++++++++++++++++
 .../sts/claims/mapper/JexlClaimsMapperTest.java | 11 ++++++++
 .../jexlClaimMappingsWithFunctions.script       |  6 ++++-
 .../jexlClaimMappingsWithoutFunctions.script    | 16 +++++++++++-
 4 files changed, 58 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/18e1ac2c/services/sts/sts-core/src/main/java/org/apache/cxf/sts/claims/mapper/ClaimUtils.java
----------------------------------------------------------------------
diff --git a/services/sts/sts-core/src/main/java/org/apache/cxf/sts/claims/mapper/ClaimUtils.java
b/services/sts/sts-core/src/main/java/org/apache/cxf/sts/claims/mapper/ClaimUtils.java
index 7c7ec4a..225c0c9 100644
--- a/services/sts/sts-core/src/main/java/org/apache/cxf/sts/claims/mapper/ClaimUtils.java
+++ b/services/sts/sts-core/src/main/java/org/apache/cxf/sts/claims/mapper/ClaimUtils.java
@@ -154,6 +154,33 @@ public class ClaimUtils {
 
     /**
      * @param claims Collection of claims containing claims with claim types of listed <code>claimType</code>
+     * @param processedClaim Claim containing arbitrary values 
+     * @param filter Regex filter to be used to match with claim values
+     * @return Returns a claim containing only values from the processedClaim which matched
the provided filter
+     */
+    public Claim filterValues(Claim claim, String filter) {
+
+        if (claim != null) {
+            List<String> values = claim.getValues();
+            List<String> filteredValues = new ArrayList<String>();
+
+            if (values == null || filter == null) {
+                claim.setValues(filteredValues);
+                return claim;
+            }
+
+            for (String value : values) {
+                if (value != null && value.matches(filter)) {
+                    filteredValues.add(value);
+                }
+            }
+            claim.setValues(filteredValues);
+        }
+        return claim;
+    }
+
+    /**
+     * @param processedClaims Collection of claims containing claims with claim types of
listed <code>claimType</code>
      *            array
      * @param targetClaimType claim type URI of merged result claim
      * @param delimiter Delimiter added between multiple claim types. Value can be <code>null</code>.

http://git-wip-us.apache.org/repos/asf/cxf/blob/18e1ac2c/services/sts/sts-core/src/test/java/org/apache/cxf/sts/claims/mapper/JexlClaimsMapperTest.java
----------------------------------------------------------------------
diff --git a/services/sts/sts-core/src/test/java/org/apache/cxf/sts/claims/mapper/JexlClaimsMapperTest.java
b/services/sts/sts-core/src/test/java/org/apache/cxf/sts/claims/mapper/JexlClaimsMapperTest.java
index 9ca1b96..42fab73 100644
--- a/services/sts/sts-core/src/test/java/org/apache/cxf/sts/claims/mapper/JexlClaimsMapperTest.java
+++ b/services/sts/sts-core/src/test/java/org/apache/cxf/sts/claims/mapper/JexlClaimsMapperTest.java
@@ -188,6 +188,17 @@ public class JexlClaimsMapperTest extends org.junit.Assert {
         assertEquals(1, claim.getValues().size());
         assertEquals("Value1,Value2,Value3", claim.getValues().get(0));
     }
+    
+    @Test
+    public void testValueFilter() throws IOException {
+        ProcessedClaimCollection result = jcm.mapClaims("A", createClaimCollection(), "B",
createProperties());
+
+        assertNotNull(result);
+        ProcessedClaim claim = findClaim(result, "http://my.schema.org/identity/claims/filter");
+        assertEquals(2, claim.getValues().size());
+        assertTrue(claim.getValues().contains("match"));
+        assertTrue(claim.getValues().contains("second_match"));
+    }
 
     @SuppressWarnings("unchecked")
     protected ClaimCollection createClaimCollection() {

http://git-wip-us.apache.org/repos/asf/cxf/blob/18e1ac2c/services/sts/sts-core/src/test/resources/jexlClaimMappingsWithFunctions.script
----------------------------------------------------------------------
diff --git a/services/sts/sts-core/src/test/resources/jexlClaimMappingsWithFunctions.script
b/services/sts/sts-core/src/test/resources/jexlClaimMappingsWithFunctions.script
index 21a606b..dc3954c 100644
--- a/services/sts/sts-core/src/test/resources/jexlClaimMappingsWithFunctions.script
+++ b/services/sts/sts-core/src/test/resources/jexlClaimMappingsWithFunctions.script
@@ -53,11 +53,15 @@
     claims:singleToMultiValue(singleValue, ",");
 	var multiValue = claims:create("http://my.schema.org/identity/claims/multi2single", "Value1",
"Value2", "Value3");
     claims:multiToSingleValue(multiValue, ",");
+    
+    // Claim filter
+    var filterClaim = claims:create("http://my.schema.org/identity/claims/filter", "different",
"match", "second_match", "other");
+    claims:filterValues(filterClaim, ".*match");
 	
     // Collect claims for new token
     claims:add(targetClaims, mappedRoles, mergedClaim, idpClaim);
     claims:add(targetClaims, uppercaseClaim, lowercaseClaim, wrappedUppercaseClaim, emailClaim);
-	claims:add(targetClaims, singleValue, multiValue);
+	claims:add(targetClaims, singleValue, multiValue, filterClaim);
     
     // Set correct issuer
     claims:updateIssuer(targetClaims, claimsParameters.stsProperties.issuer);

http://git-wip-us.apache.org/repos/asf/cxf/blob/18e1ac2c/services/sts/sts-core/src/test/resources/jexlClaimMappingsWithoutFunctions.script
----------------------------------------------------------------------
diff --git a/services/sts/sts-core/src/test/resources/jexlClaimMappingsWithoutFunctions.script
b/services/sts/sts-core/src/test/resources/jexlClaimMappingsWithoutFunctions.script
index 98dda69..4e90c88 100644
--- a/services/sts/sts-core/src/test/resources/jexlClaimMappingsWithoutFunctions.script
+++ b/services/sts/sts-core/src/test/resources/jexlClaimMappingsWithoutFunctions.script
@@ -140,7 +140,21 @@
     multi2single.values = newValues;
     targetClaims.add(multi2single);
 	
-
+	// Claim filter
+	var filterClaim = new("org.apache.cxf.sts.claims.ProcessedClaim");
+    filterClaim.claimType = new("java.net.URI", "http://my.schema.org/identity/claims/filter");
+    filterClaim.values.add("different");
+	filterClaim.values.add("match");
+	filterClaim.values.add("second_match");
+	filterClaim.values.add("other");
+	var newValues = new("java.util.ArrayList");
+    for (v : filterClaim.values) {
+    	if (v.toString().matches(".*match")) {
+    		newValues.add(v);
+    	}
+    }
+    filterClaim.values = newValues;
+    targetClaims.add(filterClaim);
     
     // Set correct issuer
     for (c : targetClaims) {


Mime
View raw message