Skip to content

Commit 7d303ff

Browse files
authored
Merge pull request auth0#97 from auth0/improve-coverage
Improve Sign methods coverage
2 parents df25e97 + bd2b873 commit 7d303ff

18 files changed

Lines changed: 556 additions & 164 deletions

‎lib/src/main/java/com/auth0/jwt/JWTCreator.java‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.auth0.jwt.impl.PublicClaims;
77
import com.fasterxml.jackson.core.JsonProcessingException;
88
import com.fasterxml.jackson.databind.ObjectMapper;
9+
import org.apache.commons.codec.binary.Base64;
910

1011
import java.util.Date;
1112
import java.util.HashMap;
@@ -173,12 +174,12 @@ private String toSafeJson(Map<String, Object> claims) throws JsonProcessingExcep
173174
}
174175

175176
private String sign() throws SignatureGenerationException {
176-
String header = SignUtils.base64Encode(headerJson.getBytes());
177-
String payload = SignUtils.base64Encode(payloadJson.getBytes());
177+
String header = Base64.encodeBase64URLSafeString((headerJson.getBytes()));
178+
String payload = Base64.encodeBase64URLSafeString((payloadJson.getBytes()));
178179
String content = String.format("%s.%s", header, payload);
179180

180181
byte[] signatureBytes = algorithm.sign(content.getBytes());
181-
String signature = SignUtils.base64Encode(signatureBytes);
182+
String signature = Base64.encodeBase64URLSafeString((signatureBytes));
182183

183184
return String.format("%s.%s", content, signature);
184185
}

‎lib/src/main/java/com/auth0/jwt/JWTDecoder.java‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import com.auth0.jwt.interfaces.Header;
77
import com.auth0.jwt.interfaces.JWT;
88
import com.auth0.jwt.interfaces.Payload;
9+
import org.apache.commons.codec.binary.Base64;
10+
import org.apache.commons.codec.binary.StringUtils;
911

1012
import java.util.Date;
1113

@@ -35,13 +37,13 @@ static JWT decode(String token) throws JWTDecodeException {
3537
}
3638

