From f21ee97d8115260db5a69cf7ff9aeb9fd4dbb832 Mon Sep 17 00:00:00 2001 From: Rajitha Kumara Date: Thu, 7 Nov 2024 10:02:00 +0530 Subject: [PATCH] Add requested changes --- ...ApiApplicationRegistrationServiceImpl.java | 26 ++----- .../api/common/GsonMessageBodyHandler.java | 13 +--- .../src/main/webapp/WEB-INF/web.xml | 4 +- .../pom.xml | 4 +- .../APIManagementProviderService.java | 19 +++--- .../APIManagementProviderServiceImpl.java | 68 +++++++++++++------ .../{dto => bean}/ApiApplicationKey.java | 7 +- .../bean/IdnAuthenticationProfile.java | 58 ---------------- .../exception/IdnAuthenticationException.java | 26 ------- .../rest/api/IOAuthClientService.java | 10 --- .../extension/rest/api/OAuthClient.java | 58 ---------------- .../core/impl/SubscriptionManagerImpl.java | 2 +- .../application/mgt/core/util/OAuthUtils.java | 14 +--- .../impl/DeviceManagementServiceImpl.java | 23 +------ 14 files changed, 80 insertions(+), 252 deletions(-) rename components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension/src/main/java/io/entgra/device/mgt/core/apimgt/application/extension/{dto => bean}/ApiApplicationKey.java (88%) delete mode 100644 components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension/src/main/java/io/entgra/device/mgt/core/apimgt/application/extension/bean/IdnAuthenticationProfile.java delete mode 100644 components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension/src/main/java/io/entgra/device/mgt/core/apimgt/application/extension/exception/IdnAuthenticationException.java diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension.api/src/main/java/io/entgra/device/mgt/core/apimgt/application/extension/api/ApiApplicationRegistrationServiceImpl.java b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension.api/src/main/java/io/entgra/device/mgt/core/apimgt/application/extension/api/ApiApplicationRegistrationServiceImpl.java index 0e9ee26123..79507f8ff1 100644 --- a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension.api/src/main/java/io/entgra/device/mgt/core/apimgt/application/extension/api/ApiApplicationRegistrationServiceImpl.java +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension.api/src/main/java/io/entgra/device/mgt/core/apimgt/application/extension/api/ApiApplicationRegistrationServiceImpl.java @@ -22,10 +22,8 @@ import io.entgra.device.mgt.core.apimgt.application.extension.APIManagementProvi import io.entgra.device.mgt.core.apimgt.application.extension.api.util.APIUtil; import io.entgra.device.mgt.core.apimgt.application.extension.api.util.RegistrationProfile; import io.entgra.device.mgt.core.apimgt.application.extension.bean.ApiApplicationProfile; -import io.entgra.device.mgt.core.apimgt.application.extension.bean.IdnAuthenticationProfile; -import io.entgra.device.mgt.core.apimgt.application.extension.dto.ApiApplicationKey; +import io.entgra.device.mgt.core.apimgt.application.extension.bean.ApiApplicationKey; import io.entgra.device.mgt.core.apimgt.application.extension.exception.APIManagerException; -import io.entgra.device.mgt.core.apimgt.application.extension.exception.IdnAuthenticationException; import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.BadRequestException; import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.UnexpectedResponseException; import io.entgra.device.mgt.core.device.mgt.common.exceptions.DeviceManagementException; @@ -66,12 +64,6 @@ public class ApiApplicationRegistrationServiceImpl implements ApiApplicationRegi String username = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserRealm() .getRealmConfiguration().getAdminUserName(); PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(username); - String password = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserRealm() - .getRealmConfiguration().getAdminPassword(); - - IdnAuthenticationProfile idnAuthenticationProfile = new IdnAuthenticationProfile(); - idnAuthenticationProfile.setUsername(username); - idnAuthenticationProfile.setPassword(password); ApiApplicationProfile apiApplicationProfile = new ApiApplicationProfile(); apiApplicationProfile.setApplicationName(applicationName); @@ -81,8 +73,7 @@ public class ApiApplicationRegistrationServiceImpl implements ApiApplicationRegi APIManagementProviderService apiManagementProviderService = APIUtil.getAPIManagementProviderService(); ApiApplicationKey apiApplicationKey = - apiManagementProviderService.registerApiApplication(idnAuthenticationProfile, - apiApplicationProfile); + apiManagementProviderService.registerApiApplication(apiApplicationProfile); return Response.status(Response.Status.CREATED).entity(apiApplicationKey.toString()).build(); } catch (APIManagerException e) { String msg = "Error occurred while registering an application '" + applicationName + "'"; @@ -96,7 +87,7 @@ public class ApiApplicationRegistrationServiceImpl implements ApiApplicationRegi String msg = "Failed to retrieve the device service"; log.error(msg, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); - } catch (IdnAuthenticationException | BadRequestException | UnexpectedResponseException e) { + } catch (BadRequestException | UnexpectedResponseException e) { String msg = "Error encountered while registering api application"; log.error(msg, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); @@ -118,10 +109,6 @@ public class ApiApplicationRegistrationServiceImpl implements ApiApplicationRegi } APIManagementProviderService apiManagementProviderService = APIUtil.getAPIManagementProviderService(); - IdnAuthenticationProfile idnAuthenticationProfile = new IdnAuthenticationProfile(); - idnAuthenticationProfile.setUsername(registrationProfile.getUsername()); - idnAuthenticationProfile.setPassword(registrationProfile.getPassword()); - ApiApplicationProfile apiApplicationProfile = new ApiApplicationProfile(); apiApplicationProfile.setApplicationName(registrationProfile.getApplicationName()); apiApplicationProfile.setTags(registrationProfile.getTags()); @@ -129,13 +116,8 @@ public class ApiApplicationRegistrationServiceImpl implements ApiApplicationRegi apiApplicationProfile.setGrantTypes(String.join(" ", registrationProfile.getSupportedGrantTypes())); ApiApplicationKey apiApplicationKey = - apiManagementProviderService.registerApiApplication(idnAuthenticationProfile, - apiApplicationProfile); + apiManagementProviderService.registerApiApplication(apiApplicationProfile); return Response.status(Response.Status.CREATED).entity(apiApplicationKey).build(); - } catch (IdnAuthenticationException e) { - String msg = "Failed to authenticate the user " + registrationProfile.getUsername(); - log.error(msg, e); - return Response.status(Response.Status.UNAUTHORIZED).entity(msg).build(); } catch (BadRequestException e) { String msg = "Received bad request for registering api application " + registrationProfile.getApplicationName(); diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension.api/src/main/java/io/entgra/device/mgt/core/apimgt/application/extension/api/common/GsonMessageBodyHandler.java b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension.api/src/main/java/io/entgra/device/mgt/core/apimgt/application/extension/api/common/GsonMessageBodyHandler.java index 1a5ff6bc18..26ed009fdb 100644 --- a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension.api/src/main/java/io/entgra/device/mgt/core/apimgt/application/extension/api/common/GsonMessageBodyHandler.java +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension.api/src/main/java/io/entgra/device/mgt/core/apimgt/application/extension/api/common/GsonMessageBodyHandler.java @@ -38,6 +38,7 @@ import java.io.OutputStream; import java.io.OutputStreamWriter; import java.lang.annotation.Annotation; import java.lang.reflect.Type; +import java.nio.charset.StandardCharsets; import static javax.ws.rs.core.MediaType.APPLICATION_JSON; @@ -47,7 +48,6 @@ import static javax.ws.rs.core.MediaType.APPLICATION_JSON; public class GsonMessageBodyHandler implements MessageBodyWriter, MessageBodyReader { private Gson gson; - private static final String UTF_8 = "UTF-8"; public boolean isReadable(Class aClass, Type type, Annotation[] annotations, MediaType mediaType) { return true; @@ -65,12 +65,8 @@ public class GsonMessageBodyHandler implements MessageBodyWriter, Messag MultivaluedMap stringStringMultivaluedMap, InputStream entityStream) throws IOException, WebApplicationException { - InputStreamReader reader = new InputStreamReader(entityStream, "UTF-8"); - - try { + try (InputStreamReader reader = new InputStreamReader(entityStream, StandardCharsets.UTF_8)) { return getGson().fromJson(reader, type); - } finally { - reader.close(); } } @@ -86,11 +82,8 @@ public class GsonMessageBodyHandler implements MessageBodyWriter, Messag MultivaluedMap stringObjectMultivaluedMap, OutputStream entityStream) throws IOException, WebApplicationException { - OutputStreamWriter writer = new OutputStreamWriter(entityStream, UTF_8); - try { + try (OutputStreamWriter writer = new OutputStreamWriter(entityStream, StandardCharsets.UTF_8)) { getGson().toJson(object, type, writer); - } finally { - writer.close(); } } } diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension.api/src/main/webapp/WEB-INF/web.xml b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension.api/src/main/webapp/WEB-INF/web.xml index 13202982cc..11f4f07340 100644 --- a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension.api/src/main/webapp/WEB-INF/web.xml +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension.api/src/main/webapp/WEB-INF/web.xml @@ -37,12 +37,12 @@ doAuthentication - false + true basicAuth - false + true diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension/pom.xml b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension/pom.xml index cde28e6b79..c1833ef2b4 100644 --- a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension/pom.xml +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension/pom.xml @@ -132,7 +132,9 @@ API Management Application Bundle io.entgra.device.mgt.core.apimgt.application.extension.internal - com.google.gson.*;version="${google.gson.version}", + com.google.gson.Gson;version="${google.gson.version}", + com.google.gson.JsonArray;version="${google.gson.version}", + com.google.gson.JsonElement;version="${google.gson.version}", io.entgra.device.mgt.core.apimgt.application.extension.bean, io.entgra.device.mgt.core.apimgt.application.extension.dto, io.entgra.device.mgt.core.apimgt.application.extension.exception, diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension/src/main/java/io/entgra/device/mgt/core/apimgt/application/extension/APIManagementProviderService.java b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension/src/main/java/io/entgra/device/mgt/core/apimgt/application/extension/APIManagementProviderService.java index e4114b0c4c..9bd74c3911 100644 --- a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension/src/main/java/io/entgra/device/mgt/core/apimgt/application/extension/APIManagementProviderService.java +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension/src/main/java/io/entgra/device/mgt/core/apimgt/application/extension/APIManagementProviderService.java @@ -19,12 +19,10 @@ package io.entgra.device.mgt.core.apimgt.application.extension; import io.entgra.device.mgt.core.apimgt.application.extension.bean.ApiApplicationProfile; -import io.entgra.device.mgt.core.apimgt.application.extension.bean.IdnAuthenticationProfile; import io.entgra.device.mgt.core.apimgt.application.extension.bean.Token; import io.entgra.device.mgt.core.apimgt.application.extension.bean.TokenCreationProfile; -import io.entgra.device.mgt.core.apimgt.application.extension.dto.ApiApplicationKey; +import io.entgra.device.mgt.core.apimgt.application.extension.bean.ApiApplicationKey; import io.entgra.device.mgt.core.apimgt.application.extension.exception.APIManagerException; -import io.entgra.device.mgt.core.apimgt.application.extension.exception.IdnAuthenticationException; import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.BadRequestException; import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.UnexpectedResponseException; @@ -51,15 +49,20 @@ public interface APIManagementProviderService { /** * Register API application base on {@link ApiApplicationProfile} - * @param idnAuthenticationProfile Application owner's authentication user details * @param apiApplicationProfile {@link ApiApplicationProfile} * @return {@link ApiApplicationKey} result on a successful execution - * @throws IdnAuthenticationException Throws when authentication error occurred * @throws APIManagerException Throws when error encountered while registering the application profile * @throws BadRequestException Throws when the application profile contains invalid attributes * @throws UnexpectedResponseException Throws when unexpected response received from the REST API client */ - ApiApplicationKey registerApiApplication(IdnAuthenticationProfile idnAuthenticationProfile, - ApiApplicationProfile apiApplicationProfile) - throws IdnAuthenticationException, APIManagerException, BadRequestException, UnexpectedResponseException; + ApiApplicationKey registerApiApplication(ApiApplicationProfile apiApplicationProfile) + throws APIManagerException, BadRequestException, UnexpectedResponseException; + + /** + * Generate custom JWT token via extended JWT client + * @param tokenCreationProfile {@link TokenCreationProfile} + * @return Retrieve {@link Token} result on a successful execution + * @throws APIManagerException Throws when error occurred while retrieving the token + */ + Token getCustomToken(TokenCreationProfile tokenCreationProfile) throws APIManagerException; } diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension/src/main/java/io/entgra/device/mgt/core/apimgt/application/extension/APIManagementProviderServiceImpl.java b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension/src/main/java/io/entgra/device/mgt/core/apimgt/application/extension/APIManagementProviderServiceImpl.java index 218b111f82..b1ba2140fb 100644 --- a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension/src/main/java/io/entgra/device/mgt/core/apimgt/application/extension/APIManagementProviderServiceImpl.java +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension/src/main/java/io/entgra/device/mgt/core/apimgt/application/extension/APIManagementProviderServiceImpl.java @@ -22,13 +22,11 @@ import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import io.entgra.device.mgt.core.apimgt.application.extension.bean.ApiApplicationProfile; -import io.entgra.device.mgt.core.apimgt.application.extension.bean.IdnAuthenticationProfile; import io.entgra.device.mgt.core.apimgt.application.extension.bean.Token; import io.entgra.device.mgt.core.apimgt.application.extension.bean.TokenCreationProfile; import io.entgra.device.mgt.core.apimgt.application.extension.constants.ApiApplicationConstants; -import io.entgra.device.mgt.core.apimgt.application.extension.dto.ApiApplicationKey; +import io.entgra.device.mgt.core.apimgt.application.extension.bean.ApiApplicationKey; import io.entgra.device.mgt.core.apimgt.application.extension.exception.APIManagerException; -import io.entgra.device.mgt.core.apimgt.application.extension.exception.IdnAuthenticationException; import io.entgra.device.mgt.core.apimgt.application.extension.internal.APIApplicationManagerExtensionDataHolder; import io.entgra.device.mgt.core.apimgt.extension.rest.api.ConsumerRESTAPIServices; import io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.APIInfo; @@ -39,11 +37,14 @@ import io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.Sub import io.entgra.device.mgt.core.apimgt.extension.rest.api.constants.Constants; import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.APIServicesException; import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.BadRequestException; -import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.OAuthClientException; import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.UnexpectedResponseException; import io.entgra.device.mgt.core.device.mgt.common.exceptions.MetadataManagementException; import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.Metadata; import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.MetadataManagementService; +import io.entgra.device.mgt.core.identity.jwt.client.extension.JWTClient; +import io.entgra.device.mgt.core.identity.jwt.client.extension.dto.AccessTokenInfo; +import io.entgra.device.mgt.core.identity.jwt.client.extension.exception.JWTClientException; +import io.entgra.device.mgt.core.identity.jwt.client.extension.service.JWTClientManagerService; import okhttp3.Credentials; import okhttp3.MediaType; import okhttp3.OkHttpClient; @@ -110,7 +111,7 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe return requestBody; } - private static ApiApplicationKey registerApiApplication(ApiApplicationProfile apiApplicationProfile) + private static ApiApplicationKey createApiApplication(ApiApplicationProfile apiApplicationProfile) throws APIManagerException, BadRequestException, UnexpectedResponseException { if (apiApplicationProfile.getGrantTypes().contains("authorization_code") && StringUtils.isEmpty(apiApplicationProfile.getCallbackUrl())) { @@ -140,7 +141,10 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe updateAndRetrieveApplicationKeys(applications.get(0), apiApplicationProfile, apis); } catch (APIServicesException e) { - throw new RuntimeException(e); + String msg = + "Error encountered while creating API application : [ " + apiApplicationProfile.getApplicationName() + " ]"; + log.error(msg, e); + throw new APIManagerException(msg, e); } } @@ -285,9 +289,8 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe } @Override - public ApiApplicationKey registerApiApplication(IdnAuthenticationProfile idnAuthenticationProfile, - ApiApplicationProfile apiApplicationProfile) - throws IdnAuthenticationException, APIManagerException, BadRequestException, UnexpectedResponseException { + public ApiApplicationKey registerApiApplication(ApiApplicationProfile apiApplicationProfile) throws APIManagerException, + BadRequestException, UnexpectedResponseException { String flowStartingDomain = MultitenantConstants.SUPER_TENANT_DOMAIN_NAME; MetadataManagementService metadataManagementService = APIApplicationManagerExtensionDataHolder.getInstance().getMetadataManagementService(); @@ -299,8 +302,10 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe JsonArray tenants = gson.fromJson(metaData.getMetaValue(), JsonArray.class); for (JsonElement tenant : tenants) { - if (Objects.equals(tenant.getAsString(), idnAuthenticationProfile.getTenantDomain())) { - flowStartingDomain = idnAuthenticationProfile.getTenantDomain(); + String currentTenantDomain = + PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(true); + if (Objects.equals(tenant.getAsString(), currentTenantDomain)) { + flowStartingDomain = currentTenantDomain; } } } @@ -315,22 +320,41 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe try { PrivilegedCarbonContext.startTenantFlow(); PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(flowStartingDomain, true); - if (APIApplicationManagerExtensionDataHolder.getInstance().getIoAuthClientService(). - doAuthenticate(idnAuthenticationProfile.getUsername(), idnAuthenticationProfile.getPassword())) { - apiApplicationProfile.setOwner(idnAuthenticationProfile.getUsername()); - return registerApiApplication(apiApplicationProfile); + return createApiApplication(apiApplicationProfile); + + } finally { + PrivilegedCarbonContext.endTenantFlow(); + } + } + + @Override + public Token getCustomToken(TokenCreationProfile tokenCreationProfile) throws APIManagerException { + JWTClientManagerService jwtClientManagerService = + APIApplicationManagerExtensionDataHolder.getInstance().getJwtClientManagerService(); + try { + JWTClient jwtClient = jwtClientManagerService.getJWTClient(); + AccessTokenInfo accessTokenInfo = jwtClient.getAccessToken(tokenCreationProfile.getBasicAuthUsername(), + tokenCreationProfile.getBasicAuthPassword(), tokenCreationProfile.getUsername(), + tokenCreationProfile.getScope()); + + if (accessTokenInfo == null) { + String msg = "Received a null token when generating a custom JWT token"; + log.error(msg); + throw new APIManagerException(msg); } - throw new IdnAuthenticationException( - "Failed to authenticate the user : [ " + idnAuthenticationProfile.getUsername() + " ]"); + Token token = new Token(); + token.setAccess_token(accessTokenInfo.getAccessToken()); + token.setRefresh_token(accessTokenInfo.getRefreshToken()); + token.setToken_type(accessTokenInfo.getTokenType()); + token.setScope(accessTokenInfo.getScopes()); + token.setExpires_in(accessTokenInfo.getExpiresIn()); - } catch (OAuthClientException e) { - String msg = - "Error encountered while performing authentication for user : [ " + idnAuthenticationProfile.getUsername() + " ]"; + return token; + } catch (JWTClientException e) { + String msg = "Error encountered while acquiring custom JWT token"; log.error(msg, e); throw new APIManagerException(msg, e); - } finally { - PrivilegedCarbonContext.endTenantFlow(); } } } diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension/src/main/java/io/entgra/device/mgt/core/apimgt/application/extension/dto/ApiApplicationKey.java b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension/src/main/java/io/entgra/device/mgt/core/apimgt/application/extension/bean/ApiApplicationKey.java similarity index 88% rename from components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension/src/main/java/io/entgra/device/mgt/core/apimgt/application/extension/dto/ApiApplicationKey.java rename to components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension/src/main/java/io/entgra/device/mgt/core/apimgt/application/extension/bean/ApiApplicationKey.java index ed99e96cca..1648596637 100644 --- a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension/src/main/java/io/entgra/device/mgt/core/apimgt/application/extension/dto/ApiApplicationKey.java +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension/src/main/java/io/entgra/device/mgt/core/apimgt/application/extension/bean/ApiApplicationKey.java @@ -1,12 +1,12 @@ /* - * Copyright (c) 2018 - 2023, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.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 + * 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 @@ -14,9 +14,10 @@ * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. + * */ -package io.entgra.device.mgt.core.apimgt.application.extension.dto; +package io.entgra.device.mgt.core.apimgt.application.extension.bean; import io.entgra.device.mgt.core.apimgt.application.extension.constants.ApiApplicationConstants; import org.json.simple.JSONObject; diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension/src/main/java/io/entgra/device/mgt/core/apimgt/application/extension/bean/IdnAuthenticationProfile.java b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension/src/main/java/io/entgra/device/mgt/core/apimgt/application/extension/bean/IdnAuthenticationProfile.java deleted file mode 100644 index 102d601f43..0000000000 --- a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension/src/main/java/io/entgra/device/mgt/core/apimgt/application/extension/bean/IdnAuthenticationProfile.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.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.device.mgt.core.apimgt.application.extension.bean; - -import org.wso2.carbon.utils.multitenancy.MultitenantConstants; - -public class IdnAuthenticationProfile { - private String username; - private String password; - private String tenantDomain; - - public String getUsername() { - return username; - } - - public void setUsername(String username) { - this.username = username; - String []usernameParts = this.username.split("@(?=[^@]*$)"); - if (usernameParts.length == 2) { - this.tenantDomain = usernameParts[usernameParts.length - 1]; - return; - } - this.tenantDomain = MultitenantConstants.SUPER_TENANT_DOMAIN_NAME; - } - - public String getPassword() { - return password; - } - - public void setPassword(String password) { - this.password = password; - } - - public String getTenantDomain() { - return tenantDomain; - } - - public void setTenantDomain(String tenantDomain) { - this.tenantDomain = tenantDomain; - } -} diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension/src/main/java/io/entgra/device/mgt/core/apimgt/application/extension/exception/IdnAuthenticationException.java b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension/src/main/java/io/entgra/device/mgt/core/apimgt/application/extension/exception/IdnAuthenticationException.java deleted file mode 100644 index 5de7829896..0000000000 --- a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension/src/main/java/io/entgra/device/mgt/core/apimgt/application/extension/exception/IdnAuthenticationException.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.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.device.mgt.core.apimgt.application.extension.exception; - -public class IdnAuthenticationException extends Exception { - public IdnAuthenticationException(String msg) { - super(msg); - } -} diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/IOAuthClientService.java b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/IOAuthClientService.java index 4a113e40ea..705a3153ee 100644 --- a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/IOAuthClientService.java +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/IOAuthClientService.java @@ -38,14 +38,4 @@ public interface IOAuthClientService { */ OAuthClientResponse execute(Request request) throws OAuthClientException, BadRequestException, UnexpectedResponseException; - - /** - * Use to authenticate user against Identify Server - * - * @param username Username of the user - * @param password Password of the User - * @return Returns true if the requested user is authenticated user, otherwise false - * @throws OAuthClientException Throws when error encountered while authenticating - */ - boolean doAuthenticate(String username, String password) throws OAuthClientException; } diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/OAuthClient.java b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/OAuthClient.java index 53e86b19b5..2876b40dbe 100644 --- a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/OAuthClient.java +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/OAuthClient.java @@ -146,64 +146,6 @@ public class OAuthClient implements IOAuthClientService { return oAuthClientResponse; } - @Override - public boolean doAuthenticate(String username, String password) throws OAuthClientException { - String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(); - - CacheWrapper cacheWrapper = cache.computeIfAbsent(tenantDomain, key -> { - CacheWrapper constructedWrapper = null; - try { - Keys keys = idnDynamicClientRegistration(); - Tokens tokens = idnTokenGeneration(keys); - constructedWrapper = new CacheWrapper(keys, tokens); - } catch (Exception e) { - log.error("Error encountered while updating the cache", e); - } - return constructedWrapper; - }); - - if (cacheWrapper != null) { - String tokenRequestJsonStr = (new JSONObject()) - .put("grant_type", Constants.PASSWORD_GRANT_TYPE) - .put("username", username) - .put("password", password) - .put("scope", Constants.SCOPES) - .put("callbackUrl", Constants.PLACEHOLDING_CALLBACK_URL) - .toString(); - - RequestBody requestBody = RequestBody.Companion.create(tokenRequestJsonStr, JSON); - Request tokenRequest = new Request.Builder() - .url(tokenEndpoint) - .addHeader(Constants.AUTHORIZATION_HEADER_NAME, Credentials.basic(cacheWrapper.keys.consumerKey, - cacheWrapper.keys.consumerSecret)) - .post(requestBody) - .build(); - - try (Response response = client.newCall(tokenRequest).execute()) { - if (response.isSuccessful()) { - Tokens tokens = mapTokens(response.body()); - if (tokens.accessToken != null) { - if (log.isDebugEnabled()) { - log.info("IDN authentication success for user : [ " + username + " ]"); - } - return true; - } - } - } catch (IOException e) { - String msg = - "Error encountered while performing IDN authentication for received user : [ " + username + - " ]"; - log.error(msg, e); - throw new OAuthClientException(msg, e); - - } - } - if (log.isDebugEnabled()) { - log.info("IDN authentication failed for user : [ " + username + " ]"); - } - return false; - } - /** * Dynamic client registration will be handled through here. These clients can be located under carbon console's * service provider section in respective tenants. diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/SubscriptionManagerImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/SubscriptionManagerImpl.java index 92f0734c9f..cf41f30ee8 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/SubscriptionManagerImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/SubscriptionManagerImpl.java @@ -67,7 +67,7 @@ import org.apache.http.impl.client.HttpClients; import org.apache.http.ssl.TrustStrategy; import org.json.JSONArray; import org.json.JSONObject; -import io.entgra.device.mgt.core.apimgt.application.extension.dto.ApiApplicationKey; +import io.entgra.device.mgt.core.apimgt.application.extension.bean.ApiApplicationKey; import io.entgra.device.mgt.core.apimgt.application.extension.exception.APIManagerException; import org.wso2.carbon.context.PrivilegedCarbonContext; import io.entgra.device.mgt.core.application.mgt.common.exception.ApplicationManagementException; diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/OAuthUtils.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/OAuthUtils.java index 581ea51301..dd72f80a92 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/OAuthUtils.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/OAuthUtils.java @@ -20,10 +20,8 @@ package io.entgra.device.mgt.core.application.mgt.core.util; import io.entgra.device.mgt.core.apimgt.application.extension.APIManagementProviderService; import io.entgra.device.mgt.core.apimgt.application.extension.bean.ApiApplicationProfile; -import io.entgra.device.mgt.core.apimgt.application.extension.bean.IdnAuthenticationProfile; -import io.entgra.device.mgt.core.apimgt.application.extension.dto.ApiApplicationKey; +import io.entgra.device.mgt.core.apimgt.application.extension.bean.ApiApplicationKey; import io.entgra.device.mgt.core.apimgt.application.extension.exception.APIManagerException; -import io.entgra.device.mgt.core.apimgt.application.extension.exception.IdnAuthenticationException; import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.BadRequestException; import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.UnexpectedResponseException; import io.entgra.device.mgt.core.application.mgt.common.dto.ApiRegistrationProfile; @@ -63,23 +61,17 @@ public class OAuthUtils { String username = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserRealm() .getRealmConfiguration().getAdminUserName(); PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(username); - String password = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserRealm() - .getRealmConfiguration().getAdminPassword(); PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); APIManagementProviderService apiManagementProviderService = (APIManagementProviderService) ctx. getOSGiService(APIManagementProviderService.class, null); - IdnAuthenticationProfile idnAuthenticationProfile = new IdnAuthenticationProfile(); - idnAuthenticationProfile.setUsername(username); - idnAuthenticationProfile.setPassword(password); - ApiApplicationProfile apiApplicationProfile = new ApiApplicationProfile(); apiApplicationProfile.setApplicationName(registrationProfile.getApplicationName()); apiApplicationProfile.setTags(registrationProfile.getTags()); apiApplicationProfile.setGrantTypes("refresh_token client_credentials password"); apiApplicationKeyInfo = apiManagementProviderService. - registerApiApplication(idnAuthenticationProfile, apiApplicationProfile); - } catch (IdnAuthenticationException | BadRequestException | UnexpectedResponseException e) { + registerApiApplication(apiApplicationProfile); + } catch (BadRequestException | UnexpectedResponseException e) { String msg = "Error encountered while registering api application"; log.error(msg); throw new APIManagerException(msg, e); diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/DeviceManagementServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/DeviceManagementServiceImpl.java index 2ad038ae57..a7426b8c72 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/DeviceManagementServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/DeviceManagementServiceImpl.java @@ -21,10 +21,8 @@ package io.entgra.device.mgt.core.device.mgt.api.jaxrs.service.impl; import com.fasterxml.jackson.databind.ObjectMapper; import io.entgra.device.mgt.core.apimgt.application.extension.APIManagementProviderService; import io.entgra.device.mgt.core.apimgt.application.extension.bean.ApiApplicationProfile; -import io.entgra.device.mgt.core.apimgt.application.extension.bean.IdnAuthenticationProfile; -import io.entgra.device.mgt.core.apimgt.application.extension.dto.ApiApplicationKey; +import io.entgra.device.mgt.core.apimgt.application.extension.bean.ApiApplicationKey; import io.entgra.device.mgt.core.apimgt.application.extension.exception.APIManagerException; -import io.entgra.device.mgt.core.apimgt.application.extension.exception.IdnAuthenticationException; import io.entgra.device.mgt.core.apimgt.application.extension.bean.Token; import io.entgra.device.mgt.core.apimgt.application.extension.bean.TokenCreationProfile; import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.UnexpectedResponseException; @@ -956,28 +954,13 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { try { ApiApplicationKey apiApplicationKey; try { - String adminUserName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserRealm() - .getRealmConfiguration().getAdminUserName(); - String adminPassword = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserRealm() - .getRealmConfiguration().getAdminPassword(); - - IdnAuthenticationProfile idnAuthenticationProfile = new IdnAuthenticationProfile(); - idnAuthenticationProfile.setUsername(adminUserName); - idnAuthenticationProfile.setPassword(adminPassword); - ApiApplicationProfile apiApplicationProfile = new ApiApplicationProfile(); apiApplicationProfile.setApplicationName(applicationName); apiApplicationProfile.setTags(new String[] {"device_management"}); apiApplicationProfile.setGrantTypes("client_credentials password refresh_token"); - apiApplicationKey = apiManagementProviderService.registerApiApplication(idnAuthenticationProfile, apiApplicationProfile); - } catch (UserStoreException e) { - String msg = "Failed to retrieve the tenant" + tenantDomain + "'"; - log.error(msg, e); - return Response.serverError().entity( - new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); - } catch (IdnAuthenticationException | - io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.BadRequestException | + apiApplicationKey = apiManagementProviderService.registerApiApplication(apiApplicationProfile); + } catch (io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.BadRequestException | UnexpectedResponseException e) { String msg = "Error encountered while registering api application"; log.error(msg, e);