use of eu.bcvsolutions.idm.core.security.api.domain.IdmJwtAuthentication in project CzechIdMng by bcvsolutions.
the class LoginControllerRestTest method testCasLoginResponse.
@Test
public void testCasLoginResponse() throws Exception {
// without login
getMockMvc().perform(get(BaseController.BASE_PATH + LoginController.AUTH_PATH + LoginController.CAS_LOGIN_RESPONSE_PATH).contentType(TestHelper.HAL_CONTENT_TYPE)).andExpect(status().isFound()).andExpect(MockMvcResultMatchers.redirectedUrl(LoginController.CAS_LOGIN_RESPONSE_PATH + "?status-code=" + CoreResultCode.LOG_IN_FAILED.getCode().toLowerCase()));
// with login
try {
loginAsAdmin();
IdmTokenDto currentToken = tokenManager.getCurrentToken();
IdmJwtAuthentication authentication = jwtTokenMapper.fromDto(currentToken);
String token = jwtTokenMapper.writeToken(authentication);
//
getMockMvc().perform(get(BaseController.BASE_PATH + LoginController.AUTH_PATH + LoginController.CAS_LOGIN_RESPONSE_PATH).header(JwtAuthenticationMapper.AUTHENTICATION_TOKEN_NAME, token).contentType(TestHelper.HAL_CONTENT_TYPE)).andExpect(status().isFound());
} finally {
logout();
}
}
use of eu.bcvsolutions.idm.core.security.api.domain.IdmJwtAuthentication 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.domain.IdmJwtAuthentication in project CzechIdMng by bcvsolutions.
the class OAuthAuthenticationManagerUnitTest method testAuthSuccess.
/**
* Successful authentication
*/
@Test
public void testAuthSuccess() {
IdmIdentityDto i = getTestIdentity();
IdmJwtAuthentication authentication = getAuthentication(UUID.randomUUID(), i, ZonedDateTime.now().plusHours(1), ZonedDateTime.now());
doNothing().when(workflowIdentityService).setAuthenticatedUserId(USER_NAME);
doNothing().when(securityService).setAuthentication(authentication);
IdmTokenDto token = new IdmTokenDto(authentication.getId());
token.setOwnerId(i.getId());
when(tokenService.get(authentication.getId())).thenReturn(token);
when(cacheManager.getValue(TokenManager.TOKEN_CACHE_NAME, token.getId())).thenReturn(null);
Authentication auth = authManager.authenticate(authentication);
Assert.assertEquals(USER_NAME, auth.getName());
Assert.assertEquals(USER_NAME, auth.getPrincipal());
Assert.assertTrue(auth.getAuthorities().isEmpty());
verify(workflowIdentityService).setAuthenticatedUserId(USER_NAME);
verify(securityService).setAuthentication(authentication);
verify(tokenService).get(authentication.getId());
verify(cacheManager).getValue(TokenManager.TOKEN_CACHE_NAME, token.getId());
}
use of eu.bcvsolutions.idm.core.security.api.domain.IdmJwtAuthentication in project CzechIdMng by bcvsolutions.
the class OAuthAuthenticationManagerUnitTest method testAuthExpired.
/**
* Expired tokens are not accepted.
*/
@Test(expected = ResultCodeException.class)
public void testAuthExpired() {
IdmIdentityDto i = getTestIdentity();
IdmTokenDto token = new IdmTokenDto(UUID.randomUUID());
token.setExpiration(ZonedDateTime.now().minusHours(1));
when(tokenService.get(token.getId())).thenReturn(token);
when(cacheManager.getValue(TokenManager.TOKEN_CACHE_NAME, token.getId())).thenReturn(null);
IdmJwtAuthentication authentication = getAuthentication(token.getId(), i, ZonedDateTime.now().minusHours(1), ZonedDateTime.now().plusHours(2));
authManager.authenticate(authentication);
Assert.fail("Cannot authenticate with expired token.");
}
use of eu.bcvsolutions.idm.core.security.api.domain.IdmJwtAuthentication in project CzechIdMng by bcvsolutions.
the class AbstractIntegrationTest method loginWithout.
/**
* Login as user without authorities given in parameter authorities.
*
* @param user
* @param authorities
*/
public void loginWithout(String user, String... authorities) {
Collection<GrantedAuthority> authoritiesWithout = IdmAuthorityUtils.toAuthorities(moduleService.getAvailablePermissions()).stream().filter(authority -> {
for (String auth : authorities) {
if (auth.equals(authority.getAuthority())) {
return false;
}
}
return true;
}).collect(Collectors.toList());
IdmIdentityDto identity = (IdmIdentityDto) lookupService.getDtoLookup(IdmIdentityDto.class).lookup(user);
SecurityContextHolder.getContext().setAuthentication(new IdmJwtAuthentication(identity, null, authoritiesWithout, "test"));
}
Aggregations