Search in sources :

Example 46 with IdmTokenDto

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

the class JwtAuthenticationMapper method createToken.

/**
 * Create token with assigned identity authorities
 *
 * @param identity
 * @param preparedToken
 * @return preparedToken with filled required
 */
public IdmTokenDto createToken(IdmIdentityDto identity, IdmTokenDto preparedToken) {
    Assert.notNull(identity, "Identity is required.");
    UUID identityId = identity.getId();
    Assert.notNull(identityId, "Identity identifier is required.");
    // 
    // persist token
    IdmTokenDto token = new IdmTokenDto();
    if (preparedToken != null) {
        // fill optional token properties
        token.setId(preparedToken.getId());
        token.setModuleId(preparedToken.getModuleId());
        token.setExternalId(preparedToken.getExternalId());
        token.getProperties().putAll(preparedToken.getProperties());
        token.setDisabled(preparedToken.isDisabled());
        token.setIssuedAt(preparedToken.getIssuedAt());
        token.setOwnerId(preparedToken.getOwnerId());
        token.setOwnerType(preparedToken.getOwnerType());
        token.setTokenType(preparedToken.getTokenType());
        token.setSecretVerified(preparedToken.isSecretVerified());
    }
    // required properties
    if (token.getTokenType() == null) {
        token.setTokenType(AUTHENTICATION_TOKEN_NAME);
    }
    if (token.getOwnerId() == null) {
        token.setOwnerId(identityId);
    }
    if (token.getOwnerType() == null) {
        token.setOwnerType(tokenManager.getOwnerType(identity));
    }
    if (token.getIssuedAt() == null) {
        token.setIssuedAt(ZonedDateTime.now());
    }
    // 
    ConfigurationMap properties = token.getProperties();
    properties.put(PROPERTY_AUTHORITIES, getDtoAuthorities(grantedAuthoritiesFactory.getGrantedAuthoritiesForIdentity(identity.getId())));
    properties.put(PROPERTY_CURRENT_USERNAME, identity.getUsername());
    properties.put(PROPERTY_CURRENT_IDENTITY_ID, identityId);
    // original user can be set in prepared token
    properties.putIfAbsent(PROPERTY_ORIGINAL_USERNAME, identity.getUsername());
    // original user can be set in prepared token
    properties.putIfAbsent(PROPERTY_ORIGINAL_IDENTITY_ID, identityId);
    // 
    if (token.getId() == null) {
        // token id has to be written into token
        token.setId(UUID.randomUUID());
    }
    // resolve expiration
    if (properties.getBoolean(PROPERTY_PRESERVE_EXPIRATION)) {
        token.setExpiration(preparedToken.getExpiration());
        // 
        LOG.debug("Expiration for token [{}] is preserved, expiration is set to [{}].", token.getId(), token.getExpiration());
    } else {
        token.setExpiration(getNewExpiration());
    }
    // 
    token.setToken(getTokenHash(token));
    token = tokenManager.saveToken(identity, token);
    // 
    return token;
}
Also used : IdmTokenDto(eu.bcvsolutions.idm.core.api.dto.IdmTokenDto) ConfigurationMap(eu.bcvsolutions.idm.core.api.domain.ConfigurationMap) UUID(java.util.UUID)

Example 47 with IdmTokenDto

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

the class JwtAuthenticationMapper method fromDto.

/**
 * Converts dto to authentication.
 *
 * @param token
 * @return
 */
