Search in sources :

Example 6 with IdmJwtAuthenticationDto

use of eu.bcvsolutions.idm.core.security.api.dto.IdmJwtAuthenticationDto in project CzechIdMng by bcvsolutions.

the class AuthenticationTestUtils method getAuthDto.

public static IdmJwtAuthenticationDto getAuthDto(IdmIdentityDto user, Collection<GrantedAuthority> authorities) {
    IdmJwtAuthenticationDto d = new IdmJwtAuthenticationDto();
    d.setCurrentUsername(user.getUsername());
    d.setCurrentIdentityId(user.getId());
    d.setIssuedAt(getIat());
    d.setExpiration(getExp());
    d.setAuthorities(new ArrayList<>());
    d.setFromModule("test");
    d.setAuthorities(authorities.stream().map(a -> new DefaultGrantedAuthorityDto(a.toString())).collect(Collectors.toList()));
    return d;
}
Also used : DefaultGrantedAuthorityDto(eu.bcvsolutions.idm.core.security.api.dto.DefaultGrantedAuthorityDto) IdmJwtAuthenticationDto(eu.bcvsolutions.idm.core.security.api.dto.IdmJwtAuthenticationDto)

Example 7 with IdmJwtAuthenticationDto

use of eu.bcvsolutions.idm.core.security.api.dto.IdmJwtAuthenticationDto in project CzechIdMng by bcvsolutions.

the class DefaultJwtAuthenticationServiceTest method createJwtAuthenticationAndAuthenticateTest.

@Test
public void createJwtAuthenticationAndAuthenticateTest() {
    createTestUser();
    IdmIdentityDto identityDto = getTestIdentity();
    LoginDto loginDto = getTestLoginDto();
    LoginDto resultLoginDto = jwtAuthenticationService.createJwtAuthenticationAndAuthenticate(loginDto, identityDto, MODULE);
    Assert.assertTrue(securityService.isAuthenticated());
    Assert.assertEquals(USERNAME, securityService.getCurrentUsername());
    Assert.assertEquals(USERNAME, resultLoginDto.getUsername());
    Assert.assertEquals(MODULE, resultLoginDto.getAuthenticationModule());
    Assert.assertNotNull(resultLoginDto.getToken());
    IdmJwtAuthenticationDto jwtAuthenticationDto = resultLoginDto.getAuthentication();
    Assert.assertNotNull(jwtAuthenticationDto);
    Assert.assertEquals(USERNAME, jwtAuthenticationDto.getCurrentUsername());
    Assert.assertEquals(MODULE, jwtAuthenticationDto.getFromModule());
    Assert.assertTrue(jwtAuthenticationDto.getAuthorities().isEmpty());
}
Also used : IdmJwtAuthenticationDto(eu.bcvsolutions.idm.core.security.api.dto.IdmJwtAuthenticationDto) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto) LoginDto(eu.bcvsolutions.idm.core.security.api.dto.LoginDto) AbstractIntegrationTest(eu.bcvsolutions.idm.test.api.AbstractIntegrationTest) Test(org.junit.Test)

Example 8 with IdmJwtAuthenticationDto

use of eu.bcvsolutions.idm.core.security.api.dto.IdmJwtAuthenticationDto 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!");
    }
}
Also used : IdmJwtAuthentication(eu.bcvsolutions.idm.core.security.api.domain.IdmJwtAuthentication) IdmJwtAuthenticationDto(eu.bcvsolutions.idm.core.security.api.dto.IdmJwtAuthenticationDto) IOException(java.io.IOException)

Example 9 with IdmJwtAuthenticationDto

use of eu.bcvsolutions.idm.core.security.api.dto.IdmJwtAuthenticationDto in project CzechIdMng by bcvsolutions.

the class JwtIdmAuthenticationFilter method authorize.

@Override
public boolean authorize(String token, HttpServletRequest request, HttpServletResponse response) {
    IdmJwtAuthenticationDto claims = null;
    try {
        Optional<Jwt> jwt = HttpFilterUtils.parseToken(token);
        if (!jwt.isPresent()) {
            return false;
        }
        HttpFilterUtils.verifyToken(jwt.get(), jwtTokenMapper.getVerifier());
        claims = jwtTokenMapper.getClaims(jwt.get());
        ctx.setToken(claims);
        Authentication auth = authenticationManager.authenticate(jwtTokenMapper.fromDto(claims));
        LOG.debug("User [{}] successfully logged in.", auth.getName());
        return auth.isAuthenticated();
    } catch (ResultCodeException ex) {
        LOG.warn("Invalid token, reason: [{}]", ex.getMessage());
        ctx.setCodeEx(ex);
        // only expired or authorities changed
        ctx.setToken(claims);
    } catch (AuthenticationException ex) {
        LOG.warn("Invalid authentication, reason: [{}]", ex.getMessage());
        ctx.setAuthEx(ex);
    } catch (InvalidSignatureException | IOException | IllegalArgumentException ex) {
        // client sent some rubbish, just log and ignore
        LOG.warn("Invalid IdM auth token received.", ex);
    }
    return false;
}
Also used : InvalidSignatureException(org.springframework.security.jwt.crypto.sign.InvalidSignatureException) AuthenticationException(org.springframework.security.core.AuthenticationException) Jwt(org.springframework.security.jwt.Jwt) Authentication(org.springframework.security.core.Authentication) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) IdmJwtAuthenticationDto(eu.bcvsolutions.idm.core.security.api.dto.IdmJwtAuthenticationDto) IOException(java.io.IOException)

Example 10 with IdmJwtAuthenticationDto

use of eu.bcvsolutions.idm.core.security.api.dto.IdmJwtAuthenticationDto 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);
    }
}
Also used : IdmAuthenticationException(eu.bcvsolutions.idm.core.security.exception.IdmAuthenticationException) IdmJwtAuthentication(eu.bcvsolutions.idm.core.security.api.domain.IdmJwtAuthentication) IdmJwtAuthenticationDto(eu.bcvsolutions.idm.core.security.api.dto.IdmJwtAuthenticationDto) IOException(java.io.IOException)

Aggregations

IdmJwtAuthenticationDto (eu.bcvsolutions.idm.core.security.api.dto.IdmJwtAuthenticationDto)11 Test (org.junit.Test)5 AbstractRestTest (eu.bcvsolutions.idm.test.api.AbstractRestTest)3 IOException (java.io.IOException)3 IdmJwtAuthentication (eu.bcvsolutions.idm.core.security.api.domain.IdmJwtAuthentication)2 LoginDto (eu.bcvsolutions.idm.core.security.api.dto.LoginDto)2 AbstractIntegrationTest (eu.bcvsolutions.idm.test.api.AbstractIntegrationTest)2 Jwt (org.springframework.security.jwt.Jwt)2 MvcResult (org.springframework.test.web.servlet.MvcResult)2 IdmIdentityDto (eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto)1 ResultCodeException (eu.bcvsolutions.idm.core.api.exception.ResultCodeException)1 GuardedString (eu.bcvsolutions.idm.core.security.api.domain.GuardedString)1 DefaultGrantedAuthorityDto (eu.bcvsolutions.idm.core.security.api.dto.DefaultGrantedAuthorityDto)1 IdmAuthenticationException (eu.bcvsolutions.idm.core.security.exception.IdmAuthenticationException)1 Authentication (org.springframework.security.core.Authentication)1 AuthenticationException (org.springframework.security.core.AuthenticationException)1 InvalidSignatureException (org.springframework.security.jwt.crypto.sign.InvalidSignatureException)1