cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jbernha...@apache.org
Subject [2/3] git commit: Adding ClaimUtils#filterValues method to filter out claim values.
Date Fri, 26 Sep 2014 17:31:30 GMT
Adding ClaimUtils#filterValues method to filter out claim values.

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


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

Branch: refs/heads/3.0.x-fixes
Commit: 579a02b55dfc8ce5813faa62ca910d8074e0b053
Parents: 16f33ed
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:18:32 2014 +0200

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


http://git-wip-us.apache.org/repos/asf/cxf/blob/579a02b5/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 85cc7d8..7970bb6 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
@@ -153,6 +153,32 @@ public class ClaimUtils {
     }
     
     /**
+     * @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 ProcessedClaim filterValues(ProcessedClaim processedClaim, String filter) {
+
+        if (processedClaim != null) {
+            List<Object> values = processedClaim.getValues();
+            List<Object> filteredValues = new ArrayList<Object>();
+
+            if (values == null || filter == null) {
+                processedClaim.setValues(filteredValues);
+                return processedClaim;
+            }
+
+            for (Object value : values) {
+                if (value != null && value.toString().matches(filter)) {
+                    filteredValues.add(value);
+                }
+            }
+            processedClaim.setValues(filteredValues);
+        }
+        return processedClaim;
+    }
+
+    /**
      * @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

http://git-wip-us.apache.org/repos/asf/cxf/blob/579a02b5/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 c621806..a36486e 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
@@ -189,6 +189,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 ProcessedClaimCollection createClaimCollection() {

http://git-wip-us.apache.org/repos/asf/cxf/blob/579a02b5/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/579a02b5/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 1eeb5ee..6b24919 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