public IdmJwtAuthentication fromDto(IdmTokenDto token) {
    Assert.notNull(token, "Token is required.");
    // 
    List<GrantedAuthority> grantedAuthorities = getDtoAuthorities(token).stream().map(authority -> new DefaultGrantedAuthority(authority.getAuthority())).collect(Collectors.toList());
    // 
    IdmJwtAuthentication authentication = new IdmJwtAuthentication(new IdmIdentityDto(token.getProperties().getUuid(PROPERTY_CURRENT_IDENTITY_ID), token.getProperties().getString(PROPERTY_CURRENT_USERNAME)), new IdmIdentityDto(token.getProperties().getUuid(PROPERTY_ORIGINAL_IDENTITY_ID), token.getProperties().getString(PROPERTY_ORIGINAL_USERNAME)), token.getExpiration(), token.getIssuedAt(), grantedAuthorities, token.getModuleId());
    authentication.setId(token.getId());
    // 
    return authentication;
}
Also used : ChronoField(java.time.temporal.ChronoField) IdmTokenDto(eu.bcvsolutions.idm.core.api.dto.IdmTokenDto) ZonedDateTime(java.time.ZonedDateTime) Autowired(org.springframework.beans.factory.annotation.Autowired) Hashing(com.google.common.hash.Hashing) ConfigurationService(eu.bcvsolutions.idm.core.api.service.ConfigurationService) IdmJwtAuthentication(eu.bcvsolutions.idm.core.security.api.domain.IdmJwtAuthentication) MacSigner(org.springframework.security.jwt.crypto.sign.MacSigner) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) JwtHelper(org.springframework.security.jwt.JwtHelper) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) LoginService(eu.bcvsolutions.idm.core.security.api.service.LoginService) GrantedAuthoritiesFactory(eu.bcvsolutions.idm.core.security.api.service.GrantedAuthoritiesFactory) CoreException(eu.bcvsolutions.idm.core.api.exception.CoreException) DefaultGrantedAuthorityDto(eu.bcvsolutions.idm.core.security.api.dto.DefaultGrantedAuthorityDto) Collection(java.util.Collection) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DefaultGrantedAuthority(eu.bcvsolutions.idm.core.security.api.domain.DefaultGrantedAuthority) IOException(java.io.IOException) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) IdmAuthenticationFilter(eu.bcvsolutions.idm.core.security.api.filter.IdmAuthenticationFilter) StandardCharsets(java.nio.charset.StandardCharsets) GrantedAuthority(org.springframework.security.core.GrantedAuthority) ConfigurationMap(eu.bcvsolutions.idm.core.api.domain.ConfigurationMap) TokenManager(eu.bcvsolutions.idm.core.security.api.service.TokenManager) Jwt(org.springframework.security.jwt.Jwt) List(java.util.List) Component(org.springframework.stereotype.Component) ChronoUnit(java.time.temporal.ChronoUnit) CoreResultCode(eu.bcvsolutions.idm.core.api.domain.CoreResultCode) SignerVerifier(org.springframework.security.jwt.crypto.sign.SignerVerifier) IdmJwtAuthenticationDto(eu.bcvsolutions.idm.core.security.api.dto.IdmJwtAuthenticationDto) Lazy(org.springframework.context.annotation.Lazy) GuardedString(eu.bcvsolutions.idm.core.security.api.domain.GuardedString) Authentication(org.springframework.security.core.Authentication) Assert(org.springframework.util.Assert) DefaultGrantedAuthority(eu.bcvsolutions.idm.core.security.api.domain.DefaultGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) IdmJwtAuthentication(eu.bcvsolutions.idm.core.security.api.domain.IdmJwtAuthentication) DefaultGrantedAuthority(eu.bcvsolutions.idm.core.security.api.domain.DefaultGrantedAuthority) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto)

Example 48 with IdmTokenDto

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

the class DefaultJwtAuthenticationService method createJwtAuthenticationAndAuthenticate.

@Override
public LoginDto createJwtAuthenticationAndAuthenticate(LoginDto loginDto, IdmIdentityDto identity, String moduleId) {
    IdmTokenDto preparedToken = new IdmTokenDto();
    preparedToken.setModuleId(moduleId);
    return createJwtAuthenticationAndAuthenticate(loginDto, identity, preparedToken);
}
Also used : IdmTokenDto(eu.bcvsolutions.idm.core.api.dto.IdmTokenDto)

Example 49 with IdmTokenDto

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

the class DefaultJwtAuthenticationService method createJwtAuthenticationAndAuthenticate.

