use of eu.bcvsolutions.idm.core.security.api.domain.IdmJwtAuthentication in project CzechIdMng by bcvsolutions.
the class OAuthAuthenticationManager method authenticate.
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
if (!(authentication instanceof IdmJwtAuthentication)) {
throw new IdmAuthenticationException("Unsupported granted authority " + authentication.getClass().getName());
}
IdmJwtAuthentication idmJwtAuthentication = (IdmJwtAuthentication) authentication;
IdmIdentityDto identity = getIdentityForToken(idmJwtAuthentication);
IdmAuthorityChange authChange = getIdentityAuthorityChange(identity);
checkIssuedTime(idmJwtAuthentication.getIssuedAt(), authChange);
checkExpirationTime(idmJwtAuthentication);
checkDisabled(identity);
// Set logged user to workflow engine
workflowIdentityService.setAuthenticatedUserId(identity.getUsername());
// set authentication
securityService.setAuthentication(idmJwtAuthentication);
//
return idmJwtAuthentication;
}
use of eu.bcvsolutions.idm.core.security.api.domain.IdmJwtAuthentication in project CzechIdMng by bcvsolutions.
the class OAuthAuthenticationManagerTest method testIdentityNotExists.
/**
* Non-existent identities cannot possess auth. tokens.
*/
@Test
public void testIdentityNotExists() {
IdmJwtAuthentication authentication = getAuthentication(USER_NAME, DateTime.now().plusHours(1), DateTime.now());
when(identityService.getByUsername(USER_NAME)).thenReturn(null);
try {
authManager.authenticate(authentication);
Assert.fail("Cannot authenticate unknown identity.");
} catch (AuthenticationException e) {
verify(identityService).getByUsername(USER_NAME);
}
}
use of eu.bcvsolutions.idm.core.security.api.domain.IdmJwtAuthentication in project CzechIdMng by bcvsolutions.
the class OAuthAuthenticationManagerTest method testAuthorityModification.
/**
* Removing a role which grants authorities results in authentication
* expiration.
*/
@Test
public void testAuthorityModification() {
IdmIdentityDto i = getTestIdentity();
IdmAuthorityChange ac = getAuthChange(i, DateTime.now());
IdmJwtAuthentication authentication = getAuthentication(USER_NAME, DateTime.now().plusHours(1), DateTime.now().minusHours(1));
when(identityService.getByUsername(USER_NAME)).thenReturn(i);
when(acRepository.findOneByIdentity_Id(i.getId())).thenReturn(ac);
try {
authManager.authenticate(authentication);
Assert.fail("Cannot authenticate identity with modified authorities.");
} catch (ResultCodeException e) {
Assert.assertEquals(CoreResultCode.AUTHORITIES_CHANGED.getStatus(), e.getStatus());
Assert.assertEquals(CoreResultCode.AUTHORITIES_CHANGED.getMessage(), e.getMessage());
verify(identityService).getByUsername(USER_NAME);
verify(acRepository).findOneByIdentity_Id(i.getId());
}
}
use of eu.bcvsolutions.idm.core.security.api.domain.IdmJwtAuthentication in project CzechIdMng by bcvsolutions.
the class AbstractWorkflowIntegrationTest 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