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());
}
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);
}
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());
}
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());
}
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());
}
Aggregations