[java-opensaml] 15/16: Add CheckExpectedIssuer MessageHandler.

Brent Putman putmanb at georgetown.edu
Fri Sep 21 22:48:53 EDT 2018


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

putmanb pushed a commit to branch master
in repository java-opensaml.

View the commit online:
http://git.shibboleth.net/view/?p=java-opensaml.git;a=commit;h=cb32a42bf735bfbdbb1e9695ac9ac0c266992d47

commit cb32a42bf735bfbdbb1e9695ac9ac0c266992d47
Author: Brent Putman <putmanb at georgetown.edu>
AuthorDate: Fri Sep 21 21:02:27 2018 -0400

    Add CheckExpectedIssuer MessageHandler.
---
 .../handler/impl/CheckExpectedIssuer.java          | 102 ++++++++++++++++++++
 .../handler/impl/CheckExpectedIssuerTest.java      | 106 +++++++++++++++++++++
 2 files changed, 208 insertions(+)

diff --git a/opensaml-messaging-impl/src/main/java/org/opensaml/messaging/handler/impl/CheckExpectedIssuer.java b/opensaml-messaging-impl/src/main/java/org/opensaml/messaging/handler/impl/CheckExpectedIssuer.java
new file mode 100644
index 0000000..333bf4e
--- /dev/null
+++ b/opensaml-messaging-impl/src/main/java/org/opensaml/messaging/handler/impl/CheckExpectedIssuer.java
@@ -0,0 +1,102 @@
+/*
+ * Licensed to the University Corporation for Advanced Internet Development,
+ * Inc. (UCAID) under one or more contributor license agreements.  See the
+ * NOTICE file distributed with this work for additional information regarding
+ * copyright ownership. The UCAID licenses this file to You 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 org.opensaml.messaging.handler.impl;
+
+import java.util.Objects;
+
+import javax.annotation.Nonnull;
+
+import net.shibboleth.utilities.java.support.annotation.constraint.NonnullAfterInit;
+import net.shibboleth.utilities.java.support.component.ComponentInitializationException;
+import net.shibboleth.utilities.java.support.component.ComponentSupport;
+import net.shibboleth.utilities.java.support.logic.Constraint;
+
+import org.opensaml.messaging.context.MessageContext;
+import org.opensaml.messaging.handler.AbstractMessageHandler;
+import org.opensaml.messaging.handler.MessageHandlerException;
+
+import com.google.common.base.Function;
+
+/**
+ * Message handler that checks that a message context has an issuer.
+ */
+public final class CheckExpectedIssuer extends AbstractMessageHandler {
+
+    /** Strategy used to look up the issuer associated with the message context. */
+    @NonnullAfterInit private Function<MessageContext,String> issuerLookupStrategy;
+    
+    /** Strategy used to look up the expected issuer associated with the message context. */
+    @NonnullAfterInit private Function<MessageContext,String> expectedIssuerLookupStrategy;
+    
+    /**
+     * Set the strategy used to look up the issuer associated with the message context.
+     * 
+     * @param strategy lookup strategy
+     */
+    public void setIssuerLookupStrategy(@Nonnull final Function<MessageContext,String> strategy) {
+        ComponentSupport.ifInitializedThrowUnmodifiabledComponentException(this);
+
+        issuerLookupStrategy = Constraint.isNotNull(strategy, "Message context issuer lookup strategy cannot be null");
+    }
+    
+    /**
+     * Set the strategy used to look up the expected issuer associated with the message context.
+     * 
+     * @param strategy lookup strategy
+     */
+    public void setExpectedIssuerLookupStrategy(@Nonnull final Function<MessageContext,String> strategy) {
+        ComponentSupport.ifInitializedThrowUnmodifiabledComponentException(this);
+
+        expectedIssuerLookupStrategy = Constraint.isNotNull(strategy, 
+                "Message context expected issuer lookup strategy cannot be null");
+    }
+    
+    /** {@inheritDoc} */
+    @Override
+    protected void doInitialize() throws ComponentInitializationException {
+        super.doInitialize();
+        
+        if (issuerLookupStrategy == null) {
+            throw new ComponentInitializationException(
+                    "Message context issuer lookup strategy cannot be null");
+        }
+        if (expectedIssuerLookupStrategy == null) {
+            throw new ComponentInitializationException(
+                    "Message context expected issuer lookup strategy cannot be null");
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    protected void doInvoke(@Nonnull final MessageContext messageContext) throws MessageHandlerException {
+        final String issuer = issuerLookupStrategy.apply(messageContext);
+        if (issuer == null) {
+            throw new MessageHandlerException("Message context did not contain an issuer");
+        }
+        
+        final String expectedIssuer = expectedIssuerLookupStrategy.apply(messageContext);
+        if (expectedIssuer == null) {
+            throw new MessageHandlerException("Message context did not contain an expected issuer");
+        }
+        
+        if (!Objects.equals(issuer, expectedIssuer)) {
+            throw new MessageHandlerException("Message context issuer did not match expected issuer");
+        }
+    }
+    
+}
\ No newline at end of file
diff --git a/opensaml-messaging-impl/src/test/java/org/opensaml/messaging/handler/impl/CheckExpectedIssuerTest.java b/opensaml-messaging-impl/src/test/java/org/opensaml/messaging/handler/impl/CheckExpectedIssuerTest.java
new file mode 100644
index 0000000..fdd33fd
--- /dev/null
+++ b/opensaml-messaging-impl/src/test/java/org/opensaml/messaging/handler/impl/CheckExpectedIssuerTest.java
@@ -0,0 +1,106 @@
+/*
+ * Licensed to the University Corporation for Advanced Internet Development,
+ * Inc. (UCAID) under one or more contributor license agreements.  See the
+ * NOTICE file distributed with this work for additional information regarding
+ * copyright ownership. The UCAID licenses this file to You 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 org.opensaml.messaging.handler.impl;
+
+import javax.annotation.Nullable;
+
+import org.opensaml.messaging.context.MessageContext;
+import org.opensaml.messaging.handler.MessageHandlerException;
+import org.opensaml.messaging.handler.impl.CheckMandatoryIssuer;
+import org.testng.annotations.Test;
+
+import com.google.common.base.Function;
+
+import net.shibboleth.utilities.java.support.component.ComponentInitializationException;
+
+/** Unit test for {@link CheckMandatoryIssuer}. */
+public class CheckExpectedIssuerTest {
+
+    @Test
+    public void testMatch() throws Exception {
+        final CheckExpectedIssuer action = new CheckExpectedIssuer();
+        action.setIssuerLookupStrategy(new MockIssuer("issuer"));
+        action.setExpectedIssuerLookupStrategy(new MockIssuer("issuer"));
+        action.initialize();
+
+        final MessageContext mc = new MessageContext();
+        action.invoke(mc);
+    }
+
+    @Test(expectedExceptions=MessageHandlerException.class)
+    public void testNotMatch() throws Exception {
+        final CheckExpectedIssuer action = new CheckExpectedIssuer();
+        action.setIssuerLookupStrategy(new MockIssuer("issuer"));
+        action.setExpectedIssuerLookupStrategy(new MockIssuer("issuerNOT"));
+        action.initialize();
+
+        final MessageContext mc = new MessageContext();
+        action.invoke(mc);
+    }
+
+    @Test(expectedExceptions=MessageHandlerException.class)
+    public void testNoIssuer() throws Exception {
+        final CheckExpectedIssuer action = new CheckExpectedIssuer();
+        action.setIssuerLookupStrategy(new MockIssuer(null));
+        action.setExpectedIssuerLookupStrategy(new MockIssuer("issuer"));
+        action.initialize();
+
+        final MessageContext mc = new MessageContext();
+        action.invoke(mc);
+    }
+    
+    @Test(expectedExceptions=MessageHandlerException.class)
+    public void testNoExpectedIssuer() throws Exception {
+        final CheckExpectedIssuer action = new CheckExpectedIssuer();
+        action.setIssuerLookupStrategy(new MockIssuer("issuer"));
+        action.setExpectedIssuerLookupStrategy(new MockIssuer("null"));
+        action.initialize();
+
+        final MessageContext mc = new MessageContext();
+        action.invoke(mc);
+    }
+    
+    @Test(expectedExceptions=ComponentInitializationException.class)
+    public void testMissingIssuerStrategy() throws Exception {
+        final CheckExpectedIssuer action = new CheckExpectedIssuer();
+        action.setExpectedIssuerLookupStrategy(new MockIssuer("issuer"));
+        action.initialize();
+    }
+    
+    @Test(expectedExceptions=ComponentInitializationException.class)
+    public void testMissingExpectedIssuerStrategy() throws Exception {
+        final CheckExpectedIssuer action = new CheckExpectedIssuer();
+        action.setIssuerLookupStrategy(new MockIssuer("issuer"));
+        action.initialize();
+    }
+    
+    private class MockIssuer implements Function<MessageContext,String> {
+
+        final String issuer;
+        
+        MockIssuer(@Nullable final String s) {
+            issuer = s;
+        }
+        
+        /** {@inheritDoc} */
+        public String apply(MessageContext input) {
+            return issuer;
+        }
+        
+    }
+}
\ 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