diff --git a/components/oauth-extensions/dynamic-client-manager/src/main/java/org/wso2/carbon/identity/oauth/extension/RegistrationService.java b/components/oauth-extensions/dynamic-client-manager/src/main/java/org/wso2/carbon/identity/oauth/extension/RegistrationService.java index 71bc0089a3d..a8660aec90f 100644 --- a/components/oauth-extensions/dynamic-client-manager/src/main/java/org/wso2/carbon/identity/oauth/extension/RegistrationService.java +++ b/components/oauth-extensions/dynamic-client-manager/src/main/java/org/wso2/carbon/identity/oauth/extension/RegistrationService.java @@ -19,6 +19,7 @@ package org.wso2.carbon.identity.oauth.extension; import javax.ws.rs.Consumes; +import javax.ws.rs.DELETE; import javax.ws.rs.POST; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; @@ -31,4 +32,7 @@ public interface RegistrationService { @POST Response register(RegistrationProfile profile); + @DELETE + Response unregister(UnregistrationProfile profile); + } \ No newline at end of file diff --git a/components/oauth-extensions/dynamic-client-manager/src/main/java/org/wso2/carbon/identity/oauth/extension/UnregistrationProfile.java b/components/oauth-extensions/dynamic-client-manager/src/main/java/org/wso2/carbon/identity/oauth/extension/UnregistrationProfile.java new file mode 100644 index 00000000000..ac3f4f317b2 --- /dev/null +++ b/components/oauth-extensions/dynamic-client-manager/src/main/java/org/wso2/carbon/identity/oauth/extension/UnregistrationProfile.java @@ -0,0 +1,51 @@ +/* +* Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +* +* Licensed 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.oauth.extension; + +/** + * This bean class represents the data that are required to unregister + * the oauth application. + */ +public class UnregistrationProfile { + + private String applicationName; + private String consumerKey; + private String userId; + + public String getApplicationName() { + return applicationName; + } + + public void setApplicationName(String applicationName) { + this.applicationName = applicationName; + } + + public String getConsumerKey() { + return consumerKey; + } + + public void setConsumerKey(String consumerKey) { + this.consumerKey = consumerKey; + } + + public String getUserId() { + return userId; + } + + public void setUserId(String userId) { + this.userId = userId; + } +} diff --git a/components/oauth-extensions/dynamic-client-manager/src/main/java/org/wso2/carbon/identity/oauth/extension/impl/ClientRegistrationServiceImpl.java b/components/oauth-extensions/dynamic-client-manager/src/main/java/org/wso2/carbon/identity/oauth/extension/impl/ClientRegistrationServiceImpl.java index 3a112a6b43e..dd277295647 100644 --- a/components/oauth-extensions/dynamic-client-manager/src/main/java/org/wso2/carbon/identity/oauth/extension/impl/ClientRegistrationServiceImpl.java +++ b/components/oauth-extensions/dynamic-client-manager/src/main/java/org/wso2/carbon/identity/oauth/extension/impl/ClientRegistrationServiceImpl.java @@ -39,10 +39,12 @@ import org.wso2.carbon.identity.oauth.extension.ApplicationConstants; import org.wso2.carbon.identity.oauth.extension.OAuthApplicationInfo; import org.wso2.carbon.identity.oauth.extension.RegistrationProfile; import org.wso2.carbon.identity.oauth.extension.RegistrationService; +import org.wso2.carbon.identity.oauth.extension.UnregistrationProfile; import org.wso2.carbon.utils.multitenancy.MultitenantConstants; import org.wso2.carbon.utils.multitenancy.MultitenantUtils; import javax.ws.rs.Consumes; +import javax.ws.rs.DELETE; import javax.ws.rs.POST; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; @@ -75,6 +77,22 @@ public class ClientRegistrationServiceImpl implements RegistrationService { } } + @DELETE + @Override + public Response unregister(UnregistrationProfile profile) { + String applicationName = profile.getApplicationName(); + String consumerKey = profile.getConsumerKey(); + String userId = profile.getUserId(); + try { + this.unregisterApplication(userId, applicationName, consumerKey); + return Response.status(Response.Status.ACCEPTED).build(); + } catch (APIManagementException e) { + String msg = "Error occurred while unregistering client '" + applicationName + "'"; + log.error(msg, e); + return Response.serverError().entity(msg).build(); + } + } + private OAuthApplicationInfo registerApplication(RegistrationProfile profile) throws APIManagementException { OAuthApplicationInfo oAuthApplicationInfo = new OAuthApplicationInfo(); @@ -232,4 +250,45 @@ public class ClientRegistrationServiceImpl implements RegistrationService { return null; } + public void unregisterApplication(String userId, String applicationName, String consumerKey) + throws APIManagementException { + + String tenantDomain = MultitenantUtils.getTenantDomain(userId); + String baseUser = CarbonContext.getThreadLocalCarbonContext().getUsername(); + String userName = MultitenantUtils.getTenantAwareUsername(userId); + + PrivilegedCarbonContext.startTenantFlow(); + PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true); + PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(userName); + + if (userId == null || userId.isEmpty()) { + throw new APIManagementException("Error occurred while unregistering Application: userId cannot be null/empty"); + } + try { + OAuthAdminService oAuthAdminService = new OAuthAdminService(); + OAuthConsumerAppDTO oAuthConsumerAppDTO = oAuthAdminService.getOAuthApplicationData(consumerKey); + + if (oAuthConsumerAppDTO == null) { + throw new APIManagementException("Couldn't retrieve OAuth Consumer Application associated with the " + + "given consumer key: " + consumerKey); + } + oAuthAdminService.removeOAuthApplicationData(consumerKey); + + ApplicationManagementService appMgtService = ApplicationManagementService.getInstance(); + ServiceProvider createdServiceProvider = appMgtService.getApplication(applicationName); + + if (createdServiceProvider == null) { + throw new APIManagementException("Couldn't retrieve Service Provider Application " + applicationName); + } + appMgtService.deleteApplication(applicationName); + + } catch (IdentityApplicationManagementException e) { + APIUtil.handleException("Error occurred while removing ServiceProvider for app " + applicationName, e); + } catch (Exception e) { + APIUtil.handleException("Error occurred while removing OAuthApp " + applicationName, e); + } finally { + PrivilegedCarbonContext.endTenantFlow(); + PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(baseUser); + } + } }