Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.fasterxml.jackson.databind.node.BooleanNode;
import com.fasterxml.jackson.databind.node.DoubleNode;
import com.fasterxml.jackson.databind.node.IntNode;
import com.fasterxml.jackson.databind.node.LongNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.TextNode;

Expand All @@ -31,13 +32,17 @@
import io.vavr.collection.HashMap;
import io.vavr.collection.Map;
import io.vavr.collection.Stream;

import lombok.extern.slf4j.Slf4j;
import lombok.val;

@Slf4j
class ClaimExtractor {

private final Map<Class<?>, Function1<Object, Object>> accessors = HashMap.ofEntries( //
Tuple.of(TextNode.class, (node) -> ((TextNode) node).asText()), //
Tuple.of(IntNode.class, (node) -> ((IntNode) node).asInt()), //
Tuple.of(LongNode.class, (node) -> ((LongNode) node).asLong()), //
Tuple.of(DoubleNode.class, (node) -> ((DoubleNode) node).asDouble()), //
Tuple.of(BooleanNode.class, (node) -> ((BooleanNode) node).asBoolean()), //
Tuple.of(ArrayNode.class, (node) -> extractArray((ArrayNode) node)), //
Expand All @@ -61,6 +66,9 @@ Object extract(Claim claim) {

private Object extractNode(Object rawClaim) {
val accessorOption = accessors.get(rawClaim.getClass());
if (accessorOption.isEmpty()) {
log.warn("Could not find accessor for type {}", rawClaim.getClass());
}
return accessorOption.map(accessor -> accessor.apply(rawClaim)).getOrNull();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@
import io.vavr.Tuple2;
import io.vavr.collection.List;
import io.vavr.collection.Set;

import lombok.extern.slf4j.Slf4j;
import lombok.val;

@Slf4j
class HierarchicalClaimsExtractor {

private final TokenProcessor tokenProcessor;
Expand Down Expand Up @@ -80,6 +83,7 @@ private List<JWTClaim> extractClaims(DecodedJWT token, boolean verified) {
.toStream()
.map(claimName -> Tuple.of(claimName, token.getClaim(claimName)))
.filter(this::containsClaim)
.filter(this::claimExtractable)
.map(result -> JWTClaim
.builder()
.name(result._1())
Expand All @@ -95,6 +99,14 @@ private boolean containsClaim(Tuple2<String, Claim> result) {
return !result._2.isNull();
}

private boolean claimExtractable(Tuple2<String, Claim> tuple) {
final Object extract = claimExtractor.extract(tuple._2());
if (extract == null) {
log.warn("The claim extracts to null {}", tuple);
}
return extract != null;
}

private boolean verifyToken(DecodedJWT token) {
val verified = verifier.verifyToken(token);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public void setUp() throws Exception {
.withClaim("double", 3.1415)
.withClaim("bool", true)
.withClaim("string", "text")
.withClaim("long", Long.MAX_VALUE)
.withArrayClaim("stringArray", new String[] { "foo", "bar", "baz" });

final Method addClaim = JWTCreator.Builder.class.getDeclaredMethod("addClaim", String.class, Object.class);
Expand Down Expand Up @@ -75,6 +76,13 @@ public void extractsInteger() {
assertThat(result).isEqualTo(123);
}

@Test
public void extractsLong() {
val result = uut.extract(jwt.getClaim("long"));

assertThat(result).isEqualTo(Long.MAX_VALUE);
}

@Test
public void extractsDouble() {
val result = uut.extract(jwt.getClaim("double"));
Expand Down