From 3c5a3adc2fc874bae51215f2b60284a58e9bd4a2 Mon Sep 17 00:00:00 2001 From: prabathabey Date: Tue, 26 May 2015 17:08:38 +0530 Subject: [PATCH 01/10] Fixing failures prompted while invoking APIs that are OAuth protected --- .../framework/AuthenticationFrameworkUtil.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/AuthenticationFrameworkUtil.java b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/AuthenticationFrameworkUtil.java index c0728d5c9a..d4a61e570a 100644 --- a/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/AuthenticationFrameworkUtil.java +++ b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/AuthenticationFrameworkUtil.java @@ -64,21 +64,21 @@ public class AuthenticationFrameworkUtil { APIKeyValidationInfoDTO apiKeyValidationDTO = tokenValidator.validateKey(context, version, accessToken, requiredAuthenticationLevel, clientDomain); if (apiKeyValidationDTO.isAuthorized()) { - String userName = apiKeyValidationDTO.getEndUserName(); - PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(userName); + String username = apiKeyValidationDTO.getEndUserName(); + PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(username); try { PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId( - IdentityUtil.getTenantIdOFUser(userName)); + IdentityUtil.getTenantIdOFUser(username)); } catch (IdentityException e) { throw new AuthenticationException("Error occurred while retrieving the tenant ID of user '" + - userName + "'", e); + username + "'", e); } + return true; } else { throw new AuthenticationException(apiKeyValidationDTO.getValidationStatus(), "Access failure for API: " + context + ", version: " + version + " with key: " + accessToken); } - return false; } public static void handleResponse(Request request, Response response, int statusCode, String payload) { From 6cdd122c97e4a108d1f5ec3427a547861114e7d7 Mon Sep 17 00:00:00 2001 From: prabathabey Date: Thu, 28 May 2015 14:53:52 +0530 Subject: [PATCH 02/10] Adding the initial implementation of dynamic client registration --- .../dynamic-client-manager/pom.xml | 37 ++- .../oauth/extension/ApplicationConstants.java | 50 ++++ .../oauth/extension/OAuthApplicationInfo.java | 104 ++++++++ .../oauth/extension/RegistrationProfile.java | 134 ++++++++++ .../oauth/extension/RegistrationResponse.java | 41 +++ .../oauth/extension/RegistrationService.java | 4 +- .../impl/ClientRegistrationServiceImpl.java | 235 ++++++++++++++++++ .../webapp/META-INF/webapp-classloading.xml | 2 +- .../src/main/webapp/WEB-INF/cxf-servlet.xml | 120 +-------- .../src/main/webapp/WEB-INF/web.xml | 28 +-- pom.xml | 23 ++ 11 files changed, 636 insertions(+), 142 deletions(-) create mode 100644 components/oauth-extensions/dynamic-client-manager/src/main/java/org/wso2/carbon/identity/oauth/extension/ApplicationConstants.java create mode 100644 components/oauth-extensions/dynamic-client-manager/src/main/java/org/wso2/carbon/identity/oauth/extension/OAuthApplicationInfo.java create mode 100644 components/oauth-extensions/dynamic-client-manager/src/main/java/org/wso2/carbon/identity/oauth/extension/RegistrationProfile.java create mode 100644 components/oauth-extensions/dynamic-client-manager/src/main/java/org/wso2/carbon/identity/oauth/extension/RegistrationResponse.java create mode 100644 components/oauth-extensions/dynamic-client-manager/src/main/java/org/wso2/carbon/identity/oauth/extension/impl/ClientRegistrationServiceImpl.java diff --git a/components/oauth-extensions/dynamic-client-manager/pom.xml b/components/oauth-extensions/dynamic-client-manager/pom.xml index a04aa0ecea..b00035fa32 100644 --- a/components/oauth-extensions/dynamic-client-manager/pom.xml +++ b/components/oauth-extensions/dynamic-client-manager/pom.xml @@ -118,8 +118,41 @@ org.wso2.carbon.device.mgt.core provided - + + org.wso2.carbon + org.wso2.carbon.utils + provided + + + org.wso2.carbon.identity + org.wso2.carbon.identity.application.mgt + provided + + + org.wso2.carbon.apimgt + org.wso2.carbon.apimgt.api + provided + + + org.wso2.carbon.apimgt + org.wso2.carbon.apimgt.impl + provided + + + org.wso2.carbon.apimgt + org.wso2.carbon.apimgt.keymgt.client + provided + + + org.wso2.carbon.identity + org.wso2.carbon.identity.oauth + provided + + + com.googlecode.json-simple.wso2 + json-simple + provided + - diff --git a/components/oauth-extensions/dynamic-client-manager/src/main/java/org/wso2/carbon/identity/oauth/extension/ApplicationConstants.java b/components/oauth-extensions/dynamic-client-manager/src/main/java/org/wso2/carbon/identity/oauth/extension/ApplicationConstants.java new file mode 100644 index 0000000000..f01ad38814 --- /dev/null +++ b/components/oauth-extensions/dynamic-client-manager/src/main/java/org/wso2/carbon/identity/oauth/extension/ApplicationConstants.java @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2015, 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 org.wso2.carbon.identity.oauth.extension; + +public class ApplicationConstants { + + public static final String OAUTH_CLIENT_ID = "client_id"; //this means consumer key + public static final String OAUTH_CLIENT_SECRET = "client_secret"; + public static final String OAUTH_REDIRECT_URIS = "redirect_uris"; + public static final String OAUTH_CALLBACK_URIS = "callback_url"; + public static final String OAUTH_CLIENT_NAME = "client_name"; + public static final String OAUTH_CLIENT_TYPE = "client_type"; + public static final String APP_KEY_TYPE = "key_type"; + public static final String APP_CALLBACK_URL = "callback_url"; + public static final String APP_HOME_PAGE = "homepage"; + public static final String OAUTH_CLIENT_CONTACT = "contact"; + public static final String APP_LOGOURI = "logouri"; + public static final String OAUTH_CLIENT_SCOPE = "scope"; + public static final String OAUTH_CLIENT_GRANT = "grant_types"; + public static final String OAUTH_CLIENT_RESPONSETYPE = "response_types"; + public static final String OAUTH_CLIENT_AUTHMETHOD = "token_endpoint_auth_method"; + public static final String OAUTH_CLIENT_REGISTRATION_CLIENT_URI = "registration_client_uri"; + public static final String OAUTH_CLIENT_REGISTRATION_ACCESSTOKEN = "registration_access_token"; + public static final String OAUTH_CLIENT_CONTACTS = "contacts"; + public static final String OAUTH_CLIENT_MANUAL = "MANUAL"; + public static final String OAUTH_CLIENT_PRODUCTION = "PRODUCTION"; + public static final String OAUTH_CLIENT_SANDBOX = "SANDBOX"; + public static final String OAUTH_CLIENT_NOACCESSTOKEN = "NO ACCESS TOKEN"; + public static final String OAUTH_CLIENT_JSONPARAMSTRING = "jsonParams"; + public static final String OAUTH_CLIENT_USERNAME = "username"; + public static final String OAUTH_CLIENT_APPLICATION = "application"; + public static final String VALIDITY_PERIOD = "validityPeriod"; + +} diff --git a/components/oauth-extensions/dynamic-client-manager/src/main/java/org/wso2/carbon/identity/oauth/extension/OAuthApplicationInfo.java b/components/oauth-extensions/dynamic-client-manager/src/main/java/org/wso2/carbon/identity/oauth/extension/OAuthApplicationInfo.java new file mode 100644 index 0000000000..e994bad555 --- /dev/null +++ b/components/oauth-extensions/dynamic-client-manager/src/main/java/org/wso2/carbon/identity/oauth/extension/OAuthApplicationInfo.java @@ -0,0 +1,104 @@ +/* + * Copyright (c) 2015, 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 org.wso2.carbon.identity.oauth.extension; + +import org.json.simple.JSONObject; + +import java.util.HashMap; +import java.util.Map; + +public class OAuthApplicationInfo { + + + private String clientId; + private String clientName; + private String callBackURL; + private String clientSecret; + private Map parameters = new HashMap(); + + /** + * get client Id (consumer id) + * @return clientId + */ + public String getClientId() { + return clientId; + } + /** + * set client Id + * @param clientId + */ + public void setClientId(String clientId) { + this.clientId = clientId; + } + + public String getClientSecret() { + return clientSecret; + } + + public void setClientSecret(String clientSecret) { + this.clientSecret = clientSecret; + } + + /** + * Set client Name of OAuthApplication. + * @param clientName + */ + public void setClientName(String clientName){ + this.clientName = clientName; + } + + /** + * Set callback URL of OAuthapplication. + * @param callBackURL + */ + public void setCallBackURL(String callBackURL){ + this.callBackURL = callBackURL; + } + + public void addParameter(String name,Object value){ + parameters.put(name,value); + } + + public Object getParameter(String name){ + return parameters.get(name); + } + + public String getJsonString(){ + + return JSONObject.toJSONString(parameters); + + } + + public String getClientName(){ + return clientName; + } + + public String getCallBackURL(){ + return callBackURL; + } + + public void putAll(Map parameters){ + this.parameters.putAll(parameters); + } + + public void removeParameter(String key){ + this.parameters.remove(key); + } + +} diff --git a/components/oauth-extensions/dynamic-client-manager/src/main/java/org/wso2/carbon/identity/oauth/extension/RegistrationProfile.java b/components/oauth-extensions/dynamic-client-manager/src/main/java/org/wso2/carbon/identity/oauth/extension/RegistrationProfile.java new file mode 100644 index 0000000000..eb2b653ed6 --- /dev/null +++ b/components/oauth-extensions/dynamic-client-manager/src/main/java/org/wso2/carbon/identity/oauth/extension/RegistrationProfile.java @@ -0,0 +1,134 @@ +/* + * Copyright (c) 2015, 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 org.wso2.carbon.identity.oauth.extension; + +import javax.ws.rs.core.Request; + +public class RegistrationProfile { + + private String applicationType; + private String[] redirectUris; + private String clientName; + private String logoUri; + private String subjectType; + private String sectorIdentifierUri; + private String tokenEndpointAuthMethod; + private String jwksUri; + private String userInfoEncryptedResponseAlg; + private String userInfoEncryptedResponseEnc; + private String[] contacts; + private String[] requestUris; + + public String getApplicationType() { + return applicationType; + } + + public void setApplicationType(String applicationType) { + this.applicationType = applicationType; + } + + public String[] getRedirectUris() { + return redirectUris; + } + + public void setRedirectUris(String[] redirectUris) { + this.redirectUris = redirectUris; + } + + public String getClientName() { + return clientName; + } + + public void setClientName(String clientName) { + this.clientName = clientName; + } + + public String getLogoUri() { + return logoUri; + } + + public void setLogoUri(String logoUri) { + this.logoUri = logoUri; + } + + public String getSubjectType() { + return subjectType; + } + + public void setSubjectType(String subjectType) { + this.subjectType = subjectType; + } + + public String getSectorIdentifierUri() { + return sectorIdentifierUri; + } + + public void setSectorIdentifierUri(String sectorIdentifierUri) { + this.sectorIdentifierUri = sectorIdentifierUri; + } + + public String getTokenEndpointAuthMethod() { + return tokenEndpointAuthMethod; + } + + public void setTokenEndpointAuthMethod(String tokenEndpointAuthMethod) { + this.tokenEndpointAuthMethod = tokenEndpointAuthMethod; + } + + public String getJwksUri() { + return jwksUri; + } + + public void setJwksUri(String jwksUri) { + this.jwksUri = jwksUri; + } + + public String getUserInfoEncryptedResponseAlg() { + return userInfoEncryptedResponseAlg; + } + + public void setUserInfoEncryptedResponseAlg(String userInfoEncryptedResponseAlg) { + this.userInfoEncryptedResponseAlg = userInfoEncryptedResponseAlg; + } + + public String getUserInfoEncryptedResponseEnc() { + return userInfoEncryptedResponseEnc; + } + + public void setUserInfoEncryptedResponseEnc(String userInfoEncryptedResponseEnc) { + this.userInfoEncryptedResponseEnc = userInfoEncryptedResponseEnc; + } + + public String[] getContacts() { + return contacts; + } + + public void setContacts(String[] contacts) { + this.contacts = contacts; + } + + public String[] getRequestUris() { + return requestUris; + } + + public void setRequestUris(String[] requestUris) { + this.requestUris = requestUris; + } + +} diff --git a/components/oauth-extensions/dynamic-client-manager/src/main/java/org/wso2/carbon/identity/oauth/extension/RegistrationResponse.java b/components/oauth-extensions/dynamic-client-manager/src/main/java/org/wso2/carbon/identity/oauth/extension/RegistrationResponse.java new file mode 100644 index 0000000000..23d60a8f11 --- /dev/null +++ b/components/oauth-extensions/dynamic-client-manager/src/main/java/org/wso2/carbon/identity/oauth/extension/RegistrationResponse.java @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2015, 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 org.wso2.carbon.identity.oauth.extension; + +import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.core.Response; + +public class RegistrationResponse extends Response { + + @Override + public Object getEntity() { + return null; + } + + @Override + public int getStatus() { + return 0; + } + + @Override + public MultivaluedMap getMetadata() { + return null; + } + +} 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 49795c9f8d..9f2a0edd71 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 @@ -23,16 +23,14 @@ import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Request; import javax.ws.rs.core.Response; @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) -@Path("/connect") public interface RegistrationService { @POST @Path("/register") - Response register(Request request); + Response register(RegistrationProfile 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/impl/ClientRegistrationServiceImpl.java b/components/oauth-extensions/dynamic-client-manager/src/main/java/org/wso2/carbon/identity/oauth/extension/impl/ClientRegistrationServiceImpl.java new file mode 100644 index 0000000000..5b180823c8 --- /dev/null +++ b/components/oauth-extensions/dynamic-client-manager/src/main/java/org/wso2/carbon/identity/oauth/extension/impl/ClientRegistrationServiceImpl.java @@ -0,0 +1,235 @@ +/* + * Copyright (c) 2015, 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 org.wso2.carbon.identity.oauth.extension.impl; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.json.simple.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; +import org.wso2.carbon.apimgt.api.APIManagementException; +import org.wso2.carbon.apimgt.impl.utils.APIUtil; +import org.wso2.carbon.apimgt.keymgt.client.SubscriberKeyMgtClient; +import org.wso2.carbon.context.CarbonContext; +import org.wso2.carbon.context.PrivilegedCarbonContext; +import org.wso2.carbon.identity.application.common.IdentityApplicationManagementException; +import org.wso2.carbon.identity.application.common.model.InboundAuthenticationConfig; +import org.wso2.carbon.identity.application.common.model.InboundAuthenticationRequestConfig; +import org.wso2.carbon.identity.application.common.model.Property; +import org.wso2.carbon.identity.application.common.model.ServiceProvider; +import org.wso2.carbon.identity.application.mgt.ApplicationManagementService; +import org.wso2.carbon.identity.base.IdentityException; +import org.wso2.carbon.identity.oauth.OAuthAdminService; +import org.wso2.carbon.identity.oauth.dto.OAuthConsumerAppDTO; +import org.wso2.carbon.identity.oauth.extension.*; +import org.wso2.carbon.utils.multitenancy.MultitenantConstants; +import org.wso2.carbon.utils.multitenancy.MultitenantUtils; + +import javax.ws.rs.core.Response; + +public class ClientRegistrationServiceImpl implements RegistrationService { + + private static final Log log = LogFactory.getLog(ClientRegistrationServiceImpl.class); + + @Override + public Response register(RegistrationProfile profile) { + try { + PrivilegedCarbonContext.startTenantFlow(); + PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain( + MultitenantConstants.SUPER_TENANT_DOMAIN_NAME); + PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(MultitenantConstants.SUPER_TENANT_ID); + + OAuthApplicationInfo info = this.registerApplication(profile); + return Response.status(Response.Status.ACCEPTED).entity(info.getJsonString()).build(); + } catch (APIManagementException e) { + String msg = "Error occurred while registering client '" + profile.getClientName() + "'"; + log.error(msg, e); + return Response.serverError().entity(msg).build(); + } finally { + PrivilegedCarbonContext.endTenantFlow(); + } + } + + + private OAuthApplicationInfo registerApplication(RegistrationProfile profile) throws APIManagementException { + //OAuthApplications are created by calling to APIKeyMgtSubscriber Service + SubscriberKeyMgtClient keyMgtClient = APIUtil.getKeyManagementClient(); + OAuthApplicationInfo oAuthApplicationInfo = new OAuthApplicationInfo(); + + //Subscriber's name should be passed as a parameter, since it's under the subscriber the OAuth App is created. + String userId = (String) oAuthApplicationInfo.getParameter(ApplicationConstants.OAUTH_CLIENT_USERNAME); + String applicationName = profile.getClientName(); + + if (log.isDebugEnabled()) { + log.debug("Trying to create OAuth application :" + applicationName); + } + + String callBackURL = ""; + if (oAuthApplicationInfo.getParameter("callback_url") != null) { + JSONArray jsonArray = (JSONArray) oAuthApplicationInfo.getParameter("callback_url"); + for (Object callbackUrlObject : jsonArray) { + callBackURL = (String) callbackUrlObject; + } + } + + String tokenScope = (String) oAuthApplicationInfo.getParameter("tokenScope"); + String tokenScopes[] = new String[1]; + tokenScopes[0] = tokenScope; + + oAuthApplicationInfo.addParameter("tokenScope", tokenScopes); + OAuthApplicationInfo info; + try { + info = this.createOAuthApplication(userId, applicationName, callBackURL); + } catch (Exception e) { + throw new APIManagementException("Can not create OAuth application : " + applicationName, e); + } + + if (info == null || info.getJsonString() == null) { + throw new APIManagementException("OAuth app does not contain required data: '" + applicationName + "'"); + } + + oAuthApplicationInfo.setClientName(info.getClientName()); + oAuthApplicationInfo.setClientId(info.getClientId()); + oAuthApplicationInfo.setCallBackURL(info.getCallBackURL()); + oAuthApplicationInfo.setClientSecret(info.getClientSecret()); + + try { + JSONObject jsonObject = new JSONObject(info.getJsonString()); + if (jsonObject.has(ApplicationConstants.OAUTH_REDIRECT_URIS)) { + oAuthApplicationInfo.addParameter(ApplicationConstants.OAUTH_REDIRECT_URIS, jsonObject.get(ApplicationConstants.OAUTH_REDIRECT_URIS)); + } + + if (jsonObject.has(ApplicationConstants.OAUTH_CLIENT_NAME)) { + oAuthApplicationInfo.addParameter(ApplicationConstants. + OAUTH_CLIENT_NAME, jsonObject.get(ApplicationConstants.OAUTH_CLIENT_NAME)); + } + + if (jsonObject.has(ApplicationConstants.OAUTH_CLIENT_GRANT)) { + oAuthApplicationInfo.addParameter(ApplicationConstants. + OAUTH_CLIENT_GRANT, jsonObject.get(ApplicationConstants.OAUTH_CLIENT_GRANT)); + } + } catch (JSONException e) { + throw new APIManagementException("Can not retrieve information of the created OAuth application", e); + } + return oAuthApplicationInfo; + } + + public OAuthApplicationInfo createOAuthApplication( + String userId, String applicationName, String callbackUrl) throws APIManagementException, IdentityException { + + if (userId == null || userId.isEmpty()) { + return null; + } + + String tenantDomain = MultitenantUtils.getTenantDomain(userId); + String baseUser = CarbonContext.getThreadLocalCarbonContext().getUsername(); + String userName = MultitenantUtils.getTenantAwareUsername(userId); + + PrivilegedCarbonContext.startTenantFlow(); + PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true); + + // Acting as the provided user. When creating Service Provider/OAuth App, + // username is fetched from CarbonContext + PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(userName); + + try { + + // Append the username before Application name to make application name unique across two users. + applicationName = userName + "_" + applicationName; + + // Create the Service Provider + ServiceProvider serviceProvider = new ServiceProvider(); + serviceProvider.setApplicationName(applicationName); + serviceProvider.setDescription("Service Provider for application " + applicationName); + + ApplicationManagementService appMgtService = ApplicationManagementService.getInstance(); + appMgtService.createApplication(serviceProvider); + + ServiceProvider createdServiceProvider = appMgtService.getApplication(applicationName); + + if (createdServiceProvider == null) { + throw new APIManagementException("Couldn't create Service Provider Application " + applicationName); + } + + // Then Create OAuthApp + OAuthAdminService oAuthAdminService = new OAuthAdminService(); + + OAuthConsumerAppDTO oAuthConsumerAppDTO = new OAuthConsumerAppDTO(); + + oAuthConsumerAppDTO.setApplicationName(applicationName); + oAuthConsumerAppDTO.setCallbackUrl(callbackUrl); + log.debug("Creating OAuth App " + applicationName); + oAuthAdminService.registerOAuthApplicationData(oAuthConsumerAppDTO); + log.debug("Created OAuth App " + applicationName); + OAuthConsumerAppDTO createdApp = oAuthAdminService.getOAuthApplicationDataByAppName(oAuthConsumerAppDTO + .getApplicationName()); + log.debug("Retrieved Details for OAuth App " + createdApp.getApplicationName()); + + // Set the OAuthApp in InboundAuthenticationConfig + InboundAuthenticationConfig inboundAuthenticationConfig = new InboundAuthenticationConfig(); + InboundAuthenticationRequestConfig[] inboundAuthenticationRequestConfigs = new + InboundAuthenticationRequestConfig[1]; + InboundAuthenticationRequestConfig inboundAuthenticationRequestConfig = new + InboundAuthenticationRequestConfig(); + + inboundAuthenticationRequestConfig.setInboundAuthKey(createdApp.getOauthConsumerKey()); + inboundAuthenticationRequestConfig.setInboundAuthType("oauth2"); + if (createdApp.getOauthConsumerSecret() != null && !createdApp. + getOauthConsumerSecret().isEmpty()) { + Property property = new Property(); + property.setName("oauthConsumerSecret"); + property.setValue(createdApp.getOauthConsumerSecret()); + Property[] properties = {property}; + inboundAuthenticationRequestConfig.setProperties(properties); + } + + inboundAuthenticationRequestConfigs[0] = inboundAuthenticationRequestConfig; + inboundAuthenticationConfig.setInboundAuthenticationRequestConfigs(inboundAuthenticationRequestConfigs); + createdServiceProvider.setInboundAuthenticationConfig(inboundAuthenticationConfig); + + // Update the Service Provider app to add OAuthApp as an Inbound Authentication Config + appMgtService.updateApplication(createdServiceProvider); + + + OAuthApplicationInfo oAuthApplicationInfo = new OAuthApplicationInfo(); + oAuthApplicationInfo.setClientId(createdApp.getOauthConsumerKey()); + oAuthApplicationInfo.setCallBackURL(createdApp.getCallbackUrl()); + oAuthApplicationInfo.setClientSecret(createdApp.getOauthConsumerSecret()); + + oAuthApplicationInfo.addParameter(ApplicationConstants. + OAUTH_REDIRECT_URIS, createdApp.getCallbackUrl()); + oAuthApplicationInfo.addParameter(ApplicationConstants. + OAUTH_CLIENT_NAME, createdApp.getApplicationName()); + oAuthApplicationInfo.addParameter(ApplicationConstants. + OAUTH_CLIENT_GRANT, createdApp.getGrantTypes()); + + return oAuthApplicationInfo; + + } catch (IdentityApplicationManagementException e) { + APIUtil.handleException("Error occurred while creating ServiceProvider for app " + applicationName, e); + } catch (Exception e) { + APIUtil.handleException("Error occurred while creating OAuthApp " + applicationName, e); + } finally { + PrivilegedCarbonContext.endTenantFlow(); + PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(baseUser); + } + return null; + } + +} diff --git a/components/oauth-extensions/dynamic-client-manager/src/main/webapp/META-INF/webapp-classloading.xml b/components/oauth-extensions/dynamic-client-manager/src/main/webapp/META-INF/webapp-classloading.xml index 0371f7c294..38ac535842 100644 --- a/components/oauth-extensions/dynamic-client-manager/src/main/webapp/META-INF/webapp-classloading.xml +++ b/components/oauth-extensions/dynamic-client-manager/src/main/webapp/META-INF/webapp-classloading.xml @@ -31,5 +31,5 @@ Tomcat environment is the default and every webapps gets it even if they didn't specify it. e.g. If a webapps requires CXF, they will get both Tomcat and CXF. --> - Carbon + CXF,Carbon diff --git a/components/oauth-extensions/dynamic-client-manager/src/main/webapp/WEB-INF/cxf-servlet.xml b/components/oauth-extensions/dynamic-client-manager/src/main/webapp/WEB-INF/cxf-servlet.xml index dc68cd55e3..94aac35f68 100644 --- a/components/oauth-extensions/dynamic-client-manager/src/main/webapp/WEB-INF/cxf-servlet.xml +++ b/components/oauth-extensions/dynamic-client-manager/src/main/webapp/WEB-INF/cxf-servlet.xml @@ -19,130 +19,20 @@ + http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd"> - - - + + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + diff --git a/components/oauth-extensions/dynamic-client-manager/src/main/webapp/WEB-INF/web.xml b/components/oauth-extensions/dynamic-client-manager/src/main/webapp/WEB-INF/web.xml index 645e44dae5..a89397675a 100644 --- a/components/oauth-extensions/dynamic-client-manager/src/main/webapp/WEB-INF/web.xml +++ b/components/oauth-extensions/dynamic-client-manager/src/main/webapp/WEB-INF/web.xml @@ -18,34 +18,20 @@ ~ * under the License. ~ */ --> - - CDM-Windows-API - - - contextConfigLocation - /WEB-INF/cxf-servlet.xml - - - - org.wso2.carbon.mdm.mobileservices.windows.common.util.ConfigInitializerContextListener - - - + + Admin-Webapp - JAX-WS/JAX-RS-windows Endpoint - JAX-WS/JAX-RS-windows Servlet - JAXServlet-windows + JAX-WS/JAX-RS Device Registration Agent Endpoint + JAX-WS/JAX-RS Servlet + CXFServlet org.apache.cxf.transport.servlet.CXFServlet 1 - JAXServlet-windows - /services/* + CXFServlet + /* 60 diff --git a/pom.xml b/pom.xml index cf2b6fc79f..98d51d2cd8 100644 --- a/pom.xml +++ b/pom.xml @@ -592,6 +592,11 @@ + + org.wso2.carbon.apimgt + org.wso2.carbon.apimgt.keymgt.client + ${carbon.api.mgt.version} + org.wso2.carbon.apimgt org.wso2.carbon.apimgt.impl @@ -877,6 +882,7 @@ ${cxf.version} + org.wso2.carbon.commons @@ -890,6 +896,22 @@ ${carbon.commons.version} + + + org.wso2.carbon.identity + org.wso2.carbon.identity.application.mgt + ${carbon.identity.version} + + + org.wso2.carbon.identity + org.wso2.carbon.identity.oauth + ${carbon.identity.version} + + + com.googlecode.json-simple.wso2 + json-simple + ${json-simple.version} + @@ -1150,6 +1172,7 @@ 2.6.1 2.5.11 + 1.1.wso2v1 From e21c92a1ce84fd5973c81d0cd2482c70796443d2 Mon Sep 17 00:00:00 2001 From: prabathabey Date: Thu, 28 May 2015 14:54:21 +0530 Subject: [PATCH 03/10] Code cleanup --- .../framework/AuthenticationFrameworkUtil.java | 6 ------ 1 file changed, 6 deletions(-) diff --git a/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/AuthenticationFrameworkUtil.java b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/AuthenticationFrameworkUtil.java index d4a61e570a..e952a31384 100644 --- a/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/AuthenticationFrameworkUtil.java +++ b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/AuthenticationFrameworkUtil.java @@ -17,17 +17,12 @@ */ package org.wso2.carbon.webapp.authenticator.framework; -import org.apache.axiom.om.OMAbstractFactory; -import org.apache.axiom.om.OMElement; -import org.apache.axiom.om.OMFactory; -import org.apache.axiom.om.OMNamespace; import org.apache.catalina.connector.Request; import org.apache.catalina.connector.Response; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.w3c.dom.Document; import org.wso2.carbon.apimgt.api.APIManagementException; -import org.wso2.carbon.apimgt.core.APIManagerErrorConstants; import org.wso2.carbon.apimgt.core.authenticate.APITokenValidator; import org.wso2.carbon.apimgt.impl.APIConstants; import org.wso2.carbon.apimgt.impl.dto.APIKeyValidationInfoDTO; @@ -35,7 +30,6 @@ import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.identity.base.IdentityException; import org.wso2.carbon.identity.core.util.IdentityUtil; -import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; From de8914fdcb9eaeb14dc7358466279eb0e1361a4a Mon Sep 17 00:00:00 2001 From: mharindu Date: Thu, 28 May 2015 14:55:34 +0530 Subject: [PATCH 04/10] Added a function to retrieve device list from device name --- .../DeviceManagementServiceProviderImpl.java | 43 ++++++++++++++- .../carbon/device/mgt/core/dao/DeviceDAO.java | 10 ++++ .../mgt/core/dao/impl/DeviceDAOImpl.java | 55 +++++++++++++++++++ .../core/service/DeviceManagementService.java | 13 ++++- .../service/DeviceManagementServiceImpl.java | 6 ++ 5 files changed, 125 insertions(+), 2 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementServiceProviderImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementServiceProviderImpl.java index 4dbd0bf9c8..3242ebec9b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementServiceProviderImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementServiceProviderImpl.java @@ -596,7 +596,48 @@ public class DeviceManagementServiceProviderImpl implements DeviceManagementServ int deviceCount = this.deviceDAO.getDeviceCount(); return deviceCount; } catch (DeviceManagementDAOException e) { - throw new DeviceManagementException("Error occurred while obtaining devices all devices", e); + log.error("Error occurred while counting devices", e); + throw new DeviceManagementException("Error occurred while counting devices", e); + } + } + + @Override + public List getDevicesByName(String deviceName, int tenantId) throws DeviceManagementException { + List devicesOfUser = new ArrayList(); + List devicesList; + Device convertedDevice; + DeviceIdentifier deviceIdentifier; + DeviceManager dms; + Device dmsDevice; + org.wso2.carbon.device.mgt.core.dto.Device device; + + try { + devicesList = this.getDeviceDAO().getDevicesByName(deviceName, tenantId); + } catch (DeviceManagementDAOException e) { + throw new DeviceManagementException("Error occurred while fetching the list of devices that matches to '" + + deviceName + "'", e); + } + + for (int x = 0; x < devicesList.size(); x++) { + device = devicesList.get(x); + try { + device.setDeviceType(deviceTypeDAO.getDeviceType(device.getDeviceTypeId())); + dms = this.getPluginRepository().getDeviceManagementProvider(device.getDeviceType().getName()); + convertedDevice = DeviceManagementDAOUtil.convertDevice(device, device.getDeviceType()); + deviceIdentifier = new DeviceIdentifier(); + deviceIdentifier.setId(device.getDeviceIdentificationId()); + deviceIdentifier.setType(device.getDeviceType().getName()); + dmsDevice = dms.getDevice(deviceIdentifier); + if (dmsDevice != null) { + convertedDevice.setProperties(dmsDevice.getProperties()); + convertedDevice.setFeatures(dmsDevice.getFeatures()); + } + devicesOfUser.add(convertedDevice); + } catch (DeviceManagementDAOException e) { + log.error("Error occurred while obtaining the device type of DeviceTypeId '" + + device.getDeviceTypeId() + "'", e); + } } + return devicesOfUser; } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java index 0379cd9ede..1380194e28 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java @@ -62,8 +62,18 @@ public interface DeviceDAO { /** * Get the count of devices + * * @return device count * @throws DeviceManagementDAOException */ int getDeviceCount() throws DeviceManagementDAOException; + + /** + * Get the list of devices that matches with the given device name. + * + * @param deviceName Name of the device + * @return List of devices that matches with the given device name. + * @throws DeviceManagementDAOException + */ + List getDevicesByName(String deviceName , int tenantId) throws DeviceManagementDAOException; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java index 1d97328c4a..2a38f4acaa 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java @@ -330,4 +330,59 @@ public class DeviceDAOImpl implements DeviceDAO { return deviceCount; } + /** + * Get the list of devices that matches with the given device name. + * + * @param deviceName Name of the device. + * @param tenantId + * @return device list + * @throws DeviceManagementDAOException + */ + @Override + public List getDevicesByName(String deviceName, int tenantId) throws DeviceManagementDAOException { + Connection conn = this.getConnection(); + PreparedStatement stmt = null; + List deviceList = new ArrayList(); + try { + stmt = conn.prepareStatement( + "SELECT DM_DEVICE_TYPE.ID, DM_DEVICE_TYPE.NAME, DM_DEVICE.ID, DM_DEVICE.DESCRIPTION, " + + "DM_DEVICE.NAME, DM_DEVICE.DATE_OF_ENROLLMENT, DM_DEVICE.DATE_OF_LAST_UPDATE, " + + "DM_DEVICE.OWNERSHIP, DM_DEVICE.STATUS, DM_DEVICE.DEVICE_TYPE_ID, " + + "DM_DEVICE.DEVICE_IDENTIFICATION, DM_DEVICE.OWNER, DM_DEVICE.TENANT_ID FROM " + + "DM_DEVICE, DM_DEVICE_TYPE WHERE DM_DEVICE.DEVICE_TYPE_ID = DM_DEVICE_TYPE.ID " + + "AND DM_DEVICE.NAME LIKE %?% AND DM_DEVICE.TENANT_ID =?"); + stmt.setString(1, deviceName); + stmt.setInt(2, tenantId); + ResultSet resultSet = stmt.executeQuery(); + + while (resultSet.next()) { + Device device = new Device(); + DeviceType deviceType = new DeviceType(); + int id = resultSet.getInt(resultSet.getInt(1)); + deviceType.setId(id); + deviceType.setName(resultSet.getString(2)); + device.setId(resultSet.getInt(3)); + device.setDescription(resultSet.getString(4)); + device.setName(resultSet.getString(5)); + device.setDateOfEnrollment(resultSet.getLong(6)); + device.setDateOfLastUpdate(resultSet.getLong(7)); + //TODO:- Ownership is not a enum in DeviceDAO + device.setOwnerShip(resultSet.getString(8)); + device.setStatus(Status.valueOf(resultSet.getString(9))); + device.setDeviceTypeId(resultSet.getInt(10)); + device.setDeviceIdentificationId(resultSet.getString(11)); + device.setOwnerId(resultSet.getString(12)); + device.setTenantId(resultSet.getInt(13)); + deviceList.add(device); + } + } catch (SQLException e) { + String msg = "Error occurred while fetching the list of devices that matches to '" + deviceName + "'"; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } finally { + DeviceManagementDAOUtil.cleanupResources(conn, stmt, null); + } + return deviceList; + } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementService.java index 6b9820068e..27b0d700c9 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementService.java @@ -73,7 +73,18 @@ public interface DeviceManagementService extends DeviceManager, LicenseManager, /** * Method to get the count of all types of devices. * @return device count - * @throws DeviceManagementException + * @throws DeviceManagementException If some unusual behaviour is observed while counting + * the devices */ int getDeviceCount() throws DeviceManagementException; + + /** + * Method to get the list of devices that matches with the given device name. + * + * @param deviceName name of the device + * @return List of devices that matches with the given device name. + * @throws DeviceManagementException If some unusual behaviour is observed while fetching the + * device list + */ + List getDevicesByName(String deviceName, int tenantId) throws DeviceManagementException; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementServiceImpl.java index a76cc2f9ed..3b15a36389 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementServiceImpl.java @@ -211,4 +211,10 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider() .getDeviceCount(); } + + @Override + public List getDevicesByName(String deviceName, int tenantId) throws DeviceManagementException { + return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider() + .getDevicesByName(deviceName, tenantId); + } } From 68cefe21a5c2244e84ce6e8a7edbd27a534f4387 Mon Sep 17 00:00:00 2001 From: prabathabey Date: Thu, 28 May 2015 15:20:00 +0530 Subject: [PATCH 05/10] Fixing issues encountered in dynamic client authentication implementation --- .../oauth/extension/RegistrationProfile.java | 11 +++++++++-- .../oauth/extension/RegistrationService.java | 1 - .../impl/ClientRegistrationServiceImpl.java | 16 ++++++++-------- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/components/oauth-extensions/dynamic-client-manager/src/main/java/org/wso2/carbon/identity/oauth/extension/RegistrationProfile.java b/components/oauth-extensions/dynamic-client-manager/src/main/java/org/wso2/carbon/identity/oauth/extension/RegistrationProfile.java index eb2b653ed6..fad85d0f79 100644 --- a/components/oauth-extensions/dynamic-client-manager/src/main/java/org/wso2/carbon/identity/oauth/extension/RegistrationProfile.java +++ b/components/oauth-extensions/dynamic-client-manager/src/main/java/org/wso2/carbon/identity/oauth/extension/RegistrationProfile.java @@ -18,8 +18,6 @@ */ package org.wso2.carbon.identity.oauth.extension; -import javax.ws.rs.core.Request; - public class RegistrationProfile { private String applicationType; @@ -34,6 +32,7 @@ public class RegistrationProfile { private String userInfoEncryptedResponseEnc; private String[] contacts; private String[] requestUris; + private String owner; public String getApplicationType() { return applicationType; @@ -131,4 +130,12 @@ public class RegistrationProfile { this.requestUris = requestUris; } + public String getOwner() { + return owner; + } + + public void setOwner(String owner) { + this.owner = owner; + } + } 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 9f2a0edd71..88028bbd0f 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 @@ -30,7 +30,6 @@ import javax.ws.rs.core.Response; public interface RegistrationService { @POST - @Path("/register") Response register(RegistrationProfile 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/impl/ClientRegistrationServiceImpl.java b/components/oauth-extensions/dynamic-client-manager/src/main/java/org/wso2/carbon/identity/oauth/extension/impl/ClientRegistrationServiceImpl.java index 5b180823c8..5fdd77cf91 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 @@ -20,12 +20,11 @@ package org.wso2.carbon.identity.oauth.extension.impl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.json.simple.JSONArray; import org.json.JSONException; import org.json.JSONObject; +import org.json.simple.JSONArray; import org.wso2.carbon.apimgt.api.APIManagementException; import org.wso2.carbon.apimgt.impl.utils.APIUtil; -import org.wso2.carbon.apimgt.keymgt.client.SubscriberKeyMgtClient; import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.identity.application.common.IdentityApplicationManagementException; @@ -37,7 +36,10 @@ import org.wso2.carbon.identity.application.mgt.ApplicationManagementService; import org.wso2.carbon.identity.base.IdentityException; import org.wso2.carbon.identity.oauth.OAuthAdminService; import org.wso2.carbon.identity.oauth.dto.OAuthConsumerAppDTO; -import org.wso2.carbon.identity.oauth.extension.*; +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.utils.multitenancy.MultitenantConstants; import org.wso2.carbon.utils.multitenancy.MultitenantUtils; @@ -68,19 +70,17 @@ public class ClientRegistrationServiceImpl implements RegistrationService { private OAuthApplicationInfo registerApplication(RegistrationProfile profile) throws APIManagementException { - //OAuthApplications are created by calling to APIKeyMgtSubscriber Service - SubscriberKeyMgtClient keyMgtClient = APIUtil.getKeyManagementClient(); OAuthApplicationInfo oAuthApplicationInfo = new OAuthApplicationInfo(); //Subscriber's name should be passed as a parameter, since it's under the subscriber the OAuth App is created. - String userId = (String) oAuthApplicationInfo.getParameter(ApplicationConstants.OAUTH_CLIENT_USERNAME); + String userId = profile.getOwner(); String applicationName = profile.getClientName(); if (log.isDebugEnabled()) { - log.debug("Trying to create OAuth application :" + applicationName); + log.debug("Trying to create OAuth application: '" + applicationName + "'"); } - String callBackURL = ""; + String callBackURL = null; if (oAuthApplicationInfo.getParameter("callback_url") != null) { JSONArray jsonArray = (JSONArray) oAuthApplicationInfo.getParameter("callback_url"); for (Object callbackUrlObject : jsonArray) { From 3d549701a4bd6bb49518cbd31e51a4261b9eec9a Mon Sep 17 00:00:00 2001 From: mharindu Date: Thu, 28 May 2015 17:45:11 +0530 Subject: [PATCH 06/10] Fixed query issue in function getDevicesByName --- .../wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java index 2a38f4acaa..61ff67e5a7 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java @@ -350,8 +350,8 @@ public class DeviceDAOImpl implements DeviceDAO { "DM_DEVICE.OWNERSHIP, DM_DEVICE.STATUS, DM_DEVICE.DEVICE_TYPE_ID, " + "DM_DEVICE.DEVICE_IDENTIFICATION, DM_DEVICE.OWNER, DM_DEVICE.TENANT_ID FROM " + "DM_DEVICE, DM_DEVICE_TYPE WHERE DM_DEVICE.DEVICE_TYPE_ID = DM_DEVICE_TYPE.ID " + - "AND DM_DEVICE.NAME LIKE %?% AND DM_DEVICE.TENANT_ID =?"); - stmt.setString(1, deviceName); + "AND DM_DEVICE.NAME LIKE ? AND DM_DEVICE.TENANT_ID =?"); + stmt.setString(1, deviceName + "%"); stmt.setInt(2, tenantId); ResultSet resultSet = stmt.executeQuery(); @@ -366,7 +366,6 @@ public class DeviceDAOImpl implements DeviceDAO { device.setName(resultSet.getString(5)); device.setDateOfEnrollment(resultSet.getLong(6)); device.setDateOfLastUpdate(resultSet.getLong(7)); - //TODO:- Ownership is not a enum in DeviceDAO device.setOwnerShip(resultSet.getString(8)); device.setStatus(Status.valueOf(resultSet.getString(9))); device.setDeviceTypeId(resultSet.getInt(10)); From cc3f5437683e6773c86d0a380296a3973ac2fb6f Mon Sep 17 00:00:00 2001 From: prabathabey Date: Thu, 28 May 2015 19:45:37 +0530 Subject: [PATCH 07/10] Fixing dyanamic client registration related issues --- components/oauth-extensions/dynamic-client-manager/pom.xml | 4 ++++ .../identity/oauth/extension/RegistrationService.java | 1 - .../src/main/webapp/WEB-INF/cxf-servlet.xml | 4 ++++ pom.xml | 7 ++++++- 4 files changed, 14 insertions(+), 2 deletions(-) diff --git a/components/oauth-extensions/dynamic-client-manager/pom.xml b/components/oauth-extensions/dynamic-client-manager/pom.xml index b00035fa32..03bd254164 100644 --- a/components/oauth-extensions/dynamic-client-manager/pom.xml +++ b/components/oauth-extensions/dynamic-client-manager/pom.xml @@ -153,6 +153,10 @@ json-simple provided + + org.codehaus.jackson + jackson-jaxrs + 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 88028bbd0f..71bc0089a3 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 @@ -20,7 +20,6 @@ package org.wso2.carbon.identity.oauth.extension; import javax.ws.rs.Consumes; import javax.ws.rs.POST; -import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; diff --git a/components/oauth-extensions/dynamic-client-manager/src/main/webapp/WEB-INF/cxf-servlet.xml b/components/oauth-extensions/dynamic-client-manager/src/main/webapp/WEB-INF/cxf-servlet.xml index 94aac35f68..a38fa222e6 100644 --- a/components/oauth-extensions/dynamic-client-manager/src/main/webapp/WEB-INF/cxf-servlet.xml +++ b/components/oauth-extensions/dynamic-client-manager/src/main/webapp/WEB-INF/cxf-servlet.xml @@ -31,8 +31,12 @@ + + + + diff --git a/pom.xml b/pom.xml index 98d51d2cd8..558ff162fc 100644 --- a/pom.xml +++ b/pom.xml @@ -912,6 +912,11 @@ json-simple ${json-simple.version} + + org.codehaus.jackson + jackson-jaxrs + ${jackson.version} + @@ -1173,7 +1178,7 @@ 2.6.1 2.5.11 1.1.wso2v1 - + 1.9.0 From e909ac8bcc90e8d05c827316e7a1c9e6b043b732 Mon Sep 17 00:00:00 2001 From: prabathabey Date: Thu, 28 May 2015 19:58:41 +0530 Subject: [PATCH 08/10] Fixing issues prompted due to not properly inheriting JAX-RS annotations --- .../extension/impl/ClientRegistrationServiceImpl.java | 7 +++++++ 1 file changed, 7 insertions(+) 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 5fdd77cf91..a6791c7f97 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 @@ -43,12 +43,19 @@ import org.wso2.carbon.identity.oauth.extension.RegistrationService; import org.wso2.carbon.utils.multitenancy.MultitenantConstants; import org.wso2.carbon.utils.multitenancy.MultitenantUtils; +import javax.ws.rs.Consumes; +import javax.ws.rs.POST; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; +@Produces(MediaType.APPLICATION_JSON) +@Consumes(MediaType.APPLICATION_JSON) public class ClientRegistrationServiceImpl implements RegistrationService { private static final Log log = LogFactory.getLog(ClientRegistrationServiceImpl.class); + @POST @Override public Response register(RegistrationProfile profile) { try { From 4e17a2669f114305d6e8ccb8c3f3a53d216be15e Mon Sep 17 00:00:00 2001 From: prabathabey Date: Thu, 28 May 2015 21:44:48 +0530 Subject: [PATCH 09/10] Fixing more dynamic authentication related issues --- .../oauth/extension/RegistrationProfile.java | 18 ++++++++++++++++++ .../impl/ClientRegistrationServiceImpl.java | 10 ++-------- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/components/oauth-extensions/dynamic-client-manager/src/main/java/org/wso2/carbon/identity/oauth/extension/RegistrationProfile.java b/components/oauth-extensions/dynamic-client-manager/src/main/java/org/wso2/carbon/identity/oauth/extension/RegistrationProfile.java index fad85d0f79..8014bde2f5 100644 --- a/components/oauth-extensions/dynamic-client-manager/src/main/java/org/wso2/carbon/identity/oauth/extension/RegistrationProfile.java +++ b/components/oauth-extensions/dynamic-client-manager/src/main/java/org/wso2/carbon/identity/oauth/extension/RegistrationProfile.java @@ -33,6 +33,8 @@ public class RegistrationProfile { private String[] contacts; private String[] requestUris; private String owner; + private String callbackUrl; + private String tokenScope; public String getApplicationType() { return applicationType; @@ -138,4 +140,20 @@ public class RegistrationProfile { this.owner = owner; } + public String getCallbackUrl() { + return callbackUrl; + } + + public void setCallbackUrl(String callbackUrl) { + this.callbackUrl = callbackUrl; + } + + public String getTokenScope() { + return tokenScope; + } + + public void setTokenScope(String tokenScope) { + this.tokenScope = tokenScope; + } + } 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 a6791c7f97..23b8aac24c 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 @@ -87,15 +87,9 @@ public class ClientRegistrationServiceImpl implements RegistrationService { log.debug("Trying to create OAuth application: '" + applicationName + "'"); } - String callBackURL = null; - if (oAuthApplicationInfo.getParameter("callback_url") != null) { - JSONArray jsonArray = (JSONArray) oAuthApplicationInfo.getParameter("callback_url"); - for (Object callbackUrlObject : jsonArray) { - callBackURL = (String) callbackUrlObject; - } - } + String callBackURL = profile.getCallbackUrl(); - String tokenScope = (String) oAuthApplicationInfo.getParameter("tokenScope"); + String tokenScope = profile.getTokenScope(); String tokenScopes[] = new String[1]; tokenScopes[0] = tokenScope; From a3e385bd2ff3ea407c312658f1a10fc51a44da83 Mon Sep 17 00:00:00 2001 From: prabathabey Date: Thu, 28 May 2015 22:06:40 +0530 Subject: [PATCH 10/10] Serializing the complete OAuthAPplicationInfo bean --- .../oauth/extension/OAuthApplicationInfo.java | 11 +++++++++++ .../extension/impl/ClientRegistrationServiceImpl.java | 4 +++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/components/oauth-extensions/dynamic-client-manager/src/main/java/org/wso2/carbon/identity/oauth/extension/OAuthApplicationInfo.java b/components/oauth-extensions/dynamic-client-manager/src/main/java/org/wso2/carbon/identity/oauth/extension/OAuthApplicationInfo.java index e994bad555..74206f3def 100644 --- a/components/oauth-extensions/dynamic-client-manager/src/main/java/org/wso2/carbon/identity/oauth/extension/OAuthApplicationInfo.java +++ b/components/oauth-extensions/dynamic-client-manager/src/main/java/org/wso2/carbon/identity/oauth/extension/OAuthApplicationInfo.java @@ -18,8 +18,10 @@ */ package org.wso2.carbon.identity.oauth.extension; +import org.codehaus.jackson.map.ObjectMapper; import org.json.simple.JSONObject; +import java.io.IOException; import java.util.HashMap; import java.util.Map; @@ -101,4 +103,13 @@ public class OAuthApplicationInfo { this.parameters.remove(key); } + public String toString() { + ObjectMapper mapper = new ObjectMapper(); + try { + return mapper.writeValueAsString(this); + } catch (IOException e) { + throw new RuntimeException("Error occurred while serializing OAuthApplicationInfo bean"); + } + } + } 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 23b8aac24c..f61454ebc0 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 @@ -65,7 +65,7 @@ public class ClientRegistrationServiceImpl implements RegistrationService { PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(MultitenantConstants.SUPER_TENANT_ID); OAuthApplicationInfo info = this.registerApplication(profile); - return Response.status(Response.Status.ACCEPTED).entity(info.getJsonString()).build(); + return Response.status(Response.Status.ACCEPTED).entity(info.toString()).build(); } catch (APIManagementException e) { String msg = "Error occurred while registering client '" + profile.getClientName() + "'"; log.error(msg, e); @@ -125,6 +125,8 @@ public class ClientRegistrationServiceImpl implements RegistrationService { oAuthApplicationInfo.addParameter(ApplicationConstants. OAUTH_CLIENT_GRANT, jsonObject.get(ApplicationConstants.OAUTH_CLIENT_GRANT)); } + + } catch (JSONException e) { throw new APIManagementException("Can not retrieve information of the created OAuth application", e); }