forked from auth0/java-jwt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJWTVerifierTest.java
More file actions
184 lines (149 loc) · 7.1 KB
/
Copy pathJWTVerifierTest.java
File metadata and controls
184 lines (149 loc) · 7.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package com.auth0.jwt;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.commons.codec.binary.Base64;
import org.junit.Test;
import java.security.SignatureException;
import static org.junit.Assert.assertEquals;
public class JWTVerifierTest {
private static final Base64 decoder = new Base64(true);;
@Test(expected = IllegalArgumentException.class)
public void constructorShouldFailOnEmptySecret() {
new JWTVerifier("");
}
@Test(expected = IllegalStateException.class)
public void shouldFailOn1Segments() throws Exception {
new JWTVerifier("such secret").verify("crypto");
}
@Test(expected = IllegalStateException.class)
public void shouldFailOn2Segments() throws Exception {
new JWTVerifier("such secret").verify("much.crypto");
}
@Test(expected = IllegalStateException.class)
public void shouldFailOn4Segments() throws Exception {
new JWTVerifier("such secret").verify("much.crypto.so.token");
}
@Test(expected = IllegalStateException.class)
public void shouldFailOnEmptyStringToken() throws Exception {
new JWTVerifier("such secret").verify("");
}
@Test(expected = IllegalStateException.class)
public void shouldFailOnNullToken() throws Exception {
new JWTVerifier("such secret").verify(null);
}
@Test(expected = IllegalStateException.class)
public void shouldFailIfAlgorithmIsNotSetOnToken() throws Exception {
new JWTVerifier("such secret").getAlgorithm(JsonNodeFactory.instance.objectNode());
}
@Test(expected = IllegalStateException.class)
public void shouldFailIfAlgorithmIsNotSupported() throws Exception {
new JWTVerifier("such secret").getAlgorithm(createSingletonJSONNode("alg", "doge-crypt"));
}
@Test
public void shouldWorkIfAlgorithmIsSupported() throws Exception {
new JWTVerifier("such secret").getAlgorithm(createSingletonJSONNode("alg", "HS256"));
}
@Test(expected = SignatureException.class)
public void shouldFailOnInvalidSignature() throws Exception {
final String jws = "eyJ0eXAiOiJKV1QiLA0KICJhbGciOiJIUzI1NiJ9" +
"." +
"eyJpc3MiOiJqb2UiLA0KICJleHAiOjEzMDA4MTkzODAsDQogImh0dHA6Ly9leGFt" +
"cGxlLmNvbS9pc19yb290Ijp0cnVlfQ" +
"." +
"suchsignature_plzvalidate_zomgtokens";
String secret = "AyM1SysPpbyDfgZld3umj1qzKObwVMkoqQ-EstJQLr_T-1qS0gZH75aKtMN3Yj0iPS4hcgUuTwjAzZr1Z9CAow";
new JWTVerifier(secret, "audience").verifySignature(jws.split("\\."), "HmacSHA256");
}
@Test
public void shouldVerifySignature() throws Exception {
final String jws = "eyJ0eXAiOiJKV1QiLA0KICJhbGciOiJIUzI1NiJ9" +
"." +
"eyJpc3MiOiJqb2UiLA0KICJleHAiOjEzMDA4MTkzODAsDQogImh0dHA6Ly9leGFt" +
"cGxlLmNvbS9pc19yb290Ijp0cnVlfQ" +
"." +
"dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk";
byte[] secret = decoder.decodeBase64("AyM1SysPpbyDfgZld3umj1qzKObwVMkoqQ-EstJQLr_T-1qS0gZH75aKtMN3Yj0iPS4hcgUuTwjAzZr1Z9CAow");
new JWTVerifier(secret, "audience")
.verifySignature(jws.split("\\."), "HmacSHA256");
}
@Test(expected = JWTExpiredException.class)
public void shouldFailWhenExpired1SecondAgo() throws Exception {
new JWTVerifier("such secret").verifyExpiration(
createSingletonJSONNode("exp", Long.toString(System.currentTimeMillis() / 1000L - 1L)));
}
@Test
public void shouldVerifyExpiration() throws Exception {
new JWTVerifier("such secret").verifyExpiration(
createSingletonJSONNode("exp", Long.toString(System.currentTimeMillis() / 1000L + 50L)));
}
@Test
public void shouldVerifyIssuer() throws Exception {
new JWTVerifier("such secret", "amaze audience", "very issuer")
.verifyIssuer(createSingletonJSONNode("iss", "very issuer"));
}
@Test(expected = JWTIssuerException.class)
public void shouldFailIssuer() throws Exception {
new JWTVerifier("such secret", "amaze audience", "very issuer")
.verifyIssuer(createSingletonJSONNode("iss", "wow"));
}
@Test
public void shouldVerifyIssuerWhenNotFoundInClaimsSet() throws Exception {
new JWTVerifier("such secret", "amaze audience", "very issuer")
.verifyIssuer(JsonNodeFactory.instance.objectNode());
}
@Test
public void shouldVerifyAudience() throws Exception {
new JWTVerifier("such secret", "amaze audience")
.verifyAudience(createSingletonJSONNode("aud", "amaze audience"));
}
@Test(expected = JWTAudienceException.class)
public void shouldFailAudience() throws Exception {
new JWTVerifier("such secret", "amaze audience")
.verifyAudience(createSingletonJSONNode("aud", "wow"));
}
@Test
public void shouldVerifyAudienceWhenNotFoundInClaimsSet() throws Exception {
new JWTVerifier("such secret", "amaze audience")
.verifyAudience(JsonNodeFactory.instance.objectNode());
}
@Test
public void shouldVerifyNullAudience() throws Exception {
new JWTVerifier("such secret")
.verifyAudience(createSingletonJSONNode("aud", "wow"));
}
@Test
public void shouldVerifyArrayAudience() throws Exception {
new JWTVerifier("such secret", "amaze audience")
.verifyAudience(createSingletonJSONNode("aud",
new ObjectMapper().readValue("[ \"foo\", \"amaze audience\" ]", ArrayNode.class)));
}
@Test(expected = JWTAudienceException.class)
public void shouldFailArrayAudience() throws Exception {
new JWTVerifier("such secret", "amaze audience")
.verifyAudience(createSingletonJSONNode("aud",
new ObjectMapper().readValue("[ \"foo\" ]", ArrayNode.class)));
}
@Test
public void decodeAndParse() throws Exception {
final Base64 encoder = new Base64(true);
final String encodedJSON = new String(encoder.encode("{\"some\": \"json\", \"number\": 123}".getBytes()));
final JWTVerifier jwtVerifier = new JWTVerifier("secret", "audience");
final JsonNode decodedJSON = jwtVerifier.decodeAndParse(encodedJSON);
assertEquals("json", decodedJSON.get("some").asText());
assertEquals(null, decodedJSON.get("unexisting_property"));
assertEquals("123", decodedJSON.get("number").asText());
}
public static JsonNode createSingletonJSONNode(String key, String value) {
final ObjectNode jsonNodes = JsonNodeFactory.instance.objectNode();
jsonNodes.put(key, value);
return jsonNodes;
}
public static JsonNode createSingletonJSONNode(String key, JsonNode value) {
final ObjectNode jsonNodes = JsonNodeFactory.instance.objectNode();
jsonNodes.put(key, value);
return jsonNodes;
}
}