Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify the application patch and get requests to skip the organization role association #604

Merged
merged 2 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ public enum ErrorMessage {
DISABLE_REDIRECT_OR_POST_BINDINGS("APP-60007",
"Disabling HTTP_POST or HTTP_REDIRECT is not allowed",
"HTTP_POST or HTTP_REDIRECT cannot be disabled"),
INVALID_ROLE_ASSOCIATION_FOR_ORGANIZATION_AUDIENCE("60008",
"Applications can not be updated by modifying the associated roles.",
"Applications can not be updated by modifying the associated roles."),

// Client errors defined at API level.
INVALID_INBOUND_PROTOCOL("60501",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.wso2.carbon.identity.api.server.application.management.v1.ApplicationTemplateModel;
import org.wso2.carbon.identity.api.server.application.management.v1.ApplicationTemplatesList;
import org.wso2.carbon.identity.api.server.application.management.v1.ApplicationTemplatesListItem;
import org.wso2.carbon.identity.api.server.application.management.v1.AssociatedRolesConfig;
import org.wso2.carbon.identity.api.server.application.management.v1.AuthProtocolMetadata;
import org.wso2.carbon.identity.api.server.application.management.v1.AuthorizedAPICreationModel;
import org.wso2.carbon.identity.api.server.application.management.v1.AuthorizedAPIPatchModel;
Expand All @@ -61,6 +62,7 @@
import org.wso2.carbon.identity.api.server.application.management.v1.PassiveStsConfiguration;
import org.wso2.carbon.identity.api.server.application.management.v1.ProvisioningConfiguration;
import org.wso2.carbon.identity.api.server.application.management.v1.ResidentApplication;
import org.wso2.carbon.identity.api.server.application.management.v1.Role;
import org.wso2.carbon.identity.api.server.application.management.v1.SAML2Configuration;
import org.wso2.carbon.identity.api.server.application.management.v1.SAML2ServiceProvider;
import org.wso2.carbon.identity.api.server.application.management.v1.WSTrustConfiguration;
Expand Down Expand Up @@ -219,8 +221,8 @@ public class ServerApplicationManagementService {
private static final String[] VALID_MEDIA_TYPES_XML = {"application/xml", "text/xml"};
private static final String[] VALID_MEDIA_TYPES_YAML = {"application/yaml", "text/yaml", "application/x-yaml"};
private static final String[] VALID_MEDIA_TYPES_JSON = {"application/json", "text/json"};
private static final Class<?>[] INBOUND_CONFIG_PROTOCOLS = new Class<?>[] {ServiceProvider.class,
SAMLSSOServiceProviderDTO.class, OAuthAppDO.class};
private static final Class<?>[] INBOUND_CONFIG_PROTOCOLS = new Class<?>[]{ServiceProvider.class,
SAMLSSOServiceProviderDTO.class, OAuthAppDO.class};

static {
SUPPORTED_FILTER_ATTRIBUTES.add(NAME);
Expand Down Expand Up @@ -800,7 +802,7 @@ public String createApplication(ApplicationModel applicationModel, String templa
try {
ApplicationDTO applicationDTO = new ApiModelToServiceProvider().apply(applicationModel);
applicationId = getApplicationManagementService().createApplication(applicationDTO, tenantDomain, username);

// Update owner for B2B Self Service applications.
if (applicationDTO.getServiceProvider().isB2BSelfServiceApp()) {
String systemUserID = org.wso2.carbon.identity.organization.management.service.util.Utils
Expand Down Expand Up @@ -849,6 +851,13 @@ public String createApplication(ApplicationModel applicationModel, String templa
public void patchApplication(String applicationId, ApplicationPatchModel applicationPatchModel) {

ServiceProvider appToUpdate = cloneApplication(applicationId);
AssociatedRolesConfig associatedRoles = applicationPatchModel.getAssociatedRoles();
if (associatedRoles != null) {
List<Role> listRole = associatedRoles.getRoles();
if (listRole.isEmpty()) {
throw buildClientError(ErrorMessage.INVALID_ROLE_ASSOCIATION_FOR_ORGANIZATION_AUDIENCE);
shashimalcse marked this conversation as resolved.
Show resolved Hide resolved
}
}

// Validate whether application-based outbound provisioning support is enabled.
if (applicationPatchModel != null && applicationPatchModel.getProvisioningConfigurations() != null &&
Expand Down Expand Up @@ -1125,7 +1134,7 @@ public ApplicationTemplatesList listApplicationTemplates(Integer limit, Integer
/**
* Check updating system application allowed or not.
*
* @param appName application name
* @param appName application name
* @param applicationPatchModel application patch model
* @return true if allowed
*/
Expand Down Expand Up @@ -1721,21 +1730,21 @@ private <I> void putInbound(String applicationId,
throw error;
}
}

/**
* Create or replace the provided inbound configuration.
*
* @param applicationId Resource id of the app.
* @param inboundApiModel Inbound API model to be created or replaced.
* @param getInboundDTO A function that takes the inbound API model and application as input and provides the
* InboundProtocolConfigurationDTO that matches with the protocol.
* @param applicationId Resource id of the app.
* @param inboundApiModel Inbound API model to be created or replaced.
* @param getInboundDTO A function that takes the inbound API model and application as input and provides the
* InboundProtocolConfigurationDTO that matches with the protocol.
*/
private <I> void putApplicationInbound(String applicationId, I inboundApiModel, BiFunction<ServiceProvider, I,
InboundProtocolConfigurationDTO> getInboundDTO) {

// We need a cloned copy of the Service Provider so that we changes we do not make cache dirty.
ServiceProvider application = cloneApplication(applicationId);

// Update the service provider with the inbound configuration.
InboundProtocolConfigurationDTO inboundDTO = getInboundDTO.apply(application, inboundApiModel);
try {
Expand Down Expand Up @@ -1769,11 +1778,11 @@ private void doRollback(String applicationId, InboundAuthenticationRequestConfig
}

private void updateServiceProvider(String applicationId, ServiceProvider updatedApplication) {

try {
String tenantDomain = ContextLoader.getTenantDomainFromContext();
String username = ContextLoader.getUsernameFromContext();

// Inbound auth config details are already added to the service provider. Therefore we don't need to pass
// the inboundDTO information here.
getApplicationManagementService().updateApplicationByResourceId(
Expand All @@ -1783,14 +1792,14 @@ private void updateServiceProvider(String applicationId, ServiceProvider updated
throw handleIdentityApplicationManagementException(e, msg);
}
}

private void updateServiceProvider(String applicationId, ServiceProvider updatedApplication,
InboundProtocolConfigurationDTO inboundDTO) {

try {
String tenantDomain = ContextLoader.getTenantDomainFromContext();
String username = ContextLoader.getUsernameFromContext();

getApplicationManagementService().updateApplicationByResourceId(
applicationId, updatedApplication, inboundDTO, tenantDomain, username);
} catch (IdentityApplicationManagementException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@
import org.wso2.carbon.identity.application.common.model.LocalAuthenticatorConfig;
import org.wso2.carbon.identity.application.common.model.RequestPathAuthenticatorConfig;
import org.wso2.carbon.identity.application.common.model.RoleMapping;
import org.wso2.carbon.identity.application.common.model.RoleV2;
import org.wso2.carbon.identity.application.common.model.ServiceProvider;
import org.wso2.carbon.identity.application.common.model.ServiceProviderProperty;
import org.wso2.carbon.identity.application.common.util.IdentityApplicationConstants;
import org.wso2.carbon.identity.application.mgt.ApplicationConstants;
import org.wso2.carbon.identity.application.mgt.ApplicationMgtUtil;
import org.wso2.carbon.identity.role.v2.mgt.core.RoleConstants;
import org.wso2.carbon.utils.multitenancy.MultitenantConstants;

import java.util.ArrayList;
Expand Down Expand Up @@ -150,26 +150,31 @@ private String getLogoutReturnUrl(ServiceProvider application) {
private AssociatedRolesConfig buildAssociatedRoles(ServiceProvider application) {

AssociatedRolesConfig associatedRolesConfig = new AssociatedRolesConfig();
if (application.getAssociatedRolesConfig() == null) {
org.wso2.carbon.identity.application.common.model.AssociatedRolesConfig associatedRolesConfiguration =
application.getAssociatedRolesConfig();
if (associatedRolesConfiguration == null) {
return null;
}

String allowedAudience = application.getAssociatedRolesConfig().getAllowedAudience();
String allowedAudience = associatedRolesConfiguration.getAllowedAudience();
AssociatedRolesConfig.AllowedAudienceEnum allowedAudienceEnum = null;

switch (allowedAudience) {
case "application":
case RoleConstants.APPLICATION:
allowedAudienceEnum = AssociatedRolesConfig.AllowedAudienceEnum.APPLICATION;
break;
case "organization":
case RoleConstants.ORGANIZATION:
allowedAudienceEnum = AssociatedRolesConfig.AllowedAudienceEnum.ORGANIZATION;
break;
default:
break;
}
associatedRolesConfig.setAllowedAudience(allowedAudienceEnum);
RoleV2[] roles = application.getAssociatedRolesConfig().getRoles();
Arrays.asList(roles).forEach(role -> associatedRolesConfig.addRolesItem(
new Role().id(role.getId()).name(role.getName())));
if (RoleConstants.APPLICATION.equals(allowedAudience)) {
Arrays.stream(associatedRolesConfiguration.getRoles())
.map(role -> new Role().id(role.getId()).name(role.getName()))
.forEach(associatedRolesConfig::addRolesItem);
}
shashimalcse marked this conversation as resolved.
Show resolved Hide resolved
return associatedRolesConfig;
}

Expand Down Expand Up @@ -359,7 +364,7 @@ private SubjectConfig buildSubjectClaimConfig(ServiceProvider application) {
}

private void assignClaimForSubjectValue(ServiceProvider application, SubjectConfig subjectConfig) {

if (isLocalClaimDialectUsedBySp(application)) {
if (isUserIdUsedAsDefaultSubject(application.getSpProperties())) {
subjectConfig.claim(buildClaimModel(FrameworkConstants.USER_ID_CLAIM));
Expand All @@ -369,7 +374,7 @@ private void assignClaimForSubjectValue(ServiceProvider application, SubjectConf
}
}

private boolean isUserIdUsedAsDefaultSubject (ServiceProviderProperty[] spProperties) {
private boolean isUserIdUsedAsDefaultSubject(ServiceProviderProperty[] spProperties) {

for (ServiceProviderProperty spProperty : spProperties) {
if (useUserIdForDefaultSubject.equals(spProperty.getName())) {
Expand Down Expand Up @@ -462,7 +467,7 @@ private AdvancedApplicationConfiguration buildAdvancedAppConfiguration(ServicePr
* @return An instance of AdvancedApplicationConfigurationAttestationMetaData containing attestation data.
*/
private AdvancedApplicationConfigurationAttestationMetaData getAttestationMetaData
(ServiceProvider serviceProvider) {
(ServiceProvider serviceProvider) {

// Retrieve the client attestation metadata from the service provider.
ClientAttestationMetaData clientAttestationMetaData = serviceProvider.getClientAttestationMetaData();
Expand Down Expand Up @@ -506,26 +511,26 @@ private ServiceProviderProperty[] removeAndSetSpProperties(ServiceProviderProper

/* These properties are part of advanced configurations and hence removing
them as they can't be packed as a part of additional sp properties again.*/
List<ServiceProviderProperty> spPropertyList =
Arrays.stream(propertyList).collect(Collectors.toList());
spPropertyList.removeIf(property -> SKIP_CONSENT.equals(property.getName()));
spPropertyList.removeIf(property -> SKIP_LOGOUT_CONSENT.equals(property.getName()));
spPropertyList.removeIf(property -> USE_EXTERNAL_CONSENT_PAGE.equals(property.getName()));
spPropertyList.removeIf(property -> USE_DOMAIN_IN_ROLES.equals(property.getName()));
spPropertyList.removeIf(property -> USE_USER_ID_FOR_DEFAULT_SUBJECT.equals(property.getName()));
spPropertyList.removeIf(property -> TEMPLATE_ID_SP_PROPERTY_NAME.equals(property.getName()));
spPropertyList.removeIf(property -> IS_MANAGEMENT_APP_SP_PROPERTY_NAME.equals(property.getName()));
spPropertyList.removeIf(property -> IS_ATTESTATION_ENABLED_PROPERTY_NAME.equals(property.getName()));
spPropertyList.removeIf(property ->
IS_API_BASED_AUTHENTICATION_ENABLED_PROPERTY_NAME.equals(property.getName()));
spPropertyList.removeIf(property ->
ANDROID_PACKAGE_NAME_PROPERTY_NAME.equals(property.getName()));
spPropertyList.removeIf(property ->
ANDROID_PACKAGE_NAME_PROPERTY_NAME.equals(property.getName()));
spPropertyList.removeIf(property ->
APPLE_APP_ID_PROPERTY_NAME.equals(property.getName()));
spPropertyList.removeIf(property -> ALLOWED_ROLE_AUDIENCE_PROPERTY_NAME.equals(property.getName()));
return spPropertyList.toArray(new ServiceProviderProperty[0]);
List<ServiceProviderProperty> spPropertyList =
Arrays.stream(propertyList).collect(Collectors.toList());
spPropertyList.removeIf(property -> SKIP_CONSENT.equals(property.getName()));
spPropertyList.removeIf(property -> SKIP_LOGOUT_CONSENT.equals(property.getName()));
spPropertyList.removeIf(property -> USE_EXTERNAL_CONSENT_PAGE.equals(property.getName()));
spPropertyList.removeIf(property -> USE_DOMAIN_IN_ROLES.equals(property.getName()));
spPropertyList.removeIf(property -> USE_USER_ID_FOR_DEFAULT_SUBJECT.equals(property.getName()));
spPropertyList.removeIf(property -> TEMPLATE_ID_SP_PROPERTY_NAME.equals(property.getName()));
spPropertyList.removeIf(property -> IS_MANAGEMENT_APP_SP_PROPERTY_NAME.equals(property.getName()));
spPropertyList.removeIf(property -> IS_ATTESTATION_ENABLED_PROPERTY_NAME.equals(property.getName()));
spPropertyList.removeIf(property ->
IS_API_BASED_AUTHENTICATION_ENABLED_PROPERTY_NAME.equals(property.getName()));
spPropertyList.removeIf(property ->
ANDROID_PACKAGE_NAME_PROPERTY_NAME.equals(property.getName()));
spPropertyList.removeIf(property ->
ANDROID_PACKAGE_NAME_PROPERTY_NAME.equals(property.getName()));
spPropertyList.removeIf(property ->
APPLE_APP_ID_PROPERTY_NAME.equals(property.getName()));
spPropertyList.removeIf(property -> ALLOWED_ROLE_AUDIENCE_PROPERTY_NAME.equals(property.getName()));
return spPropertyList.toArray(new ServiceProviderProperty[0]);
}

private Certificate getCertificate(ServiceProvider serviceProvider) {
Expand All @@ -538,6 +543,7 @@ private Certificate getCertificate(ServiceProvider serviceProvider) {

return null;
}

private boolean isFragmentApp(ServiceProvider serviceProvider) {

return serviceProvider != null && serviceProvider.getSpProperties() != null &&
Expand Down Expand Up @@ -577,7 +583,7 @@ private String getInboundKey(ServiceProvider application, String authType) {
.getInboundAuthenticationRequestConfigs();

if (authRequestConfigs != null && authRequestConfigs.length > 0) {
for (InboundAuthenticationRequestConfig authRequestConfig: authRequestConfigs) {
for (InboundAuthenticationRequestConfig authRequestConfig : authRequestConfigs) {
if (authRequestConfig.getInboundAuthType().equals(authType)) {
return authRequestConfig.getInboundAuthKey();
}
Expand Down
Loading