Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -187,6 +187,11 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.wso2.carbon.identity.governance</groupId>
<artifactId>org.wso2.carbon.identity.governance</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.wso2.carbon</groupId>
<artifactId>org.wso2.carbon.utils</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,8 @@ public final class Constants {
public static final String ENABLE_DETAILED_API_RESPONSE =
"SelfRegistration.API.EnableDetailedResponseBody";

// SCIM2 Me endpoint blocked claims configuration keys.
public static final String SCIM2_ME_BLOCKED_CLAIMS = "SCIM2.Me.BlockedClaims.BlockedClaim";
public static final String SCIM2_ME_EXTENDED_BLOCKED_CLAIMS = "SCIM2.Me.ExtendedBlockedClaims.ExtendedBlockedClaim";

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.identity.application.common.model.ResolvedUser;
import org.wso2.carbon.identity.core.util.IdentityConfigParser;
import org.wso2.carbon.identity.core.util.IdentityUtil;
import org.wso2.carbon.identity.governance.service.notification.NotificationChannels;
import org.wso2.carbon.identity.recovery.IdentityRecoveryClientException;
Expand All @@ -35,6 +36,7 @@
import org.wso2.carbon.identity.recovery.signup.UserSelfRegistrationManager;
import org.wso2.carbon.identity.user.endpoint.Constants;
import org.wso2.carbon.identity.user.endpoint.MeApiService;
import org.wso2.carbon.identity.user.endpoint.dto.ClaimDTO;
import org.wso2.carbon.identity.user.endpoint.dto.ErrorDTO;
import org.wso2.carbon.identity.user.endpoint.dto.MeCodeValidationRequestDTO;
import org.wso2.carbon.identity.user.endpoint.dto.MeResendCodeRequestDTO;
Expand All @@ -49,6 +51,7 @@
import org.wso2.carbon.identity.workflow.mgt.exception.WorkflowException;
import org.wso2.carbon.user.core.util.UserCoreUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -99,6 +102,8 @@ public Response mePost(SelfUserRegistrationRequestDTO selfUserRegistrationReques
ERROR_CODE_BAD_SELF_REGISTER_REQUEST.getCode());
}

validateIdentityClaimsNotPresent(selfUserRegistrationRequestDTO);

if (StringUtils.isNotBlank(tenantFromContext)) {
selfUserRegistrationRequestDTO.getUser().setTenantDomain(tenantFromContext);
}
Expand Down Expand Up @@ -178,6 +183,48 @@ public Response meValidateCodePost(MeCodeValidationRequestDTO meCodeValidationRe
return Response.accepted().build();
}

private void validateIdentityClaimsNotPresent(SelfUserRegistrationRequestDTO selfUserRegistrationRequestDTO) {

if (selfUserRegistrationRequestDTO == null || selfUserRegistrationRequestDTO.getUser() == null ||
selfUserRegistrationRequestDTO.getUser().getClaims() == null) {
return;
}

List<String> blockedClaims = getBlockedClaims();
if (blockedClaims.isEmpty()) {
return;
}

for (ClaimDTO claim : selfUserRegistrationRequestDTO.getUser().getClaims()) {
if (claim != null && StringUtils.isNotBlank(claim.getUri()) &&
blockedClaims.contains(claim.getUri())) {
Utils.handleBadRequest(String.format("Claim '%s' is not allowed to be updated in " +
"self-registration.", claim.getUri()),
ERROR_CODE_BAD_SELF_REGISTER_REQUEST.getCode());
}
}
}

private List<String> getBlockedClaims() {

List<String> blockedClaims = new ArrayList<>();
Map<String, Object> config = IdentityConfigParser.getInstance().getConfiguration();
addClaimsFromConfig(config, Constants.SCIM2_ME_BLOCKED_CLAIMS, blockedClaims);
addClaimsFromConfig(config, Constants.SCIM2_ME_EXTENDED_BLOCKED_CLAIMS, blockedClaims);
return blockedClaims;
}

@SuppressWarnings("unchecked")
private void addClaimsFromConfig(Map<String, Object> config, String key, List<String> claimsList) {

Object value = config.get(key);
if (value instanceof List) {
claimsList.addAll((List<String>) value);
} else if (value instanceof String) {
claimsList.add((String) value);
}
}

