import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
+import io.jsonwebtoken.UnsupportedJwtException;
import io.jsonwebtoken.security.SignatureException;
import java.util.Base64;
import java.util.Collections;
checkIsStarted();
Claims claims = null;
try {
- claims = (Claims) Jwts.parserBuilder()
+ claims = Jwts.parserBuilder()
.setSigningKey(secretKey)
.build()
- .parse(token)
+ .parseClaimsJws(token)
.getBody();
requireNonNull(claims.getId(), "Token id hasn't been found");
requireNonNull(claims.getSubject(), "Token subject hasn't been found");
requireNonNull(claims.getExpiration(), "Token expiration date hasn't been found");
requireNonNull(claims.getIssuedAt(), "Token creation date hasn't been found");
return Optional.of(claims);
- } catch (ExpiredJwtException | SignatureException e) {
+ } catch (UnsupportedJwtException | ExpiredJwtException | SignatureException e) {
return Optional.empty();
} catch (Exception e) {
throw AuthenticationException.newBuilder()
assertThat(underTest.decode(token)).isEmpty();
}
+ @Test
+ public void return_no_token_if_none_algorithm() {
+ setSecretKey(A_SECRET_KEY);
+ underTest.start();
+
+ String token = Jwts.builder()
+ .setId("123")
+ .setSubject(USER_LOGIN)
+ .setIssuedAt(new Date(system2.now()))
+ .setExpiration(addMinutes(new Date(), 20))
+ .compact();
+
+ assertThat(underTest.decode(token)).isEmpty();
+ }
+
@Test
public void fail_to_decode_token_when_no_id() {
setSecretKey(A_SECRET_KEY);