From 3dd0690552de6267f3924bbfd9f9b11de18f75b1 Mon Sep 17 00:00:00 2001 From: Mohamed Rashd Date: Mon, 7 Mar 2022 21:58:43 +0530 Subject: [PATCH] Add identity server config Add api to create identity server Return List of identity servers withtout extra bean for response --- .../mgt/common/IdentityServer.java | 45 +++++---- .../mgt/common/IdentityServerList.java | 8 +- .../mgt/common/dto/IdentityServerDTO.java | 94 +++++++++++++++++++ .../common/services/SPApplicationManager.java | 6 +- .../mgt/core/config/ConfigurationManager.java | 18 ++++ .../config/IdentityServerConfiguration.java | 52 ++++++++++ .../mgt/core/config/IdentityServerDetail.java | 37 ++++++++ .../mgt/core/dao/SPApplicationDAO.java | 8 +- .../GenericApplicationDAOImpl.java | 4 - .../GenericSPApplicationDAOImpl.java | 46 ++++++++- .../OracleSPApplicationDAOImpl.java | 6 +- .../PostgreSQLSPApplicationDAOImpl.java | 6 +- .../SQLServerSPApplicationDAOImpl.java | 6 +- .../core/impl/SPApplicationManagerImpl.java | 27 ++++-- .../application/mgt/core/util/APIUtil.java | 20 ++++ .../application/mgt/core/util/Constants.java | 3 + .../application/mgt/core/util/DAOUtil.java | 40 ++++---- .../api/services/SPApplicationService.java | 24 +++++ .../impl/SPApplicationServiceImpl.java | 18 +++- .../util/SPAppRequestHandlerUtil.java | 7 +- 20 files changed, 404 insertions(+), 71 deletions(-) create mode 100644 components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/dto/IdentityServerDTO.java create mode 100644 components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/config/IdentityServerConfiguration.java create mode 100644 components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/config/IdentityServerDetail.java diff --git a/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/IdentityServer.java b/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/IdentityServer.java index 24a3cd7cf7..62daec2595 100644 --- a/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/IdentityServer.java +++ b/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/IdentityServer.java @@ -20,11 +20,12 @@ package io.entgra.application.mgt.common; public class IdentityServer { private int id; + private String providerName; private String name; private String description; private String url; - private String spAppsUri; - private String spAppsApi; + private String apiUrl; + private String serviceProviderAppsUrl; private String userName; private String password; @@ -60,22 +61,6 @@ public class IdentityServer { this.url = url; } - public String getSpAppsUri() { - return spAppsUri; - } - - public void setSpAppsURI(String spAppsUri) { - this.spAppsUri = spAppsUri; - } - - public String getSpAppsApi() { - return spAppsApi; - } - - public void setSpAppsApi(String spAppsApi) { - this.spAppsApi = spAppsApi; - } - public String getPassword() { return password; } @@ -91,4 +76,28 @@ public class IdentityServer { public void setUserName(String userName) { this.userName = userName; } + + public String getProviderName() { + return providerName; + } + + public void setProviderName(String providerName) { + this.providerName = providerName; + } + + public String getApiUrl() { + return apiUrl; + } + + public void setApiUrl(String apiUrl) { + this.apiUrl = apiUrl; + } + + public String getServiceProviderAppsUrl() { + return serviceProviderAppsUrl; + } + + public void setServiceProviderAppsUrl(String serviceProviderAppsUrl) { + this.serviceProviderAppsUrl = serviceProviderAppsUrl; + } } diff --git a/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/IdentityServerList.java b/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/IdentityServerList.java index a394458000..85a2ecdacf 100644 --- a/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/IdentityServerList.java +++ b/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/IdentityServerList.java @@ -18,16 +18,18 @@ package io.entgra.application.mgt.common; +import io.entgra.application.mgt.common.dto.IdentityServerDTO; + import java.util.List; public class IdentityServerList { - private List identityServers; + private List identityServers; - public List getIdentityServers() { + public List getIdentityServers() { return identityServers; } - public void setIdentityServers(List identityServers) { + public void setIdentityServers(List identityServers) { this.identityServers = identityServers; } } diff --git a/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/dto/IdentityServerDTO.java b/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/dto/IdentityServerDTO.java new file mode 100644 index 0000000000..aba002b76a --- /dev/null +++ b/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/dto/IdentityServerDTO.java @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2022, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * 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 + * + * 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 io.entgra.application.mgt.common.dto; + +public class IdentityServerDTO { + private int id; + private String providerName; + private String name; + private String description; + private String url; + private String apiUrl; + private String userName; + private String password; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getUserName() { + return userName; + } + + public void setUserName(String userName) { + this.userName = userName; + } + + public String getProviderName() { + return providerName; + } + + public void setProviderName(String providerName) { + this.providerName = providerName; + } + + public String getApiUrl() { + return apiUrl; + } + + public void setApiUrl(String apiUrl) { + this.apiUrl = apiUrl; + } +} diff --git a/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/services/SPApplicationManager.java b/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/services/SPApplicationManager.java index 94ee0cd0ac..78da335762 100644 --- a/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/services/SPApplicationManager.java +++ b/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/services/SPApplicationManager.java @@ -19,7 +19,7 @@ package io.entgra.application.mgt.common.services; import io.entgra.application.mgt.common.IdentityServer; -import io.entgra.application.mgt.common.IdentityServerList; +import io.entgra.application.mgt.common.dto.IdentityServerDTO; import io.entgra.application.mgt.common.SPApplication; import io.entgra.application.mgt.common.exception.ApplicationManagementException; import io.entgra.application.mgt.common.exception.RequestValidatingException; @@ -70,7 +70,9 @@ public interface SPApplicationManager { * @return Available identity servers * @throws ApplicationManagementException if error occurred while getting identity servers */ - IdentityServerList getIdentityServers() throws ApplicationManagementException; + List getIdentityServers() throws ApplicationManagementException; + + IdentityServer createIdentityServer(IdentityServerDTO identityServerDTO) throws ApplicationManagementException; /** * diff --git a/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/config/ConfigurationManager.java b/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/config/ConfigurationManager.java index ab85f87bb3..5887ef36ea 100644 --- a/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/config/ConfigurationManager.java +++ b/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/config/ConfigurationManager.java @@ -36,8 +36,12 @@ public class ConfigurationManager { private Configuration configuration; + private IdentityServerConfiguration identityServerConfiguration; + private static String configPath; + private static String identityServerConfigPath; + private static volatile ConfigurationManager configurationManager; private ConfigurationManager() { @@ -60,6 +64,14 @@ public class ConfigurationManager { return configurationManager; } + public static synchronized void setIdentityServerConfigPathConfigLocation(String configPath) throws InvalidConfigurationException { + if (identityServerConfigPath == null) { + identityServerConfigPath = configPath; + } else { + throw new InvalidConfigurationException("Configuration path " + configPath + " is already defined"); + } + } + public static synchronized void setConfigLocation(String configPath) throws InvalidConfigurationException { if (ConfigurationManager.configPath == null) { ConfigurationManager.configPath = configPath; @@ -74,9 +86,11 @@ public class ConfigurationManager { Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); if (configPath == null) { configPath = Constants.DEFAULT_CONFIG_FILE_LOCATION; + identityServerConfigPath = Constants.DEFAULT_IDENTITY_SERVERS_CONFIG_FILE_LOCATION; } //TODO: Add validation for the configurations this.configuration = (Configuration) unmarshaller.unmarshal(new File(configPath)); + this.identityServerConfiguration = (IdentityServerConfiguration) unmarshaller.unmarshal(new File(identityServerConfigPath)); } catch (Exception e) { log.error(e); throw new InvalidConfigurationException("Error occurred while initializing application config: " @@ -88,6 +102,10 @@ public class ConfigurationManager { return configuration; } + public IdentityServerConfiguration getIdentityServerConfiguration() { + return identityServerConfiguration; + } + public Extension getExtension(Extension.Name extName) throws InvalidConfigurationException { for (Extension extension : configuration.getExtensions()) { if (extension.getName().contentEquals(extName.toString())) { diff --git a/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/config/IdentityServerConfiguration.java b/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/config/IdentityServerConfiguration.java new file mode 100644 index 0000000000..fda9f8ef9f --- /dev/null +++ b/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/config/IdentityServerConfiguration.java @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2019, Entgra (pvt) Ltd. (http://entgra.io) All Rights Reserved. + * + * Entgra (pvt) Ltd. 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 io.entgra.application.mgt.core.config; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlElementWrapper; +import javax.xml.bind.annotation.XmlRootElement; +import java.util.List; + +/** + * Represents the Application Management Configuration. + */ +@XmlRootElement(name = "IdentityServerConfiguration") +public class IdentityServerConfiguration { + + private List identityServers; + + @XmlElementWrapper(name = "IdentityServers") + @XmlElement(name = "IdentityServerDTO") + public List getIdentityServers() { + return identityServers; + } + + public IdentityServerDetail getIdentityServerDetailByProviderName(String identityServerProviderName) { + for (IdentityServerDetail identityServerDetail : identityServers) { + if (identityServerDetail.getProviderName().equals(identityServerProviderName)) { + return identityServerDetail; + } + } + return null; + } + + public void setIdentityServers(List identityServers) { + this.identityServers = identityServers; + } +} + diff --git a/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/config/IdentityServerDetail.java b/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/config/IdentityServerDetail.java new file mode 100644 index 0000000000..a73b70f009 --- /dev/null +++ b/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/config/IdentityServerDetail.java @@ -0,0 +1,37 @@ +package io.entgra.application.mgt.core.config; + +import javax.xml.bind.annotation.XmlAttribute; + +public class IdentityServerDetail { + private String providerName; + private String serviceProvidersPageUri; + private String serviceProvidersAPIContextPath; + + @XmlAttribute(name = "ProviderName") + public String getProviderName() { + return providerName; + } + + public void setProviderName(String providerName) { + this.providerName = providerName; + } + + + @XmlAttribute(name = "ServiceProvidersPageUri") + public String getServiceProvidersPageUri() { + return serviceProvidersPageUri; + } + + public void setServiceProvidersPageUri(String serviceProvidersPageUri) { + this.serviceProvidersPageUri = serviceProvidersPageUri; + } + + @XmlAttribute(name = "ServiceProvidersAPIContextPath") + public String getServiceProvidersAPIContextPath() { + return serviceProvidersAPIContextPath; + } + + public void setServiceProvidersAPIContextPath(String serviceProvidersAPIContextPath) { + this.serviceProvidersAPIContextPath = serviceProvidersAPIContextPath; + } +} diff --git a/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/dao/SPApplicationDAO.java b/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/dao/SPApplicationDAO.java index 850e11bf88..62f97db9d0 100644 --- a/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/dao/SPApplicationDAO.java +++ b/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/dao/SPApplicationDAO.java @@ -18,7 +18,7 @@ package io.entgra.application.mgt.core.dao; -import io.entgra.application.mgt.common.IdentityServer; +import io.entgra.application.mgt.common.dto.IdentityServerDTO; import io.entgra.application.mgt.common.dto.ApplicationDTO; import io.entgra.application.mgt.core.exception.ApplicationManagementDAOException; @@ -26,6 +26,8 @@ import java.util.List; public interface SPApplicationDAO { + int createIdentityServer(IdentityServerDTO identityServer, int tenantId) throws ApplicationManagementDAOException; + /** * * @param identityServerId Id of identity server in which the service provider is in @@ -59,7 +61,7 @@ public interface SPApplicationDAO { * @return All available identity servers * @throws ApplicationManagementDAOException if any db error occurred */ - List getIdentityServers(int tenantId) throws ApplicationManagementDAOException; + List getIdentityServers(int tenantId) throws ApplicationManagementDAOException; /** * @@ -67,7 +69,7 @@ public interface SPApplicationDAO { * @return Identity Server of the given id * @throws ApplicationManagementDAOException if any db error occurred */ - IdentityServer getIdentityServerById(int id, int tenantId) throws ApplicationManagementDAOException; + IdentityServerDTO getIdentityServerById(int id, int tenantId) throws ApplicationManagementDAOException; /** * Verify whether application exist for given identity server id, service provider id and application id. diff --git a/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java b/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java index 05279c29ef..420afdbbde 100644 --- a/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java +++ b/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java @@ -17,9 +17,6 @@ */ package io.entgra.application.mgt.core.dao.impl.application; -import io.entgra.application.mgt.common.ApplicationType; -import io.entgra.application.mgt.common.IdentityServer; -import io.entgra.application.mgt.core.util.Constants; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -27,7 +24,6 @@ import io.entgra.application.mgt.common.AppLifecycleState; import io.entgra.application.mgt.common.dto.ApplicationDTO; import io.entgra.application.mgt.common.dto.CategoryDTO; import io.entgra.application.mgt.common.Filter; -import io.entgra.application.mgt.common.dto.ReviewDTO; import io.entgra.application.mgt.common.dto.TagDTO; import io.entgra.application.mgt.common.exception.DBConnectionException; import io.entgra.application.mgt.core.dao.ApplicationDAO; diff --git a/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/dao/impl/application/spapplication/GenericSPApplicationDAOImpl.java b/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/dao/impl/application/spapplication/GenericSPApplicationDAOImpl.java index 31b56cfa0e..4a586d8af6 100644 --- a/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/dao/impl/application/spapplication/GenericSPApplicationDAOImpl.java +++ b/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/dao/impl/application/spapplication/GenericSPApplicationDAOImpl.java @@ -18,7 +18,7 @@ package io.entgra.application.mgt.core.dao.impl.application.spapplication; -import io.entgra.application.mgt.common.IdentityServer; +import io.entgra.application.mgt.common.dto.IdentityServerDTO; import io.entgra.application.mgt.common.dto.ApplicationDTO; import io.entgra.application.mgt.common.exception.DBConnectionException; import io.entgra.application.mgt.core.dao.SPApplicationDAO; @@ -40,7 +40,7 @@ public class GenericSPApplicationDAOImpl extends AbstractDAOImpl implements SPAp private static final Log log = LogFactory.getLog(GenericApplicationDAOImpl.class); @Override - public List getIdentityServers(int tenantId) throws ApplicationManagementDAOException { + public List getIdentityServers(int tenantId) throws ApplicationManagementDAOException { String sql = "SELECT ID, NAME, DESCRIPTION, URL, SP_APPS_URI, SP_APPS_API, TENANT_ID, USERNAME, PASSWORD " + "FROM AP_IDENTITY_SERVER " + "WHERE TENANT_ID = ?"; @@ -67,7 +67,7 @@ public class GenericSPApplicationDAOImpl extends AbstractDAOImpl implements SPAp } @Override - public IdentityServer getIdentityServerById(int id, int tenantId) throws ApplicationManagementDAOException { + public IdentityServerDTO getIdentityServerById(int id, int tenantId) throws ApplicationManagementDAOException { String sql = "SELECT ID, NAME, DESCRIPTION, URL, SP_APPS_URI, SP_APPS_API, TENANT_ID, USERNAME, PASSWORD " + "FROM AP_IDENTITY_SERVER " + "WHERE TENANT_ID = ? AND " @@ -99,6 +99,46 @@ public class GenericSPApplicationDAOImpl extends AbstractDAOImpl implements SPAp } } + @Override + public int createIdentityServer(IdentityServerDTO identityServerDTO, int tenantId) throws ApplicationManagementDAOException { + if (log.isDebugEnabled()) { + log.debug("Request received in DAO Layer to create an identity server"); + } + String sql = "INSERT INTO AP_IDENTITY_SERVER " + + "(PROVIDER_NAME, " + + "NAME, " + + "DESCRIPTION, URL, API_URL, USERNAME, PASSWORD, TENANT_ID) " + + "VALUES (?, ?, ?, ?)"; + try { + Connection conn = this.getDBConnection(); + try (PreparedStatement stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) { + stmt.setString(1, identityServerDTO.getProviderName()); + stmt.setString(2, identityServerDTO.getName()); + stmt.setString(3, identityServerDTO.getDescription()); + stmt.setString(4, identityServerDTO.getUrl()); + stmt.setString(5, identityServerDTO.getApiUrl()); + stmt.setString(6, identityServerDTO.getUserName()); + stmt.setString(7, identityServerDTO.getPassword()); + stmt.setInt(8, tenantId); + stmt.executeUpdate(); + try (ResultSet rs = stmt.getGeneratedKeys()) { + if (rs.next()) { + return rs.getInt(1); + } + return -1; + } + } + } catch (DBConnectionException e) { + String msg = "Error occurred while creating identity server "; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "Error occurred while executing SQL to create an identity server "; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } + } + @Override public List getSPApplications(int identityServerId, String spUID, int tenantId) throws ApplicationManagementDAOException { if (log.isDebugEnabled()) { diff --git a/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/dao/impl/application/spapplication/OracleSPApplicationDAOImpl.java b/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/dao/impl/application/spapplication/OracleSPApplicationDAOImpl.java index 9a513f8192..3526fe88e2 100644 --- a/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/dao/impl/application/spapplication/OracleSPApplicationDAOImpl.java +++ b/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/dao/impl/application/spapplication/OracleSPApplicationDAOImpl.java @@ -18,7 +18,7 @@ package io.entgra.application.mgt.core.dao.impl.application.spapplication; -import io.entgra.application.mgt.common.IdentityServer; +import io.entgra.application.mgt.common.dto.IdentityServerDTO; import io.entgra.application.mgt.common.dto.ApplicationDTO; import io.entgra.application.mgt.common.exception.DBConnectionException; import io.entgra.application.mgt.core.dao.SPApplicationDAO; @@ -40,7 +40,7 @@ public class OracleSPApplicationDAOImpl extends AbstractDAOImpl implements SPAp private static final Log log = LogFactory.getLog(OracleSPApplicationDAOImpl.class); @Override - public List getIdentityServers(int tenantId) throws ApplicationManagementDAOException { + public List getIdentityServers(int tenantId) throws ApplicationManagementDAOException { String sql = "SELECT ID, NAME, DESCRIPTION, URL, SP_APPS_URI, SP_APPS_API, TENANT_ID, USERNAME, PASSWORD " + "FROM AP_IDENTITY_SERVER " + "WHERE TENANT_ID = ?"; @@ -67,7 +67,7 @@ public class OracleSPApplicationDAOImpl extends AbstractDAOImpl implements SPAp } @Override - public IdentityServer getIdentityServerById(int id, int tenantId) throws ApplicationManagementDAOException { + public IdentityServerDTO getIdentityServerById(int id, int tenantId) throws ApplicationManagementDAOException { String sql = "SELECT ID, NAME, DESCRIPTION, URL, SP_APPS_URI, SP_APPS_API, TENANT_ID, USERNAME, PASSWORD " + "FROM AP_IDENTITY_SERVER " + "WHERE TENANT_ID = ? AND " diff --git a/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/dao/impl/application/spapplication/PostgreSQLSPApplicationDAOImpl.java b/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/dao/impl/application/spapplication/PostgreSQLSPApplicationDAOImpl.java index 9b251aed14..88d482bf13 100644 --- a/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/dao/impl/application/spapplication/PostgreSQLSPApplicationDAOImpl.java +++ b/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/dao/impl/application/spapplication/PostgreSQLSPApplicationDAOImpl.java @@ -18,7 +18,7 @@ package io.entgra.application.mgt.core.dao.impl.application.spapplication; -import io.entgra.application.mgt.common.IdentityServer; +import io.entgra.application.mgt.common.dto.IdentityServerDTO; import io.entgra.application.mgt.common.dto.ApplicationDTO; import io.entgra.application.mgt.common.exception.DBConnectionException; import io.entgra.application.mgt.core.dao.SPApplicationDAO; @@ -40,7 +40,7 @@ public class PostgreSQLSPApplicationDAOImpl extends AbstractDAOImpl implements S private static final Log log = LogFactory.getLog(PostgreSQLSPApplicationDAOImpl.class); @Override - public List getIdentityServers(int tenantId) throws ApplicationManagementDAOException { + public List getIdentityServers(int tenantId) throws ApplicationManagementDAOException { String sql = "SELECT ID, NAME, DESCRIPTION, URL, SP_APPS_URI, SP_APPS_API, TENANT_ID, USERNAME, PASSWORD " + "FROM AP_IDENTITY_SERVER " + "WHERE TENANT_ID = ?"; @@ -67,7 +67,7 @@ public class PostgreSQLSPApplicationDAOImpl extends AbstractDAOImpl implements S } @Override - public IdentityServer getIdentityServerById(int id, int tenantId) throws ApplicationManagementDAOException { + public IdentityServerDTO getIdentityServerById(int id, int tenantId) throws ApplicationManagementDAOException { String sql = "SELECT ID, NAME, DESCRIPTION, URL, SP_APPS_URI, SP_APPS_API, TENANT_ID, USERNAME, PASSWORD " + "FROM AP_IDENTITY_SERVER " + "WHERE TENANT_ID = ? AND " diff --git a/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/dao/impl/application/spapplication/SQLServerSPApplicationDAOImpl.java b/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/dao/impl/application/spapplication/SQLServerSPApplicationDAOImpl.java index f6ac4f4d67..acbfe124b5 100644 --- a/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/dao/impl/application/spapplication/SQLServerSPApplicationDAOImpl.java +++ b/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/dao/impl/application/spapplication/SQLServerSPApplicationDAOImpl.java @@ -18,7 +18,7 @@ package io.entgra.application.mgt.core.dao.impl.application.spapplication; -import io.entgra.application.mgt.common.IdentityServer; +import io.entgra.application.mgt.common.dto.IdentityServerDTO; import io.entgra.application.mgt.common.dto.ApplicationDTO; import io.entgra.application.mgt.common.exception.DBConnectionException; import io.entgra.application.mgt.core.dao.SPApplicationDAO; @@ -40,7 +40,7 @@ public class SQLServerSPApplicationDAOImpl extends AbstractDAOImpl implements S private static final Log log = LogFactory.getLog(SQLServerSPApplicationDAOImpl.class); @Override - public List getIdentityServers(int tenantId) throws ApplicationManagementDAOException { + public List getIdentityServers(int tenantId) throws ApplicationManagementDAOException { String sql = "SELECT ID, NAME, DESCRIPTION, URL, SP_APPS_URI, SP_APPS_API, TENANT_ID, USERNAME, PASSWORD " + "FROM AP_IDENTITY_SERVER " + "WHERE TENANT_ID = ?"; @@ -67,7 +67,7 @@ public class SQLServerSPApplicationDAOImpl extends AbstractDAOImpl implements S } @Override - public IdentityServer getIdentityServerById(int id, int tenantId) throws ApplicationManagementDAOException { + public IdentityServerDTO getIdentityServerById(int id, int tenantId) throws ApplicationManagementDAOException { String sql = "SELECT ID, NAME, DESCRIPTION, URL, SP_APPS_URI, SP_APPS_API, TENANT_ID, USERNAME, PASSWORD " + "FROM AP_IDENTITY_SERVER " + "WHERE TENANT_ID = ? AND " diff --git a/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/impl/SPApplicationManagerImpl.java b/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/impl/SPApplicationManagerImpl.java index 1f90f5db06..f1ed00f98e 100644 --- a/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/impl/SPApplicationManagerImpl.java +++ b/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/impl/SPApplicationManagerImpl.java @@ -19,7 +19,7 @@ package io.entgra.application.mgt.core.impl; import io.entgra.application.mgt.common.IdentityServer; -import io.entgra.application.mgt.common.IdentityServerList; +import io.entgra.application.mgt.common.dto.IdentityServerDTO; import io.entgra.application.mgt.common.SPApplication; import io.entgra.application.mgt.common.dto.ApplicationDTO; import io.entgra.application.mgt.common.exception.ApplicationManagementException; @@ -44,6 +44,7 @@ import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.PrivilegedCarbonContext; import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; public class SPApplicationManagerImpl implements SPApplicationManager { @@ -76,7 +77,8 @@ public class SPApplicationManagerImpl implements SPApplicationManager { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { ConnectionManagerUtil.openDBConnection(); - return spApplicationDAO.getIdentityServerById(identityServerId, tenantId); + IdentityServerDTO identityServerDTO = spApplicationDAO.getIdentityServerById(identityServerId, tenantId); + return APIUtil.identityServerDtoToIdentityServerResponse(identityServerDTO); } catch (DBConnectionException e) { String msg = "Error occurred when getting database connection to get identity server with the id: " + identityServerId; log.error(msg, e); @@ -92,13 +94,12 @@ public class SPApplicationManagerImpl implements SPApplicationManager { } @Override - public IdentityServerList getIdentityServers() throws ApplicationManagementException { + public List getIdentityServers() throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { ConnectionManagerUtil.openDBConnection(); - IdentityServerList identityServerList = new IdentityServerList(); - identityServerList.setIdentityServers(spApplicationDAO.getIdentityServers(tenantId)); - return identityServerList; + return spApplicationDAO.getIdentityServers(tenantId).stream(). + map(APIUtil::identityServerDtoToIdentityServerResponse).collect(Collectors.toList()); } catch (DBConnectionException e) { String msg = "Error occurred when getting database connection to get identity servers"; log.error(msg, e); @@ -113,6 +114,20 @@ public class SPApplicationManagerImpl implements SPApplicationManager { } } + @Override + public IdentityServer createIdentityServer(IdentityServerDTO identityServerDTO) throws ApplicationManagementException { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + try { + int id = spApplicationDAO.createIdentityServer(identityServerDTO, tenantId); + identityServerDTO.setId(id); + return APIUtil.identityServerDtoToIdentityServerResponse(identityServerDTO); + } catch (ApplicationManagementDAOException e) { + String msg = "Error occurred while creating identity server " + identityServerDTO.getName(); + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } + } + @Override public List getSPApplications(int identityServerId, String spUID) throws ApplicationManagementException { diff --git a/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/util/APIUtil.java b/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/util/APIUtil.java index cb4eb7edff..00229f6760 100644 --- a/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/util/APIUtil.java +++ b/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/util/APIUtil.java @@ -18,6 +18,9 @@ package io.entgra.application.mgt.core.util; +import io.entgra.application.mgt.common.IdentityServer; +import io.entgra.application.mgt.common.dto.IdentityServerDTO; +import io.entgra.application.mgt.core.config.IdentityServerDetail; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -363,6 +366,23 @@ public class APIUtil { return applicationReleaseDTO; } + public static IdentityServer identityServerDtoToIdentityServerResponse(IdentityServerDTO identityServerDTO) { + IdentityServer identityServer = new IdentityServer(); + identityServer.setId(identityServerDTO.getId()); + identityServer.setProviderName(identityServerDTO.getProviderName()); + identityServer.setName(identityServerDTO.getName()); + identityServer.setDescription(identityServerDTO.getDescription()); + identityServer.setUrl(identityServerDTO.getUrl()); + identityServer.setApiUrl(identityServerDTO.getApiUrl()); + identityServer.setUserName(identityServerDTO.getUserName()); + identityServer.setPassword(identityServerDTO.getPassword()); + IdentityServerDetail identityServerDetail = ConfigurationManager.getInstance().getIdentityServerConfiguration() + .getIdentityServerDetailByProviderName(identityServerDTO.getProviderName()); + String serviceProviderAppsUrl = identityServerDTO.getUrl() + Constants.FORWARD_SLASH + identityServerDetail.getServiceProvidersPageUri(); + identityServer.setServiceProviderAppsUrl(serviceProviderAppsUrl); + return identityServer; + } + public static Application appDtoToAppResponse(ApplicationDTO applicationDTO) throws ApplicationManagementException { Application application = new Application(); diff --git a/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/util/Constants.java b/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/util/Constants.java index 03f94ef1d0..4082edb492 100644 --- a/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/util/Constants.java +++ b/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/util/Constants.java @@ -31,9 +31,12 @@ import java.util.Map; public class Constants { public static final String APPLICATION_CONFIG_XML_FILE = "application-mgt.xml"; + public static final String IDENTITY_SERVERS_CONFIG_XML_FILE = "identity-servers-config.xml"; public static final String DEFAULT_CONFIG_FILE_LOCATION = CarbonUtils.getCarbonConfigDirPath() + File.separator + Constants.APPLICATION_CONFIG_XML_FILE; + public static final String DEFAULT_IDENTITY_SERVERS_CONFIG_FILE_LOCATION = CarbonUtils.getCarbonConfigDirPath() + File.separator + + IDENTITY_SERVERS_CONFIG_XML_FILE; public static final String DEFAULT_VERSION = "1.0.0"; public static final String SCREENSHOT_NAME = "screenshot"; public static final String ICON_NAME = "icon"; diff --git a/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/util/DAOUtil.java b/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/util/DAOUtil.java index 4502c8987b..4291d95525 100644 --- a/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/util/DAOUtil.java +++ b/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/util/DAOUtil.java @@ -19,7 +19,7 @@ package io.entgra.application.mgt.core.util; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; -import io.entgra.application.mgt.common.IdentityServer; +import io.entgra.application.mgt.common.dto.IdentityServerDTO; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.json.JSONException; @@ -172,22 +172,22 @@ public class DAOUtil { * To create application object from the result set retrieved from the Database. * * @param rs ResultSet - * @return IdentityServer that is retrieved from the Database. + * @return IdentityServerDTO that is retrieved from the Database. * @throws SQLException SQL Exception * @throws JSONException JSONException. */ - public static IdentityServer loadIdentityServer(ResultSet rs) + public static IdentityServerDTO loadIdentityServer(ResultSet rs) throws SQLException, JSONException, UnexpectedServerErrorException { - List identityServers = loadIdentityServers(rs); - if (identityServers.isEmpty()) { + List identityServerDTOS = loadIdentityServers(rs); + if (identityServerDTOS.isEmpty()) { return null; } - if (identityServers.size() > 1) { + if (identityServerDTOS.size() > 1) { String msg = "Internal server error. Found more than one identity server for requested ID"; log.error(msg); throw new UnexpectedServerErrorException(msg); } - return identityServers.get(0); + return identityServerDTOS.get(0); } /** @@ -198,21 +198,21 @@ public class DAOUtil { * @throws SQLException SQL Exception * @throws JSONException JSONException. */ - public static List loadIdentityServers(ResultSet rs) throws SQLException, JSONException { - List identityServers = new ArrayList<>(); + public static List loadIdentityServers(ResultSet rs) throws SQLException, JSONException { + List identityServerDTOS = new ArrayList<>(); while (rs.next()) { - IdentityServer identityServer = new IdentityServer(); - identityServer.setId(rs.getInt("ID")); - identityServer.setName(rs.getString("NAME")); - identityServer.setDescription(rs.getString("DESCRIPTION")); - identityServer.setUrl(rs.getString("URL")); - identityServer.setSpAppsURI(rs.getString("SP_APPS_URI")); - identityServer.setSpAppsApi(rs.getString("SP_APPS_API")); - identityServer.setUserName(rs.getString("USERNAME")); - identityServer.setPassword(rs.getString("PASSWORD")); - identityServers.add(identityServer); + IdentityServerDTO identityServerDTO = new IdentityServerDTO(); + identityServerDTO.setId(rs.getInt("ID")); + identityServerDTO.setProviderName(rs.getString("PROVIDER_NAME")); + identityServerDTO.setName(rs.getString("NAME")); + identityServerDTO.setDescription(rs.getString("DESCRIPTION")); + identityServerDTO.setUrl(rs.getString("URL")); + identityServerDTO.setApiUrl(rs.getString("API_URI")); + identityServerDTO.setUserName(rs.getString("USERNAME")); + identityServerDTO.setPassword(rs.getString("PASSWORD")); + identityServerDTOS.add(identityServerDTO); } - return identityServers; + return identityServerDTOS; } /** diff --git a/components/application-mgt/io.entgra.application.mgt.publisher.api/src/main/java/io/entgra/application/mgt/publisher/api/services/SPApplicationService.java b/components/application-mgt/io.entgra.application.mgt.publisher.api/src/main/java/io/entgra/application/mgt/publisher/api/services/SPApplicationService.java index 9070ff29ba..ffaa3c7a8a 100644 --- a/components/application-mgt/io.entgra.application.mgt.publisher.api/src/main/java/io/entgra/application/mgt/publisher/api/services/SPApplicationService.java +++ b/components/application-mgt/io.entgra.application.mgt.publisher.api/src/main/java/io/entgra/application/mgt/publisher/api/services/SPApplicationService.java @@ -18,6 +18,7 @@ package io.entgra.application.mgt.publisher.api.services; +import io.entgra.application.mgt.common.dto.IdentityServerDTO; import io.entgra.application.mgt.common.wrapper.ApplicationWrapper; import io.entgra.application.mgt.common.wrapper.CustomAppWrapper; import io.entgra.application.mgt.common.wrapper.PublicAppWrapper; @@ -66,6 +67,13 @@ import java.util.List; roles = {"Internal/devicemgt-user"}, permissions = {"/app-mgt/publisher/service-provider/application/view"} ), + @Scope( + name = "Create new identity server", + description = "Connect to new identity server", + key = "perm:app:publisher:service-provider:connect", + roles = {"Internal/devicemgt-user"}, + permissions = {"/app-mgt/publisher/service-provider/application/connect"} + ), @Scope( name = "Create a service provider application", description = "Create an application and attach (map) to service provider", @@ -128,6 +136,22 @@ public interface SPApplicationService { ) Response getIdentityServer(@PathParam("id") int id); + @POST + @Produces(MediaType.APPLICATION_JSON) + @Path("/identity-servers") + @ApiOperation( + produces = MediaType.APPLICATION_JSON, + httpMethod = "POST", + value = "create new identity server", + tags = "Identity Server Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = SCOPE, value = "perm:app:publisher:service-provider:connect") + }) + } + ) + Response createIdentityServer(IdentityServerDTO identityServerDTO); + /** * This method is used to register an APIM application for tenant domain. */ diff --git a/components/application-mgt/io.entgra.application.mgt.publisher.api/src/main/java/io/entgra/application/mgt/publisher/api/services/impl/SPApplicationServiceImpl.java b/components/application-mgt/io.entgra.application.mgt.publisher.api/src/main/java/io/entgra/application/mgt/publisher/api/services/impl/SPApplicationServiceImpl.java index 4a649c75ea..0fac112211 100644 --- a/components/application-mgt/io.entgra.application.mgt.publisher.api/src/main/java/io/entgra/application/mgt/publisher/api/services/impl/SPApplicationServiceImpl.java +++ b/components/application-mgt/io.entgra.application.mgt.publisher.api/src/main/java/io/entgra/application/mgt/publisher/api/services/impl/SPApplicationServiceImpl.java @@ -19,6 +19,7 @@ package io.entgra.application.mgt.publisher.api.services.impl; import io.entgra.application.mgt.common.IdentityServer; +import io.entgra.application.mgt.common.dto.IdentityServerDTO; import io.entgra.application.mgt.common.IdentityServerList; import io.entgra.application.mgt.common.SPApplicationListResponse; import io.entgra.application.mgt.common.exception.ApplicationManagementException; @@ -56,7 +57,7 @@ public class SPApplicationServiceImpl implements SPApplicationService { public Response getIdentityServers() { try { SPApplicationManager spAppManager = APIUtil.getSPApplicationManager(); - IdentityServerList identityServers = spAppManager.getIdentityServers(); + List identityServers = spAppManager.getIdentityServers(); return Response.status(Response.Status.OK).entity(identityServers).build(); } catch (ApplicationManagementException e) { String errMsg = "Error occurred while trying to merge identity server apps with existing apps"; @@ -80,6 +81,21 @@ public class SPApplicationServiceImpl implements SPApplicationService { } } + @Path("/identity-servers") + @POST + @Override + public Response createIdentityServer(IdentityServerDTO identityServerDTO) { + try { + SPApplicationManager spAppManager = APIUtil.getSPApplicationManager(); + IdentityServer identityServer = spAppManager.createIdentityServer(identityServerDTO); + return Response.status(Response.Status.CREATED).entity(identityServer).build(); + } catch (ApplicationManagementException e) { + String errMsg = "Error occurred while trying to merge identity server apps with existing apps"; + log.error(errMsg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errMsg).build(); + } + } + @Path("/{identity-server-id}/service-providers") @GET @Override diff --git a/components/application-mgt/io.entgra.application.mgt.publisher.api/src/main/java/io/entgra/application/mgt/publisher/api/services/util/SPAppRequestHandlerUtil.java b/components/application-mgt/io.entgra.application.mgt.publisher.api/src/main/java/io/entgra/application/mgt/publisher/api/services/util/SPAppRequestHandlerUtil.java index b9d6b60e99..d71b353472 100644 --- a/components/application-mgt/io.entgra.application.mgt.publisher.api/src/main/java/io/entgra/application/mgt/publisher/api/services/util/SPAppRequestHandlerUtil.java +++ b/components/application-mgt/io.entgra.application.mgt.publisher.api/src/main/java/io/entgra/application/mgt/publisher/api/services/util/SPAppRequestHandlerUtil.java @@ -24,6 +24,8 @@ import io.entgra.application.mgt.common.SPApplication; import io.entgra.application.mgt.common.SPApplicationListResponse; import io.entgra.application.mgt.common.exception.ApplicationManagementException; import io.entgra.application.mgt.common.services.SPApplicationManager; +import io.entgra.application.mgt.core.config.ConfigurationManager; +import io.entgra.application.mgt.core.config.IdentityServerDetail; import io.entgra.application.mgt.core.util.APIUtil; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -167,8 +169,9 @@ public class SPAppRequestHandlerUtil { } private static String getSPApplicationsAPI(IdentityServer identityServer) { - String api = identityServer.getSpAppsApi(); - return api; + IdentityServerDetail identityServerDetail = ConfigurationManager.getInstance().getIdentityServerConfiguration(). + getIdentityServerDetailByProviderName(identityServer.getProviderName()); + return identityServer.getApiUrl() + identityServerDetail.getServiceProvidersAPIContextPath(); } } \ No newline at end of file