[java-idp-oidc] 20/44: JOIDC-5 Improved code and javadoc + added unit testing.

Henri Mikkonen henri.mikkonen at iki.fi
Thu Oct 22 13:08:32 UTC 2020


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

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

View the commit online:
http://git.shibboleth.net/view/?p=java-idp-oidc.git;a=commit;h=82a03029eee7a71214a0e5850caddd4408eb334a

commit 82a03029eee7a71214a0e5850caddd4408eb334a
Author: Henri Mikkonen <henri.mikkonen at iki.fi>
AuthorDate: Fri Jun 5 16:20:33 2020 +0300

    JOIDC-5 Improved code and javadoc + added unit testing.
    
    https://issues.shibboleth.net/jira/browse/JOIDC-5
---
 .../impl/SetEntityIdToSAMLPeerEntityContext.java   |  15 +++
 .../SetEntityIdToSAMLPeerEntityContextTest.java    | 126 +++++++++++++++++++++
 2 files changed, 141 insertions(+)

diff --git a/idp-oidc-extension-impl/src/main/java/org/geant/idpextension/oidc/profile/impl/SetEntityIdToSAMLPeerEntityContext.java b/idp-oidc-extension-impl/src/main/java/org/geant/idpextension/oidc/profile/impl/SetEntityIdToSAMLPeerEntityContext.java
index 8d78c05a..a8da43c2 100644
--- a/idp-oidc-extension-impl/src/main/java/org/geant/idpextension/oidc/profile/impl/SetEntityIdToSAMLPeerEntityContext.java
+++ b/idp-oidc-extension-impl/src/main/java/org/geant/idpextension/oidc/profile/impl/SetEntityIdToSAMLPeerEntityContext.java
@@ -22,6 +22,8 @@ import javax.annotation.Nonnull;
 
 import org.geant.idpextension.oidc.profile.context.navigate.DefaultClientIDLookupFunction;
 import org.opensaml.messaging.context.MessageContext;
+import org.opensaml.profile.action.ActionSupport;
+import org.opensaml.profile.action.EventIds;
 import org.opensaml.profile.context.ProfileRequestContext;
 import org.opensaml.saml.common.messaging.context.AbstractSAMLEntityContext;
 import org.opensaml.saml.common.messaging.context.SAMLPeerEntityContext;
@@ -34,6 +36,10 @@ import net.shibboleth.idp.profile.AbstractProfileAction;
 import net.shibboleth.utilities.java.support.component.ComponentSupport;
 import net.shibboleth.utilities.java.support.logic.Constraint;
 
