[java-idp-oidc] branch main updated: JOIDC-128 - Support OAuth authorization requests

Henri Mikkonen henri.mikkonen at iki.fi
Tue Nov 22 13:03:52 UTC 2022


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=97355947f0ae62afdccc7dc7300dce7258b0f884

The following commit(s) were added to refs/heads/main by this push:
     new 97355947 JOIDC-128 - Support OAuth authorization requests
97355947 is described below

commit 97355947f0ae62afdccc7dc7300dce7258b0f884
Author: Henri Mikkonen <henri.mikkonen at iki.fi>
AuthorDate: Tue Nov 22 15:00:39 2022 +0200

    JOIDC-128 - Support OAuth authorization requests
    
    https://shibboleth.atlassian.net/browse/JOIDC-128
    
    Included new action to the authorize-flow: ValidateAuthorizationRequestType.
    It can be used for validating the inbound authorization requests via configurable
    strategy. By default the strategy requires the use of OIDC authentication requests.
    
    New configuration properties:
    - idp.oauth2.requireAuthenticationRequestPredicate (defaults to shibboleth.Conditions.TRUE)
    - idp.oauth2.authorizationRequestTypeValidationStrategy (defaults to a predicate exploiting property above)
---
 .../impl/ValidateAuthorizationRequestType.java     |  77 ++++++++++++++++
 ...AuthorizationRequestTypeValidationStrategy.java |  83 +++++++++++++++++
 .../idp/flows/oidc/authorize/authorize-beans.xml   |   9 ++
 .../idp/flows/oidc/authorize/authorize-flow.xml    |   1 +
 .../idp/plugin/oidc/op/conf/oidc.properties        |   5 +
 .../impl/ValidateAuthorizationRequestTypeTest.java | 102 +++++++++++++++++++++
 ...orizationRequestTypeValidationStrategyTest.java |  83 +++++++++++++++++
 .../src/test/resources/conf/oidc.properties        |   1 +
 8 files changed, 361 insertions(+)

