[java-idp-plugin-duo] branch main updated: Unit test for cookie manager wrapper.

Scott Cantor cantor.2 at osu.edu
Tue Apr 16 18:44:16 UTC 2024


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

scantor pushed a commit to branch main
in repository java-idp-plugin-duo.

View the commit online:
http://git.shibboleth.net/view/?p=java-idp-plugin-duo.git;a=commit;h=d5fc9488d607952d9d8378e2b4caeeca25a7ca4c

The following commit(s) were added to refs/heads/main by this push:
     new d5fc9488 Unit test for cookie manager wrapper.
d5fc9488 is described below

commit d5fc9488d607952d9d8378e2b4caeeca25a7ca4c
Author: Scott Cantor <cantor.2 at osu.edu>
AuthorDate: Tue Apr 16 14:44:13 2024 -0400

    Unit test for cookie manager wrapper.
---
 idp-duo-api/pom.xml                                |  21 +++
 .../authn/duo/PasswordlessCookieManagerTest.java   | 149 +++++++++++++++++++++
 .../plugin/authn/duo/TestResourceConverter.java    | 127 ++++++++++++++++++
 .../idp/plugin/authn/duo/SealerKeyStore.jks        | Bin 0 -> 984 bytes
 .../idp/plugin/authn/duo/SealerKeyStore.kver       |   1 +
 5 files changed, 298 insertions(+)

diff --git a/idp-duo-api/pom.xml b/idp-duo-api/pom.xml
index a26038c2..0262a671 100644
--- a/idp-duo-api/pom.xml
+++ b/idp-duo-api/pom.xml
@@ -35,6 +35,16 @@
             <artifactId>opensaml-profile-api</artifactId>
             <scope>provided</scope>
         </dependency>
+        <dependency>
+            <groupId>${shib-shared.groupId}</groupId>
+            <artifactId>shib-networking</artifactId>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>${shib-shared.groupId}</groupId>
+            <artifactId>shib-security</artifactId>
+            <scope>provided</scope>
+        </dependency>
         <dependency>
             <groupId>${shib-shared.groupId}</groupId>
             <artifactId>shib-support</artifactId>
@@ -78,6 +88,17 @@
         </dependency>
 
         <!-- Test dependencies -->
+        <dependency>
+            <groupId>${shib-shared.groupId}</groupId>
+            <artifactId>shib-testing</artifactId>
+            <scope>test</scope>
+        </dependency>
+        
+        <dependency>
+            <groupId>${spring.groupId}</groupId>
+            <artifactId>spring-test</artifactId>
+            <scope>test</scope>
+        </dependency>
 
     </dependencies>
 
