Search in sources :

Example 11 with IdmProfileDto

use of eu.bcvsolutions.idm.core.api.dto.IdmProfileDto in project CzechIdMng by bcvsolutions.

the class IdmIdentityControllerRestTest method testCollapsiblePanel.

@Test
public void testCollapsiblePanel() throws Exception {
    IdmIdentityDto dto = createDto();
    String panelId = getHelper().createName();
    // 
    getMockMvc().perform(patch(String.format("%s/profile/panels/%s/collapse", getDetailUrl(dto.getId()), panelId)).with(authentication(getAdminAuthentication())).contentType(TestHelper.HAL_CONTENT_TYPE)).andExpect(status().isOk()).andExpect(content().contentType(TestHelper.HAL_CONTENT_TYPE));
    IdmProfileDto profile = profileService.findOneByIdentity(dto.getId());
    Assert.assertTrue(((PanelDto) profile.getSetting().get(panelId)).getCollapsed());
    // 
    getMockMvc().perform(patch(String.format("%s/profile/panels/%s/expand", getDetailUrl(dto.getId()), panelId)).with(authentication(getAdminAuthentication())).contentType(TestHelper.HAL_CONTENT_TYPE)).andExpect(status().isOk()).andExpect(content().contentType(TestHelper.HAL_CONTENT_TYPE));
    profile = profileService.findOneByIdentity(dto.getId());
    Assert.assertFalse(((PanelDto) profile.getSetting().get(panelId)).getCollapsed());
    // 
    // not found
    getMockMvc().perform(patch(String.format("%s/profile/panels/%s/expand", getDetailUrl(UUID.randomUUID()), panelId)).with(authentication(getAdminAuthentication())).contentType(TestHelper.HAL_CONTENT_TYPE)).andExpect(status().isNotFound());
    getMockMvc().perform(patch(String.format("%s/profile/panels/%s/collapse", getDetailUrl(UUID.randomUUID()), panelId)).with(authentication(getAdminAuthentication())).contentType(TestHelper.HAL_CONTENT_TYPE)).andExpect(status().isNotFound());
}
Also used : IdmProfileDto(eu.bcvsolutions.idm.core.api.dto.IdmProfileDto) GuardedString(eu.bcvsolutions.idm.core.security.api.domain.GuardedString) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto) AbstractReadWriteDtoControllerRestTest(eu.bcvsolutions.idm.core.api.rest.AbstractReadWriteDtoControllerRestTest) Test(org.junit.Test)

Example 12 with IdmProfileDto

use of eu.bcvsolutions.idm.core.api.dto.IdmProfileDto in project CzechIdMng by bcvsolutions.

the class DefaultIdmProfileService method setPanelCollapsed.

private IdmProfileDto setPanelCollapsed(Serializable identityIdentifier, String panelIdentifier, boolean collapsed, BasePermission... permission) {
    IdmProfileDto profile = findOrCreateByIdentity(identityIdentifier, permission);
    // 
    ConfigurationMap setting = profile.getSetting();
    PanelDto panel = (PanelDto) setting.get(panelIdentifier);
    if (panel == null) {
        panel = new PanelDto(panelIdentifier);
    }
    panel.setCollapsed(collapsed);
    setting.put(panelIdentifier, panel);
    profile.setSetting(setting);
    // 
    return save(profile, permission);
}
Also used : IdmProfileDto(eu.bcvsolutions.idm.core.api.dto.IdmProfileDto) ConfigurationMap(eu.bcvsolutions.idm.core.api.domain.ConfigurationMap) PanelDto(eu.bcvsolutions.idm.core.api.dto.PanelDto)

Example 13 with IdmProfileDto

use of eu.bcvsolutions.idm.core.api.dto.IdmProfileDto in project CzechIdMng by bcvsolutions.

the class LoginControllerRestTest method testMustChangePasswordAfterTwoFactorLogin.