diff --git a/idp-oidc-extension-impl/src/main/java/net/shibboleth/idp/plugin/oidc/op/oauth2/profile/impl/ValidateAuthorizationRequestType.java b/idp-oidc-extension-impl/src/main/java/net/shibboleth/idp/plugin/oidc/op/oauth2/profile/impl/ValidateAuthorizationRequestType.java
new file mode 100644
index 00000000..f885de24
--- /dev/null
+++ b/idp-oidc-extension-impl/src/main/java/net/shibboleth/idp/plugin/oidc/op/oauth2/profile/impl/ValidateAuthorizationRequestType.java
@@ -0,0 +1,77 @@
+/*
+ * 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 net.shibboleth.idp.plugin.oidc.op.oauth2.profile.impl;
+
+import java.util.function.Predicate;
+
+import javax.annotation.Nonnull;
+
+import org.opensaml.profile.action.ActionSupport;
+import org.opensaml.profile.action.EventIds;
+import org.opensaml.profile.context.ProfileRequestContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import net.shibboleth.idp.plugin.oidc.op.profile.logic.DefaultAuthorizationRequestTypeValidationStrategy;
+import net.shibboleth.utilities.java.support.component.ComponentSupport;
+import net.shibboleth.utilities.java.support.logic.Constraint;
+
+/**
+ * An action that validates the incoming OAuth2 authorization request via configurable strategy.
+ */
+public class ValidateAuthorizationRequestType extends AbstractOAuthAuthorizationRequestAction {
+
+    /** Class logger. */
+    @Nonnull private Logger log = LoggerFactory.getLogger(ValidateAuthorizationRequestType.class);
+
+    /**
+     * Strategy used to validate the inbound message ({@link AuthorizationRequest}) type.
+     */
+    @Nonnull private Predicate<ProfileRequestContext> authorizationRequestTypeValidationStrategy;
+
+    /**
+     * Constructor.
+     */
+    public ValidateAuthorizationRequestType() {
+        authorizationRequestTypeValidationStrategy = new DefaultAuthorizationRequestTypeValidationStrategy();
+    }
+
+    /**
+     * Set the strategy used to validate the inbound message ({@link AuthorizationRequest}) type.
+     *
+     * @param strategy strategy used to validate the inbound message ({@link AuthorizationRequest}) type.
+     */
+    public void setAuthorizationRequestTypeValidationStrategy(@Nonnull final Predicate<ProfileRequestContext> strategy) {
+        ComponentSupport.ifInitializedThrowUnmodifiabledComponentException(this);
+
+        authorizationRequestTypeValidationStrategy = Constraint.isNotNull(strategy,
+                "Authorization request type validation strategy cannot be null");
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    protected void doExecute(@Nonnull final ProfileRequestContext profileRequestContext) {
+        if (!authorizationRequestTypeValidationStrategy.test(profileRequestContext)) {
+            log.warn("{} The request did not match with the configured authorization request type requirements",
+                    getLogPrefix());
+            ActionSupport.buildEvent(profileRequestContext, EventIds.INVALID_MESSAGE);
+            return;            
+        }
+    }
+
+}
\ No newline at end of file
diff --git a/idp-oidc-extension-impl/src/main/java/net/shibboleth/idp/plugin/oidc/op/profile/logic/DefaultAuthorizationRequestTypeValidationStrategy.java b/idp-oidc-extension-impl/src/main/java/net/shibboleth/idp/plugin/oidc/op/profile/logic/DefaultAuthorizationRequestTypeValidationStrategy.java
new file mode 100644
index 00000000..20773bec
--- /dev/null
+++ b/idp-oidc-extension-impl/src/main/java/net/shibboleth/idp/plugin/oidc/op/profile/logic/DefaultAuthorizationRequestTypeValidationStrategy.java
@@ -0,0 +1,83 @@
+/*
+ * 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 net.shibboleth.idp.plugin.oidc.op.profile.logic;
+
+import java.util.function.Predicate;
+
+import javax.annotation.Nonnull;
+
+import org.opensaml.messaging.context.MessageContext;
+import org.opensaml.profile.context.ProfileRequestContext;
+
+import com.google.common.base.Predicates;
+import com.nimbusds.oauth2.sdk.AuthorizationRequest;
+import com.nimbusds.openid.connect.sdk.AuthenticationRequest;
+
+import net.shibboleth.utilities.java.support.logic.Constraint;
+
+/**
+ * Validation strategy for the inbound message. This predicate returns true if the message is an instance of
+ * {@link AuthorizationRequest} and the configurable authentication request requirement predicate returns false.
+ * If the configurable predicate returns true, then the message is required to be an instance of
+ * {@link AuthenticationRequest}.
+ */
+public class DefaultAuthorizationRequestTypeValidationStrategy implements Predicate<ProfileRequestContext> {
+
+    /**
+     * Predicate used to indicate if the incoming message is required to be an OIDC authentication request.
+     */
+    @Nonnull private Predicate<ProfileRequestContext> requireAuthenticationRequest;
+
+    /**
+     * Constructor.
+     */
+    public DefaultAuthorizationRequestTypeValidationStrategy() {
+        requireAuthenticationRequest = Predicates.alwaysTrue();
+    }
+
+    /**
+     * Set the predicate used to indicate if the incoming message is required to be an OIDC authentication request.
+     *
+     * @param predicate What to set.
+     */
+    public void setRequireAuthenticationRequestPredicate(@Nonnull final Predicate<ProfileRequestContext> predicate) {
+        requireAuthenticationRequest = Constraint.isNotNull(predicate,
+                "Require authentication request predicate cannot be null");
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public boolean test(@Nonnull final ProfileRequestContext input) {
+        final MessageContext messageContext = input.getInboundMessageContext();
+        if (messageContext == null || messageContext.getMessage() == null) {
+            return false;
+        }
+        final Object message = messageContext.getMessage();
+        if (message instanceof AuthorizationRequest) {
+            if (requireAuthenticationRequest.test(input)) {
+                if (message instanceof AuthenticationRequest) {
+                    return true;
+                }
+                return false;
+            }
+            return true;
+        }
+        return false;
+    }
+
+}
diff --git a/idp-oidc-extension-impl/src/main/resources/META-INF/net/shibboleth/idp/flows/oidc/authorize/authorize-beans.xml b/idp-oidc-extension-impl/src/main/resources/META-INF/net/shibboleth/idp/flows/oidc/authorize/authorize-beans.xml
index 054eab3b..b4a23762 100644
--- a/idp-oidc-extension-impl/src/main/resources/META-INF/net/shibboleth/idp/flows/oidc/authorize/authorize-beans.xml
+++ b/idp-oidc-extension-impl/src/main/resources/META-INF/net/shibboleth/idp/flows/oidc/authorize/authorize-beans.xml
@@ -67,6 +67,15 @@
         </property>
     </bean>
 
+    <bean id="ValidateAuthorizationRequestType"
+        class="net.shibboleth.idp.plugin.oidc.op.oauth2.profile.impl.ValidateAuthorizationRequestType"
+        scope="prototype"
+        p:authorizationRequestTypeValidationStrategy-ref="#{'%{idp.oauth2.authorizationRequestTypeValidationStrategy:DefaultAuthorizationRequestTypeValidationStrategy}'.trim()}" />
+
+    <bean id="DefaultAuthorizationRequestTypeValidationStrategy"
+        class="net.shibboleth.idp.plugin.oidc.op.profile.logic.DefaultAuthorizationRequestTypeValidationStrategy"
+        p:requireAuthenticationRequestPredicate-ref="#{'%{idp.oauth2.requireAuthenticationRequestPredicate:shibboleth.Conditions.TRUE}'.trim()}" />
+
     <bean id="InitializeOutboundMessageContext"
         class="net.shibboleth.idp.plugin.oidc.op.profile.impl.InitializeOutboundAuthenticationResponseMessageContext"
         scope="prototype" />
diff --git a/idp-oidc-extension-impl/src/main/resources/META-INF/net/shibboleth/idp/flows/oidc/authorize/authorize-flow.xml b/idp-oidc-extension-impl/src/main/resources/META-INF/net/shibboleth/idp/flows/oidc/authorize/authorize-flow.xml
index 33be18f7..d38a5145 100644
--- a/idp-oidc-extension-impl/src/main/resources/META-INF/net/shibboleth/idp/flows/oidc/authorize/authorize-flow.xml
+++ b/idp-oidc-extension-impl/src/main/resources/META-INF/net/shibboleth/idp/flows/oidc/authorize/authorize-flow.xml
@@ -54,6 +54,7 @@
     </subflow-state>
 
     <action-state id="OutboundContextsAndSecurityParameters">
+        <evaluate expression="ValidateAuthorizationRequestType" />
         <evaluate expression="InitializeOutboundMessageContext" />
         <evaluate expression="SetRequestObjectToResponseContext" />
         <evaluate expression="PopulateRequestObjectDecryptionParameters" />
diff --git a/idp-oidc-extension-impl/src/main/resources/net/shibboleth/idp/plugin/oidc/op/conf/oidc.properties b/idp-oidc-extension-impl/src/main/resources/net/shibboleth/idp/plugin/oidc/op/conf/oidc.properties
index 00b4dc6e..20a41642 100644
--- a/idp-oidc-extension-impl/src/main/resources/net/shibboleth/idp/plugin/oidc/op/conf/oidc.properties
+++ b/idp-oidc-extension-impl/src/main/resources/net/shibboleth/idp/plugin/oidc/op/conf/oidc.properties
@@ -124,6 +124,11 @@ idp.oidc.subject.salt = this_too_should_be_ch4ng3d
 #
 # OAuth2 Settings - these typically involve generic OAuth 2.0 use cases
 #
+# Defaults to always requiring OIDC authentication request as inbound message in the authorization endpoint.
+# Set to 'shibboleth.Conditions.FALSE' to always allow plain OAuth2 authorization requests
+#idp.oauth2.requireAuthenticationRequestPredicate = 
+# Default predicate exploits the configuration property above to control whether or not to require OIDC authentication request.
+#idp.oauth2.authorizationRequestTypeValidationStrategy = 
 
 # Supported grant_type values for token requests
 #idp.oauth2.grantTypes = authorization_code,refresh_token
diff --git a/idp-oidc-extension-impl/src/test/java/net/shibboleth/idp/plugin/oidc/op/oauth2/profile/impl/ValidateAuthorizationRequestTypeTest.java b/idp-oidc-extension-impl/src/test/java/net/shibboleth/idp/plugin/oidc/op/oauth2/profile/impl/ValidateAuthorizationRequestTypeTest.java
new file mode 100644
index 00000000..beccac7e
--- /dev/null
+++ b/idp-oidc-extension-impl/src/test/java/net/shibboleth/idp/plugin/oidc/op/oauth2/profile/impl/ValidateAuthorizationRequestTypeTest.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 net.shibboleth.idp.plugin.oidc.op.oauth2.profile.impl;
+
+import java.util.function.Predicate;
+
+import org.mockito.Mockito;
+import org.opensaml.messaging.context.MessageContext;
+import org.opensaml.profile.action.EventIds;
+import org.opensaml.profile.context.ProfileRequestContext;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+import com.nimbusds.oauth2.sdk.AuthorizationRequest;
+import com.nimbusds.openid.connect.sdk.AuthenticationRequest;
+
+import net.shibboleth.idp.plugin.oidc.op.profile.impl.BaseOIDCResponseActionTest;
+import net.shibboleth.idp.profile.testing.ActionTestingSupport;
+import net.shibboleth.utilities.java.support.component.ComponentInitializationException;
+import net.shibboleth.utilities.java.support.component.UnmodifiableComponentException;
+
+/**
+ * Unit tests for {@link ValidateAuthorizationRequestType}.
+ */
+public class ValidateAuthorizationRequestTypeTest extends BaseOIDCResponseActionTest {
+
+    ValidateAuthorizationRequestType action;
+
+    public void init() {
+        init(null);
+    }
+
+    public void init(final Predicate<ProfileRequestContext> predicate) {
+        action = new ValidateAuthorizationRequestType();
+        if (predicate != null) {
+            action.setAuthorizationRequestTypeValidationStrategy(predicate);
+        }
+        try {
+            action.initialize();
+        } catch (final ComponentInitializationException e) {
+            Assert.fail();
+        }
+    }
+
+    @Test(expectedExceptions = UnmodifiableComponentException.class)
+    public void setAuthorizationRequestTypeValidationStrategy_throwsIfInitialized() {
+        init();
+        action.setAuthorizationRequestTypeValidationStrategy(prc -> true);
+    }
+
+    @Test
+    public void execute_oidcAuthenticationRequestShouldReturnProceed() {
+        final MessageContext messageCtx = new MessageContext();
+        messageCtx.setMessage(Mockito.mock(AuthenticationRequest.class));
+        profileRequestCtx.setInboundMessageContext(messageCtx);
+        init();
+        ActionTestingSupport.assertProceedEvent(action.execute(requestCtx));
+    }
+
+    @Test
+    public void execute_oidcAuthenticationRequestShouldReturnInvalidMessageWithFalsePredicate() {
+        final MessageContext messageCtx = new MessageContext();
+        messageCtx.setMessage(Mockito.mock(AuthenticationRequest.class));
+        profileRequestCtx.setInboundMessageContext(messageCtx);
+        init(prc -> false);
+        ActionTestingSupport.assertEvent(action.execute(requestCtx), EventIds.INVALID_MESSAGE);
+    }
+
+    @Test
+    public void execute_oidcAuthorizationRequestShouldReturnInvalidMessage() {
+        final MessageContext messageCtx = new MessageContext();
+        messageCtx.setMessage(Mockito.mock(AuthorizationRequest.class));
+        profileRequestCtx.setInboundMessageContext(messageCtx);
+        init();
+        ActionTestingSupport.assertEvent(action.execute(requestCtx), EventIds.INVALID_MESSAGE);
+    }
+
+    @Test
+    public void execute_oidcAuthorizationRequestShouldReturnProceedWithTruePredicate() {
+        final MessageContext messageCtx = new MessageContext();
+        messageCtx.setMessage(Mockito.mock(AuthorizationRequest.class));
+        profileRequestCtx.setInboundMessageContext(messageCtx);
+        init(prc -> true);
+        ActionTestingSupport.assertProceedEvent(action.execute(requestCtx));
+    }
+
+}
diff --git a/idp-oidc-extension-impl/src/test/java/net/shibboleth/idp/plugin/oidc/op/profile/logic/DefaultAuthorizationRequestTypeValidationStrategyTest.java b/idp-oidc-extension-impl/src/test/java/net/shibboleth/idp/plugin/oidc/op/profile/logic/DefaultAuthorizationRequestTypeValidationStrategyTest.java
new file mode 100644
index 00000000..454ae9b5
--- /dev/null
+++ b/idp-oidc-extension-impl/src/test/java/net/shibboleth/idp/plugin/oidc/op/profile/logic/DefaultAuthorizationRequestTypeValidationStrategyTest.java
@@ -0,0 +1,83 @@
+/*
+ * 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 net.shibboleth.idp.plugin.oidc.op.profile.logic;
+
+import org.mockito.Mockito;
+import org.opensaml.messaging.context.MessageContext;
+import org.opensaml.profile.context.ProfileRequestContext;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+import com.nimbusds.oauth2.sdk.AuthorizationRequest;
+import com.nimbusds.openid.connect.sdk.AuthenticationRequest;
+
+/**
+ * Unit tests for {@link DefaultAuthorizationRequestTypeValidationStrategy}.
+ */
+public class DefaultAuthorizationRequestTypeValidationStrategyTest {
+
+    @Test
+    public void test_default_oidcAuthenticationRequestReturnsTrue() {
+        Assert.assertTrue(new DefaultAuthorizationRequestTypeValidationStrategy().test(prcWithOIDCRequest()));
+    }
+
+    @Test
+    public void test_default_oauthAuthorizationRequestReturnsFalse() {
+        Assert.assertFalse(new DefaultAuthorizationRequestTypeValidationStrategy().test(prcWithOAuthRequest()));
+    }
+
+    @Test
+    public void test_predicateSetToTrue() {
+        final DefaultAuthorizationRequestTypeValidationStrategy strategy =
+                new DefaultAuthorizationRequestTypeValidationStrategy();
+        strategy.setRequireAuthenticationRequestPredicate(prc -> true);
+        Assert.assertTrue(strategy.test(prcWithOIDCRequest()));
+        Assert.assertFalse(strategy.test(prcWithOAuthRequest()));
+        Assert.assertFalse(strategy.test(prcWithRawObject()));
+    }
+
+    @Test
+    public void test_predicateSetToFalse() {
+        final DefaultAuthorizationRequestTypeValidationStrategy strategy =
+                new DefaultAuthorizationRequestTypeValidationStrategy();
+        strategy.setRequireAuthenticationRequestPredicate(prc -> false);
+        Assert.assertTrue(strategy.test(prcWithOIDCRequest()));
+        Assert.assertTrue(strategy.test(prcWithOAuthRequest()));
+        Assert.assertFalse(strategy.test(prcWithRawObject()));
+    }
+
+    private static ProfileRequestContext prcWithOIDCRequest() {
+        return prcWithMessage(Mockito.mock(AuthenticationRequest.class));
+    }
+
+    private static ProfileRequestContext prcWithOAuthRequest() {
+        return prcWithMessage(Mockito.mock(AuthorizationRequest.class));
+    }
+
+    private static ProfileRequestContext prcWithRawObject() {
+        return prcWithMessage(new Object());
+    }
+
+    private static ProfileRequestContext prcWithMessage(final Object message) {
+        final ProfileRequestContext prc = new ProfileRequestContext();
+        final MessageContext messageCtx = new MessageContext();
+        messageCtx.setMessage(message);
+        prc.setInboundMessageContext(messageCtx);
+        return prc;
+    }
+}
diff --git a/idp-oidc-extension-impl/src/test/resources/conf/oidc.properties b/idp-oidc-extension-impl/src/test/resources/conf/oidc.properties
index b5bea8ba..e341dcf4 100644
--- a/idp-oidc-extension-impl/src/test/resources/conf/oidc.properties
+++ b/idp-oidc-extension-impl/src/test/resources/conf/oidc.properties
@@ -14,6 +14,7 @@ idp.oidc.admin.registration.clientIdPolicy = AccessByIPAddress
 
 idp.oidc.dynreg.defaultMetadataPolicyFile = src/test/resources/conf/metadata-policy1.json
 
+idp.oauth2.requireAuthenticationRequestPredicate = shibboleth.Conditions.FALSE
 idp.oauth2.grantTypes = authorization_code,refresh_token,client_credentials
 idp.oauth2.defaultAllowedAudience = https://rp.example.org
 

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


More information about the commits mailing list