[java-oidc-common] branch main updated: JCOMOIDC-96 - Support custom/additional metadata policy operators

Henri Mikkonen henri.mikkonen at iki.fi
Fri Mar 8 07:39:44 UTC 2024


This is an automated email from the git hooks/post-receive script.

hjmikkon pushed a commit to branch main
in repository java-oidc-common.

View the commit online:
http://git.shibboleth.net/view/?p=java-oidc-common.git;a=commit;h=93ae03b44b811ff1f49103c70aefb99956ebfbca

The following commit(s) were added to refs/heads/main by this push:
     new 93ae03b  JCOMOIDC-96 - Support custom/additional metadata policy operators
93ae03b is described below

commit 93ae03b44b811ff1f49103c70aefb99956ebfbca
Author: Henri Mikkonen <henri.mikkonen at iki.fi>
AuthorDate: Fri Mar 8 09:37:39 2024 +0200

    JCOMOIDC-96 - Support custom/additional metadata policy operators
    
    https://shibboleth.atlassian.net/browse/JCOMOIDC-96
    
    Refactored bean operator to use bean for policy validation too. Added
    DefaultMetadataPolicyOperator that can be wired with custom validator
    predicate and operator bifunction.
---
 .../policy/impl/BeanMetadataPolicyOperator.java    | 24 +++++-
 .../policy/impl/DefaultMetadataPolicyOperator.java | 85 ++++++++++++++++++++++
 .../impl/BeanMetadataPolicyOperatorTest.java       | 50 +++++++++++++
 3 files changed, 155 insertions(+), 4 deletions(-)

