use of eu.bcvsolutions.idm.core.security.api.dto.TwoFactorRegistrationResponseDto 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());
}
use of eu.bcvsolutions.idm.core.security.api.dto.TwoFactorRegistrationResponseDto in project CzechIdMng by bcvsolutions.
the class DefaultTwoFactorAuthenticationManagerIntegrationTest method testAuthenticatePasswordIsDeleted.
@Test(expected = EntityNotFoundException.class)
public void testAuthenticatePasswordIsDeleted() {
// password is needed
IdmIdentityDto identity = getHelper().createIdentity();
IdmPasswordDto password = passwordService.findOneByIdentity(identity.getId());
//
TwoFactorRegistrationResponseDto initResponse = manager.init(identity.getId(), TwoFactorAuthenticationType.NOTIFICATION);
Assert.assertNotNull(initResponse);
Assert.assertNotNull(initResponse.getVerificationSecret());
Assert.assertEquals(identity.getUsername(), initResponse.getUsername());
Assert.assertNull(initResponse.getQrcode());
//
// confirm
TwoFactorRegistrationConfirmDto confirm = new TwoFactorRegistrationConfirmDto();
confirm.setVerificationSecret(new GuardedString(initResponse.getVerificationSecret()));
confirm.setVerificationCode(manager.generateCode(new GuardedString(initResponse.getVerificationSecret())));
confirm.setTwoFactorAuthenticationType(TwoFactorAuthenticationType.NOTIFICATION);
Assert.assertTrue(manager.confirm(identity.getId(), confirm));
Assert.assertEquals(initResponse.getVerificationSecret(), getHelper().getPassword(identity).getVerificationSecret());
//
LoginDto loginDto = new LoginDto();
loginDto.setUsername(identity.getUsername());
loginDto.setPassword(identity.getPassword());
// creadentials are valid
Assert.assertTrue(authenticationManager.validate(loginDto));
// but two factor authentication is required
String token = null;
try {
authenticationManager.authenticate(loginDto);
} catch (TwoFactorAuthenticationRequiredException ex) {
token = ex.getToken();
}
Assert.assertNotNull(token);
//
loginDto.setToken(token);
loginDto.setPassword(manager.generateCode(identity.getId()));
//
// delete password
passwordService.delete(password);
//
manager.authenticate(loginDto);
}
use of eu.bcvsolutions.idm.core.security.api.dto.TwoFactorRegistrationResponseDto in project CzechIdMng by bcvsolutions.
the class DefaultTwoFactorAuthenticationManagerIntegrationTest method testAuthenticateTokenExpired.
@Test(expected = ResultCodeException.class)
public void testAuthenticateTokenExpired() throws Exception {
// password is needed
IdmIdentityDto identity = getHelper().createIdentity();
//
TwoFactorRegistrationResponseDto initResponse = manager.init(identity.getId(), TwoFactorAuthenticationType.NOTIFICATION);
Assert.assertNotNull(initResponse);
Assert.assertNotNull(initResponse.getVerificationSecret());
Assert.assertEquals(identity.getUsername(), initResponse.getUsername());
Assert.assertNull(initResponse.getQrcode());
//
// confirm
TwoFactorRegistrationConfirmDto confirm = new TwoFactorRegistrationConfirmDto();
confirm.setVerificationSecret(new GuardedString(initResponse.getVerificationSecret()));
confirm.setVerificationCode(manager.generateCode(new GuardedString(initResponse.getVerificationSecret())));
confirm.setTwoFactorAuthenticationType(TwoFactorAuthenticationType.NOTIFICATION);
Assert.assertTrue(manager.confirm(identity.getId(), confirm));
Assert.assertEquals(initResponse.getVerificationSecret(), getHelper().getPassword(identity).getVerificationSecret());
//
LoginDto loginDto = new LoginDto();
loginDto.setUsername(identity.getUsername());
loginDto.setPassword(identity.getPassword());
// creadentials are valid
Assert.assertTrue(authenticationManager.validate(loginDto));
// but two factor authentication is required
String token = null;
try {
authenticationManager.authenticate(loginDto);
} catch (TwoFactorAuthenticationRequiredException ex) {
token = ex.getToken();
}
Assert.assertNotNull(token);
//
// set token expiration
IdmJwtAuthentication jwt = jwtAuthenticationMapper.readToken(token);
jwt.setExpiration(ZonedDateTime.now().minusDays(1));
token = jwtAuthenticationMapper.writeToken(jwt);
//
loginDto.setToken(token);
loginDto.setPassword(manager.generateCode(identity.getId()));
//
manager.authenticate(loginDto);
}
use of eu.bcvsolutions.idm.core.security.api.dto.TwoFactorRegistrationResponseDto in project CzechIdMng by bcvsolutions.
the class DefaultTwoFactorAuthenticationManagerIntegrationTest method testAuthenticateWrongVerificationCode.
@Test(expected = ResultCodeException.class)
public void testAuthenticateWrongVerificationCode() {
// password is needed
IdmIdentityDto identity = getHelper().createIdentity();
//
TwoFactorRegistrationResponseDto initResponse = manager.init(identity.getId(), TwoFactorAuthenticationType.NOTIFICATION);
Assert.assertNotNull(initResponse);
Assert.assertNotNull(initResponse.getVerificationSecret());
Assert.assertEquals(identity.getUsername(), initResponse.getUsername());
Assert.assertNull(initResponse.getQrcode());
//
// confirm
TwoFactorRegistrationConfirmDto confirm = new TwoFactorRegistrationConfirmDto();
confirm.setVerificationSecret(new GuardedString(initResponse.getVerificationSecret()));
confirm.setVerificationCode(manager.generateCode(new GuardedString(initResponse.getVerificationSecret())));
confirm.setTwoFactorAuthenticationType(TwoFactorAuthenticationType.NOTIFICATION);
Assert.assertTrue(manager.confirm(identity.getId(), confirm));
Assert.assertEquals(initResponse.getVerificationSecret(), getHelper().getPassword(identity).getVerificationSecret());
//
LoginDto loginDto = new LoginDto();
loginDto.setUsername(identity.getUsername());
loginDto.setPassword(identity.getPassword());
// creadentials are valid
Assert.assertTrue(authenticationManager.validate(loginDto));
// but two factor authentication is required
String token = null;
try {
authenticationManager.authenticate(loginDto);
} catch (TwoFactorAuthenticationRequiredException ex) {
token = ex.getToken();
}
Assert.assertNotNull(token);
//
loginDto.setToken(token);
loginDto.setPassword(new GuardedString("mock"));
//
manager.authenticate(loginDto);
}
use of eu.bcvsolutions.idm.core.security.api.dto.TwoFactorRegistrationResponseDto in project CzechIdMng by bcvsolutions.
the class DefaultTwoFactorAuthenticationManagerIntegrationTest method testRegistrationNotification.
@Test
public void testRegistrationNotification() {
// password is needed
IdmIdentityDto identity = getHelper().createIdentity();
Assert.assertNull(manager.getTwoFactorAuthenticationType(identity.getId()));
//
TwoFactorRegistrationResponseDto initResponse = manager.init(identity.getId(), TwoFactorAuthenticationType.NOTIFICATION);
Assert.assertNotNull(initResponse);
Assert.assertNotNull(initResponse.getVerificationSecret());
Assert.assertEquals(identity.getUsername(), initResponse.getUsername());
Assert.assertNull(initResponse.getQrcode());
//
// confirm
TwoFactorRegistrationConfirmDto confirm = new TwoFactorRegistrationConfirmDto();
confirm.setVerificationSecret(new GuardedString(initResponse.getVerificationSecret()));
confirm.setVerificationCode(manager.generateCode(new GuardedString(initResponse.getVerificationSecret())));
confirm.setTwoFactorAuthenticationType(TwoFactorAuthenticationType.NOTIFICATION);
Assert.assertTrue(manager.confirm(identity.getId(), confirm));
Assert.assertEquals(initResponse.getVerificationSecret(), getHelper().getPassword(identity).getVerificationSecret());
Assert.assertEquals(TwoFactorAuthenticationType.NOTIFICATION, manager.getTwoFactorAuthenticationType(identity.getId()));
}
Aggregations