use of eu.bcvsolutions.idm.core.security.api.domain.IdmJwtAuthentication in project CzechIdMng by bcvsolutions.
the class TestAppAuthenticationFilter method authorize.
@Override
public boolean authorize(String token, HttpServletRequest request, HttpServletResponse response) {
try {
Optional<Jwt> jwt = HttpFilterUtils.parseToken(token);
if (!jwt.isPresent()) {
return false;
}
Map<String, Object> claims = verifyTokenAndGetClaims(jwt.get());
String userName = (String) claims.get(HttpFilterUtils.JWT_USER_NAME);
IdmIdentityDto identity = identityService.getByUsername(userName);
// not important - either new refreshed token or data are returned to user
ZonedDateTime expiration = null;
Collection<GrantedAuthority> authorities = null;
if (shouldGrantAuthoritiesForPath(request.getServletPath())) {
authorities = grantedAuthoritiesFactory.getGrantedAuthoritiesForIdentity(identity.getId());
} else {
authorities = new ArrayList<>();
}
IdmJwtAuthentication ija = new IdmJwtAuthentication(identity, expiration, authorities, EntityUtils.getModule(this.getClass()));
SecurityContextHolder.getContext().setAuthentication(ija);
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
use of eu.bcvsolutions.idm.core.security.api.domain.IdmJwtAuthentication in project CzechIdMng by bcvsolutions.
the class IdmTokenControllerRestTest method testGenerate.
@Test
public void testGenerate() throws Exception {
IdmIdentityDto identity = getHelper().createIdentity((GuardedString) null);
IdmTokenDto token = new IdmTokenDto();
token.setOwnerId(identity.getId());
token.setOwnerType(tokenManager.getOwnerType(identity));
token.setTokenType("custom");
token.setExpiration(ZonedDateTime.now().plusDays(1));
ObjectMapper mapper = getMapper();
//
String response = getMockMvc().perform(post(getBaseUrl()).with(authentication(getAdminAuthentication())).content(mapper.writeValueAsString(token)).contentType(TestHelper.HAL_CONTENT_TYPE)).andExpect(status().isCreated()).andExpect(content().contentType(TestHelper.HAL_CONTENT_TYPE)).andReturn().getResponse().getContentAsString();
IdmTokenDto createdToken = (IdmTokenDto) mapper.readValue(response, token.getClass());
Assert.assertNotNull(createdToken);
Assert.assertNotNull(createdToken.getId());
Assert.assertTrue(token.getExpiration().isEqual(createdToken.getExpiration()));
Assert.assertEquals(token.getTokenType(), createdToken.getTokenType());
//
// token is filled
String jwtToken = createdToken.getProperties().getString(IdmAuthenticationFilter.AUTHENTICATION_TOKEN_NAME);
Assert.assertNotNull(jwtToken);
IdmJwtAuthentication readToken = jwtTokenMapper.readToken(jwtToken);
Assert.assertEquals(createdToken.getId(), readToken.getId());
Assert.assertTrue(token.getExpiration().isEqual(readToken.getExpiration()));
//
IdmTokenDto getToken = getDto(createdToken.getId());
// token is not filled after get
Assert.assertNull(getToken.getProperties().get(IdmAuthenticationFilter.AUTHENTICATION_TOKEN_NAME));
}
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