Search in sources :

Example 31 with IdmJwtAuthentication

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;
    // 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());
    } else {
        // prolong expiration
        token = jwtTokenMapper.prolongExpiration(ctx.getToken());
    }
    // 
    res.setHeader(JwtAuthenticationMapper.AUTHENTICATION_TOKEN_NAME, jwtTokenMapper.writeToken(token));
}
Also used : IdmJwtAuthentication(eu.bcvsolutions.idm.core.security.api.domain.IdmJwtAuthentication) IdmJwtAuthenticationDto(eu.bcvsolutions.idm.core.security.api.dto.IdmJwtAuthenticationDto)

Aggregations

IdmJwtAuthentication (eu.bcvsolutions.idm.core.security.api.domain.IdmJwtAuthentication)31 IdmIdentityDto (eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto)22 Test (org.junit.Test)14 ResultCodeException (eu.bcvsolutions.idm.core.api.exception.ResultCodeException)10 GrantedAuthority (org.springframework.security.core.GrantedAuthority)10 AbstractUnitTest (eu.bcvsolutions.idm.test.api.AbstractUnitTest)8 Collection (java.util.Collection)8 IdmTokenDto (eu.bcvsolutions.idm.core.api.dto.IdmTokenDto)7 GuardedString (eu.bcvsolutions.idm.core.security.api.domain.GuardedString)7 UUID (java.util.UUID)7 Collectors (java.util.stream.Collectors)7 Autowired (org.springframework.beans.factory.annotation.Autowired)7 ModuleService (eu.bcvsolutions.idm.core.api.service.ModuleService)6 IdmGroupPermission (eu.bcvsolutions.idm.core.security.api.domain.IdmGroupPermission)6 IdmAuthorityUtils (eu.bcvsolutions.idm.core.security.api.utils.IdmAuthorityUtils)6 Before (org.junit.Before)6 SecurityContextHolder (org.springframework.security.core.context.SecurityContextHolder)6 IdmJwtAuthenticationDto (eu.bcvsolutions.idm.core.security.api.dto.IdmJwtAuthenticationDto)4 LoginDto (eu.bcvsolutions.idm.core.security.api.dto.LoginDto)4 After (org.junit.After)4