Fixing the issue where client type is not honoured in dynamic client registration service

4.x.x
prabathabey 9 years ago
parent 10fd6f51d1
commit 8578ff60ca

@ -41,7 +41,9 @@ import org.wso2.carbon.identity.sso.saml.dto.SAMLSSOServiceProviderDTO;
import org.wso2.carbon.registry.core.Registry; import org.wso2.carbon.registry.core.Registry;
import org.wso2.carbon.utils.multitenancy.MultitenantUtils; import org.wso2.carbon.utils.multitenancy.MultitenantUtils;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List;
/** /**
* Implementation of DynamicClientRegistrationService. * Implementation of DynamicClientRegistrationService.
@ -60,9 +62,12 @@ public class DynamicClientRegistrationServiceImpl implements DynamicClientRegist
private static final int STEP_ORDER = 1; private static final int STEP_ORDER = 1;
private static final String OAUTH_VERSION = "OAuth-2.0"; private static final String OAUTH_VERSION = "OAuth-2.0";
private static final String APPLICATION_TYPE_WEBAPP = "webapp";
private static final String APPLICATION_TYPE_DEVICE = "device";
@Override @Override
public OAuthApplicationInfo registerOAuthApplication(RegistrationProfile profile) throws public OAuthApplicationInfo registerOAuthApplication(
DynamicClientRegistrationException { RegistrationProfile profile) throws DynamicClientRegistrationException {
OAuthApplicationInfo oAuthApplicationInfo = new OAuthApplicationInfo(); OAuthApplicationInfo oAuthApplicationInfo = new OAuthApplicationInfo();
String applicationName = profile.getClientName(); String applicationName = profile.getClientName();
@ -78,9 +83,7 @@ public class DynamicClientRegistrationServiceImpl implements DynamicClientRegist
OAuthApplicationInfo info; OAuthApplicationInfo info;
try { try {
info = this.createOAuthApplication(profile); info = this.createOAuthApplication(profile);
} catch (DynamicClientRegistrationException e) { } catch (DynamicClientRegistrationException | IdentityException e) {
throw new DynamicClientRegistrationException("Can not create OAuth application : " + applicationName, e);
} catch (IdentityException e) {
throw new DynamicClientRegistrationException("Can not create OAuth application : " + applicationName, e); throw new DynamicClientRegistrationException("Can not create OAuth application : " + applicationName, e);
} }
@ -98,13 +101,13 @@ public class DynamicClientRegistrationServiceImpl implements DynamicClientRegist
JSONObject jsonObject = new JSONObject(info.getJsonString()); JSONObject jsonObject = new JSONObject(info.getJsonString());
if (jsonObject.has(DCRConstants.ClientMetadata.OAUTH_REDIRECT_URIS)) { if (jsonObject.has(DCRConstants.ClientMetadata.OAUTH_REDIRECT_URIS)) {
oAuthApplicationInfo.addParameter(DCRConstants.ClientMetadata.OAUTH_REDIRECT_URIS, oAuthApplicationInfo.addParameter(DCRConstants.ClientMetadata.OAUTH_REDIRECT_URIS,
jsonObject jsonObject
.get(DCRConstants.ClientMetadata.OAUTH_REDIRECT_URIS)); .get(DCRConstants.ClientMetadata.OAUTH_REDIRECT_URIS));
} }
if (jsonObject.has(DCRConstants.ClientMetadata.OAUTH_CLIENT_GRANT)) { if (jsonObject.has(DCRConstants.ClientMetadata.OAUTH_CLIENT_GRANT)) {
oAuthApplicationInfo.addParameter(DCRConstants.ClientMetadata.OAUTH_CLIENT_GRANT, jsonObject oAuthApplicationInfo.addParameter(DCRConstants.ClientMetadata.OAUTH_CLIENT_GRANT, jsonObject
.get(DCRConstants.ClientMetadata.OAUTH_CLIENT_GRANT)); .get(DCRConstants.ClientMetadata.OAUTH_CLIENT_GRANT));
} }
} catch (JSONException e) { } catch (JSONException e) {
throw new DynamicClientRegistrationException( throw new DynamicClientRegistrationException(
@ -166,7 +169,7 @@ public class DynamicClientRegistrationServiceImpl implements DynamicClientRegist
ServiceProvider createdServiceProvider = appMgtService.getServiceProvider(applicationName, tenantDomain); ServiceProvider createdServiceProvider = appMgtService.getServiceProvider(applicationName, tenantDomain);
if (createdServiceProvider == null) { if (createdServiceProvider == null) {
throw new DynamicClientRegistrationException("Couldn't create Service Provider Application " + throw new DynamicClientRegistrationException("Couldn't create Service Provider Application " +
applicationName); applicationName);
} }
//Set SaaS app option //Set SaaS app option
createdServiceProvider.setSaasApp(isSaaSApp); createdServiceProvider.setSaasApp(isSaaSApp);
@ -199,8 +202,7 @@ public class DynamicClientRegistrationServiceImpl implements DynamicClientRegist
// Set the OAuthApp in InboundAuthenticationConfig // Set the OAuthApp in InboundAuthenticationConfig
InboundAuthenticationConfig inboundAuthenticationConfig = InboundAuthenticationConfig inboundAuthenticationConfig =
new InboundAuthenticationConfig(); new InboundAuthenticationConfig();
InboundAuthenticationRequestConfig[] inboundAuthenticationRequestConfigs = new List<InboundAuthenticationRequestConfig> inboundAuthenticationRequestConfigs = new ArrayList<>();
InboundAuthenticationRequestConfig[2];
InboundAuthenticationRequestConfig inboundAuthenticationRequestConfig = new InboundAuthenticationRequestConfig inboundAuthenticationRequestConfig = new
InboundAuthenticationRequestConfig(); InboundAuthenticationRequestConfig();
@ -211,19 +213,22 @@ public class DynamicClientRegistrationServiceImpl implements DynamicClientRegist
Property property = new Property(); Property property = new Property();
property.setName(OAUTH_CONSUMER_SECRET); property.setName(OAUTH_CONSUMER_SECRET);
property.setValue(oauthConsumerSecret); property.setValue(oauthConsumerSecret);
Property[] properties = { property }; Property[] properties = {property};
inboundAuthenticationRequestConfig.setProperties(properties); inboundAuthenticationRequestConfig.setProperties(properties);
} }
SAMLSSOServiceProviderDTO samlssoServiceProviderDTO = new SAMLSSOServiceProviderDTO(); if (APPLICATION_TYPE_WEBAPP.equals(profile.getApplicationType())) {
samlssoServiceProviderDTO.setIssuer(MDM); SAMLSSOServiceProviderDTO samlssoServiceProviderDTO = new SAMLSSOServiceProviderDTO();
samlssoServiceProviderDTO.setIssuer(applicationName);
SAMLSSOConfigAdmin configAdmin = new SAMLSSOConfigAdmin(getConfigSystemRegistry()); SAMLSSOConfigAdmin configAdmin = new SAMLSSOConfigAdmin(getConfigSystemRegistry());
configAdmin.addRelyingPartyServiceProvider(samlssoServiceProviderDTO); configAdmin.addRelyingPartyServiceProvider(samlssoServiceProviderDTO);
InboundAuthenticationRequestConfig samlAuthenticationRequest = new InboundAuthenticationRequestConfig(); InboundAuthenticationRequestConfig samlAuthenticationRequest = new InboundAuthenticationRequestConfig();
samlAuthenticationRequest.setInboundAuthKey(MDM); samlAuthenticationRequest.setInboundAuthKey(applicationName);
samlAuthenticationRequest.setInboundAuthType(SAML_SSO); samlAuthenticationRequest.setInboundAuthType(SAML_SSO);
inboundAuthenticationRequestConfigs.add(samlAuthenticationRequest);
}
LocalAuthenticatorConfig localAuth = new LocalAuthenticatorConfig(); LocalAuthenticatorConfig localAuth = new LocalAuthenticatorConfig();
localAuth.setName(BASIC_AUTHENTICATOR); localAuth.setName(BASIC_AUTHENTICATOR);
@ -235,18 +240,18 @@ public class DynamicClientRegistrationServiceImpl implements DynamicClientRegist
authStep.setSubjectStep(true); authStep.setSubjectStep(true);
authStep.setAttributeStep(true); authStep.setAttributeStep(true);
authStep.setLocalAuthenticatorConfigs(new LocalAuthenticatorConfig[] { localAuth }); authStep.setLocalAuthenticatorConfigs(new LocalAuthenticatorConfig[]{localAuth});
LocalAndOutboundAuthenticationConfig localOutboundAuthConfig = new LocalAndOutboundAuthenticationConfig(); LocalAndOutboundAuthenticationConfig localOutboundAuthConfig = new LocalAndOutboundAuthenticationConfig();
localOutboundAuthConfig.setAuthenticationType(LOCAL); localOutboundAuthConfig.setAuthenticationType(LOCAL);
localOutboundAuthConfig.setAuthenticationSteps(new AuthenticationStep[] { authStep }); localOutboundAuthConfig.setAuthenticationSteps(new AuthenticationStep[]{authStep});
createdServiceProvider.setLocalAndOutBoundAuthenticationConfig(localOutboundAuthConfig);
inboundAuthenticationRequestConfigs[0] = inboundAuthenticationRequestConfig; inboundAuthenticationRequestConfigs.add(inboundAuthenticationRequestConfig);
inboundAuthenticationRequestConfigs[1] = samlAuthenticationRequest;
inboundAuthenticationConfig inboundAuthenticationConfig
.setInboundAuthenticationRequestConfigs(inboundAuthenticationRequestConfigs); .setInboundAuthenticationRequestConfigs(inboundAuthenticationRequestConfigs.toArray(
new InboundAuthenticationRequestConfig[inboundAuthenticationRequestConfigs.size()]));
createdServiceProvider.setInboundAuthenticationConfig(inboundAuthenticationConfig); createdServiceProvider.setInboundAuthenticationConfig(inboundAuthenticationConfig);
createdServiceProvider.setLocalAndOutBoundAuthenticationConfig(localOutboundAuthConfig);
// Update the Service Provider app to add OAuthApp as an Inbound Authentication Config // Update the Service Provider app to add OAuthApp as an Inbound Authentication Config
appMgtService.updateApplication(createdServiceProvider, tenantDomain, userName); appMgtService.updateApplication(createdServiceProvider, tenantDomain, userName);
@ -277,12 +282,12 @@ public class DynamicClientRegistrationServiceImpl implements DynamicClientRegist
protected Registry getConfigSystemRegistry() { protected Registry getConfigSystemRegistry() {
return (Registry) PrivilegedCarbonContext.getThreadLocalCarbonContext().getRegistry(RegistryType. return (Registry) PrivilegedCarbonContext.getThreadLocalCarbonContext().getRegistry(RegistryType.
SYSTEM_CONFIGURATION); SYSTEM_CONFIGURATION);
} }
@Override @Override
public boolean unregisterOAuthApplication(String userId, String applicationName, String consumerKey) throws public boolean unregisterOAuthApplication(String userId, String applicationName, String consumerKey) throws
DynamicClientRegistrationException { DynamicClientRegistrationException {
DynamicClientRegistrationUtil.validateUsername(userId); DynamicClientRegistrationUtil.validateUsername(userId);
DynamicClientRegistrationUtil.validateApplicationName(applicationName); DynamicClientRegistrationUtil.validateApplicationName(applicationName);
DynamicClientRegistrationUtil.validateConsumerKey(consumerKey); DynamicClientRegistrationUtil.validateConsumerKey(consumerKey);
@ -319,7 +324,7 @@ public class DynamicClientRegistrationServiceImpl implements DynamicClientRegist
if (appMgtService == null) { if (appMgtService == null) {
throw new IllegalStateException( throw new IllegalStateException(
"Error occurred while retrieving Application Management" + "Error occurred while retrieving Application Management" +
"Service"); "Service");
} }
ServiceProvider createdServiceProvider = appMgtService.getServiceProvider(applicationName, tenantDomain); ServiceProvider createdServiceProvider = appMgtService.getServiceProvider(applicationName, tenantDomain);
if (createdServiceProvider == null) { if (createdServiceProvider == null) {
@ -333,7 +338,7 @@ public class DynamicClientRegistrationServiceImpl implements DynamicClientRegist
"Error occurred while removing ServiceProvider for application '" + applicationName + "'", e); "Error occurred while removing ServiceProvider for application '" + applicationName + "'", e);
} catch (IdentityOAuthAdminException e) { } catch (IdentityOAuthAdminException e) {
throw new DynamicClientRegistrationException("Error occurred while removing application '" + throw new DynamicClientRegistrationException("Error occurred while removing application '" +
applicationName + "'", e); applicationName + "'", e);
} finally { } finally {
PrivilegedCarbonContext.endTenantFlow(); PrivilegedCarbonContext.endTenantFlow();
} }
@ -347,12 +352,12 @@ public class DynamicClientRegistrationServiceImpl implements DynamicClientRegist
if (appMgtService == null) { if (appMgtService == null) {
throw new IllegalStateException( throw new IllegalStateException(
"Error occurred while retrieving Application Management" + "Error occurred while retrieving Application Management" +
"Service"); "Service");
} }
try { try {
return appMgtService.getServiceProvider(applicationName, return appMgtService.getServiceProvider(applicationName,
CarbonContext.getThreadLocalCarbonContext().getTenantDomain()) != CarbonContext.getThreadLocalCarbonContext().getTenantDomain()) !=
null; null;
} catch (IdentityApplicationManagementException e) { } catch (IdentityApplicationManagementException e) {
throw new DynamicClientRegistrationException( throw new DynamicClientRegistrationException(
"Error occurred while retrieving information of OAuthApp " + applicationName, e); "Error occurred while retrieving information of OAuthApp " + applicationName, e);
@ -360,7 +365,7 @@ public class DynamicClientRegistrationServiceImpl implements DynamicClientRegist
} }
private String replaceInvalidChars(String username) { private String replaceInvalidChars(String username) {
return username.replaceAll("@","_AT_"); return username.replaceAll("@", "_AT_");
} }
} }

@ -23,7 +23,7 @@ package org.wso2.carbon.dynamic.client.registration.profile;
* *
* */ * */
public class RegistrationProfile { public class RegistrationProfile {
//todo mark mandatory fields
private String applicationType; private String applicationType;
private String[] redirectUris; private String[] redirectUris;
private String clientName; private String clientName;
@ -42,6 +42,8 @@ public class RegistrationProfile {
private String grantType; private String grantType;
private boolean saasApp; private boolean saasApp;
private String audience; private String audience;
private String recepientValidationURL;
private String assertionConsumerURL;
public String getRecepientValidationURL() { public String getRecepientValidationURL() {
return recepientValidationURL; return recepientValidationURL;
@ -59,9 +61,6 @@ public class RegistrationProfile {
this.assertionConsumerURL = assertionConsumerURL; this.assertionConsumerURL = assertionConsumerURL;
} }
private String recepientValidationURL;
private String assertionConsumerURL;
public String getAudience() { public String getAudience() {
return audience; return audience;
} }

Loading…
Cancel
Save