Skip to content

Commit 62ad8b9

Browse files
author
Samuli Kärkkäinen
committed
Add tests for array-valued audience verification
1 parent 98a3c55 commit 62ad8b9

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

src/test/java/com/auth0/jwt/JWTVerifierTest.java

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package com.auth0.jwt;
22

33
import com.fasterxml.jackson.databind.JsonNode;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import com.fasterxml.jackson.databind.node.ArrayNode;
46
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
57
import com.fasterxml.jackson.databind.node.ObjectNode;
8+
69
import org.apache.commons.codec.binary.Base64;
710
import org.junit.Test;
811

912
import java.security.SignatureException;
10-
import java.util.Collections;
11-
import java.util.Map;
1213

1314
import static org.junit.Assert.assertEquals;
1415

@@ -137,6 +138,26 @@ public void shouldVerifyAudienceWhenNotFoundInClaimsSet() throws Exception {
137138
.verifyAudience(JsonNodeFactory.instance.objectNode());
138139
}
139140

141+
@Test
142+
public void shouldVerifyNullAudience() throws Exception {
143+
new JWTVerifier("such secret")
144+
.verifyAudience(createSingletonJSONNode("aud", "wow"));
145+
}
146+
147+
@Test
148+
public void shouldVerifyArrayAudience() throws Exception {
149+
new JWTVerifier("such secret", "amaze audience")
150+
.verifyAudience(createSingletonJSONNode("aud",
151+
new ObjectMapper().readValue("[ \"foo\", \"amaze audience\" ]", ArrayNode.class)));
152+
}
153+
154+
@Test(expected = IllegalStateException.class)
155+
public void shouldFailArrayAudience() throws Exception {
156+
new JWTVerifier("such secret", "amaze audience")
157+
.verifyAudience(createSingletonJSONNode("aud",
158+
new ObjectMapper().readValue("[ \"foo\" ]", ArrayNode.class)));
159+
}
160+
140161
@Test
141162
public void decodeAndParse() throws Exception {
142163
final Base64 encoder = new Base64(true);
@@ -156,4 +177,10 @@ public static JsonNode createSingletonJSONNode(String key, String value) {
156177
jsonNodes.put(key, value);
157178
return jsonNodes;
158179
}
180+
181+
public static JsonNode createSingletonJSONNode(String key, JsonNode value) {
182+
final ObjectNode jsonNodes = JsonNodeFactory.instance.objectNode();
183+
jsonNodes.put(key, value);
184+
return jsonNodes;
185+
}
159186
}

0 commit comments

Comments
 (0)