Skip to content

Commit 6732006

Browse files
committed
Refactor tests & Update to JUnit 4.12
1 parent 9c8132d commit 6732006

File tree

4 files changed

+89
-50
lines changed

4 files changed

+89
-50
lines changed

.travis.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
language: java
2+
jdk:
3+
- oraclejdk7
4+
branches:
5+
only:
6+
- master

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
<dependency>
7474
<groupId>junit</groupId>
7575
<artifactId>junit</artifactId>
76-
<version>4.11</version>
76+
<version>4.12</version>
7777
<scope>test</scope>
7878
</dependency>
7979

src/main/java/com/auth0/jwt/JWTVerifier.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,9 @@ public JWTVerifier(final PublicKey publicKey) {
9494
* @throws JWTAlgorithmException when the algorithm is missing or unsupported
9595
* @throws IllegalStateException when token's structure is invalid or secret / public key does not match algorithm of token
9696
*/
97+
@SuppressWarnings("WeakerAccess")
9798
public Map<String, Object> verify(final String token) throws NoSuchAlgorithmException, InvalidKeyException, IllegalStateException,
98-
IOException, SignatureException, JWTVerifyException, JWTAlgorithmException {
99+
IOException, SignatureException, JWTVerifyException {
99100
if (token == null || "".equals(token)) {
100101
throw new IllegalStateException("token not set");
101102
}
@@ -113,7 +114,7 @@ public Map<String, Object> verify(final String token) throws NoSuchAlgorithmExce
113114
return mapper.treeToValue(jwtPayload, Map.class);
114115
}
115116

116-
protected void verifySignature(final String[] pieces, final Algorithm algorithm) throws NoSuchAlgorithmException,
117+
void verifySignature(final String[] pieces, final Algorithm algorithm) throws NoSuchAlgorithmException,
117118
InvalidKeyException, SignatureException, JWTAlgorithmException, IllegalStateException {
118119
Validate.notNull(pieces);
119120
Validate.notNull(algorithm);
@@ -136,24 +137,24 @@ protected void verifySignature(final String[] pieces, final Algorithm algorithm)
136137
}
137138
}
138139

139-
void verifyHmac(final Algorithm algorithm, final String[] pieces, final byte[] secret) throws SignatureException, NoSuchAlgorithmException, InvalidKeyException {
140+
private void verifyHmac(final Algorithm algorithm, final String[] pieces, final byte[] secret) throws SignatureException, NoSuchAlgorithmException, InvalidKeyException {
140141
if (secret == null || secret.length == 0) {
141142
throw new IllegalStateException("Secret cannot be null or empty when using algorithm: " + algorithm.getValue());
142143
}
143144
final Mac hmac = Mac.getInstance(algorithm.getValue());
144145
hmac.init(new SecretKeySpec(secret, algorithm.getValue()));
145-
final byte[] sig = hmac.doFinal(new StringBuilder(pieces[0]).append(".").append(pieces[1]).toString().getBytes());
146+
final byte[] sig = hmac.doFinal((pieces[0] + "." + pieces[1]).getBytes());
146147
if (!MessageDigest.isEqual(sig, decoder.decode(pieces[2]))) {
147148
throw new SignatureException("signature verification failed");
148149
}
149150
}
150151

151-
void verifyRs(final Algorithm algorithm, final String[] pieces, final PublicKey publicKey) throws SignatureException, NoSuchAlgorithmException, InvalidKeyException, JWTAlgorithmException {
152+
private void verifyRs(final Algorithm algorithm, final String[] pieces, final PublicKey publicKey) throws SignatureException, NoSuchAlgorithmException, InvalidKeyException, JWTAlgorithmException {
152153
if (publicKey == null) {
153154
throw new IllegalStateException("PublicKey cannot be null when using algorithm: " + algorithm.getValue());
154155
}
155156
final byte[] decodedSignatureBytes = new Base64(true).decode(pieces[2]);
156-
final byte[] headerPayloadBytes = new StringBuilder(pieces[0]).append(".").append(pieces[1]).toString().getBytes();
157+
final byte[] headerPayloadBytes = (pieces[0] + "." + pieces[1]).getBytes();
157158
final boolean verified = verifySignatureWithPublicKey(this.publicKey, headerPayloadBytes, decodedSignatureBytes, algorithm);
158159
if (!verified) {
159160
throw new SignatureException("signature verification failed");
@@ -175,15 +176,15 @@ private boolean verifySignatureWithPublicKey(final PublicKey publicKey, final by
175176
}
176177
}
177178

178-
protected void verifyExpiration(final JsonNode jwtClaims) throws JWTExpiredException {
179+
void verifyExpiration(final JsonNode jwtClaims) throws JWTExpiredException {
179180
Validate.notNull(jwtClaims);
180181
final long expiration = jwtClaims.has("exp") ? jwtClaims.get("exp").asLong(0) : 0;
181182
if (expiration != 0 && System.currentTimeMillis() / 1000L >= expiration) {
182183
throw new JWTExpiredException("jwt expired", expiration);
183184
}
184185
}
185186

186-
protected void verifyIssuer(final JsonNode jwtClaims) throws JWTIssuerException {
187+
void verifyIssuer(final JsonNode jwtClaims) throws JWTIssuerException {
187188
Validate.notNull(jwtClaims);
188189

189190
if (this.issuer == null ) {
@@ -197,7 +198,7 @@ protected void verifyIssuer(final JsonNode jwtClaims) throws JWTIssuerException
197198
}
198199
}
199200

200-
protected void verifyAudience(final JsonNode jwtClaims) throws JWTAudienceException {
201+
void verifyAudience(final JsonNode jwtClaims) throws JWTAudienceException {
201202
Validate.notNull(jwtClaims);
202203
if (audience == null) {
203204
return;
@@ -220,7 +221,7 @@ protected void verifyAudience(final JsonNode jwtClaims) throws JWTAudienceExcept
220221
throw new JWTAudienceException("jwt audience invalid", audNode);
221222
}
222223

223-
protected Algorithm getAlgorithm(final JsonNode jwtHeader) throws JWTAlgorithmException {
224+
Algorithm getAlgorithm(final JsonNode jwtHeader) throws JWTAlgorithmException {
224225
Validate.notNull(jwtHeader);
225226
final String algorithmName = jwtHeader.has("alg") ? jwtHeader.get("alg").asText() : null;
226227
if (jwtHeader.get("alg") == null) {
@@ -229,11 +230,10 @@ protected Algorithm getAlgorithm(final JsonNode jwtHeader) throws JWTAlgorithmEx
229230
return Algorithm.findByName(algorithmName);
230231
}
231232

232-
protected JsonNode decodeAndParse(final String b64String) throws IOException {
233+
JsonNode decodeAndParse(final String b64String) throws IOException {
233234
Validate.notNull(b64String);
234235
final String jsonString = new String(decoder.decode(b64String), "UTF-8");
235-
final JsonNode jwtHeader = mapper.readValue(jsonString, JsonNode.class);
236-
return jwtHeader;
236+
return mapper.readValue(jsonString, JsonNode.class);
237237
}
238238

239239
}

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

Lines changed: 69 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -24,61 +24,70 @@ public class JWTVerifierTest {
2424
@Rule
2525
public ExpectedException expectedException = ExpectedException.none();
2626

27-
@Test(expected = IllegalArgumentException.class)
27+
@Test
2828
public void constructorShouldFailOnEmptySecret() {
29+
expectedException.expect(IllegalArgumentException.class);
2930
new JWTVerifier("");
3031
}
3132

32-
@Test(expected = IllegalStateException.class)
33+
@Test
3334
public void shouldFailOn1Segments() throws Exception {
34-
new JWTVerifier("such secret").verify("crypto");
35+
expectedException.expect(IllegalStateException.class);
36+
signatureVerifier().verify("crypto");
3537
}
3638

37-
@Test(expected = IllegalStateException.class)
39+
@Test
3840
public void shouldFailOn2Segments() throws Exception {
39-
new JWTVerifier("such secret").verify("much.crypto");
41+
expectedException.expect(IllegalStateException.class);
42+
signatureVerifier().verify("much.crypto");
4043
}
4144

42-
@Test(expected = IllegalStateException.class)
45+
@Test
4346
public void shouldFailOn4Segments() throws Exception {
44-
new JWTVerifier("such secret").verify("much.crypto.so.token");
47+
expectedException.expect(IllegalStateException.class);
48+
signatureVerifier().verify("much.crypto.so.token");
4549
}
4650

47-
@Test(expected = IllegalStateException.class)
51+
@Test
4852
public void shouldFailOnEmptyStringToken() throws Exception {
49-
new JWTVerifier("such secret").verify("");
53+
expectedException.expect(IllegalStateException.class);
54+
signatureVerifier().verify("");
5055
}
5156

52-
@Test(expected = IllegalStateException.class)
57+
@Test
5358
public void shouldFailOnNullToken() throws Exception {
54-
new JWTVerifier("such secret").verify(null);
59+
expectedException.expect(IllegalStateException.class);
60+
signatureVerifier().verify(null);
5561
}
5662

57-
@Test(expected = IllegalStateException.class)
63+
@Test
5864
public void shouldFailIfAlgorithmIsNotSetOnToken() throws Exception {
59-
new JWTVerifier("such secret").getAlgorithm(JsonNodeFactory.instance.objectNode());
65+
expectedException.expect(IllegalStateException.class);
66+
signatureVerifier().getAlgorithm(JsonNodeFactory.instance.objectNode());
6067
}
6168

62-
@Test(expected = JWTAlgorithmException.class)
69+
@Test
6370
public void shouldFailIfAlgorithmIsNotSupported() throws Exception {
64-
new JWTVerifier("such secret").getAlgorithm(createSingletonJSONNode("alg", "doge-crypt"));
71+
expectedException.expect(JWTAlgorithmException.class);
72+
signatureVerifier().getAlgorithm(createSingletonJSONNode("alg", "doge-crypt"));
6573
}
6674

6775
@Test
6876
public void shouldWorkIfAlgorithmIsSupported() throws Exception {
69-
new JWTVerifier("such secret").getAlgorithm(createSingletonJSONNode("alg", "HS256"));
77+
signatureVerifier().getAlgorithm(createSingletonJSONNode("alg", "HS256"));
7078
}
7179

72-
@Test(expected = SignatureException.class)
80+
@Test
7381
public void shouldFailOnInvalidSignature() throws Exception {
82+
expectedException.expect(SignatureException.class);
7483
final String jws = "eyJ0eXAiOiJKV1QiLA0KICJhbGciOiJIUzI1NiJ9" +
7584
"." +
7685
"eyJpc3MiOiJqb2UiLA0KICJleHAiOjEzMDA4MTkzODAsDQogImh0dHA6Ly9leGFt" +
7786
"cGxlLmNvbS9pc19yb290Ijp0cnVlfQ" +
7887
"." +
7988
"suchsignature_plzvalidate_zomgtokens";
8089
String secret = "AyM1SysPpbyDfgZld3umj1qzKObwVMkoqQ-EstJQLr_T-1qS0gZH75aKtMN3Yj0iPS4hcgUuTwjAzZr1Z9CAow";
81-
new JWTVerifier(secret, "audience").verifySignature(jws.split("\\."), Algorithm.HS256);
90+
signatureVerifier(secret).verifySignature(jws.split("\\."), Algorithm.HS256);
8291
}
8392

8493
@Test
@@ -90,76 +99,80 @@ public void shouldVerifySignature() throws Exception {
9099
"." +
91100
"dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk";
92101
byte[] secret = decoder.decode("AyM1SysPpbyDfgZld3umj1qzKObwVMkoqQ-EstJQLr_T-1qS0gZH75aKtMN3Yj0iPS4hcgUuTwjAzZr1Z9CAow");
93-
new JWTVerifier(secret, "audience")
102+
signatureVerifier(secret)
94103
.verifySignature(jws.split("\\."), Algorithm.HS256);
95104
}
96105

97-
@Test(expected = JWTExpiredException.class)
106+
@Test
98107
public void shouldFailWhenExpired1SecondAgo() throws Exception {
99-
new JWTVerifier("such secret").verifyExpiration(
108+
expectedException.expect(JWTExpiredException.class);
109+
signatureVerifier().verifyExpiration(
100110
createSingletonJSONNode("exp", Long.toString(System.currentTimeMillis() / 1000L - 1L)));
101111
}
102112

103113
@Test
104114
public void shouldVerifyExpiration() throws Exception {
105-
new JWTVerifier("such secret").verifyExpiration(
115+
signatureVerifier().verifyExpiration(
106116
createSingletonJSONNode("exp", Long.toString(System.currentTimeMillis() / 1000L + 50L)));
107117
}
108118

109119
@Test
110120
public void shouldVerifyIssuer() throws Exception {
111-
new JWTVerifier("such secret", "amaze audience", "very issuer")
121+
issuerVerifier("very issuer")
112122
.verifyIssuer(createSingletonJSONNode("iss", "very issuer"));
113123
}
114124

115-
@Test(expected = JWTIssuerException.class)
125+
@Test
116126
public void shouldFailIssuer() throws Exception {
117-
new JWTVerifier("such secret", "amaze audience", "very issuer")
127+
expectedException.expect(JWTIssuerException.class);
128+
issuerVerifier("very issuer")
118129
.verifyIssuer(createSingletonJSONNode("iss", "wow"));
119130
}
120131

121132
@Test
122133
public void shouldVerifyIssuerWhenNotFoundInClaimsSet() throws Exception {
123134
expectedException.expect(JWTIssuerException.class);
124-
new JWTVerifier("such secret", "amaze audience", "very issuer")
135+
issuerVerifier("very issuer")
125136
.verifyIssuer(JsonNodeFactory.instance.objectNode());
126137
}
127138

128139
@Test
129140
public void shouldVerifyAudience() throws Exception {
130-
new JWTVerifier("such secret", "amaze audience")
141+
audienceVerifier("amaze audience")
131142
.verifyAudience(createSingletonJSONNode("aud", "amaze audience"));
132143
}
133144

134-
@Test(expected = JWTAudienceException.class)
145+
@Test
135146
public void shouldFailAudience() throws Exception {
136-
new JWTVerifier("such secret", "amaze audience")
147+
expectedException.expect(JWTAudienceException.class);
148+
audienceVerifier("amaze audience")
137149
.verifyAudience(createSingletonJSONNode("aud", "wow"));
138150
}
139151

140152
@Test
141153
public void shouldVerifyAudienceWhenNotFoundInClaimsSet() throws Exception {
142154
expectedException.expect(JWTAudienceException.class);
143-
new JWTVerifier("such secret", "amaze audience")
155+
audienceVerifier("amaze audience")
144156
.verifyAudience(JsonNodeFactory.instance.objectNode());
145157
}
146158

147159
@Test
148160
public void shouldVerifyNullAudience() throws Exception {
149-
new JWTVerifier("such secret")
161+
signatureVerifier()
150162
.verifyAudience(createSingletonJSONNode("aud", "wow"));
151163
}
152164

153165
@Test
154166
public void shouldVerifyArrayAudience() throws Exception {
155-
new JWTVerifier("such secret", "amaze audience")
167+
audienceVerifier("amaze audience")
156168
.verifyAudience(createSingletonJSONNode("aud",
157169
new ObjectMapper().readValue("[ \"foo\", \"amaze audience\" ]", ArrayNode.class)));
158170
}
159171

160-
@Test(expected = JWTAudienceException.class)
172+
@Test
161173
public void shouldFailArrayAudience() throws Exception {
162-
new JWTVerifier("such secret", "amaze audience")
174+
expectedException.expect(JWTAudienceException.class);
175+
audienceVerifier("amaze audience")
163176
.verifyAudience(createSingletonJSONNode("aud",
164177
new ObjectMapper().readValue("[ \"foo\" ]", ArrayNode.class)));
165178
}
@@ -191,13 +204,33 @@ public void shouldVerifyIssuerFromToken() throws Exception {
191204
verifier.verify("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.wLlz9xDltxqKHQC7BeauPi5Q4KQK4nDjlRqQPvKVLYk");
192205
}
193206

194-
public static JsonNode createSingletonJSONNode(String key, String value) {
207+
private static JWTVerifier signatureVerifier() {
208+
return new JWTVerifier("such secret");
209+
}
210+
211+
private static JWTVerifier signatureVerifier(String secret) {
212+
return new JWTVerifier(secret);
213+
}
214+
215+
private static JWTVerifier signatureVerifier(byte[] secret) {
216+
return new JWTVerifier(secret);
217+
}
218+
219+
private static JWTVerifier issuerVerifier(String issuer) {
220+
return new JWTVerifier("such secret", null, issuer);
221+
}
222+
223+
private static JWTVerifier audienceVerifier(String audience) {
224+
return new JWTVerifier("such secret", audience);
225+
}
226+
227+
private static JsonNode createSingletonJSONNode(String key, String value) {
195228
final ObjectNode jsonNodes = JsonNodeFactory.instance.objectNode();
196229
jsonNodes.put(key, value);
197230
return jsonNodes;
198231
}
199232

200-
public static JsonNode createSingletonJSONNode(String key, JsonNode value) {
233+
private static JsonNode createSingletonJSONNode(String key, JsonNode value) {
201234
final ObjectNode jsonNodes = JsonNodeFactory.instance.objectNode();
202235
jsonNodes.put(key, value);
203236
return jsonNodes;

0 commit comments

Comments
 (0)