Search in sources :

Example 1 with IdmJwtAuthenticationDto

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

the class JwtAuthenticationMapper method toDto.

/**
 * Converts authentication.
 *
 * @param authentication to dto
 * @return
 */
public IdmJwtAuthenticationDto toDto(IdmJwtAuthentication authentication) {
    Assert.notNull(authentication);
    // 
    IdmJwtAuthenticationDto authenticationDto = new IdmJwtAuthenticationDto();
    authenticationDto.setCurrentUsername(authentication.getCurrentUsername());
    authenticationDto.setCurrentIdentityId(getIdentityId(authentication.getCurrentIdentity()));
    authenticationDto.setOriginalUsername(authentication.getOriginalUsername());
    authenticationDto.setOriginalIdentityId(getIdentityId(authentication.getOriginalIdentity()));
    authenticationDto.setExpiration(authentication.getExpiration());
    authenticationDto.setFromModule(authentication.getFromModule());
    authenticationDto.setIssuedAt(DateTime.now());
    authenticationDto.setAuthorities(getDtoAuthorities(authentication));
    return authenticationDto;
}
Also used : IdmJwtAuthenticationDto(eu.bcvsolutions.idm.core.security.api.dto.IdmJwtAuthenticationDto)

Example 2 with IdmJwtAuthenticationDto

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

the class LoginControllerTest method testSuccesfulLogIn.

@Test
public void testSuccesfulLogIn() throws Exception {
    LoginDto loginDto = new LoginDto();
    loginDto.setUsername(InitTestData.TEST_ADMIN_USERNAME);
    loginDto.setPassword(new GuardedString(InitTestData.TEST_ADMIN_PASSWORD));
    Resource<LoginDto> response = loginController.login(loginDto);
    IdmJwtAuthenticationDto authentication = response.getContent().getAuthentication();
    assertNotNull(authentication);
    assertEquals(InitTestData.TEST_ADMIN_USERNAME, authentication.getCurrentUsername());
    assertEquals(InitTestData.TEST_ADMIN_USERNAME, authentication.getOriginalUsername());
}
Also used : IdmJwtAuthenticationDto(eu.bcvsolutions.idm.core.security.api.dto.IdmJwtAuthenticationDto) GuardedString(eu.bcvsolutions.idm.core.security.api.domain.GuardedString) LoginDto(eu.bcvsolutions.idm.core.security.api.dto.LoginDto) AbstractIntegrationTest(eu.bcvsolutions.idm.test.api.AbstractIntegrationTest) Test(org.junit.Test)

Example 3 with IdmJwtAuthenticationDto

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

the class ExtendExpirationFilterTest method testSuccessfulTokenExtension.

@Test
public void testSuccessfulTokenExtension() throws Exception {
    IdmJwtAuthenticationDto authDto = AuthenticationTestUtils.getAuthDto(identityService.getByUsername(TEST_ADMIN_USERNAME), Lists.newArrayList(IdmAuthorityUtils.getAdminAuthority()));
    String token = getAuthToken(authDto);
    sleep();
    MvcResult result = getMockMvc().perform(get(AuthenticationTestUtils.getSelfPath(TEST_ADMIN_USERNAME)).header(JwtAuthenticationMapper.AUTHENTICATION_TOKEN_NAME, token).contentType(HAL_CONTENT_TYPE)).andExpect(status().isOk()).andExpect(content().contentType(HAL_CONTENT_TYPE)).andExpect(jsonPath("$.username", equalTo(TEST_ADMIN_USERNAME))).andReturn();
    IdmJwtAuthenticationDto extendedDto = getIdmJwtDto(result);
    checkSuccessfulTokenExtension(authDto, extendedDto);
}
Also used : IdmJwtAuthenticationDto(eu.bcvsolutions.idm.core.security.api.dto.IdmJwtAuthenticationDto) MvcResult(org.springframework.test.web.servlet.MvcResult) Test(org.junit.Test) AbstractRestTest(eu.bcvsolutions.idm.test.api.AbstractRestTest)

Example 4 with IdmJwtAuthenticationDto

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

the class ExtendExpirationFilterTest method getIdmJwtDto.

private IdmJwtAuthenticationDto getIdmJwtDto(MvcResult result) throws IOException {
    String extended = result.getResponse().getHeader(JwtAuthenticationMapper.AUTHENTICATION_TOKEN_NAME);
    Assert.assertNotNull(extended);
    Jwt decoded = JwtHelper.decode(extended);
    decoded.verifySignature(jwtMapper.getVerifier());
    IdmJwtAuthenticationDto extendedDto = jwtMapper.getClaims(decoded);
    return extendedDto;
}
Also used : Jwt(org.springframework.security.jwt.Jwt) IdmJwtAuthenticationDto(eu.bcvsolutions.idm.core.security.api.dto.IdmJwtAuthenticationDto)

Example 5 with IdmJwtAuthenticationDto

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

the class JwtIdmAuthenticationFilterTest method testTokenModified.

@Test
public void testTokenModified() throws Exception {
    IdmJwtAuthenticationDto authDto = AuthenticationTestUtils.getAuthDto(identityService.getByUsername(TEST_ADMIN_USERNAME), Lists.newArrayList(IdmAuthorityUtils.getAdminAuthority()));
    String tokenOriginal = getAuthToken(authDto);
    // mix two different tokens - payload from second, signature from first
    authDto.setExpiration(DateTime.now().plus(10000000));
    String[] token2Split = getAuthToken(authDto).split("\\.");
    String[] tokenOrigSplit = tokenOriginal.split("\\.");
    String token = token2Split[0] + "." + token2Split[1] + "." + tokenOrigSplit[2];
    getMockMvc().perform(get(AuthenticationTestUtils.getSelfPath(TEST_ADMIN_USERNAME)).header(JwtAuthenticationMapper.AUTHENTICATION_TOKEN_NAME, token).contentType(HAL_CONTENT_TYPE)).andExpect(status().is(403));
}
Also used : IdmJwtAuthenticationDto(eu.bcvsolutions.idm.core.security.api.dto.IdmJwtAuthenticationDto) Test(org.junit.Test) AbstractRestTest(eu.bcvsolutions.idm.test.api.AbstractRestTest)

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