diff --git a/idp-duo-api/src/test/java/net/shibboleth/idp/plugin/authn/duo/PasswordlessCookieManagerTest.java b/idp-duo-api/src/test/java/net/shibboleth/idp/plugin/authn/duo/PasswordlessCookieManagerTest.java
new file mode 100644
index 00000000..b0e8a030
--- /dev/null
+++ b/idp-duo-api/src/test/java/net/shibboleth/idp/plugin/authn/duo/PasswordlessCookieManagerTest.java
@@ -0,0 +1,149 @@
+/*
+ * 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.idp.plugin.authn.duo;
+
+import net.shibboleth.shared.annotation.constraint.NotEmpty;
+import net.shibboleth.shared.component.ComponentInitializationException;
+import net.shibboleth.shared.net.CookieManager;
+import net.shibboleth.shared.primitive.NonnullSupplier;
+import net.shibboleth.shared.resource.Resource;
+import net.shibboleth.shared.security.DataSealer;
+import net.shibboleth.shared.security.DataSealerException;
+import net.shibboleth.shared.security.impl.BasicKeystoreKeyStrategy;
+
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
+import org.springframework.core.io.ClassPathResource;
+import org.springframework.mock.web.MockHttpServletRequest;
+import org.springframework.mock.web.MockHttpServletResponse;
+import org.testng.Assert;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import jakarta.servlet.http.Cookie;
+import jakarta.servlet.http.HttpServletRequest;
+import jakarta.servlet.http.HttpServletResponse;
+
+/**
+ * Test for {@link DataSealer}.
+ */
+ at SuppressWarnings("javadoc")
+public class PasswordlessCookieManagerTest {
+
+    @Nonnull @NotEmpty private static final String COOKIE_NAME = "_cookieName";
+    
+    @Nonnull @NotEmpty private static final String SEALED_VALUE = "AAdzZWNyZXQxEUmXRgYcpPmfun5aNJtN60oxB3Mknti8GyvBQfxAuIuId4VOMyO65fN9iSagz5m8oiP5s0dJMBQa4%2FQ%3D";
+    
+    private Resource keystoreResource;
+    private Resource versionResource;
+    
+    private DataSealer dataSealer;
+    private PasswordlessCookieManager cookieManager;
+    
+    private MockHttpServletRequest request;
+    private MockHttpServletResponse response;
+
+    @BeforeClass
+    public void beforeClass() throws DataSealerException, ComponentInitializationException {
+        ClassPathResource resource =
+                new ClassPathResource("/net/shibboleth/idp/plugin/authn/duo/SealerKeyStore.jks");
+        Assert.assertTrue(resource.exists());
+        keystoreResource = TestResourceConverter.of(resource);
+
+        resource =
+                new ClassPathResource("/net/shibboleth/idp/plugin/authn/duo/SealerKeyStore.kver");
+        Assert.assertTrue(resource.exists());
+        versionResource = TestResourceConverter.of(resource);
+        
+        dataSealer = createDataSealer(null);
+    }
+    
+    @BeforeMethod
+    public void beforeMethod() throws ComponentInitializationException {
+        request = new MockHttpServletRequest();
+        response = new MockHttpServletResponse();
+
+        final CookieManager cm = new CookieManager();
+        cm.setHttpServletRequestSupplier(new NonnullSupplier<>() { @Nonnull public HttpServletRequest get() {return request;}});
+        cm.setHttpServletResponseSupplier(new NonnullSupplier<>() { @Nonnull public HttpServletResponse get() {return response;}});
+        cm.initialize();
+        
+        cookieManager = new PasswordlessCookieManager();
+        cookieManager.setCookieName(COOKIE_NAME);
+        cookieManager.setCookieManager(cm);
+        cookieManager.setDataSealer(dataSealer);
+        cookieManager.initialize();
+    }
+
+    private DataSealer createDataSealer(@Nullable @NotEmpty final String nodePrefix)
+            throws DataSealerException, ComponentInitializationException {
+        final BasicKeystoreKeyStrategy strategy = new BasicKeystoreKeyStrategy();
+        
+        strategy.setKeyAlias("secret");
+        strategy.setKeyPassword("kpassword");
+
+        strategy.setKeystorePassword("password");
+        strategy.setKeystoreResource(keystoreResource);
+        
+        strategy.setKeyVersionResource(versionResource);
+
+        strategy.initialize();
+        
+        final DataSealer sealer = new DataSealer();
+        sealer.setKeyStrategy(strategy);
+        sealer.setNodePrefix(nodePrefix);
+        sealer.initialize();
+        return sealer;
+    }
+
+    @Test
+    public void testMissing() {
+        Assert.assertNull(cookieManager.readCookie());
+        Assert.assertTrue(cookieManager.refreshCookie());
+    }
+    
+    @Test
+    public void testRoundtrip() {
+        Assert.assertTrue(cookieManager.writeCookie("foo"));
+        
+        final Cookie cookie = response.getCookie(COOKIE_NAME);
+        assert cookie != null;
+        
+        request.setCookies(cookie);
+        Assert.assertEquals(cookieManager.readCookie(), "foo");
+        
+        Assert.assertTrue(cookieManager.refreshCookie());
+        
+        // Get the second cookie added.
+        final Cookie refreshed = response.getCookies()[1];
+        assert refreshed != null;
+        Assert.assertNotEquals(cookie.getValue(), refreshed.getValue());
+    }
+
+    @Test
+    public void testOptOut() {
+        Assert.assertTrue(cookieManager.writeCookie(null));
+        
+        final Cookie cookie = response.getCookie(COOKIE_NAME);
+        assert cookie != null;
+        Assert.assertEquals(cookie.getValue(), "__NO");
+
+        request.setCookies(cookie);
+        Assert.assertNull(cookieManager.readCookie());
+    }
+    
+}
\ No newline at end of file
diff --git a/idp-duo-api/src/test/java/net/shibboleth/idp/plugin/authn/duo/TestResourceConverter.java b/idp-duo-api/src/test/java/net/shibboleth/idp/plugin/authn/duo/TestResourceConverter.java
new file mode 100644
index 00000000..8b3a1b26
--- /dev/null
+++ b/idp-duo-api/src/test/java/net/shibboleth/idp/plugin/authn/duo/TestResourceConverter.java
@@ -0,0 +1,127 @@
+/*
+ * 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.idp.plugin.authn.duo;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URI;
+import java.net.URL;
+
+import javax.annotation.Nonnull;
+
+import org.springframework.core.io.Resource;
+
+import net.shibboleth.shared.logic.Constraint;
+
+/**
+ * Bridging class between {@link Resource} and {@link net.shibboleth.shared.resource.Resource}.
+ */
+public final class TestResourceConverter implements net.shibboleth.shared.resource.Resource {
+
+    /** The cached Spring {@link Resource}. */
+    private Resource springResource;
+
+    /**
+     * A private for shimming the provided input.
+     * 
+     * @param theResource the spring resource;
+     */
+    private TestResourceConverter(@Nonnull Resource theResource) {
+
+        springResource = Constraint.isNotNull(theResource, "provided Spring Resource should not be null");
+    }
+
+    /**
+     * Return a {@link Resource} that does all the work of the provided {@link Resource}.
+     * 
+     * <p>
+     * If the input implements {@link Resource} then it is cast to the output, other a shim class is
+     * generated.
+     * </p>
+     * 
+     * @param springResource the input
+     * @return a {@link Resource} which reflects what the Spring one does
+     */
+    @Nonnull public static net.shibboleth.shared.resource.Resource of(@Nonnull Resource springResource) {
+        if (springResource instanceof net.shibboleth.shared.resource.Resource) {
+            return (net.shibboleth.shared.resource.Resource) springResource;
+        }
+        return new TestResourceConverter(springResource);
+    }
+
+    /** {@inheritDoc} */
+    @Override @Nonnull public InputStream getInputStream() throws IOException {
+        return springResource.getInputStream();
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean exists() {
+        return springResource.exists();
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean isReadable() {
+        return springResource.isReadable();
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean isOpen() {
+        return springResource.isOpen();
+    }
+
+    /** {@inheritDoc} */
+    @Override @Nonnull public URL getURL() throws IOException {
+        return springResource.getURL();
+    }
+
+    /** {@inheritDoc} */
+    @Override @Nonnull public URI getURI() throws IOException {
+        return springResource.getURI();
+    }
+
+    /** {@inheritDoc} */
+    @Override @Nonnull public File getFile() throws IOException {
+        return springResource.getFile();
+    }
+
+    /** {@inheritDoc} */
+    @Override public long contentLength() throws IOException {
+        return springResource.contentLength();
+    }
+
+    /** {@inheritDoc} */
+    @Override public long lastModified() throws IOException {
+        return springResource.lastModified();
+    }
+
+    /** {@inheritDoc} */
+    @Override @Nonnull public net.shibboleth.shared.resource.Resource createRelativeResource(
+            @Nonnull String relativePath) throws IOException {
+
+        return of(springResource.createRelative(relativePath));
+    }
+
+    /** {@inheritDoc} */
+    @Override public String getFilename() {
+        return springResource.getFilename();
+    }
+
+    /** {@inheritDoc} */
+    @Override @Nonnull public String getDescription() {
+        return springResource.getDescription();
+    }
+
+}
diff --git a/idp-duo-api/src/test/resources/net/shibboleth/idp/plugin/authn/duo/SealerKeyStore.jks b/idp-duo-api/src/test/resources/net/shibboleth/idp/plugin/authn/duo/SealerKeyStore.jks
new file mode 100644
index 00000000..147d92bb
Binary files /dev/null and b/idp-duo-api/src/test/resources/net/shibboleth/idp/plugin/authn/duo/SealerKeyStore.jks differ
diff --git a/idp-duo-api/src/test/resources/net/shibboleth/idp/plugin/authn/duo/SealerKeyStore.kver b/idp-duo-api/src/test/resources/net/shibboleth/idp/plugin/authn/duo/SealerKeyStore.kver
new file mode 100644
index 00000000..2cd48df3
--- /dev/null
+++ b/idp-duo-api/src/test/resources/net/shibboleth/idp/plugin/authn/duo/SealerKeyStore.kver
@@ -0,0 +1 @@
+CurrentVersion = 1

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


More information about the commits mailing list