@Override
public LoginDto createJwtAuthenticationAndAuthenticate(LoginDto loginDto, IdmIdentityDto identity, IdmTokenDto preparedToken) {
    Assert.notNull(identity, "Identity is required.");
    UUID identityId = identity.getId();
    Assert.notNull(identityId, "Identity identifier is required.");
    // check identity is valid
    if (identity.isDisabled()) {
        throw new IdentityDisabledException(MessageFormat.format("Check identity can login: The identity [{0}] is disabled.", identity.getUsername()));
    }
    // two factor authentication is not configured for given identity
    if (tokenManager.isNew(preparedToken)) {
        if (// public password change page => login is needed, before password is changed
        loginDto.isSkipMustChange() || twoFactorAuthenticationManager.getTwoFactorAuthenticationType(identityId) == null) {
            preparedToken.setSecretVerified(true);
        } else {
            // two factor needed
            preparedToken.setSecretVerified(false);
        }
    }
    // create token
    IdmTokenDto token = jwtTokenMapper.createToken(identity, preparedToken);
    // check two factor authentication is required
    if (twoFactorAuthenticationManager.requireTwoFactorAuthentication(identityId, token.getId())) {
        IdmJwtAuthenticationDto authenticationDto = jwtTokenMapper.toDto(token);
        // token is needed in exception => sso login can be used and client doesn't have token
        throw new TwoFactorAuthenticationRequiredException(jwtTokenMapper.writeToken(authenticationDto));
    }
    // 
    return login(loginDto, token);
}
Also used : IdentityDisabledException(eu.bcvsolutions.idm.core.security.api.exception.IdentityDisabledException) IdmTokenDto(eu.bcvsolutions.idm.core.api.dto.IdmTokenDto) IdmJwtAuthenticationDto(eu.bcvsolutions.idm.core.security.api.dto.IdmJwtAuthenticationDto) UUID(java.util.UUID) TwoFactorAuthenticationRequiredException(eu.bcvsolutions.idm.core.security.api.exception.TwoFactorAuthenticationRequiredException)

Example 50 with IdmTokenDto

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

the class DefaultTokenManager method disableToken.

@Override
@Transactional
public IdmTokenDto disableToken(UUID tokenId, BasePermission... permission) {
    Assert.notNull(tokenId, "Token identifier is required.");
    // 
    IdmTokenDto token = getToken(tokenId);
    if (token == null) {
        LOG.trace("Persisted token with id [{}] not found, disable token will be skipped.", tokenId);
        return null;
    }
    return disableToken(token, permission);
}
Also used : IdmTokenDto(eu.bcvsolutions.idm.core.api.dto.IdmTokenDto) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

IdmTokenDto (eu.bcvsolutions.idm.core.api.dto.IdmTokenDto)58 IdmIdentityDto (eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto)38 Test (org.junit.Test)34 GuardedString (eu.bcvsolutions.idm.core.security.api.domain.GuardedString)16 UUID (java.util.UUID)16 AbstractIntegrationTest (eu.bcvsolutions.idm.test.api.AbstractIntegrationTest)15 ResultCodeException (eu.bcvsolutions.idm.core.api.exception.ResultCodeException)9 IdmRoleDto (eu.bcvsolutions.idm.core.api.dto.IdmRoleDto)8 IdmJwtAuthentication (eu.bcvsolutions.idm.core.security.api.domain.IdmJwtAuthentication)8 IdmIdentityContractDto (eu.bcvsolutions.idm.core.api.dto.IdmIdentityContractDto)7 IdmJwtAuthenticationDto (eu.bcvsolutions.idm.core.security.api.dto.IdmJwtAuthenticationDto)7 LoginDto (eu.bcvsolutions.idm.core.security.api.dto.LoginDto)7 AbstractRestTest (eu.bcvsolutions.idm.test.api.AbstractRestTest)7 ConfigurationMap (eu.bcvsolutions.idm.core.api.domain.ConfigurationMap)6 Transactional (org.springframework.transaction.annotation.Transactional)6 ZonedDateTime (java.time.ZonedDateTime)5 IdmTokenFilter (eu.bcvsolutions.idm.core.api.dto.filter.IdmTokenFilter)4 EntityNotFoundException (eu.bcvsolutions.idm.core.api.exception.EntityNotFoundException)4 AbstractReadWriteDtoControllerRestTest (eu.bcvsolutions.idm.core.api.rest.AbstractReadWriteDtoControllerRestTest)4 TwoFactorRegistrationResponseDto (eu.bcvsolutions.idm.core.security.api.dto.TwoFactorRegistrationResponseDto)4