@Test
public void testMustChangePasswordAfterTwoFactorLogin() throws Exception {
    IdmIdentityDto identity = getHelper().createIdentity();
    IdmProfileDto profile = getHelper().createProfile(identity);
    IdmRoleDto role = getHelper().createRole();
    getHelper().createIdentityRole(identity, role);
    getHelper().createBasePolicy(role.getId(), CoreGroupPermission.IDENTITY, IdmIdentity.class, IdmBasePermission.READ);
    // login
    Map<String, String> login = new HashMap<>();
    login.put("username", identity.getUsername());
    login.put("password", identity.getPassword().asString());
    String response = getMockMvc().perform(post(BaseController.BASE_PATH + LoginController.AUTH_PATH).content(serialize(login)).contentType(TestHelper.HAL_CONTENT_TYPE)).andExpect(status().isOk()).andExpect(content().contentType(TestHelper.HAL_CONTENT_TYPE)).andReturn().getResponse().getContentAsString();
    String token = getToken(response);
    // 
    // init two factor authentication by profile controller
    response = getMockMvc().perform(put(BaseController.BASE_PATH + "/profiles/" + profile.getId() + "/two-factor/init").param(IdmAuthenticationFilter.AUTHENTICATION_TOKEN_NAME, token).param("twoFactorAuthenticationType", TwoFactorAuthenticationType.APPLICATION.name()).contentType(TestHelper.HAL_CONTENT_TYPE)).andExpect(status().isOk()).andExpect(content().contentType(TestHelper.HAL_CONTENT_TYPE)).andReturn().getResponse().getContentAsString();
    TwoFactorRegistrationResponseDto twoFactorInit = getMapper().readValue(response, TwoFactorRegistrationResponseDto.class);
    Assert.assertNotNull(twoFactorInit);
    Assert.assertNotNull(twoFactorInit.getVerificationSecret());
    // 
    // confirm two factor authentication by profile controller
    Map<String, String> twoFactorConfirm = new HashMap<>();
    twoFactorConfirm.put("verificationCode", twoFactorAuthenticationManager.generateCode(new GuardedString(twoFactorInit.getVerificationSecret())).asString());
    twoFactorConfirm.put("verificationSecret", twoFactorInit.getVerificationSecret());
    twoFactorConfirm.put("twoFactorAuthenticationType", TwoFactorAuthenticationType.APPLICATION.name());
    response = getMockMvc().perform(put(BaseController.BASE_PATH + "/profiles/" + profile.getId() + "/two-factor/confirm").param(IdmAuthenticationFilter.AUTHENTICATION_TOKEN_NAME, token).content(serialize(twoFactorConfirm)).contentType(TestHelper.HAL_CONTENT_TYPE)).andExpect(status().isOk()).andExpect(content().contentType(TestHelper.HAL_CONTENT_TYPE)).andReturn().getResponse().getContentAsString();
    IdmProfileDto updatedProfile = getMapper().readValue(response, IdmProfileDto.class);
    Assert.assertNotNull(updatedProfile);
    Assert.assertEquals(TwoFactorAuthenticationType.APPLICATION, updatedProfile.getTwoFactorAuthenticationType());
    // 
    // set password must change
    IdmPasswordDto password = getHelper().getPassword(identity);
    password.setMustChange(true);
    passwordService.save(password);
    // 
    // login as identity again
    response = getMockMvc().perform(post(BaseController.BASE_PATH + LoginController.AUTH_PATH).content(serialize(login)).contentType(TestHelper.HAL_CONTENT_TYPE)).andExpect(status().isUnauthorized()).andReturn().getResponse().getContentAsString();
    // 
    // get token form response
    token = getMapper().readTree(response).get("_errors").get(0).get("parameters").get("token").asText();
    Assert.assertNotNull(token);
    // 
    // two factor authentication
    Map<String, String> twoFactorLogin = new HashMap<>();
    GuardedString generateCode = twoFactorAuthenticationManager.generateCode(identity.getId());
    Assert.assertTrue(twoFactorAuthenticationManager.verifyCode(identity.getId(), generateCode));
    twoFactorLogin.put("verificationCode", generateCode.asString());
    twoFactorLogin.put("token", token);
    getMockMvc().perform(post(BaseController.BASE_PATH + "/authentication/two-factor").content(serialize(twoFactorLogin)).contentType(TestHelper.HAL_CONTENT_TYPE)).andExpect(status().isUnauthorized());
}
Also used : IdmRoleDto(eu.bcvsolutions.idm.core.api.dto.IdmRoleDto) IdmProfileDto(eu.bcvsolutions.idm.core.api.dto.IdmProfileDto) HashMap(java.util.HashMap) TwoFactorRegistrationResponseDto(eu.bcvsolutions.idm.core.security.api.dto.TwoFactorRegistrationResponseDto) IdmPasswordDto(eu.bcvsolutions.idm.core.api.dto.IdmPasswordDto) GuardedString(eu.bcvsolutions.idm.core.security.api.domain.GuardedString) GuardedString(eu.bcvsolutions.idm.core.security.api.domain.GuardedString) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto) AbstractRestTest(eu.bcvsolutions.idm.test.api.AbstractRestTest) Test(org.junit.Test)

