[java-plugin-shibd-saml] branch main updated: More tests.

Scott Cantor cantor.2 at osu.edu
Thu Sep 19 15:38:38 UTC 2024


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

scantor pushed a commit to branch main
in repository java-plugin-shibd-saml.

View the commit online:
http://git.shibboleth.net/view/?p=java-plugin-shibd-saml.git;a=commit;h=6673687fe34a285752f716a38ccfceaaaaeae5b1

The following commit(s) were added to refs/heads/main by this push:
     new 6673687  More tests.
6673687 is described below

commit 6673687fe34a285752f716a38ccfceaaaaeae5b1
Author: Scott Cantor <cantor.2 at osu.edu>
AuthorDate: Thu Sep 19 11:38:35 2024 -0400

    More tests.
---
 .../profile/impl/SAMLTokenContextConsumer.java     |   4 +-
 .../profile/impl/PrepareAgentResponseTest.java     | 131 ++++++++++++++++++++-
 2 files changed, 126 insertions(+), 9 deletions(-)

diff --git a/sp-saml-impl/src/main/java/net/shibboleth/sp/saml/saml2/profile/impl/SAMLTokenContextConsumer.java b/sp-saml-impl/src/main/java/net/shibboleth/sp/saml/saml2/profile/impl/SAMLTokenContextConsumer.java
index a91a014..942a0c7 100644
--- a/sp-saml-impl/src/main/java/net/shibboleth/sp/saml/saml2/profile/impl/SAMLTokenContextConsumer.java
+++ b/sp-saml-impl/src/main/java/net/shibboleth/sp/saml/saml2/profile/impl/SAMLTokenContextConsumer.java
@@ -19,7 +19,6 @@ import java.util.function.BiConsumer;
 import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
 
-import org.opensaml.core.xml.XMLObject;
 import org.opensaml.profile.context.ProfileRequestContext;
 import org.opensaml.saml.saml2.core.Assertion;
 import org.opensaml.saml.saml2.core.AuthnStatement;
@@ -47,8 +46,7 @@ public class SAMLTokenContextConsumer implements BiConsumer<ProfileRequestContex
         final SAMLTokenContext tokenContext = profileRequestContext.ensureSubcontext(SAMLTokenContext.class);
         tokenContext.setAuthnStatement(statement);
 
-        final XMLObject parent = statement.getParent();
-        if (parent instanceof Assertion assertion) {
+        if (statement.getParent() instanceof Assertion assertion) {
             tokenContext.setSubject(assertion.getSubject());
         }
     }
diff --git a/sp-saml-impl/src/test/java/net/shibboleth/sp/saml/saml2/profile/impl/PrepareAgentResponseTest.java b/sp-saml-impl/src/test/java/net/shibboleth/sp/saml/saml2/profile/impl/PrepareAgentResponseTest.java
index f8d4623..84d83aa 100644
--- a/sp-saml-impl/src/test/java/net/shibboleth/sp/saml/saml2/profile/impl/PrepareAgentResponseTest.java
+++ b/sp-saml-impl/src/test/java/net/shibboleth/sp/saml/saml2/profile/impl/PrepareAgentResponseTest.java
@@ -14,12 +14,26 @@
 
 package net.shibboleth.sp.saml.saml2.profile.impl;
 
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.io.IOException;
 import java.nio.charset.StandardCharsets;
 import java.time.Instant;
 
 import javax.annotation.Nonnull;
 
+import org.opensaml.core.xml.XMLObject;
+import org.opensaml.core.xml.io.UnmarshallingException;
+import org.opensaml.core.xml.util.XMLObjectSupport;
 import org.opensaml.profile.action.EventIds;
+import org.opensaml.saml.common.binding.SAMLBindingSupport;
+import org.opensaml.saml.saml2.core.Assertion;
+import org.opensaml.saml.saml2.core.AuthnStatement;
+import org.opensaml.saml.saml2.core.NameID;
+import org.opensaml.saml.saml2.core.Subject;
+import org.opensaml.saml.saml2.testing.SAML2ActionTestingSupport;
+import org.springframework.mock.web.MockHttpServletRequest;
+import org.springframework.mock.web.MockHttpServletResponse;
 import org.springframework.webflow.execution.Event;
 import org.testng.Assert;
 import org.testng.annotations.AfterMethod;
