use of eu.bcvsolutions.idm.core.api.dto.IdmTokenDto in project CzechIdMng by bcvsolutions.
the class IdmTokenController method post.
/**
*/
@Override
@ResponseBody
@RequestMapping(method = RequestMethod.POST)
@PreAuthorize("hasAuthority('" + CoreGroupPermission.TOKEN_CREATE + "')")
@ApiOperation(value = "Geerate new token", nickname = "generateToken", response = IdmTokenDto.class, tags = { IdmTokenController.TAG }, authorizations = { @Authorization(value = SwaggerConfig.AUTHENTICATION_BASIC, scopes = { @AuthorizationScope(scope = CoreGroupPermission.TOKEN_CREATE, description = "") }), @Authorization(value = SwaggerConfig.AUTHENTICATION_CIDMST, scopes = { @AuthorizationScope(scope = CoreGroupPermission.TOKEN_CREATE, description = "") }) })
public ResponseEntity<?> post(@RequestBody IdmTokenDto dto) {
// generate token
BaseDto owner = getLookupService().lookupDto(dto.getOwnerType(), dto.getOwnerId());
Assert.notNull(owner, "Owner is required to generate new token.");
Assert.isInstanceOf(IdmIdentityDto.class, owner, "Identity owner is required to generate new token.");
IdmIdentityDto identity = (IdmIdentityDto) owner;
Assert.isTrue(!identity.isDisabled(), MessageFormat.format("Check identity can login: The identity [{0}] is disabled.", identity.getUsername()));
//
// set static properties
dto.setModuleId(CoreModule.MODULE_ID);
dto.getProperties().put(JwtAuthenticationMapper.PROPERTY_PRESERVE_EXPIRATION, Boolean.TRUE);
//
IdmTokenDto token = jwtTokenMapper.createToken(identity, dto);
IdmJwtAuthenticationDto authenticationDto = jwtTokenMapper.toDto(token);
//
// usable token in response after create - only once after create
// we need to create copy to prevent changes cached token by reference
IdmTokenDto clone = new IdmTokenDto(token);
clone.setProperties(new ConfigurationMap(token.getProperties()));
clone.setDisabled(token.isDisabled());
clone.setSecretVerified(token.isSecretVerified());
clone.setExpiration(token.getExpiration());
clone.setIssuedAt(token.getIssuedAt());
clone.setTokenType(token.getTokenType());
clone.setOwnerId(token.getOwnerId());
clone.setOwnerType(token.getOwnerType());
clone.setExternalId(token.getExternalId());
clone.setToken(token.getToken());
clone.setModuleId(token.getModuleId());
clone.getProperties().put(IdmAuthenticationFilter.AUTHENTICATION_TOKEN_NAME, jwtTokenMapper.writeToken(authenticationDto));
//
return new ResponseEntity<>(toResource(clone), HttpStatus.CREATED);
}
use of eu.bcvsolutions.idm.core.api.dto.IdmTokenDto in project CzechIdMng by bcvsolutions.
the class IdmTokenController method find.
@Override
public Page<IdmTokenDto> find(IdmTokenFilter filter, Pageable pageable, BasePermission permission) {
Page<IdmTokenDto> results = super.find(filter, pageable, permission);
// fill entity embedded for FE
Map<UUID, BaseDto> loadedDtos = new HashMap<>();
results.getContent().forEach(dto -> {
UUID ownerId = dto.getOwnerId();
if (!loadedDtos.containsKey(ownerId)) {
try {
loadedDtos.put(ownerId, getLookupService().lookupDto(dto.getOwnerType(), ownerId));
} catch (IllegalArgumentException ex) {
LOG.debug("Class [{}] not found on classpath (e.g. module was uninstalled)", dto.getOwnerType(), ex);
}
}
dto.getEmbedded().put("ownerId", loadedDtos.get(ownerId));
});
return results;
}
use of eu.bcvsolutions.idm.core.api.dto.IdmTokenDto in project CzechIdMng by bcvsolutions.
the class LoginControllerRestTest method testUseDeletedToken.
@Test
public void testUseDeletedToken() throws Exception {
IdmIdentityDto manager = getHelper().createIdentity();
getHelper().createIdentityRole(manager, roleConfiguration.getAdminRole());
//
// login as manager
Map<String, String> login = new HashMap<>();
login.put("username", manager.getUsername());
login.put("password", manager.getPassword().asString());
String response = getMockMvc().perform(post(BaseController.BASE_PATH + LoginController.AUTH_PATH).content(serialize(login)).contentType(TestHelper.HAL_CONTENT_TYPE)).andExpect(status().isOk()).andExpect(content().contentType(TestHelper.HAL_CONTENT_TYPE)).andReturn().getResponse().getContentAsString();
UUID tokenId = getTokenId(response);
String token = getToken(response);
//
Assert.assertNotNull(tokenId);
IdmTokenDto tokenDto = tokenManager.getToken(tokenId);
Assert.assertFalse(tokenDto.isDisabled());
//
// delete token
tokenManager.deleteToken(tokenDto.getId());
//
// test call api
getMockMvc().perform(put(BaseController.BASE_PATH + "/identities").param(IdmAuthenticationFilter.AUTHENTICATION_TOKEN_NAME, token).contentType(TestHelper.HAL_CONTENT_TYPE)).andExpect(status().is4xxClientError());
}
use of eu.bcvsolutions.idm.core.api.dto.IdmTokenDto in project CzechIdMng by bcvsolutions.
the class LoginControllerRestTest method testLogoutWithParameter.
@Test
public void testLogoutWithParameter() throws Exception {
IdmIdentityDto identity = getHelper().createIdentity();
Map<String, String> login = new HashMap<>();
login.put("username", identity.getUsername());
login.put("password", identity.getPassword().asString());
String response = getMockMvc().perform(post(BaseController.BASE_PATH + LoginController.AUTH_PATH).content(serialize(login)).contentType(TestHelper.HAL_CONTENT_TYPE)).andExpect(status().isOk()).andExpect(content().contentType(TestHelper.HAL_CONTENT_TYPE)).andReturn().getResponse().getContentAsString();
UUID tokenId = getTokenId(response);
String token = getToken(response);
//
Assert.assertNotNull(tokenId);
//
IdmTokenDto tokenDto = tokenManager.getToken(tokenId);
Assert.assertFalse(tokenDto.isDisabled());
//
getMockMvc().perform(delete(BaseController.BASE_PATH + "/logout").param(IdmAuthenticationFilter.AUTHENTICATION_TOKEN_NAME, token).contentType(TestHelper.HAL_CONTENT_TYPE)).andExpect(status().isNoContent());
//
tokenDto = tokenManager.getToken(tokenId);
Assert.assertTrue(tokenDto.isDisabled());
}
use of eu.bcvsolutions.idm.core.api.dto.IdmTokenDto in project CzechIdMng by bcvsolutions.
the class LoginControllerRestTest method testSwitchUser.
@Test
public void testSwitchUser() throws Exception {
IdmIdentityDto manager = getHelper().createIdentity();
getHelper().createIdentityRole(manager, roleConfiguration.getAdminRole());
//
// login as manager
Map<String, String> login = new HashMap<>();
login.put("username", manager.getUsername());
login.put("password", manager.getPassword().asString());
String response = getMockMvc().perform(post(BaseController.BASE_PATH + LoginController.AUTH_PATH).content(serialize(login)).contentType(TestHelper.HAL_CONTENT_TYPE)).andExpect(status().isOk()).andExpect(content().contentType(TestHelper.HAL_CONTENT_TYPE)).andReturn().getResponse().getContentAsString();
UUID tokenId = getTokenId(response);
String token = getToken(response);
//
Assert.assertNotNull(tokenId);
IdmTokenDto tokenDto = tokenManager.getToken(tokenId);
Assert.assertFalse(tokenDto.isDisabled());
List<DefaultGrantedAuthorityDto> dtoAuthorities = jwtTokenMapper.getDtoAuthorities(tokenDto);
//
// check token authorities - APP_ADMIN
Assert.assertTrue(dtoAuthorities.stream().anyMatch(a -> a.getAuthority().equals(IdmGroupPermission.APP_ADMIN)));
//
// create different identity - identity create
IdmIdentityDto identity = getHelper().createIdentity((GuardedString) null);
IdmRoleDto role = getHelper().createRole();
getHelper().createIdentityRole(identity, role);
getHelper().createBasePolicy(role.getId(), CoreGroupPermission.IDENTITY, IdmIdentity.class, IdmBasePermission.ADMIN);
response = getMockMvc().perform(put(BaseController.BASE_PATH + "/authentication/switch-user?username=" + identity.getUsername()).param(IdmAuthenticationFilter.AUTHENTICATION_TOKEN_NAME, token)).andExpect(status().isOk()).andExpect(content().contentType(TestHelper.HAL_CONTENT_TYPE)).andReturn().getResponse().getContentAsString();
//
// preserve token id
UUID switchTokenId = getTokenId(response);
token = getToken(response);
Assert.assertEquals(tokenId, switchTokenId);
IdmTokenDto switchTokenDto = tokenManager.getToken(switchTokenId);
Assert.assertFalse(switchTokenDto.isDisabled());
dtoAuthorities = jwtTokenMapper.getDtoAuthorities(switchTokenDto);
//
// check authorities - no APP_ADMIN
Assert.assertTrue(dtoAuthorities.stream().allMatch(a -> !a.getAuthority().equals(IdmGroupPermission.APP_ADMIN)));
//
// check token => same owner, same id, different username in properties
Assert.assertEquals(tokenDto.getOwnerId(), switchTokenDto.getOwnerId());
Assert.assertEquals(identity.getUsername(), switchTokenDto.getProperties().getString(JwtAuthenticationMapper.PROPERTY_CURRENT_USERNAME));
Assert.assertEquals(manager.getUsername(), switchTokenDto.getProperties().getString(JwtAuthenticationMapper.PROPERTY_ORIGINAL_USERNAME));
//
// test create identity with switched token + check audit fields
IdmIdentityDto createIdentity = new IdmIdentityDto(getHelper().createName());
getMockMvc().perform(post(BaseController.BASE_PATH + "/identities").param(IdmAuthenticationFilter.AUTHENTICATION_TOKEN_NAME, token).content(getMapper().writeValueAsString(createIdentity)).contentType(TestHelper.HAL_CONTENT_TYPE)).andExpect(status().isCreated()).andExpect(content().contentType(TestHelper.HAL_CONTENT_TYPE)).andReturn().getResponse().getContentAsString();
IdmIdentityDto createdIdentity = identityService.getByUsername(createIdentity.getUsername());
Assert.assertEquals(manager.getUsername(), createdIdentity.getOriginalCreator());
Assert.assertEquals(manager.getId(), createdIdentity.getOriginalCreatorId());
Assert.assertEquals(identity.getUsername(), createdIdentity.getCreator());
Assert.assertEquals(identity.getId(), createdIdentity.getCreatorId());
//
// rename identity - use id in logout phase
manager.setUsername(getHelper().createName());
manager = identityService.save(manager);
//
// switch logout => test token, authorities
response = getMockMvc().perform(delete(BaseController.BASE_PATH + "/authentication/switch-user").param(IdmAuthenticationFilter.AUTHENTICATION_TOKEN_NAME, token)).andExpect(status().isOk()).andExpect(content().contentType(TestHelper.HAL_CONTENT_TYPE)).andReturn().getResponse().getContentAsString();
tokenId = getTokenId(response);
token = getToken(response);
//
Assert.assertNotNull(tokenId);
tokenDto = tokenManager.getToken(tokenId);
Assert.assertFalse(tokenDto.isDisabled());
dtoAuthorities = jwtTokenMapper.getDtoAuthorities(tokenDto);
//
// check token authorities - APP_ADMIN
Assert.assertTrue(dtoAuthorities.stream().anyMatch(a -> a.getAuthority().equals(IdmGroupPermission.APP_ADMIN)));
Assert.assertEquals(tokenDto.getOwnerId(), switchTokenDto.getOwnerId());
Assert.assertEquals(manager.getUsername(), tokenDto.getProperties().getString(JwtAuthenticationMapper.PROPERTY_CURRENT_USERNAME));
Assert.assertEquals(manager.getUsername(), tokenDto.getProperties().getString(JwtAuthenticationMapper.PROPERTY_ORIGINAL_USERNAME));
}
Aggregations