Search in sources :

Example 1 with SignerVerifier

use of org.springframework.security.jwt.crypto.sign.SignerVerifier in project CzechIdMng by bcvsolutions.

the class JwtAuthenticationMapper method readToken.

/**
 * Reads {@link IdmJwtAuthentication} from given token
 *
 * @param token
 * @return
 * @throws IOException
 */
public IdmJwtAuthentication readToken(String token) throws IOException {
    if (StringUtils.isEmpty(token)) {
        return null;
    }
    // 
    SignerVerifier verifier = getVerifier();
    String decoded = JwtHelper.decodeAndVerify(token, verifier).getClaims();
    return fromDto(mapper.readValue(decoded, IdmJwtAuthenticationDto.class));
}
Also used : SignerVerifier(org.springframework.security.jwt.crypto.sign.SignerVerifier) IdmJwtAuthenticationDto(eu.bcvsolutions.idm.core.security.api.dto.IdmJwtAuthenticationDto) GuardedString(eu.bcvsolutions.idm.core.security.api.domain.GuardedString)

Example 2 with SignerVerifier

use of org.springframework.security.jwt.crypto.sign.SignerVerifier in project theskeleton by codenergic.

the class TokenStoreServiceImpl method findAndVerifyToken.

@Override
public TokenStoreRestData findAndVerifyToken(String token) {
    try {
        Jwt jwt = JwtHelper.decode(token);
        TokenStoreRestData data = objectMapper.readValue(jwt.getClaims(), TokenStoreRestData.class);
        UserEntity user = userRepository.findById(data.getUserId()).orElseThrow(() -> new UsernameNotFoundException(data.getUserId()));
        SignerVerifier verifier = createSignerVerifier(user);
        jwt.verifySignature(verifier);
        return ImmutableTokenStoreRestData.builder().from(data).signedToken(jwt.getEncoded()).user(user).build();
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) SignerVerifier(org.springframework.security.jwt.crypto.sign.SignerVerifier) Jwt(org.springframework.security.jwt.Jwt) IOException(java.io.IOException) UserEntity(org.codenergic.theskeleton.user.UserEntity)

Example 3 with SignerVerifier

use of org.springframework.security.jwt.crypto.sign.SignerVerifier in project theskeleton by codenergic.

the class TokenStoreServiceImpl method sendTokenNotification.

@Override
public TokenStoreRestData sendTokenNotification(TokenStoreType type, UserEntity user) {
    try {
        TokenStoreRestData data = ImmutableTokenStoreRestData.builder().userId(user.getId()).expiryDate(Date.from(Instant.now().plus(Period.ofDays(1)))).tokenType(type).uuid(UUID.randomUUID()).build();
        SignerVerifier signer = createSignerVerifier(user);
        Jwt token = JwtHelper.encode(objectMapper.writeValueAsString(data), signer);
        sendEmail(token.getEncoded(), type, user.getEmail());
        return ImmutableTokenStoreRestData.builder().from(data).signedToken(token.getEncoded()).user(user).build();
    } catch (JsonProcessingException e) {
        throw new IllegalStateException(e);
    }
}
Also used : SignerVerifier(org.springframework.security.jwt.crypto.sign.SignerVerifier) Jwt(org.springframework.security.jwt.Jwt) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Aggregations

SignerVerifier (org.springframework.security.jwt.crypto.sign.SignerVerifier)3 Jwt (org.springframework.security.jwt.Jwt)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 GuardedString (eu.bcvsolutions.idm.core.security.api.domain.GuardedString)1 IdmJwtAuthenticationDto (eu.bcvsolutions.idm.core.security.api.dto.IdmJwtAuthenticationDto)1 IOException (java.io.IOException)1 UserEntity (org.codenergic.theskeleton.user.UserEntity)1 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)1