Example 14 with IdmProfileDto

use of eu.bcvsolutions.idm.core.api.dto.IdmProfileDto in project CzechIdMng by bcvsolutions.

the class LoginControllerRestTest method testTwoFactorLogin.

@Test
public void testTwoFactorLogin() throws Exception {
    IdmIdentityDto identity = getHelper().createIdentity();
    IdmProfileDto profile = getHelper().createProfile(identity);
    IdmRoleDto role = getHelper().createRole();
    getHelper().createIdentityRole(identity, role);
    getHelper().createBasePolicy(role.getId(), CoreGroupPermission.IDENTITY, IdmIdentity.class, IdmBasePermission.READ);
    // login
    Map<String, String> login = new HashMap<>();
    login.put("username", identity.getUsername());
    login.put("password", identity.getPassword().asString());
    String response = getMockMvc().perform(post(BaseController.BASE_PATH + LoginController.AUTH_PATH).content(serialize(login)).contentType(TestHelper.HAL_CONTENT_TYPE)).andExpect(status().isOk()).andExpect(content().contentType(TestHelper.HAL_CONTENT_TYPE)).andReturn().getResponse().getContentAsString();
    String token = getToken(response);
    // 
    // init two factor authentication by profile controller
    response = getMockMvc().perform(put(BaseController.BASE_PATH + "/profiles/" + profile.getId() + "/two-factor/init").param(IdmAuthenticationFilter.AUTHENTICATION_TOKEN_NAME, token).param("twoFactorAuthenticationType", TwoFactorAuthenticationType.APPLICATION.name()).contentType(TestHelper.HAL_CONTENT_TYPE)).andExpect(status().isOk()).andExpect(content().contentType(TestHelper.HAL_CONTENT_TYPE)).andReturn().getResponse().getContentAsString();
    TwoFactorRegistrationResponseDto twoFactorInit = getMapper().readValue(response, TwoFactorRegistrationResponseDto.class);
    Assert.assertNotNull(twoFactorInit);
    Assert.assertNotNull(twoFactorInit.getVerificationSecret());
    // 
    // confirm two factor authentication by profile controller
    Map<String, String> twoFactorConfirm = new HashMap<>();
    twoFactorConfirm.put("verificationCode", twoFactorAuthenticationManager.generateCode(new GuardedString(twoFactorInit.getVerificationSecret())).asString());
    twoFactorConfirm.put("verificationSecret", twoFactorInit.getVerificationSecret());
    twoFactorConfirm.put("twoFactorAuthenticationType", TwoFactorAuthenticationType.APPLICATION.name());
    response = getMockMvc().perform(put(BaseController.BASE_PATH + "/profiles/" + profile.getId() + "/two-factor/confirm").param(IdmAuthenticationFilter.AUTHENTICATION_TOKEN_NAME, token).content(serialize(twoFactorConfirm)).contentType(TestHelper.HAL_CONTENT_TYPE)).andExpect(status().isOk()).andExpect(content().contentType(TestHelper.HAL_CONTENT_TYPE)).andReturn().getResponse().getContentAsString();
    IdmProfileDto updatedProfile = getMapper().readValue(response, IdmProfileDto.class);
    Assert.assertNotNull(updatedProfile);
    Assert.assertEquals(TwoFactorAuthenticationType.APPLICATION, updatedProfile.getTwoFactorAuthenticationType());
    Assert.assertEquals(TwoFactorAuthenticationType.APPLICATION, twoFactorAuthenticationManager.getTwoFactorAuthenticationType(identity.getId()));
    // 
    // login as identity again
    response = getMockMvc().perform(post(BaseController.BASE_PATH + LoginController.AUTH_PATH).content(serialize(login)).contentType(TestHelper.HAL_CONTENT_TYPE)).andExpect(status().isUnauthorized()).andReturn().getResponse().getContentAsString();
    // 
    // get token form response
    token = getMapper().readTree(response).get("_errors").get(0).get("parameters").get("token").asText();
    Assert.assertNotNull(token);
    // 
    // two factor authentication
    Map<String, String> twoFactorLogin = new HashMap<>();
    GuardedString generateCode = twoFactorAuthenticationManager.generateCode(identity.getId());
    Assert.assertTrue(twoFactorAuthenticationManager.verifyCode(identity.getId(), generateCode));
    twoFactorLogin.put("verificationCode", generateCode.asString());
    twoFactorLogin.put("token", token);
    response = getMockMvc().perform(post(BaseController.BASE_PATH + "/authentication/two-factor").content(serialize(twoFactorLogin)).contentType(TestHelper.HAL_CONTENT_TYPE)).andExpect(status().isOk()).andExpect(content().contentType(TestHelper.HAL_CONTENT_TYPE)).andReturn().getResponse().getContentAsString();
    token = getToken(response);
    // 
    // 
    // load identities with valid token
    getMockMvc().perform(get(BaseController.BASE_PATH + "/identities").param(IdmAuthenticationFilter.AUTHENTICATION_TOKEN_NAME, token).contentType(TestHelper.HAL_CONTENT_TYPE)).andExpect(status().isOk());
}
Also used : IdmRoleDto(eu.bcvsolutions.idm.core.api.dto.IdmRoleDto) IdmProfileDto(eu.bcvsolutions.idm.core.api.dto.IdmProfileDto) HashMap(java.util.HashMap) TwoFactorRegistrationResponseDto(eu.bcvsolutions.idm.core.security.api.dto.TwoFactorRegistrationResponseDto) GuardedString(eu.bcvsolutions.idm.core.security.api.domain.GuardedString) GuardedString(eu.bcvsolutions.idm.core.security.api.domain.GuardedString) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto) AbstractRestTest(eu.bcvsolutions.idm.test.api.AbstractRestTest) Test(org.junit.Test)