@@ -34,9 +48,15 @@ import net.shibboleth.idp.attribute.StringAttributeValue;
 import net.shibboleth.idp.attribute.context.AttributeContext;
 import net.shibboleth.idp.profile.testing.ActionTestingSupport;
 import net.shibboleth.shared.annotation.constraint.NotEmpty;
+import net.shibboleth.shared.codec.Base64Support;
+import net.shibboleth.shared.codec.DecodingException;
 import net.shibboleth.shared.collection.CollectionSupport;
 import net.shibboleth.shared.component.ComponentInitializationException;
+import net.shibboleth.shared.primitive.NonnullSupplier;
+import net.shibboleth.shared.xml.XMLParserException;
+import net.shibboleth.shared.xml.impl.BasicParserPool;
 import net.shibboleth.sp.ddf.DDF;
+import net.shibboleth.sp.impl.CookieStateTokenManager;
 import net.shibboleth.sp.messaging.RemotedHttpServletResponse;
 import net.shibboleth.sp.profile.ConsumerConstants;
 import net.shibboleth.sp.profile.impl.BaseAgplicationActionTest;
@@ -48,10 +68,7 @@ import net.shibboleth.sp.saml.saml2.context.SAMLTokenContext;
 public class PrepareAgentResponseTest extends BaseAgplicationActionTest {
 
     /** Test request URL. */
-    @Nonnull @NotEmpty private final static String TEST_URL = "https://sp.example.org/cgi-bin/test.cgi";
-
-    /** Test query string. */
-    @Nonnull @NotEmpty private final static String TEST_QUERY = "foo=bar%20baz&frobnitz=zorkmid";
+    @Nonnull @NotEmpty private static final String TEST_URL = "https://sp.example.org/cgi-bin/test.cgi";
 
     private SAMLTokenContext samlTokenContext;
     private AttributeContext attributeContext;
@@ -112,6 +129,89 @@ public class PrepareAgentResponseTest extends BaseAgplicationActionTest {
         Assert.assertTrue(http.getmember("headers").isnull());
     }
 
+    /**
+     * Unit test for opaque session blob.
+     * 
+     * @throws DecodingException 
+     * @throws IOException 
+     * @throws ComponentInitializationException 
+     */
+    @Test
+    public void testSessionState() throws DecodingException, IOException, ComponentInitializationException {
+        buildAssertion();
+        
+        final Event event = action.execute(src);
+        ActionTestingSupport.assertProceedEvent(event);
+        
+        final DDF out = arc.getOutput();
+        assert out != null;
+        Assert.assertTrue(out.isstruct());
+        
+        final DDF state = out.getmember(ConsumerConstants.SESSION_OPAQUE);
+        Assert.assertTrue(state.isstring());
+        
+        final String encoded = state.string();
+        assert encoded != null;
+        final byte[] decoded = Base64Support.decodeURLSafe(encoded);
+
+        try (final InputStream in = new ByteArrayInputStream(decoded)) {
+            final BasicParserPool parserPool = new BasicParserPool();
+            parserPool.initialize();
+            final XMLObject xmlobj =
+                    XMLObjectSupport.unmarshallFromInputStream(parserPool, in);
+            parserPool.destroy();
+            if (xmlobj instanceof NameID nameID) {
+                Assert.assertEquals(nameID.getValue(), "jdoe");
+                Assert.assertEquals(nameID.getSPProvidedID(), "foo");
+            } else {
+                Assert.fail("XMLObject stored in NameID field was not a NameID");
+            }
+        } catch (final XMLParserException | UnmarshallingException e) {
+            Assert.fail("Unable to parse or unmarshall NameID", e);
+        }
+    }
+
+    /**
+     * Unit test for RelayState.
+     * 
+     * @throws ComponentInitializationException 
+     * @throws IOException 
+     */
+    @Test
+    public void testRelayState() throws ComponentInitializationException, IOException {
+
+        final MockHttpServletRequest request = new MockHttpServletRequest();
+        final MockHttpServletResponse response = new MockHttpServletResponse();
+        
+        final CookieStateTokenManager manager = new CookieStateTokenManager();
+        manager.setId("test");
+        manager.setHttpServletRequestSupplier(NonnullSupplier.of(request));
+        manager.setHttpServletResponseSupplier(NonnullSupplier.of(response));
+        manager.initialize();
+        
+        application.setStateTokenManager(manager);
+        application.initialize();
+        
+        final String state = manager.preserveToStateToken(agent, application, TEST_URL.getBytes(StandardCharsets.UTF_8));
+        request.setCookies(response.getCookies());
+        
+        SAMLBindingSupport.setRelayState(prc.ensureInboundMessageContext(), state);
+        
+        final Event event = action.execute(src);
+        ActionTestingSupport.assertProceedEvent(event);
+        
+        final DDF out = arc.getOutput();
+        assert out != null;
+        Assert.assertTrue(out.isstruct());
+        Assert.assertTrue(out.getmember("session").isnull());
+        
+        final DDF http = out.getmember(RemotedHttpServletResponse.STRUCTURE_NAME);
+        Assert.assertTrue(http.isstruct());
+        Assert.assertEquals(http.getmember("redirect").unsafe_string(), TEST_URL.getBytes(StandardCharsets.UTF_8));
+        Assert.assertTrue(http.getmember("response").isnull());
+        Assert.assertTrue(http.getmember("headers").isnull());
+    }
+
     /**
      * Unit test for getting attributes serialized into output.
      */
@@ -121,7 +221,7 @@ public class PrepareAgentResponseTest extends BaseAgplicationActionTest {
         buildAttributes(ts);
 
         final DDF input = new DDF(null).structure();
-        input.addmember(ConsumerConstants.BASE_URL).unsafe_string("/secure/index.html".getBytes(StandardCharsets.UTF_8));
+        input.addmember(ConsumerConstants.BASE_URL).unsafe_string(TEST_URL.getBytes(StandardCharsets.UTF_8));
         arc.setInput(input);
         
         final Event event = action.execute(src);
@@ -131,9 +231,11 @@ public class PrepareAgentResponseTest extends BaseAgplicationActionTest {
         assert out != null;
         Assert.assertTrue(out.isstruct());
     
+        Assert.assertTrue(out.getmember(ConsumerConstants.SESSION_OPAQUE).isnull());
+        
         final DDF http = out.getmember(RemotedHttpServletResponse.STRUCTURE_NAME);
         Assert.assertTrue(http.isstruct());
-        Assert.assertEquals(http.getmember("redirect").unsafe_string(), "/secure/index.html".getBytes(StandardCharsets.UTF_8));
+        Assert.assertEquals(http.getmember("redirect").unsafe_string(), TEST_URL.getBytes(StandardCharsets.UTF_8));
 
         final DDF attrs = out.getmember(ConsumerConstants.SESSION_ATTRIBUTES);
         Assert.assertTrue(attrs.islist());
@@ -214,4 +316,21 @@ public class PrepareAgentResponseTest extends BaseAgplicationActionTest {
         attributeContext.setIdPAttributes(CollectionSupport.listOf(attr1, attr2, attr3, attr4));
     }
 
+    /**
+     * Adds mock assertion content to the SAMLTokenContext.
+     */
+    private void buildAssertion() {
+        final Subject subject = SAML2ActionTestingSupport.buildSubject("jdoe");
+        samlTokenContext.setSubject(subject);
+        
+        final AuthnStatement statement = SAML2ActionTestingSupport.buildAuthnStatement();
+        statement.setSessionIndex("foo");
+        
+        samlTokenContext.setAuthnStatement(statement);
+        
+        final Assertion assertion = SAML2ActionTestingSupport.buildAssertion();
+        assertion.setSubject(subject);
+        assertion.getAuthnStatements().add(statement);
+    }
+    
 }
\ 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