From c0e8c18aa13e5658782621966a8117e1a779c9c7 Mon Sep 17 00:00:00 2001 From: Lashini Jayasekara <30428591+lashinijay@users.noreply.github.com> Date: Tue, 17 Dec 2024 09:51:34 +0530 Subject: [PATCH] Revert "[Spring Cleanup] Remove spring dependency in client authn filter (#2635)" This reverts commit 305c1c59eba8861e2eedb4eda9ea75165b24f92d. --- .../pom.xml | 5 +++ .../filter/OAuthClientAuthenticatorProxy.java | 16 +++++++- .../OAuthClientAuthnServiceFactory.java | 38 +++++++++++-------- 3 files changed, 42 insertions(+), 17 deletions(-) diff --git a/components/org.wso2.carbon.identity.oauth.client.authn.filter/pom.xml b/components/org.wso2.carbon.identity.oauth.client.authn.filter/pom.xml index 450389e631d..45bf9342841 100644 --- a/components/org.wso2.carbon.identity.oauth.client.authn.filter/pom.xml +++ b/components/org.wso2.carbon.identity.oauth.client.authn.filter/pom.xml @@ -45,6 +45,11 @@ org.wso2.carbon.identity.inbound.auth.oauth2 org.wso2.carbon.identity.oauth + + org.springframework + spring-web + provided + diff --git a/components/org.wso2.carbon.identity.oauth.client.authn.filter/src/main/java/org/wso2/carbon/identity/oauth/client/authn/filter/OAuthClientAuthenticatorProxy.java b/components/org.wso2.carbon.identity.oauth.client.authn.filter/src/main/java/org/wso2/carbon/identity/oauth/client/authn/filter/OAuthClientAuthenticatorProxy.java index d1229ed2296..cad84aadd7f 100644 --- a/components/org.wso2.carbon.identity.oauth.client.authn.filter/src/main/java/org/wso2/carbon/identity/oauth/client/authn/filter/OAuthClientAuthenticatorProxy.java +++ b/components/org.wso2.carbon.identity.oauth.client.authn.filter/src/main/java/org/wso2/carbon/identity/oauth/client/authn/filter/OAuthClientAuthenticatorProxy.java @@ -30,6 +30,7 @@ import org.wso2.carbon.identity.oauth.common.OAuth2ErrorCodes; import org.wso2.carbon.identity.oauth.common.OAuthConstants; import org.wso2.carbon.identity.oauth2.bean.OAuthClientAuthnContext; +import org.wso2.carbon.identity.oauth2.client.authentication.OAuthClientAuthnService; import java.util.Arrays; import java.util.HashMap; @@ -50,6 +51,7 @@ public class OAuthClientAuthenticatorProxy extends AbstractPhaseInterceptor PROXY_ENDPOINT_LIST = Arrays.asList("/oauth2/token", "/oauth2/revoke", "/oauth2/device_authorize", "/oauth2/ciba", "/oauth2/par", "/oauth2/authorize"); + private OAuthClientAuthnService oAuthClientAuthnService; private static final String SLASH = "/"; public OAuthClientAuthenticatorProxy() { @@ -58,6 +60,16 @@ public OAuthClientAuthenticatorProxy() { super(Phase.PRE_INVOKE); } + public OAuthClientAuthnService getOAuthClientAuthnService() { + + return oAuthClientAuthnService; + } + + public void setOAuthClientAuthnService(OAuthClientAuthnService oAuthClientAuthnService) { + + this.oAuthClientAuthnService = oAuthClientAuthnService; + } + /** * Handles the incoming JAX-RS message for the purpose of OAuth2 client authentication. * @@ -70,8 +82,8 @@ public void handleMessage(Message message) { HttpServletRequest request = ((HttpServletRequest) message.get(HTTP_REQUEST)); if (canHandle(message)) { try { - OAuthClientAuthnContext oAuthClientAuthnContext = OAuthClientAuthnServiceFactory - .getOAuthClientAuthnService().authenticateClient(request, bodyContentParams); + OAuthClientAuthnContext oAuthClientAuthnContext = oAuthClientAuthnService + .authenticateClient(request, bodyContentParams); if (!oAuthClientAuthnContext.isPreviousAuthenticatorEngaged()) { /* If the previous authenticator is not engaged it means that either client authentication flow failed or no supported authenticaiton mechanism was found.If the error details are already diff --git a/components/org.wso2.carbon.identity.oauth.client.authn.filter/src/main/java/org/wso2/carbon/identity/oauth/client/authn/filter/OAuthClientAuthnServiceFactory.java b/components/org.wso2.carbon.identity.oauth.client.authn.filter/src/main/java/org/wso2/carbon/identity/oauth/client/authn/filter/OAuthClientAuthnServiceFactory.java index 8bb57472b36..8cf4955e76d 100644 --- a/components/org.wso2.carbon.identity.oauth.client.authn.filter/src/main/java/org/wso2/carbon/identity/oauth/client/authn/filter/OAuthClientAuthnServiceFactory.java +++ b/components/org.wso2.carbon.identity.oauth.client.authn.filter/src/main/java/org/wso2/carbon/identity/oauth/client/authn/filter/OAuthClientAuthnServiceFactory.java @@ -1,7 +1,7 @@ /* - * Copyright (c) 2019-2024, WSO2 LLC. (http://www.wso2.com). + * Copyright (c) 2019, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. * - * WSO2 LLC. licenses this file to you under the Apache License, + * WSO2 Inc. 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 @@ -18,29 +18,37 @@ package org.wso2.carbon.identity.oauth.client.authn.filter; +import org.springframework.beans.factory.config.AbstractFactoryBean; import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.identity.oauth2.client.authentication.OAuthClientAuthnService; /** - * Factory class to get OAuthClientAuthnService OSGI service. + * Factory Beans serves as a factory for creating other beans within the IOC container. This factory bean is used to + * instantiate the OAuthClientAuthnService type of object inside the container. */ -public class OAuthClientAuthnServiceFactory { +public class OAuthClientAuthnServiceFactory extends AbstractFactoryBean { - private static final OAuthClientAuthnService SERVICE; + public OAuthClientAuthnService oAuthClientAuthnService; - static { - OAuthClientAuthnService oAuthClientAuthnService = (OAuthClientAuthnService) PrivilegedCarbonContext - .getThreadLocalCarbonContext().getOSGiService(OAuthClientAuthnService.class, null); - if (oAuthClientAuthnService == null) { - throw new IllegalStateException("OAuthClientAuthnService is not available from OSGI context."); - } + @Override + public Class getObjectType() { - SERVICE = oAuthClientAuthnService; + return OAuthClientAuthnService.class; } - public static OAuthClientAuthnService getOAuthClientAuthnService() { - - return SERVICE; + @Override + protected OAuthClientAuthnService createInstance() throws Exception { + + if (this.oAuthClientAuthnService != null) { + return this.oAuthClientAuthnService; + } else { + OAuthClientAuthnService oAuthClientAuthnService = (OAuthClientAuthnService) PrivilegedCarbonContext + .getThreadLocalCarbonContext().getOSGiService(OAuthClientAuthnService.class, null); + if (oAuthClientAuthnService != null) { + this.oAuthClientAuthnService = oAuthClientAuthnService; + } + return oAuthClientAuthnService; + } } }