Example 15 with IdmProfileDto

use of eu.bcvsolutions.idm.core.api.dto.IdmProfileDto in project CzechIdMng by bcvsolutions.

the class LoginControllerRestTest method testTwoFactorLoginWithInvalidToken.

@Test(expected = TwoFactorAuthenticationRequiredException.class)
public void testTwoFactorLoginWithInvalidToken() throws Exception {
    IdmIdentityDto identity = getHelper().createIdentity();
    IdmProfileDto profile = getHelper().createProfile(identity);
    IdmRoleDto role = getHelper().createRole();
    getHelper().createIdentityRole(identity, role);
    getHelper().createBasePolicy(role.getId(), CoreGroupPermission.IDENTITY, IdmIdentity.class, IdmBasePermission.READ);
    // login
    Map<String, String> login = new HashMap<>();
    login.put("username", identity.getUsername());
    login.put("password", identity.getPassword().asString());
    String response = getMockMvc().perform(post(BaseController.BASE_PATH + LoginController.AUTH_PATH).content(serialize(login)).contentType(TestHelper.HAL_CONTENT_TYPE)).andExpect(status().isOk()).andExpect(content().contentType(TestHelper.HAL_CONTENT_TYPE)).andReturn().getResponse().getContentAsString();
    String token = getToken(response);
    // 
    // init two factor authentication by profile controller
    response = getMockMvc().perform(put(BaseController.BASE_PATH + "/profiles/" + profile.getId() + "/two-factor/init").param(IdmAuthenticationFilter.AUTHENTICATION_TOKEN_NAME, token).param("twoFactorAuthenticationType", TwoFactorAuthenticationType.NOTIFICATION.name()).contentType(TestHelper.HAL_CONTENT_TYPE)).andExpect(status().isOk()).andExpect(content().contentType(TestHelper.HAL_CONTENT_TYPE)).andReturn().getResponse().getContentAsString();
    TwoFactorRegistrationResponseDto twoFactorInit = getMapper().readValue(response, TwoFactorRegistrationResponseDto.class);
    Assert.assertNotNull(twoFactorInit);
    Assert.assertNotNull(twoFactorInit.getVerificationSecret());
    // 
    // confirm two factor authentication by profile controller
    Map<String, String> twoFactorConfirm = new HashMap<>();
    twoFactorConfirm.put("verificationCode", twoFactorAuthenticationManager.generateCode(new GuardedString(twoFactorInit.getVerificationSecret())).asString());
    twoFactorConfirm.put("verificationSecret", twoFactorInit.getVerificationSecret());
    twoFactorConfirm.put("twoFactorAuthenticationType", TwoFactorAuthenticationType.NOTIFICATION.name());
    response = getMockMvc().perform(put(BaseController.BASE_PATH + "/profiles/" + profile.getId() + "/two-factor/confirm").param(IdmAuthenticationFilter.AUTHENTICATION_TOKEN_NAME, token).content(serialize(twoFactorConfirm)).contentType(TestHelper.HAL_CONTENT_TYPE)).andExpect(status().isOk()).andExpect(content().contentType(TestHelper.HAL_CONTENT_TYPE)).andReturn().getResponse().getContentAsString();
    IdmProfileDto updatedProfile = getMapper().readValue(response, IdmProfileDto.class);
    Assert.assertNotNull(updatedProfile);
    Assert.assertEquals(TwoFactorAuthenticationType.NOTIFICATION, updatedProfile.getTwoFactorAuthenticationType());
    Assert.assertEquals(TwoFactorAuthenticationType.NOTIFICATION, twoFactorAuthenticationManager.getTwoFactorAuthenticationType(identity.getId()));
    // 
    // login as identity again
    response = getMockMvc().perform(post(BaseController.BASE_PATH + LoginController.AUTH_PATH).content(serialize(login)).contentType(TestHelper.HAL_CONTENT_TYPE)).andExpect(status().isUnauthorized()).andReturn().getResponse().getContentAsString();
    // 
    // get token form response
    token = getMapper().readTree(response).get("_errors").get(0).get("parameters").get("token").asText();
    Assert.assertNotNull(token);
    // 
    // try to load identities with invalid token
    getMockMvc().perform(post(BaseController.BASE_PATH + "/identities").param(IdmAuthenticationFilter.AUTHENTICATION_TOKEN_NAME, token).contentType(TestHelper.HAL_CONTENT_TYPE)).andExpect(status().isUnauthorized());
}
Also used : IdmRoleDto(eu.bcvsolutions.idm.core.api.dto.IdmRoleDto) IdmProfileDto(eu.bcvsolutions.idm.core.api.dto.IdmProfileDto) HashMap(java.util.HashMap) TwoFactorRegistrationResponseDto(eu.bcvsolutions.idm.core.security.api.dto.TwoFactorRegistrationResponseDto) GuardedString(eu.bcvsolutions.idm.core.security.api.domain.GuardedString) GuardedString(eu.bcvsolutions.idm.core.security.api.domain.GuardedString) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto) AbstractRestTest(eu.bcvsolutions.idm.test.api.AbstractRestTest) Test(org.junit.Test)

