use of eu.bcvsolutions.idm.core.api.dto.IdmTokenDto in project CzechIdMng by bcvsolutions.
the class DefaultTokenManagerIntegrationTest method createToken.
protected IdmTokenDto createToken(IdmIdentityDto owner, String tokenType, ZonedDateTime expiration) {
IdmTokenDto token = new IdmTokenDto();
token.setIssuedAt(ZonedDateTime.now());
token.setToken("mock");
token.setExpiration(expiration);
if (tokenType != null) {
token.setTokenType(tokenType);
} else {
token.setTokenType("mock");
}
//
return manager.saveToken(owner, token);
}
use of eu.bcvsolutions.idm.core.api.dto.IdmTokenDto in project CzechIdMng by bcvsolutions.
the class DefaultTokenManagerIntegrationTest method testVerifyTokenDisabled.
@Test(expected = ResultCodeException.class)
public void testVerifyTokenDisabled() {
IdmIdentityDto owner = new IdmIdentityDto(UUID.randomUUID());
IdmTokenDto token = createToken(owner, null, ZonedDateTime.now().minusNanos(1));
token.setDisabled(true);
token = manager.saveToken(owner, token);
//
manager.verifyToken(token.getId());
}
use of eu.bcvsolutions.idm.core.api.dto.IdmTokenDto in project CzechIdMng by bcvsolutions.
the class DefaultLoginService method switchUser.
@Override
public LoginDto switchUser(IdmIdentityDto identity, BasePermission... permission) {
Assert.notNull(identity, "Target identity (to switch) is required.");
String loggedAction = siemLogger.buildAction(SiemLoggerManager.LOGIN_LEVEL_KEY, SiemLoggerManager.SWITCH_SUBLEVEL_KEY);
String targetUuid = Objects.toString(identity.getId(), "");
String subjectUsername = securityService.getCurrentUsername();
String subjectUuid = Objects.toString(securityService.getCurrentId(), "");
try {
identityService.checkAccess(identity, permission);
//
IdmTokenDto currentToken = tokenManager.getCurrentToken();
ConfigurationMap properties = currentToken.getProperties();
// Preserve the first original user => switch is available repetitively, but original user is preserved.
properties.putIfAbsent(JwtAuthenticationMapper.PROPERTY_ORIGINAL_USERNAME, securityService.getCurrentUsername());
properties.putIfAbsent(JwtAuthenticationMapper.PROPERTY_ORIGINAL_IDENTITY_ID, securityService.getCurrentId());
currentToken.setProperties(properties);
IdmTokenDto switchedToken = jwtTokenMapper.createToken(identity, currentToken);
//
// login by updated token
LOG.info("Identity with username [{}] - login as switched user [{}].", properties.get(JwtAuthenticationMapper.PROPERTY_ORIGINAL_USERNAME), identity.getUsername());
//
LoginDto login = login(identity, switchedToken);
siemLogger.log(loggedAction, SiemLoggerManager.SUCCESS_ACTION_STATUS, identity.getUsername(), targetUuid, subjectUsername, subjectUuid, null, null);
return login;
} catch (Exception e) {
siemLogger.log(loggedAction, SiemLoggerManager.FAILED_ACTION_STATUS, identity.getUsername(), targetUuid, subjectUsername, subjectUuid, null, e.getMessage());
throw e;
}
}
use of eu.bcvsolutions.idm.core.api.dto.IdmTokenDto in project CzechIdMng by bcvsolutions.
the class DefaultLoginService method switchUserLogout.
@Override
public LoginDto switchUserLogout() {
IdmTokenDto currentToken = tokenManager.getCurrentToken();
ConfigurationMap properties = currentToken.getProperties();
String originalUsername = properties.getString(JwtAuthenticationMapper.PROPERTY_ORIGINAL_USERNAME);
UUID originalId = properties.getUuid(JwtAuthenticationMapper.PROPERTY_ORIGINAL_IDENTITY_ID);
String loggedAction = siemLogger.buildAction(SiemLoggerManager.LOGIN_LEVEL_KEY, SiemLoggerManager.SWITCH_SUBLEVEL_KEY);
String subjectUsername = securityService.getCurrentUsername();
String subjectUuid = Objects.toString(securityService.getCurrentId(), "");
String targetUuid = Objects.toString(originalId, "");
try {
//
if (originalId == null) {
throw new ResultCodeException(CoreResultCode.NULL_ATTRIBUTE, ImmutableMap.of("attribute", "originalUsername"));
}
// change logged token authorities
IdmIdentityDto identity = identityService.get(originalId);
if (identity == null) {
throw new EntityNotFoundException(IdmIdentity.class, originalId);
}
//
// Preserve the first original user => switch is available repetitively, but original user is preserved.
properties.remove(JwtAuthenticationMapper.PROPERTY_ORIGINAL_USERNAME);
properties.remove(JwtAuthenticationMapper.PROPERTY_ORIGINAL_IDENTITY_ID);
currentToken.setProperties(properties);
IdmTokenDto switchedToken = jwtTokenMapper.createToken(identity, currentToken);
//
// login by updated token
LOG.info("Identity with username [{}] - logout from switched user [{}].", originalUsername, securityService.getCurrentUsername());
//
LoginDto login = login(identity, switchedToken);
siemLogger.log(loggedAction, SiemLoggerManager.SUCCESS_ACTION_STATUS, originalUsername, targetUuid, subjectUsername, subjectUuid, null, null);
return login;
} catch (Exception e) {
siemLogger.log(loggedAction, SiemLoggerManager.FAILED_ACTION_STATUS, originalUsername, targetUuid, subjectUsername, subjectUuid, null, e.getMessage());
throw e;
}
}
use of eu.bcvsolutions.idm.core.api.dto.IdmTokenDto in project CzechIdMng by bcvsolutions.
the class DefaultTwoFactorAuthenticationManager method requireTwoFactorAuthentication.
@Override
@Transactional
public boolean requireTwoFactorAuthentication(UUID identityId, UUID tokenId) {
// check two factor authentication is enabled
TwoFactorAuthenticationType twoFactorAuthenticationType = getTwoFactorAuthenticationType(identityId);
if (twoFactorAuthenticationType == null) {
return false;
}
//
IdmTokenDto token = tokenManager.getToken(tokenId);
if (token.isSecretVerified()) {
// token was already verified
return false;
}
//
if (TwoFactorAuthenticationType.NOTIFICATION == twoFactorAuthenticationType) {
IdmPasswordDto password = passwordService.findOneByIdentity(identityId);
if (password == null) {
throw new EntityNotFoundException(IdmIdentityDto.class, identityId);
}
sendVerificationCode(identityService.get(identityId), generateCode(new GuardedString(password.getVerificationSecret())));
}
//
return true;
}
Aggregations