+/**
+ * Action that sets the entityID to the given {@link SAMLPeerEntityContext} class, found as the subcontext of the
+ * inbound message context. The value to be set is resolved via the given lookup strategy for client ID.
+ */
 public class SetEntityIdToSAMLPeerEntityContext extends AbstractProfileAction {
 
     /** Class logger. */
@@ -47,6 +53,9 @@ public class SetEntityIdToSAMLPeerEntityContext extends AbstractProfileAction {
      * Defaults to: {@link SAMLPeerEntityContext}. */
     @Nonnull private Class<? extends AbstractSAMLEntityContext> entityContextClass = SAMLPeerEntityContext.class;
 
+    /**
+     * Constructor.
+     */
     public SetEntityIdToSAMLPeerEntityContext() {
         clientIDLookupStrategy = new DefaultClientIDLookupFunction();
     }
@@ -80,10 +89,16 @@ public class SetEntityIdToSAMLPeerEntityContext extends AbstractProfileAction {
     protected void doExecute(@Nonnull final ProfileRequestContext profileRequestContext) {
         final MessageContext messageContext = profileRequestContext.getInboundMessageContext();
         final AbstractSAMLEntityContext entityCtx =  messageContext.getSubcontext(entityContextClass);
+        if (entityCtx == null) {
+            ActionSupport.buildEvent(profileRequestContext, EventIds.INVALID_MSG_CTX);
+            return;
+        }
         final ClientID clientID = clientIDLookupStrategy.apply(messageContext);
         if (clientID != null) {
             log.debug("{} Set clientID '{}' to the peer entity context", getLogPrefix(), clientID.getValue());
             entityCtx.setEntityId(clientID.getValue());            
+        } else {
+            log.debug("{} No clientID could be resolved, nothing to do", getLogPrefix());
         }
     }
 }
diff --git a/idp-oidc-extension-impl/src/test/java/org/geant/idpextension/oidc/profile/impl/SetEntityIdToSAMLPeerEntityContextTest.java b/idp-oidc-extension-impl/src/test/java/org/geant/idpextension/oidc/profile/impl/SetEntityIdToSAMLPeerEntityContextTest.java
new file mode 100644
index 00000000..e168f346
--- /dev/null
+++ b/idp-oidc-extension-impl/src/test/java/org/geant/idpextension/oidc/profile/impl/SetEntityIdToSAMLPeerEntityContextTest.java
@@ -0,0 +1,126 @@
+/*
+ * Copyright (c) 2017 - 2020, GÉANT
+ *
+ * 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 org.geant.idpextension.oidc.profile.impl;
+
+import java.util.function.Function;
+
+import org.mockito.Mockito;
+import org.opensaml.messaging.context.MessageContext;
+import org.opensaml.profile.action.EventIds;
+import org.opensaml.profile.context.ProfileRequestContext;
+import org.opensaml.saml.common.messaging.context.SAMLPeerEntityContext;
+import org.springframework.webflow.execution.Event;
+import org.springframework.webflow.execution.RequestContext;
+import org.testng.Assert;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import com.nimbusds.oauth2.sdk.id.ClientID;
+
+import net.shibboleth.idp.profile.ActionTestingSupport;
+import net.shibboleth.idp.profile.RequestContextBuilder;
+import net.shibboleth.idp.profile.context.navigate.WebflowRequestContextProfileRequestContextLookup;
+import net.shibboleth.utilities.java.support.component.ComponentInitializationException;
+import net.shibboleth.utilities.java.support.component.UnmodifiableComponentException;
+import net.shibboleth.utilities.java.support.logic.ConstraintViolationException;
+
+/**
+ * Unit tests for {@link SetEntityIdToSAMLPeerEntityContext}.
+ */
+public class SetEntityIdToSAMLPeerEntityContextTest {
+
+    private SetEntityIdToSAMLPeerEntityContext action;
+    
+    private RequestContext requestCtx;
+
+    private ProfileRequestContext prc;
+    
+    private String clientId = "mockClientId";
+    
+    @BeforeMethod
+    public void setup() {
+        action = new SetEntityIdToSAMLPeerEntityContext();
+    }
+    
+    public void initialize(final SAMLPeerEntityContext entityContext, 
+            final Function<MessageContext, ClientID> clientIdLookupStrategy) throws ComponentInitializationException {
+        action.setClientIDLookupStrategy(clientIdLookupStrategy);
+        action.initialize();
+        requestCtx = new RequestContextBuilder().buildRequestContext();
+        prc = new WebflowRequestContextProfileRequestContextLookup().apply(requestCtx);
+        if (entityContext != null) {
+            prc.getInboundMessageContext().addSubcontext(entityContext);
+        }
+    }
+    
+    @Test(expectedExceptions = { ConstraintViolationException.class })
+    public void setClientIDLookupStrategy_shouldThrowIfGivenNull() {
+        action.setClientIDLookupStrategy(null);
+    }
+    
+    @Test(expectedExceptions = { UnmodifiableComponentException.class })
+    public void setClientIDLookupStrategy_shouldThrowIfCalledAfterInitialization()
+            throws ComponentInitializationException {
+        action.initialize();
+        action.setClientIDLookupStrategy(null);
+    }
+
+    @Test(expectedExceptions = { ConstraintViolationException.class })
+    public void setEntityContextClass_shouldThrowIfGivenNull() {
+        action.setEntityContextClass(null);
+    }
+    
+    @Test(expectedExceptions = { UnmodifiableComponentException.class })
+    public void setEntityContextClass_shouldThrowIfCalledAfterInitialization()
+            throws ComponentInitializationException {
+        action.initialize();
+        action.setEntityContextClass(null);
+    }
+    
+    @Test
+    public void execute_shouldSetInvalidMsgEventIfNoEntityContext() throws ComponentInitializationException {
+        initialize(null, mockClientIdLookupStrategy(clientId));
+        final Event event = action.execute(requestCtx);
+        ActionTestingSupport.assertEvent(event, EventIds.INVALID_MSG_CTX);
+    }
+
+    @Test
+    public void execute_shouldSetNullClientIdWhenLookupReturnsNull() throws ComponentInitializationException {
+        initialize(new SAMLPeerEntityContext(), mockClientIdLookupStrategy(null));
+        final Event event = action.execute(requestCtx);
+        ActionTestingSupport.assertProceedEvent(event);
+        final SAMLPeerEntityContext entityCtx =
+                prc.getInboundMessageContext().getSubcontext(SAMLPeerEntityContext.class);
+        Assert.assertNull(entityCtx.getEntityId());
+    }
+
+    @Test
+    public void execute_shouldSetClientIdWhenPrequisitesMet() throws ComponentInitializationException {
+        initialize(new SAMLPeerEntityContext(), mockClientIdLookupStrategy(clientId));
+        final Event event = action.execute(requestCtx);
+        ActionTestingSupport.assertProceedEvent(event);
+        final SAMLPeerEntityContext entityCtx =
+                prc.getInboundMessageContext().getSubcontext(SAMLPeerEntityContext.class);
+        Assert.assertEquals(entityCtx.getEntityId(), clientId);
+    }
+
+    protected Function<MessageContext, ClientID> mockClientIdLookupStrategy(final String clientID) {
+        Function<MessageContext, ClientID> function = Mockito.mock(Function.class);
+        Mockito.when(function.apply(Mockito.any())).thenReturn(clientID == null ? null : new ClientID(clientID));
+        return function;
+    }
+}

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


More information about the commits mailing list