Aggregations

IdmProfileDto (eu.bcvsolutions.idm.core.api.dto.IdmProfileDto)32 IdmIdentityDto (eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto)22 Test (org.junit.Test)16 GuardedString (eu.bcvsolutions.idm.core.security.api.domain.GuardedString)11 IdmRoleDto (eu.bcvsolutions.idm.core.api.dto.IdmRoleDto)9 IdmPasswordDto (eu.bcvsolutions.idm.core.api.dto.IdmPasswordDto)6 ApiOperation (io.swagger.annotations.ApiOperation)6 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)6 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)6 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)6 IdmProfileFilter (eu.bcvsolutions.idm.core.api.dto.filter.IdmProfileFilter)5 AbstractReadWriteDtoControllerRestTest (eu.bcvsolutions.idm.core.api.rest.AbstractReadWriteDtoControllerRestTest)5 IdmAttachmentDto (eu.bcvsolutions.idm.core.ecm.api.dto.IdmAttachmentDto)5 AbstractIntegrationTest (eu.bcvsolutions.idm.test.api.AbstractIntegrationTest)5 ResponseEntity (org.springframework.http.ResponseEntity)5 Transactional (org.springframework.transaction.annotation.Transactional)4 ConfigurationMap (eu.bcvsolutions.idm.core.api.domain.ConfigurationMap)3 DefaultEventResult (eu.bcvsolutions.idm.core.api.event.DefaultEventResult)3 EntityNotFoundException (eu.bcvsolutions.idm.core.api.exception.EntityNotFoundException)3 ResultCodeException (eu.bcvsolutions.idm.core.api.exception.ResultCodeException)3