/**
* Build response for a successful user self registration.
*
Expand Down Expand Up @@ -466,4 +513,3 @@ private boolean isWorkflowAssociated(NotificationResponseBean notificationRespon
return false;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.identity.application.common.model.ResolvedUser;
import org.wso2.carbon.identity.application.common.model.User;
import org.wso2.carbon.identity.core.util.IdentityConfigParser;
import org.wso2.carbon.identity.core.util.IdentityUtil;
import org.wso2.carbon.identity.recovery.IdentityRecoveryClientException;
import org.wso2.carbon.identity.recovery.IdentityRecoveryConstants;
Expand All @@ -43,13 +44,15 @@
import org.wso2.carbon.identity.recovery.model.Property;
import org.wso2.carbon.identity.recovery.model.UserRecoveryData;
import org.wso2.carbon.identity.recovery.signup.UserSelfRegistrationManager;
import org.wso2.carbon.identity.user.endpoint.Constants;
import org.wso2.carbon.identity.user.endpoint.dto.ClaimDTO;
import org.wso2.carbon.identity.user.endpoint.dto.MeCodeValidationRequestDTO;
import org.wso2.carbon.identity.user.endpoint.dto.MeResendCodeRequestDTO;
import org.wso2.carbon.identity.user.endpoint.dto.PropertyDTO;
import org.wso2.carbon.identity.user.endpoint.dto.ResendCodeRequestDTO;
import org.wso2.carbon.identity.user.endpoint.dto.SelfRegistrationUserDTO;
import org.wso2.carbon.identity.user.endpoint.dto.SelfUserRegistrationRequestDTO;
import org.wso2.carbon.identity.user.endpoint.exceptions.BadRequestException;
import org.wso2.carbon.identity.user.endpoint.util.Utils;
import org.wso2.carbon.identity.user.export.core.UserExportException;
import org.wso2.carbon.identity.workflow.mgt.WorkflowManagementService;
Expand All @@ -58,14 +61,17 @@

import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.expectThrows;
import static org.wso2.carbon.identity.recovery.IdentityRecoveryConstants.SELF_REGISTER_USER_EVENT;

/**
Expand All @@ -76,6 +82,8 @@ public class MeApiServiceImplTest {
private static final String USERNAME = "dummyUser";
private MockedStatic<IdentityUtil> mockedIdentityUtil;
private MockedStatic<Utils> mockedUtils;
private MockedStatic<IdentityConfigParser> mockedIdentityConfigParser;
private IdentityConfigParser identityConfigParserMock;
private ResendConfirmationManager resendConfirmationManager;

@Mock
Expand All @@ -101,13 +109,18 @@ public void setUp() {
resendConfirmationManager = Mockito.mock(ResendConfirmationManager.class);
mockedUtils = Mockito.mockStatic(Utils.class);
mockedUtils.when(Utils::getUserSelfRegistrationManager).thenReturn(userSelfRegistrationManager);
identityConfigParserMock = Mockito.mock(IdentityConfigParser.class);
Mockito.when(identityConfigParserMock.getConfiguration()).thenReturn(new HashMap<>());
mockedIdentityConfigParser = Mockito.mockStatic(IdentityConfigParser.class);
mockedIdentityConfigParser.when(IdentityConfigParser::getInstance).thenReturn(identityConfigParserMock);
}

@AfterMethod
public void tearDown() {

mockedIdentityUtil.close();
mockedUtils.close();
mockedIdentityConfigParser.close();
}

@Test
Expand All @@ -119,6 +132,26 @@ public void testMePost() throws IdentityRecoveryException {
assertEquals(meApiService.mePost(null).getStatus(), 201);
}

@Test
public void testMePostRejectsIdentityClaims() throws IdentityRecoveryException {

mockedUtils.when(() -> Utils.handleBadRequest(anyString(), anyString())).thenCallRealMethod();
mockedUtils.when(() -> Utils.buildBadRequestException(anyString(), anyString())).thenCallRealMethod();
mockedUtils.when(() -> Utils.getErrorDTO(anyString(), anyString(), anyString())).thenCallRealMethod();

String blockedClaimUri = "http://wso2.org/claims/identity/emailVerified";
Map<String, Object> config = new HashMap<>();
config.put(Constants.SCIM2_ME_BLOCKED_CLAIMS, blockedClaimUri);
Mockito.when(identityConfigParserMock.getConfiguration()).thenReturn(config);

BadRequestException exception = expectThrows(BadRequestException.class,
() -> meApiService.mePost(selfUserRegistrationRequestDTOWithClaimUri(blockedClaimUri)));
assertEquals(exception.getMessage(), String.format("Claim '%s' is not allowed to be updated in " +
"self-registration.", blockedClaimUri));
Mockito.verify(userSelfRegistrationManager, Mockito.never()).registerUser(any(User.class), anyString(),
any(Claim[].class), any(Property[].class));
}

@Test
public void testMePostWorkflowEngaged() throws IdentityRecoveryException, WorkflowException {

Expand Down Expand Up @@ -327,8 +360,13 @@ private PropertyDTO buildSelfUserRegistrationRequestDTO() {

private ClaimDTO buildClaimDTO() {

return buildClaimDTO("http://wso2.org.email");
}

private ClaimDTO buildClaimDTO(String claimUri) {

ClaimDTO claimDTO = new ClaimDTO();
claimDTO.setUri("http://wso2.org.email");
claimDTO.setUri(claimUri);
claimDTO.setValue("test@gmail.com");
return claimDTO;
}
Expand All @@ -346,6 +384,15 @@ private SelfUserRegistrationRequestDTO selfUserRegistrationRequestDTO() {
return selfUserRegistrationRequestDTO;
}

private SelfUserRegistrationRequestDTO selfUserRegistrationRequestDTOWithClaimUri(String claimUri) {

SelfUserRegistrationRequestDTO selfUserRegistrationRequestDTO = selfUserRegistrationRequestDTO();
List<ClaimDTO> listClaimDTO = new ArrayList<>();
listClaimDTO.add(buildClaimDTO(claimUri));
selfUserRegistrationRequestDTO.getUser().setClaims(listClaimDTO);
return selfUserRegistrationRequestDTO;
}

private MeCodeValidationRequestDTO createMeCodeValidationRequestDTO() {

MeCodeValidationRequestDTO codeValidationRequestDTO = new MeCodeValidationRequestDTO();
Expand Down
Loading