use of eu.bcvsolutions.idm.core.security.api.domain.IdmJwtAuthentication in project CzechIdMng by bcvsolutions.
the class OAuthAuthenticationManagerTest method testAuthExpired.
/**
* Expired tokens are not accepted.
*/
@Test(expected = ResultCodeException.class)
public void testAuthExpired() {
IdmIdentityDto i = getTestIdentity();
when(identityService.getByUsername(USER_NAME)).thenReturn(i);
IdmJwtAuthentication authentication = getAuthentication(USER_NAME, DateTime.now().minusHours(1), DateTime.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 AbstractWorkflowIntegrationTest method loginAsNoAdmin.
public void loginAsNoAdmin(String user) {
Collection<GrantedAuthority> authorities = IdmAuthorityUtils.toAuthorities(moduleService.getAvailablePermissions()).stream().filter(authority -> {
return !IdmGroupPermission.APP_ADMIN.equals(authority.getAuthority());
}).collect(Collectors.toList());
IdmIdentityDto identity = (IdmIdentityDto) lookupService.getDtoLookup(IdmIdentityDto.class).lookup(user);
SecurityContextHolder.getContext().setAuthentication(new IdmJwtAuthentication(identity, null, authorities, "test"));
}
use of eu.bcvsolutions.idm.core.security.api.domain.IdmJwtAuthentication in project CzechIdMng by bcvsolutions.
the class DefaultIdmAutomaticRoleRequestServiceIntegrationTest method notRightForExecuteImmediatelyExceptionTest.
@Test(expected = RoleRequestException.class)
public void notRightForExecuteImmediatelyExceptionTest() {
this.logout();
IdmIdentityDto identity = helper.createIdentity();
// Log as user without right for immediately execute role request (without
// approval)
Collection<GrantedAuthority> authorities = IdmAuthorityUtils.toAuthorities(moduleService.getAvailablePermissions()).stream().filter(authority -> {
return !CoreGroupPermission.AUTOMATIC_ROLE_REQUEST_ADMIN.equals(authority.getAuthority()) && !IdmGroupPermission.APP_ADMIN.equals(authority.getAuthority());
}).collect(Collectors.toList());
SecurityContextHolder.getContext().setAuthentication(new IdmJwtAuthentication(new IdmIdentityDto(identity.getUsername()), null, authorities, "test"));
IdmRoleDto role = prepareRole();
IdmAutomaticRoleRequestDto request = new IdmAutomaticRoleRequestDto();
request.setState(RequestState.EXECUTED);
request.setOperation(RequestOperationType.ADD);
request.setRequestType(AutomaticRoleRequestType.ATTRIBUTE);
request.setExecuteImmediately(true);
request.setName(role.getName());
request.setRole(role.getId());
request = roleRequestService.save(request);
Assert.assertEquals(RequestState.CONCEPT, request.getState());
IdmAutomaticRoleAttributeRuleRequestDto rule = new IdmAutomaticRoleAttributeRuleRequestDto();
rule.setRequest(request.getId());
rule.setOperation(RequestOperationType.ADD);
rule.setAttributeName(IdmIdentity_.username.getName());
rule.setComparison(AutomaticRoleAttributeRuleComparison.EQUALS);
rule.setType(AutomaticRoleAttributeRuleType.IDENTITY);
rule.setValue("test");
rule = ruleRequestService.save(rule);
// We expect exception state (we don`t have right for execute without approval)
roleRequestService.startRequestInternal(request.getId(), true);
}
use of eu.bcvsolutions.idm.core.security.api.domain.IdmJwtAuthentication in project CzechIdMng by bcvsolutions.
the class ExtendExpirationFilter method doExtendExpiration.
/**
* Extends token expiration time. There two types of extensions,
* either by just setting new expiration time or by issuing
* a fresh token. A fresh token is issued only if the original
* one in HTTP request is expired or authorities change and
* user signed in by other means than IdM JWT token (remote OAuth / Basic...).
*
* The token with extended expiration is set into a response header.
*
* @param req
* @param res
*/
private void doExtendExpiration(HttpServletRequest req, HttpServletResponse res) {
if (ctx.isDisabledOrNotExists()) {
// he cannot be disabled or nonexistent
return;
}
IdmJwtAuthenticationDto token = ctx.getToken();
token.setExpiration(getNewExpiration());
// this is a valid state and we only issue a fresh IdM token
if (ctx.isExpired() || ctx.isAuthoritiesChanged()) {
token = jwtTokenMapper.toDto((IdmJwtAuthentication) SecurityContextHolder.getContext().getAuthentication());
}
try {
res.setHeader(JwtAuthenticationMapper.AUTHENTICATION_TOKEN_NAME, jwtTokenMapper.writeToken(token));
} catch (IOException e) {
LOG.warn("Cannot write token with extended expiration header!");
}
}
use of eu.bcvsolutions.idm.core.security.api.domain.IdmJwtAuthentication in project CzechIdMng by bcvsolutions.
the class DefaultJwtAuthenticationService method createJwtAuthenticationAndAuthenticate.
@Override
public LoginDto createJwtAuthenticationAndAuthenticate(LoginDto loginDto, IdmIdentityDto identity, String module) {
IdmJwtAuthentication authentication = new IdmJwtAuthentication(identity, getAuthExpiration(), grantedAuthoritiesFactory.getGrantedAuthorities(loginDto.getUsername()), module);
oauthAuthenticationManager.authenticate(authentication);
IdmJwtAuthenticationDto authenticationDto = jwtTokenMapper.toDto(authentication);
try {
loginDto.setAuthenticationModule(module);
loginDto.setAuthentication(authenticationDto);
loginDto.setToken(jwtTokenMapper.writeToken(authenticationDto));
return loginDto;
} catch (IOException ex) {
throw new IdmAuthenticationException(ex.getMessage(), ex);
}
}
Aggregations