3739
private void parseToken(String token) throws JWTDecodeException {
38-
final String[] parts = SignUtils.splitToken(token);
40+
final String[] parts = TokenUtils.splitToken(token);
3941
final JWTParser converter = new JWTParser();
4042
String headerJson;
4143
String payloadJson;
4244
try {
43-
headerJson = SignUtils.toUTF8String(SignUtils.base64Decode(parts[0]));
44-
payloadJson = SignUtils.toUTF8String(SignUtils.base64Decode(parts[1]));
45+
headerJson = StringUtils.newStringUtf8(Base64.decodeBase64(parts[0]));
46+
payloadJson = StringUtils.newStringUtf8(Base64.decodeBase64(parts[1]));
4547
} catch (NullPointerException e) {
4648
throw new JWTDecodeException("The UTF-8 Charset isn't initialized.", e);
4749
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.auth0.jwt.algorithms.Algorithm;
44
import com.auth0.jwt.exceptions.*;
55
import com.auth0.jwt.impl.PublicClaims;
6+
import org.apache.commons.codec.binary.Base64;
67

78
import java.util.*;
89

@@ -207,14 +208,14 @@ private void requireClaim(String name, Object value) {
207208
public JWT verify(String token) throws JWTDecodeException, JWTVerificationException {
208209
JWT jwt = new JWT(JWTDecoder.decode(token));
209210
verifyAlgorithm(jwt, algorithm);
210-
verifySignature(SignUtils.splitToken(token));
211+
verifySignature(TokenUtils.splitToken(token));
211212
verifyClaims(jwt, claims);
212213
return jwt;
213214
}
214215

215216
private void verifySignature(String[] parts) throws SignatureVerificationException {
216217
byte[] content = String.format("%s.%s", parts[0], parts[1]).getBytes();
217-
byte[] signature = SignUtils.base64Decode(parts[2]);
218+
byte[] signature = Base64.decodeBase64(parts[2]);
218219
algorithm.verify(content, signature);
219220
}
220221

‎lib/src/main/java/com/auth0/jwt/SignUtils.java‎

Lines changed: 0 additions & 60 deletions
This file was deleted.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.auth0.jwt;
2+
3+
import com.auth0.jwt.exceptions.JWTDecodeException;
4+
5+
abstract class TokenUtils {
6+
7+
/**
8+
* Splits the given token on the "." chars into a String array with 3 parts.
9+
*
10+
* @param token the string to split.
11+
* @return the array representing the 3 parts of the token.
12+
* @throws JWTDecodeException if the Token doesn't have 3 parts.
13+
*/
14+
static String[] splitToken(String token) throws JWTDecodeException {
15+
String[] parts = token.split("\\.");
16+
if (parts.length == 2 && token.endsWith(".")) {
17+
//Tokens with alg='none' have empty String as Signature.
18+
parts = new String[]{parts[0], parts[1], ""};
19+
}
20+
if (parts.length != 3) {
21+
throw new JWTDecodeException(String.format("The token was expected to have 3 parts, but got %s.", parts.length));
22+
}
23+
return parts;
24+
}
25+
}

‎lib/src/main/java/com/auth0/jwt/algorithms/Algorithm.java‎

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ public abstract class Algorithm {
1919
*
2020
* @param key the key to use in the verify or signing instance.
2121
* @return a valid RSA256 Algorithm.
22+
* @throws IllegalArgumentException if the provided Key is null.
2223
*/
23-
public static Algorithm RSA256(RSAKey key) {
24+
public static Algorithm RSA256(RSAKey key) throws IllegalArgumentException {
2425
return new RSAAlgorithm("RS256", "SHA256withRSA", key);
2526
}
2627

@@ -29,8 +30,9 @@ public static Algorithm RSA256(RSAKey key) {
2930
*
3031
* @param key the key to use in the verify or signing instance.
3132
* @return a valid RSA384 Algorithm.
33+
* @throws IllegalArgumentException if the provided Key is null.
3234
*/
33-
public static Algorithm RSA384(RSAKey key) {
35+
public static Algorithm RSA384(RSAKey key) throws IllegalArgumentException {
3436
return new RSAAlgorithm("RS384", "SHA384withRSA", key);
3537
}
3638

@@ -39,8 +41,9 @@ public static Algorithm RSA384(RSAKey key) {
3941
*
4042
* @param key the key to use in the verify or signing instance.
4143
* @return a valid RSA512 Algorithm.
44+
* @throws IllegalArgumentException if the provided Key is null.
4245
*/
43-
public static Algorithm RSA512(RSAKey key) {
46+
public static Algorithm RSA512(RSAKey key) throws IllegalArgumentException {
4447
return new RSAAlgorithm("RS512", "SHA512withRSA", key);
4548
}
4649

@@ -49,8 +52,9 @@ public static Algorithm RSA512(RSAKey key) {
4952
*
5053
* @param secret the secret to use in the verify or signing instance.
5154
* @return a valid HMAC256 Algorithm.
55+
* @throws IllegalArgumentException if the provided Secret is null.
5256
*/
53-
public static Algorithm HMAC256(String secret) {
57+
public static Algorithm HMAC256(String secret) throws IllegalArgumentException {
5458
return new HMACAlgorithm("HS256", "HmacSHA256", secret);
5559
}
5660

@@ -59,8 +63,9 @@ public static Algorithm HMAC256(String secret) {
5963
*
6064
* @param secret the secret to use in the verify or signing instance.
6165
* @return a valid HMAC384 Algorithm.
66+
* @throws IllegalArgumentException if the provided Secret is null.
6267
*/
63-
public static Algorithm HMAC384(String secret) {
68+
public static Algorithm HMAC384(String secret) throws IllegalArgumentException {
6469
return new HMACAlgorithm("HS384", "HmacSHA384", secret);
6570
}
6671

@@ -69,8 +74,9 @@ public static Algorithm HMAC384(String secret) {
6974
*
7075
* @param secret the secret to use in the verify or signing instance.
7176
* @return a valid HMAC512 Algorithm.
77+
* @throws IllegalArgumentException if the provided Secret is null.
7278
*/
73-
public static Algorithm HMAC512(String secret) {
79+
public static Algorithm HMAC512(String secret) throws IllegalArgumentException {
7480
return new HMACAlgorithm("HS512", "HmacSHA512", secret);
7581
}
7682

@@ -79,8 +85,9 @@ public static Algorithm HMAC512(String secret) {
7985
*
8086
* @param key the key to use in the verify or signing instance.
8187
* @return a valid ECDSA256 Algorithm.
88+
* @throws IllegalArgumentException if the provided Key is null.
8289
*/
83-
public static Algorithm ECDSA256(ECKey key) {
90+
public static Algorithm ECDSA256(ECKey key) throws IllegalArgumentException {
8491
return new ECDSAAlgorithm("ES256", "SHA256withECDSA", 32, key);
8592
}
8693

@@ -89,8 +96,9 @@ public static Algorithm ECDSA256(ECKey key) {
8996
*
9097
* @param key the key to use in the verify or signing instance.
9198
* @return a valid ECDSA384 Algorithm.
99+
* @throws IllegalArgumentException if the provided Key is null.
92100
*/
93-
public static Algorithm ECDSA384(ECKey key) {
101+
public static Algorithm ECDSA384(ECKey key) throws IllegalArgumentException {
94102
return new ECDSAAlgorithm("ES384", "SHA384withECDSA", 48, key);
95103
}
96104

@@ -99,8 +107,9 @@ public static Algorithm ECDSA384(ECKey key) {
99107
*
100108
* @param key the key to use in the verify or signing instance.
101109
* @return a valid ECDSA512 Algorithm.
110+
* @throws IllegalArgumentException if the provided Key is null.
102111
*/
103-
public static Algorithm ECDSA512(ECKey key) {
112+
public static Algorithm ECDSA512(ECKey key) throws IllegalArgumentException {
104113
return new ECDSAAlgorithm("ES512", "SHA512withECDSA", 66, key);
105114
}
106115

‎lib/src/main/java/com/auth0/jwt/algorithms/ECDSAAlgorithm.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class ECDSAAlgorithm extends Algorithm {
1717
private final int ecNumberSize;
1818
private final ECKey key;
1919

20-
ECDSAAlgorithm(CryptoHelper crypto, String id, String algorithm, int ecNumberSize, ECKey key) {
20+
ECDSAAlgorithm(CryptoHelper crypto, String id, String algorithm, int ecNumberSize, ECKey key) throws IllegalArgumentException {
2121
super(id, algorithm);
2222
if (key == null) {
2323
throw new IllegalArgumentException("The ECKey cannot be null");
@@ -27,7 +27,7 @@ class ECDSAAlgorithm extends Algorithm {
2727
this.crypto = crypto;
2828
}
2929

30-
ECDSAAlgorithm(String id, String algorithm, int ecNumberSize, ECKey key) {
30+
ECDSAAlgorithm(String id, String algorithm, int ecNumberSize, ECKey key) throws IllegalArgumentException {
3131
this(new CryptoHelper(), id, algorithm, ecNumberSize, key);
3232
}
3333

@@ -49,7 +49,7 @@ public void verify(byte[] contentBytes, byte[] signatureBytes) throws SignatureV
4949
if (!valid) {
5050
throw new SignatureVerificationException(this);
5151
}
52-
} catch (NoSuchAlgorithmException | SignatureException | InvalidKeyException e) {
52+
} catch (NoSuchAlgorithmException | SignatureException | InvalidKeyException | IllegalArgumentException e) {
5353
throw new SignatureVerificationException(this, e);
5454
}
5555
}

‎lib/src/main/java/com/auth0/jwt/algorithms/HMACAlgorithm.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class HMACAlgorithm extends Algorithm {
1111
private final CryptoHelper crypto;
1212
private final String secret;
1313

14-
HMACAlgorithm(CryptoHelper crypto, String id, String algorithm, String secret) {
14+
HMACAlgorithm(CryptoHelper crypto, String id, String algorithm, String secret) throws IllegalArgumentException {
1515
super(id, algorithm);
1616
if (secret == null) {
1717
throw new IllegalArgumentException("The Secret cannot be null");
@@ -20,7 +20,7 @@ class HMACAlgorithm extends Algorithm {
2020
this.crypto = crypto;
2121
}
2222

23-
HMACAlgorithm(String id, String algorithm, String secret) {
23+
HMACAlgorithm(String id, String algorithm, String secret) throws IllegalArgumentException {
2424
this(new CryptoHelper(), id, algorithm, secret);
2525
}
2626

‎lib/src/main/java/com/auth0/jwt/algorithms/RSAAlgorithm.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class RSAAlgorithm extends Algorithm {
1313
private final RSAKey key;
1414
private final CryptoHelper crypto;
1515

16-
RSAAlgorithm(CryptoHelper crypto, String id, String algorithm, RSAKey key) {
16+
RSAAlgorithm(CryptoHelper crypto, String id, String algorithm, RSAKey key) throws IllegalArgumentException {
1717
super(id, algorithm);
1818
if (key == null) {
1919
throw new IllegalArgumentException("The RSAKey cannot be null");
@@ -22,7 +22,7 @@ class RSAAlgorithm extends Algorithm {
2222
this.crypto = crypto;
2323
}
2424

25-
RSAAlgorithm(String id, String algorithm, RSAKey key) {
25+
RSAAlgorithm(String id, String algorithm, RSAKey key) throws IllegalArgumentException {
2626
this(new CryptoHelper(), id, algorithm, key);
2727
}
2828

@@ -40,7 +40,7 @@ public void verify(byte[] contentBytes, byte[] signatureBytes) throws SignatureV
4040
if (!valid) {
4141
throw new SignatureVerificationException(this);
4242
}
43-
} catch (NoSuchAlgorithmException | SignatureException | InvalidKeyException e) {
43+
} catch (NoSuchAlgorithmException | SignatureException | InvalidKeyException | IllegalArgumentException e) {
4444
throw new SignatureVerificationException(this, e);
4545
}
4646
}

‎lib/src/main/java/com/auth0/jwt/exceptions/JWTCreationException.java‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
package com.auth0.jwt.exceptions;
22

33
public class JWTCreationException extends RuntimeException {
4-
public JWTCreationException(String message) {
5-
this(message, null);
6-
}
7-
84
public JWTCreationException(String message, Throwable cause) {
95
super(message, cause);
106
}

0 commit comments

Comments
 (0)