Skip to content

Commit

Permalink
Use SAMLSSOServiceProviderManager OSGI service
Browse files Browse the repository at this point in the history
  • Loading branch information
hwupathum committed Jun 22, 2023
1 parent 2a0b200 commit 7123cb3
Show file tree
Hide file tree
Showing 14 changed files with 185 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;
import org.osgi.service.component.annotations.ReferencePolicy;
import org.wso2.carbon.identity.core.SAMLSSOServiceProviderManager;
import org.wso2.carbon.user.core.service.RealmService;

/**
Expand All @@ -41,6 +42,8 @@ public class SAMLQueryServiceComponent {

private static RealmService realmservice = null;

private static SAMLSSOServiceProviderManager samlSSOServiceProviderManager = null;

/**
* This method is used to get created realm service
*
Expand Down Expand Up @@ -106,4 +109,46 @@ protected void unsetRealmService(RealmService realmService) {
log.debug("DefaultUserRealm unset in to bundle");
}
}

/**
* This method is used to set SAMLSSOServiceProviderManager instance
*
* @param samlSSOServiceProviderManager SAMLSSOServiceProviderManager instance
*/
@Reference(
name = "saml.sso.service.provider.manager",
service = SAMLSSOServiceProviderManager.class,
cardinality = ReferenceCardinality.MANDATORY,
policy = ReferencePolicy.DYNAMIC,
unbind = "unsetSAMLSSOServiceProviderManager")
protected void setSAMLSSOServiceProviderManager(SAMLSSOServiceProviderManager samlSSOServiceProviderManager) {

this.samlSSOServiceProviderManager = samlSSOServiceProviderManager;
if (log.isDebugEnabled()) {
log.debug("SAMLSSOServiceProviderManager set in to bundle");
}
}

/**
* This method is used to un-set SAMLSSOServiceProviderManager instance
*
* @param samlSSOServiceProviderManager SAMLSSOServiceProviderManager instance
*/
protected void unsetSAMLSSOServiceProviderManager(SAMLSSOServiceProviderManager samlSSOServiceProviderManager) {

this.samlSSOServiceProviderManager = null;
if (log.isDebugEnabled()) {
log.debug("SAMLSSOServiceProviderManager unset in to bundle");
}
}