diff --git a/oidc-common-metadata-impl/src/main/java/net/shibboleth/oidc/metadata/policy/impl/BeanMetadataPolicyOperator.java b/oidc-common-metadata-impl/src/main/java/net/shibboleth/oidc/metadata/policy/impl/BeanMetadataPolicyOperator.java
index 539ce7e..eec9c79 100644
--- a/oidc-common-metadata-impl/src/main/java/net/shibboleth/oidc/metadata/policy/impl/BeanMetadataPolicyOperator.java
+++ b/oidc-common-metadata-impl/src/main/java/net/shibboleth/oidc/metadata/policy/impl/BeanMetadataPolicyOperator.java
@@ -41,19 +41,35 @@ public class BeanMetadataPolicyOperator implements CustomMetadataPolicyOperator,
 
     @Override
     public boolean validate(@Nonnull final MetadataPolicy policy) {
-        return true;
+        try {
+            final CustomMetadataPolicyOperator operator = fetchPolicyOperatorBean(policy);
+            return operator.validate(policy);
+        } catch (final ConstraintViolationException e) {
+            return false;
+        }
     }
 
     @Override
     public Object apply(final @Nullable Object inputValue, final @Nonnull MetadataPolicy policy)
             throws ConstraintViolationException {
+        final CustomMetadataPolicyOperator operator = fetchPolicyOperatorBean(policy);
+        return operator.apply(inputValue, policy);
+    }
+
+    /**
+     * Fetch the bean from {@link ApplicationContext} via identifier set in the given policy.
+     * 
+     * @param policy The metadata policy possibly containing the bean operator.
+     * @return The bean whose identifier matches the one given in the policy.
+     * @throws ConstraintViolationException If the matching bean could not be wired or its id could not be parsed.
+     */
+    @Nonnull protected CustomMetadataPolicyOperator fetchPolicyOperatorBean(final @Nonnull MetadataPolicy policy)
+            throws ConstraintViolationException {
         Constraint.isNotNull(policy, "metadata policy cannot be null");
         final Object rawBeanId = policy.getCustomOperators().get(BEAN_OPERATOR_NAME);
         if (rawBeanId instanceof String beanId) {
             try {
-                final CustomMetadataPolicyOperator operator = 
-                        applicationContext.getBean(beanId, CustomMetadataPolicyOperator.class);
-                return operator.apply(inputValue, policy);
+                return applicationContext.getBean(beanId, CustomMetadataPolicyOperator.class);
             } catch (final BeansException e) {
                 throw new ConstraintViolationException("Could not wire a compatible bean " + beanId);
             }
diff --git a/oidc-common-metadata-impl/src/main/java/net/shibboleth/oidc/metadata/policy/impl/DefaultMetadataPolicyOperator.java b/oidc-common-metadata-impl/src/main/java/net/shibboleth/oidc/metadata/policy/impl/DefaultMetadataPolicyOperator.java
new file mode 100644
index 0000000..e5c0b71
--- /dev/null
+++ b/oidc-common-metadata-impl/src/main/java/net/shibboleth/oidc/metadata/policy/impl/DefaultMetadataPolicyOperator.java
@@ -0,0 +1,85 @@
+/*
+ * Licensed 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 net.shibboleth.oidc.metadata.policy.impl;
+
+import java.util.function.BiFunction;
+import java.util.function.Predicate;
+
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
+import net.shibboleth.oidc.metadata.policy.MetadataPolicy;
+import net.shibboleth.shared.annotation.constraint.NonnullAfterInit;
+import net.shibboleth.shared.component.AbstractInitializableComponent;
+import net.shibboleth.shared.component.ComponentInitializationException;
+import net.shibboleth.shared.logic.Constraint;
+import net.shibboleth.shared.logic.ConstraintViolationException;
+
+/**
+ * A {@link CustomMetadataPolicyOperator} implementation that uses customizable {@link Predicate} for validating the
+ * metadata policy and {@link BiFunction} for performing the operation.
+ * 
+ * @since 3.1.0
+ */
+public class DefaultMetadataPolicyOperator extends AbstractInitializableComponent implements CustomMetadataPolicyOperator {
+
+    /** The metadata policy validator. */
+    @NonnullAfterInit private Predicate<MetadataPolicy> validator;
+
+    /** The metadata policy operator. */
+    @NonnullAfterInit private BiFunction<Object, MetadataPolicy, Object> valueOperator;
+
+    /**
+     * Set the metadata policy validator.
+     * 
+     * @param predicate What to set.
+     */
+    public void setValidator(@Nonnull final Predicate<MetadataPolicy> predicate) {
+        checkSetterPreconditions();
+        validator = Constraint.isNotNull(predicate, "The validator predicate cannot be null");
+    }
+
+    /**
+     * Set the metadata policy operator.
+     * 
+     * @param function What to set.
+     */
+    public void setValueOperator(@Nonnull final BiFunction<Object, MetadataPolicy, Object> function) {
+        checkSetterPreconditions();
+        valueOperator = Constraint.isNotNull(function, "The value operator cannot be null");
+    }
+
+    @Override
+    public void doInitialize() throws ComponentInitializationException {
+        if (validator == null) {
+            throw new ComponentInitializationException("The validator predicate cannot be null");
+        }
+        if (valueOperator == null) {
+            throw new ComponentInitializationException("The value operator cannot be null");
+        }
+    }
+
+    @Override
+    public boolean validate(@Nullable final MetadataPolicy policy) {
+        return validator.test(policy);
+    }
+
+    @Override
+    public Object apply(@Nullable final Object inputValue, @Nullable final MetadataPolicy policy)
+            throws ConstraintViolationException {
+        return valueOperator.apply(inputValue, policy);
+    }
+
+}
diff --git a/oidc-common-metadata-impl/src/test/java/net/shibboleth/oidc/metadata/policy/impl/BeanMetadataPolicyOperatorTest.java b/oidc-common-metadata-impl/src/test/java/net/shibboleth/oidc/metadata/policy/impl/BeanMetadataPolicyOperatorTest.java
index 21d287a..e3a0ffc 100644
--- a/oidc-common-metadata-impl/src/test/java/net/shibboleth/oidc/metadata/policy/impl/BeanMetadataPolicyOperatorTest.java
+++ b/oidc-common-metadata-impl/src/test/java/net/shibboleth/oidc/metadata/policy/impl/BeanMetadataPolicyOperatorTest.java
@@ -41,6 +41,41 @@ public class BeanMetadataPolicyOperatorTest {
         operator.setApplicationContext(applicationContext);
     }
 
+    @Test
+    public void testValidateValid() {
+        Mockito.when(applicationContext.getBean(Mockito.any(), Mockito.eq(CustomMetadataPolicyOperator.class)))
+            .thenReturn(alwaysSameOperator());
+        Assert.assertEquals(operator.validate(metadataPolicy("alwaysSame")), true);
+    }
+
+    @Test
+    public void testValidateInvalid() {
+        Mockito.when(applicationContext.getBean(Mockito.any(), Mockito.eq(CustomMetadataPolicyOperator.class)))
+            .thenReturn(alwaysInvalidOperator());
+        Assert.assertEquals(operator.validate(metadataPolicy("alwaysSame")), false);
+    }
+
+    @Test
+    public void testValidateNotFound() {
+        Mockito.doThrow(CustomBeansException.class)
+            .when(applicationContext).getBean(Mockito.any(), Mockito.eq(CustomMetadataPolicyOperator.class));
+        Assert.assertEquals(operator.validate(metadataPolicy("alwaysSame")), false);
+    }
+
+    @Test
+    public void testValidateNullId() {
+        Mockito.doThrow(CustomBeansException.class)
+            .when(applicationContext).getBean(Mockito.any(), Mockito.eq(CustomMetadataPolicyOperator.class));
+        Assert.assertEquals(operator.validate(metadataPolicy("alwaysSame")), false);
+    }
+
+    @Test
+    public void testValidateNonStringId() {
+        Mockito.doThrow(CustomBeansException.class)
+            .when(applicationContext).getBean(Mockito.any(), Mockito.eq(CustomMetadataPolicyOperator.class));
+        Assert.assertEquals(operator.validate(metadataPolicy("alwaysSame")), false);
+    }
+
     @Test
     public void testSame() {
         Mockito.when(applicationContext.getBean(Mockito.any(), Mockito.eq(CustomMetadataPolicyOperator.class)))
@@ -117,6 +152,21 @@ public class BeanMetadataPolicyOperatorTest {
         };
     }
 
+    protected CustomMetadataPolicyOperator alwaysInvalidOperator() {
+        return new CustomMetadataPolicyOperator() {
+
+            @Override
+            public Object apply(Object inputValue, MetadataPolicy policy) throws ConstraintViolationException {
+                return "changedValue";
+            }
+
+            @Override
+            public boolean validate(MetadataPolicy policy) {
+                return false;
+            }
+        };
+    }
+    
     protected CustomMetadataPolicyOperator alwaysChangedOperator() {
         return new CustomMetadataPolicyOperator() {
 

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list