[java-oidc-common] branch main updated: Add metadata policy lookup strategy factory

Phil Smart philip.smart at jisc.ac.uk
Tue Mar 8 21:38:36 UTC 2022


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

philsmart 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=ab62d23a86224834c4454929cff3a7e47d7b1d8a

The following commit(s) were added to refs/heads/main by this push:
     new ab62d23  Add metadata policy lookup strategy factory
ab62d23 is described below

commit ab62d23a86224834c4454929cff3a7e47d7b1d8a
Author: Phil Smart <philip.smart at jisc.ac.uk>
AuthorDate: Tue Mar 8 21:38:30 2022 +0000

    Add metadata policy lookup strategy factory
---
 .../impl/MetadataPolicyLookupStrategyFactory.java  | 74 +++++++++++++++++++
 .../MetadataPolicyLookupStrategyFactoryTest.java   | 83 ++++++++++++++++++++++
 ...icy-lookup-strategy-factory-test-dummy-file.txt |  1 +
 ...etadata-policy-lookup-strategy-factory-test.xml | 64 +++++++++++++++++
 4 files changed, 222 insertions(+)

diff --git a/oidc-common-metadata-impl/src/main/java/net/shibboleth/oidc/metadata/cache/impl/MetadataPolicyLookupStrategyFactory.java b/oidc-common-metadata-impl/src/main/java/net/shibboleth/oidc/metadata/cache/impl/MetadataPolicyLookupStrategyFactory.java
new file mode 100644
index 0000000..aa95fb6
--- /dev/null
+++ b/oidc-common-metadata-impl/src/main/java/net/shibboleth/oidc/metadata/cache/impl/MetadataPolicyLookupStrategyFactory.java
@@ -0,0 +1,74 @@
+package net.shibboleth.oidc.metadata.cache.impl;
+
+import java.io.IOException;
+import java.util.Map;
+import java.util.function.Function;
+
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
+import org.opensaml.profile.context.ProfileRequestContext;
+import org.springframework.core.io.Resource;
+
+import net.shibboleth.oidc.metadata.cache.LoadingStrategy;
+import net.shibboleth.oidc.metadata.policy.MetadataPolicy;
+import net.shibboleth.oidc.metadata.policy.MetadataPolicyResolver;
+import net.shibboleth.oidc.metadata.policy.impl.OIDCMetadataPolicyResolver;
+import net.shibboleth.oidc.profile.config.navigate.ResolverBasedRegistrationMetadataPolicyLookupFunction;
+import net.shibboleth.utilities.java.support.annotation.ParameterName;
+import net.shibboleth.utilities.java.support.component.ComponentInitializationException;
+import net.shibboleth.utilities.java.support.resolver.CriteriaSet;
+
+/**
+ * A factory for building a {@link BatchMetadataCache} which uses a File based {@link LoadingStrategy} with the supplied
+ * file resource. Adds the cache to a new {@link MetadataPolicyResolver} instance. In this sense, every invocation of 
+ * the builder will produce a new cache inside a new resolver. ADD TYO
+ * 
+ * <p>Not thread-safe, should be a prototype factory.</p>
+ */
+public class MetadataPolicyLookupStrategyFactory {
+    
+
+    /**
+     * Build a Function that maps a profile request context to a map of metadata policies. The map is resolved
+     * from a newly instantiated {@link MetadataPolicyResolver}. The Resolver calls a new {@link BatchMetadataCache}
+     * which is hard-wired to use a default file loading strategy with the file resource supplied.
+     * 
+     * @param file the file to inject into the loading strategy
+     * @param spec the metadata cache specification
+     * @param criteriaSetLookupStrategy the lookup strategy for the criteria set used for the metadata policy resolver.
+     * @param cacheId the identifier/name for the back-end cache created
+     * 
+     * @return the function
+     * 
+     * @throws ComponentInitializationException on error.
+     * @throws IOException on error.
+     */
+    public Function<ProfileRequestContext,Map<String,MetadataPolicy>> buildFileLoadingMetadataPolicyResolver(
+            @ParameterName(name="resource") @Nonnull final Resource file,
+            @ParameterName(name="cacheSpec") @Nonnull final BatchMetadataCacheBuilderSpec<String, Map<String, MetadataPolicy>> spec,
+            @ParameterName(name="criteriaSetLookupStrategy") @Nullable final Function<ProfileRequestContext, CriteriaSet> criteriaSetLookupStrategy,
+            @ParameterName(name="cacheId") @Nonnull final String cacheId) throws ComponentInitializationException, IOException{
+        
+        final BatchMetadataCacheBuilder.Builder<String, Map<String, MetadataPolicy>> builder = 
+                new BatchMetadataCacheBuilder.Builder<>();
+        
+        final DefaultFileLoadingStrategy fileStrategy = new DefaultFileLoadingStrategy(file);
+        spec.setLoadingStrategy(fileStrategy);
+        spec.setCacheId(cacheId);
+
+        final BatchMetadataCache<String, Map<String, MetadataPolicy>> cache = builder.build(spec);
+        
+        final MetadataPolicyResolver resolver = new OIDCMetadataPolicyResolver(cache);
+        
+        final ResolverBasedRegistrationMetadataPolicyLookupFunction function = 
+                new ResolverBasedRegistrationMetadataPolicyLookupFunction();
+        function.setMetadataPolicyResolver(resolver);
+        function.setCriteriaSetLookupStrategy(criteriaSetLookupStrategy);
+
+        return function;
+        
+    }
+    
+
+}
diff --git a/oidc-common-metadata-impl/src/test/java/net/shibboleth/oidc/metadata/cache/impl/MetadataPolicyLookupStrategyFactoryTest.java b/oidc-common-metadata-impl/src/test/java/net/shibboleth/oidc/metadata/cache/impl/MetadataPolicyLookupStrategyFactoryTest.java
new file mode 100644
index 0000000..87093dc
--- /dev/null
+++ b/oidc-common-metadata-impl/src/test/java/net/shibboleth/oidc/metadata/cache/impl/MetadataPolicyLookupStrategyFactoryTest.java
@@ -0,0 +1,83 @@
+package net.shibboleth.oidc.metadata.cache.impl;
+
+import static org.testng.Assert.assertNotNull;
+
+import java.io.IOException;
+import java.time.Duration;
+import java.time.Instant;
+import java.util.Collections;
+import java.util.Map;
+
+import org.mockito.Mockito;
+import org.springframework.context.support.AbstractRefreshableApplicationContext;
+import org.springframework.context.support.GenericXmlApplicationContext;
+import org.springframework.core.io.Resource;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import net.shibboleth.ext.spring.util.AnnotationParameterNameDiscoverer;
+import net.shibboleth.oidc.metadata.cache.CacheLoadingContext;
+import net.shibboleth.oidc.metadata.cache.LoadingStrategy;
+import net.shibboleth.oidc.metadata.policy.MetadataPolicy;
+import net.shibboleth.utilities.java.support.component.ComponentInitializationException;
+
+/** Test for MetadataPolicyLookupStrategyFactory.*/
+public class MetadataPolicyLookupStrategyFactoryTest {
+    
+    /** Factory to test.*/
+    private MetadataPolicyLookupStrategyFactory factory;
+    
+    private BatchMetadataCacheBuilderSpec<String, Map<String, MetadataPolicy>> spec;
+    
+    // Dummmy values not really tested here. 
+    @BeforeMethod
+    public void setup() {
+        factory = new MetadataPolicyLookupStrategyFactory();
+        spec = new BatchMetadataCacheBuilderSpec<>();
+        spec.setIdentifierExtractionStrategy(key -> "");
+        spec.setMinRefreshDelay(Duration.ofMinutes(5));
+        spec.setMaxRefreshDelay(Duration.ofMinutes(10));
+        spec.setSourceMetadataExpiryStrategy(b -> Instant.now().plus(Duration.ofMinutes(5)));
+        spec.setCriteriaToIdentifierStrategy(crit -> "");
+        spec.setCacheId("Mock Batch Cache");
+        spec.setRefreshDelayFactor(0.75f);
+        spec.setMetadataFilterStrategy((metadata, context) -> metadata);
+        spec.setLoadingStrategy(new LoadingStrategy() {
+            
+            @Override
+            public byte[] load(final CacheLoadingContext t) {
+                return "test".getBytes();
+            }
+            
+            @Override
+            public String getSourceIdentifier() {
+                return "Mock loading source";
+            }
+        });
+        spec.setParsingStrategy(bytesIn -> Collections.emptyList());
+    }
+    
+    /* Test the factory creates the function correctly. Not throwing an exception is likely enough to test this.*/
+    @Test
+    public void testFactoryBuild_FileBased_Success() throws ComponentInitializationException, IOException {
+        final var createdFunction = factory.buildFileLoadingMetadataPolicyResolver(Mockito.mock(Resource.class),spec, null, "Mock");
+        assertNotNull(createdFunction);
+    }
+    
+    @Test
+    public void testXMLDefinitionLoads() {
+        final GenericXmlApplicationContext context = new GenericXmlApplicationContext();
+        context.load(
+                "net/shibboleth/oidc/metadata/impl/metadata-policy-lookup-strategy-factory-test.xml");
+        context.getDefaultListableBeanFactory().setParameterNameDiscoverer(new AnnotationParameterNameDiscoverer());
+        context.refresh();
+        assertNotNull(context.getId());
+        final Object stratOne = context.getBean("shibboleth.oidc.test.MetadataPolicyLookupStrategyOne");
+        assertNotNull(stratOne);
+        final Object stratTwo = context.getBean("shibboleth.oidc.test.MetadataPolicyLookupStrategyTwo");
+        assertNotNull(stratTwo);
+        context.close();
+        
+    }
+
+}
diff --git a/oidc-common-metadata-impl/src/test/resources/net/shibboleth/oidc/metadata/impl/metadata-policy-lookup-strategy-factory-test-dummy-file.txt b/oidc-common-metadata-impl/src/test/resources/net/shibboleth/oidc/metadata/impl/metadata-policy-lookup-strategy-factory-test-dummy-file.txt
new file mode 100644
index 0000000..715b02d
--- /dev/null
+++ b/oidc-common-metadata-impl/src/test/resources/net/shibboleth/oidc/metadata/impl/metadata-policy-lookup-strategy-factory-test-dummy-file.txt
@@ -0,0 +1 @@
+{"key":"value"}
\ No newline at end of file
diff --git a/oidc-common-metadata-impl/src/test/resources/net/shibboleth/oidc/metadata/impl/metadata-policy-lookup-strategy-factory-test.xml b/oidc-common-metadata-impl/src/test/resources/net/shibboleth/oidc/metadata/impl/metadata-policy-lookup-strategy-factory-test.xml
new file mode 100644
index 0000000..75e832f
--- /dev/null
+++ b/oidc-common-metadata-impl/src/test/resources/net/shibboleth/oidc/metadata/impl/metadata-policy-lookup-strategy-factory-test.xml
@@ -0,0 +1,64 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<beans xmlns="http://www.springframework.org/schema/beans"
+    xmlns:context="http://www.springframework.org/schema/context"
+    xmlns:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p"
+    xmlns:c="http://www.springframework.org/schema/c" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
+                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
+                           http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"
+
+    default-init-method="initialize" default-destroy-method="destroy">
+
+    <!-- A not so useful batch metadata cache definition -->
+    <bean id="shibboleth.oidc.test.BatchMetadataCacheBuilderSpec"
+        class="net.shibboleth.oidc.metadata.cache.impl.BatchMetadataCacheBuilderSpec"
+        p:sourceMetadataExpiryStrategy-ref="shibboleth.oidc.test.DefaultExpirationTimeStrategy"
+        p:parsingStrategy-ref="shibboleth.oidc.test.DefaultJSONMapParsingStrategy" p:cacheId="TestPolicyCache"
+        p:identifierExtractionStrategy-ref="shibboleth.oidc.test.DefaultMetadataPolicyIdentifierExtractionStrategy"
+        p:criteriaToIdentifierStrategy-ref="shibboleth.oidc.test.DefaultMetadataCriteriaToIdentifierStrategy" />
+
+    <bean id="shibboleth.Functions.Constant" class="net.shibboleth.utilities.java.support.logic.FunctionSupport"
+        factory-method="constant" abstract="true" />
+
+    <bean id="shibboleth.oidc.test.DefaultExpirationTimeStrategy" parent="shibboleth.Functions.Constant"
+        c:target="#{getObject('NowInstant')}" />
+
+    <bean id="NowInstant" class="java.time.Instant" factory-method="now" />
+
+    <bean id="shibboleth.oidc.test.DefaultMetadataPolicyIdentifierExtractionStrategy"
+        parent="shibboleth.Functions.Constant" c:target="#{'undefined'}" />
+
+    <bean id="shibboleth.oidc.test.DefaultMetadataCriteriaToIdentifierStrategy"
+        parent="shibboleth.Functions.Constant" c:target="#{'identifier'}" />
+
+    <bean id="shibboleth.oidc.test.DefaultJSONMapParsingStrategy" parent="shibboleth.Functions.Constant"
+        c:target="#{getObject('emptyList')}" />
+
+    <util:list id="emptyList" />
+
+
+    <!-- The factory beans to test the wiring of -->
+    <bean id="shibboleth.oidc.test.MetadataPolicyLookupStrategyFactory"
+        class="net.shibboleth.oidc.metadata.cache.impl.MetadataPolicyLookupStrategyFactory" />
+
+    <bean id="shibboleth.oidc.test.MetadataPolicyLookupStrategy" abstract="true"
+        factory-bean="shibboleth.oidc.test.MetadataPolicyLookupStrategyFactory" 
+        factory-method="buildFileLoadingMetadataPolicyResolver"
+        c:_1-ref="shibboleth.oidc.test.BatchMetadataCacheBuilderSpec" 
+        c:_2="#{null}"/>
+
+
+    <bean id="shibboleth.oidc.test.MetadataPolicyLookupStrategyOne"
+        parent="shibboleth.oidc.test.MetadataPolicyLookupStrategy"
+        c:_0="classpath:net/shibboleth/oidc/metadata/impl/metadata-policy-lookup-strategy-factory-test-dummy-file.txt"       
+        c:cacheId="StrategyCache" />
+
+    <bean id="shibboleth.oidc.test.MetadataPolicyLookupStrategyTwo"
+        parent="shibboleth.oidc.test.MetadataPolicyLookupStrategy"
+        c:_0="classpath:net/shibboleth/oidc/metadata/impl/metadata-policy-lookup-strategy-factory-test-dummy-file.txt"
+        c:cacheId="StrategyTwoCache" />
+
+
+
+
+</beans>
\ No newline at end of file

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


More information about the commits mailing list