/**
* This method is used to get SAMLSSOServiceProviderManager instance
*
* @return SAMLSSOServiceProviderManager instance
*/
public static SAMLSSOServiceProviderManager getSAMLSSOServiceProviderManager() {

return samlSSOServiceProviderManager;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@
import org.w3c.dom.ls.LSSerializer;
import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.identity.base.IdentityException;
import org.wso2.carbon.identity.core.SAMLSSOServiceProviderManager;
import org.wso2.carbon.identity.core.model.SAMLSSOServiceProviderDO;
import org.wso2.carbon.identity.query.saml.SignKeyDataHolder;
import org.wso2.carbon.identity.query.saml.exception.IdentitySAML2QueryException;
import org.wso2.carbon.identity.query.saml.internal.SAMLQueryServiceComponent;
import org.wso2.carbon.identity.saml.common.util.SAMLInitializer;
import org.wso2.carbon.identity.sso.saml.SAMLSSOConstants;
import org.wso2.carbon.identity.sso.saml.SSOServiceProviderConfigManager;
Expand Down Expand Up @@ -211,7 +211,8 @@ public static SAMLSSOServiceProviderDO getServiceProviderConfig(String issuer)
SAMLSSOServiceProviderDO ssoIdpConfigs = idPConfigManager.getServiceProvider(issuer);
if (ssoIdpConfigs == null) {
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
ssoIdpConfigs = SAMLSSOServiceProviderManager.getInstance().getServiceProvider(issuer, tenantId);
ssoIdpConfigs = SAMLQueryServiceComponent.getSAMLSSOServiceProviderManager()
.getServiceProvider(issuer, tenantId);
}
return ssoIdpConfigs;
} catch (IdentityException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.core.util.KeyStoreManager;
import org.wso2.carbon.identity.base.IdentityException;
import org.wso2.carbon.identity.core.SAMLSSOServiceProviderManager;
import org.wso2.carbon.identity.core.model.SAMLSSOServiceProviderDO;
import org.wso2.carbon.identity.core.util.IdentityTenantUtil;
import org.wso2.carbon.identity.core.util.IdentityUtil;
Expand All @@ -38,6 +37,7 @@
import org.wso2.carbon.identity.sso.saml.dto.SAMLSSOServiceProviderInfoDTO;
import org.wso2.carbon.identity.sso.saml.exception.IdentitySAML2ClientException;
import org.wso2.carbon.identity.sso.saml.internal.IdentitySAMLSSOServiceComponent;
import org.wso2.carbon.identity.sso.saml.internal.IdentitySAMLSSOServiceComponentHolder;
import org.wso2.carbon.identity.sso.saml.util.SAMLSSOUtil;
import org.wso2.carbon.registry.core.Registry;
import org.wso2.carbon.registry.core.session.UserRegistry;
Expand Down Expand Up @@ -86,7 +86,7 @@ public boolean addRelyingPartyServiceProvider(SAMLSSOServiceProviderDTO serviceP
log.error(message);
return false;
}
return SAMLSSOServiceProviderManager.getInstance().addServiceProvider(serviceProviderDO, tenantId);
return IdentitySAMLSSOServiceComponentHolder.getInstance().getSAMLSSOServiceProviderManager().addServiceProvider(serviceProviderDO, tenantId);
} catch (IdentityException e) {
String message = "Error obtaining a registry for adding a new service provider";
throw new IdentityException(message, e);
Expand Down Expand Up @@ -130,7 +130,7 @@ private String getIssuerWithQualifier(SAMLSSOServiceProviderDO serviceProviderDO
private SAMLSSOServiceProviderDTO persistSAMLServiceProvider(SAMLSSOServiceProviderDO samlssoServiceProviderDO)
throws IdentityException {

boolean response = SAMLSSOServiceProviderManager.getInstance()
boolean response = IdentitySAMLSSOServiceComponentHolder.getInstance().getSAMLSSOServiceProviderManager()
.addServiceProvider(samlssoServiceProviderDO, tenantId);
if (response) {
return createSAMLSSOServiceProviderDTO(samlssoServiceProviderDO);
Expand Down Expand Up @@ -410,7 +410,7 @@ private SAMLSSOServiceProviderDTO createSAMLSSOServiceProviderDTO(SAMLSSOService
public SAMLSSOServiceProviderInfoDTO getServiceProviders() throws IdentityException {
SAMLSSOServiceProviderDTO[] serviceProviders = null;
try {
SAMLSSOServiceProviderDO[] providersSet = SAMLSSOServiceProviderManager.getInstance()
SAMLSSOServiceProviderDO[] providersSet = IdentitySAMLSSOServiceComponentHolder.getInstance().getSAMLSSOServiceProviderManager()
.getServiceProviders(tenantId);
serviceProviders = new SAMLSSOServiceProviderDTO[providersSet.length];

Expand Down Expand Up @@ -499,7 +499,7 @@ public SAMLSSOServiceProviderInfoDTO getServiceProviders() throws IdentityExcept
*/
public boolean removeServiceProvider(String issuer) throws IdentityException {
try {
return SAMLSSOServiceProviderManager.getInstance().removeServiceProvider(issuer, tenantId);
return IdentitySAMLSSOServiceComponentHolder.getInstance().getSAMLSSOServiceProviderManager().removeServiceProvider(issuer, tenantId);
} catch (IdentityException e) {
throw new IdentityException("Error removing a Service Provider with issuer: " + issuer, e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.wso2.carbon.identity.application.mgt.ApplicationManagementService;
import org.wso2.carbon.identity.application.mgt.listener.ApplicationMgtListener;
import org.wso2.carbon.identity.base.IdentityConstants;
import org.wso2.carbon.identity.core.SAMLSSOServiceProviderManager;
import org.wso2.carbon.identity.core.util.IdentityCoreInitializedEvent;
import org.wso2.carbon.identity.core.util.IdentityUtil;
import org.wso2.carbon.identity.event.handler.AbstractEventHandler;
Expand Down Expand Up @@ -458,4 +459,27 @@ protected void unsetApplicationManagementService(ApplicationManagementService ap
log.debug("Unset the ApplicationManagementService");
}
}


@Reference(
name = "saml.sso.service.provider.manager",
service = org.wso2.carbon.identity.core.SAMLSSOServiceProviderManager.class,
cardinality = ReferenceCardinality.MANDATORY,
policy = ReferencePolicy.DYNAMIC,
unbind = "unsetSAMLSSOServiceProviderManager")
protected void setSAMLSSOServiceProviderManager(SAMLSSOServiceProviderManager samlSSOServiceProviderManager) {

IdentitySAMLSSOServiceComponentHolder.getInstance().setSAMLSSOServiceProviderManager(samlSSOServiceProviderManager);
if (log.isDebugEnabled()) {
log.debug("SAMLSSOServiceProviderManager set in to bundle");
}
}

protected void unsetSAMLSSOServiceProviderManager(SAMLSSOServiceProviderManager samlSSOServiceProviderManager) {

IdentitySAMLSSOServiceComponentHolder.getInstance().setSAMLSSOServiceProviderManager(null);
if (log.isDebugEnabled()) {
log.debug("SAMLSSOServiceProviderManager unset in to bundle");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright (c) 2023, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 LLC. 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.wso2.carbon.identity.sso.saml.internal;

import org.wso2.carbon.identity.core.SAMLSSOServiceProviderManager;

/**
* Identity SAML SSO Service Component Holder.
*/
public class IdentitySAMLSSOServiceComponentHolder {

private SAMLSSOServiceProviderManager samlSSOServiceProviderManager;

private static final IdentitySAMLSSOServiceComponentHolder instance = new IdentitySAMLSSOServiceComponentHolder();

private IdentitySAMLSSOServiceComponentHolder() {

}

public static IdentitySAMLSSOServiceComponentHolder getInstance() {

return instance;
}

/**
* Set SAMLSSOServiceProviderManager.
*
* @param samlSSOServiceProviderManager SAMLSSOServiceProviderManager.
*/
public void setSAMLSSOServiceProviderManager(SAMLSSOServiceProviderManager samlSSOServiceProviderManager) {
this.samlSSOServiceProviderManager = samlSSOServiceProviderManager;
}

/**
* Get SAMLSSOServiceProviderManager.
*
* @return SAMLSSOServiceProviderManager.
*/
public SAMLSSOServiceProviderManager getSAMLSSOServiceProviderManager() {
return samlSSOServiceProviderManager;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.opensaml.saml.saml2.core.Response;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.identity.base.IdentityException;
import org.wso2.carbon.identity.core.SAMLSSOServiceProviderManager;
import org.wso2.carbon.identity.core.model.SAMLSSOServiceProviderDO;
import org.wso2.carbon.identity.sso.saml.SAMLSSOConstants;
import org.wso2.carbon.identity.sso.saml.SSOServiceProviderConfigManager;
Expand All @@ -32,6 +31,7 @@
import org.wso2.carbon.identity.sso.saml.builders.SAMLArtifactBuilder;
import org.wso2.carbon.identity.sso.saml.dto.SAMLSSOAuthnReqDTO;
import org.wso2.carbon.identity.sso.saml.dto.SAMLSSORespDTO;
import org.wso2.carbon.identity.sso.saml.internal.IdentitySAMLSSOServiceComponentHolder;
import org.wso2.carbon.identity.sso.saml.session.SSOSessionPersistenceManager;
import org.wso2.carbon.identity.sso.saml.util.SAMLSSOUtil;
import org.wso2.carbon.registry.core.utils.UUIDGenerator;
Expand Down Expand Up @@ -231,7 +231,7 @@ private SAMLSSOServiceProviderDO getServiceProviderConfig(SAMLSSOAuthnReqDTO aut
.getServiceProvider(authnReqDTO.getIssuer());
if (ssoIdpConfigs == null) {
int tenantID = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
ssoIdpConfigs = SAMLSSOServiceProviderManager.getInstance()
ssoIdpConfigs = IdentitySAMLSSOServiceComponentHolder.getInstance().getSAMLSSOServiceProviderManager()
.getServiceProvider(authnReqDTO.getIssuer(), tenantID);
authnReqDTO.setStratosDeployment(false); // not stratos
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.identity.application.common.util.IdentityApplicationManagementUtil;
import org.wso2.carbon.identity.base.IdentityException;
import org.wso2.carbon.identity.core.SAMLSSOServiceProviderManager;
import org.wso2.carbon.identity.core.model.SAMLSSOServiceProviderDO;
import org.wso2.carbon.identity.core.util.IdentityTenantUtil;
import org.wso2.carbon.identity.core.util.IdentityUtil;
import org.wso2.carbon.identity.sso.saml.SAMLSSOConstants;
import org.wso2.carbon.identity.sso.saml.SSOServiceProviderConfigManager;
import org.wso2.carbon.identity.sso.saml.builders.SingleLogoutMessageBuilder;
import org.wso2.carbon.identity.sso.saml.dto.SAMLSSOReqValidationResponseDTO;
import org.wso2.carbon.identity.sso.saml.internal.IdentitySAMLSSOServiceComponentHolder;
import org.wso2.carbon.identity.sso.saml.session.SSOSessionPersistenceManager;
import org.wso2.carbon.identity.sso.saml.session.SessionInfoData;
import org.wso2.carbon.identity.sso.saml.util.LambdaExceptionUtils;
Expand Down Expand Up @@ -269,7 +269,8 @@ private SAMLSSOServiceProviderDO getServiceProviderConfig(String issuer, String
privilegedCarbonContext.setTenantId(tenantId);
privilegedCarbonContext.setTenantDomain(tenantDomain);
IdentityTenantUtil.initializeRegistry(tenantId, tenantDomain);
ssoIdpConfigs = SAMLSSOServiceProviderManager.getInstance().getServiceProvider(issuer, tenantId);
ssoIdpConfigs = IdentitySAMLSSOServiceComponentHolder.getInstance().getSAMLSSOServiceProviderManager()
.getServiceProvider(issuer, tenantId);
} finally {
PrivilegedCarbonContext.endTenantFlow();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.opensaml.saml.saml2.core.Response;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.identity.base.IdentityException;
import org.wso2.carbon.identity.core.SAMLSSOServiceProviderManager;
import org.wso2.carbon.identity.core.model.SAMLSSOServiceProviderDO;

import org.wso2.carbon.identity.core.util.IdentityTenantUtil;
Expand All @@ -35,6 +34,7 @@
import org.wso2.carbon.identity.sso.saml.builders.SAMLArtifactBuilder;
import org.wso2.carbon.identity.sso.saml.dto.SAMLSSOAuthnReqDTO;
import org.wso2.carbon.identity.sso.saml.dto.SAMLSSORespDTO;
import org.wso2.carbon.identity.sso.saml.internal.IdentitySAMLSSOServiceComponentHolder;
import org.wso2.carbon.identity.sso.saml.session.SSOSessionPersistenceManager;
import org.wso2.carbon.identity.sso.saml.util.SAMLSSOUtil;
import org.wso2.carbon.registry.core.utils.UUIDGenerator;
Expand Down Expand Up @@ -219,7 +219,7 @@ private SAMLSSOServiceProviderDO getServiceProviderConfig(SAMLSSOAuthnReqDTO aut
IdentityTenantUtil.initializeRegistry(PrivilegedCarbonContext.getThreadLocalCarbonContext()
.getTenantId(), PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain());
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
ssoIdpConfigs = SAMLSSOServiceProviderManager.getInstance()
ssoIdpConfigs = IdentitySAMLSSOServiceComponentHolder.getInstance().getSAMLSSOServiceProviderManager()
.getServiceProvider(authnReqDTO.getIssuer(), tenantId);
authnReqDTO.setStratosDeployment(false); // not stratos
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
import org.wso2.carbon.identity.application.mgt.ApplicationManagementService;
import org.wso2.carbon.identity.base.IdentityConstants;
import org.wso2.carbon.identity.base.IdentityException;
import org.wso2.carbon.identity.core.SAMLSSOServiceProviderManager;
import org.wso2.carbon.identity.core.ServiceURLBuilder;
import org.wso2.carbon.identity.core.URLBuilderException;
import org.wso2.carbon.identity.core.model.IdentityCookieConfig;
Expand Down Expand Up @@ -77,6 +76,7 @@
import org.wso2.carbon.identity.sso.saml.exception.IdentitySAML2ClientException;
import org.wso2.carbon.identity.sso.saml.exception.IdentitySAML2SSOException;
import org.wso2.carbon.identity.sso.saml.internal.IdentitySAMLSSOServiceComponent;
import org.wso2.carbon.identity.sso.saml.internal.IdentitySAMLSSOServiceComponentHolder;
import org.wso2.carbon.identity.sso.saml.session.SSOSessionPersistenceManager;
import org.wso2.carbon.identity.sso.saml.session.SessionInfoData;
import org.wso2.carbon.identity.sso.saml.util.SAMLSOAPUtils;
Expand Down Expand Up @@ -1798,8 +1798,8 @@ private SAMLSSOServiceProviderDO getServiceProviderConfig(SAMLSSOAuthnReqDTO aut
privilegedCarbonContext.setTenantId(tenantId);
privilegedCarbonContext.setTenantDomain(tenantDomain);

serviceProviderConfigs = SAMLSSOServiceProviderManager.getInstance()
.getServiceProvider(issuer, tenantId);
serviceProviderConfigs = IdentitySAMLSSOServiceComponentHolder.getInstance()
.getSAMLSSOServiceProviderManager().getServiceProvider(issuer, tenantId);
authnReqDTO.setStratosDeployment(false); // not stratos
} catch (IdentityException e) {
throw new IdentitySAML2SSOException("Error occurred while retrieving SAML service provider for "
Expand Down
Loading

0 comments on commit 7123cb3

Please sign in to comment.