Skip to content

Commit 8b3fbd2

Browse files
committed
refactor SignUtils and base64 helper methods
1 parent 97cde74 commit 8b3fbd2

14 files changed

Lines changed: 90 additions & 72 deletions

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ private void parseToken(String token) throws JWTDecodeException {
4040
String headerJson;
4141
String payloadJson;
4242
try {
43-
headerJson = SignUtils.base64Decode(parts[0]);
44-
payloadJson = SignUtils.base64Decode(parts[1]);
43+
headerJson = SignUtils.toUTF8String(SignUtils.base64Decode(parts[0]));
44+
payloadJson = SignUtils.toUTF8String(SignUtils.base64Decode(parts[1]));
4545
} catch (NullPointerException e) {
4646
throw new JWTDecodeException("The UTF-8 Charset isn't initialized.", e);
4747
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.auth0.jwt;
2+
3+
import com.auth0.jwt.algorithms.Algorithm;
4+
5+
public class JWTSigner {
6+
7+
public String sign(String headerJson, String payloadJson) {
8+
String header = SignUtils.base64Encode(headerJson.getBytes());
9+
String payload = SignUtils.base64Encode(payloadJson.getBytes());
10+
String content = String.format("%s.%s", header, payload);
11+
Algorithm algorithm = Algorithm.HMAC256("secret");
12+
13+
byte[] signatureBytes = algorithm.sign(content.getBytes());
14+
String signature = SignUtils.base64Encode(signatureBytes);
15+
16+
return String.format("%s.%s", content, signature);
17+
}
18+
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
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;
76

87
import java.util.*;
98

@@ -215,7 +214,7 @@ public JWT verify(String token) throws JWTDecodeException, JWTVerificationExcept
215214

216215
private void verifySignature(String[] parts) throws SignatureVerificationException {
217216
byte[] content = String.format("%s.%s", parts[0], parts[1]).getBytes();
218-
byte[] signature = Base64.decodeBase64(parts[2]);
217+
byte[] signature = SignUtils.base64Decode(parts[2]);
219218
algorithm.verify(content, signature);
220219
}
221220

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

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,37 @@
77
abstract class SignUtils {
88

99
/**
10-
* Decodes a given String from it's Base64 string representation into a UTF-8 String.
10+
* Encodes the given bytes into a UTF-8 String representation.
1111
*
12-
* @param source the source of the decode process.
13-
* @return a UTF-8 String representing the Base64 decoded source.
12+
* @param source the source of the .
13+
* @return a UTF-8 String representing the source bytes.
1414
* @throws NullPointerException if the UTF-8 Charset isn't initialized.
1515
*/
16-
static String base64Decode(String source) throws NullPointerException {
17-
return StringUtils.newStringUtf8(Base64.decodeBase64(source));
16+
static String toUTF8String(byte[] source) throws NullPointerException {
17+
return StringUtils.newStringUtf8(source);
1818
}
1919

2020
/**
21-
* Encodes a given String into it's Base64 string representation.
21+
* Decodes a given String from it's Base64 String representation into an array of bytes.
2222
*
23-
* @param source the source of the decode process.
24-
* @return a UTF-8 String encoded into it's Base64 representation.
25-
* @throws NullPointerException if the UTF-8 Charset isn't initialized.
26-
* @throws IllegalArgumentException if the source string is too long.
23+
* @param source the source bytes to decode.
24+
* @return an array of bytes representing the Base64 decoded source.
2725
*/
28-
static String base64Encode(String source) throws NullPointerException, IllegalArgumentException {
29-
return StringUtils.newStringUtf8(Base64.encodeBase64(source.getBytes(), false, true));
26+
static byte[] base64Decode(String source) {
27+
return Base64.decodeBase64(source);
3028
}
3129

30+
/**
31+
* Encodes a given String into it's Base64 String representation.
32+
*
33+
* @param source the source bytes to encode.
34+
* @return a String containing Base64 characters.
35+
*/
36+
static String base64Encode(byte[] source) {
37+
return Base64.encodeBase64URLSafeString(source);
38+
}
39+
40+
3241
/**
3342
* Splits the given token on the "." chars into a String array with 3 parts.
3443
*

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ public String toString() {
149149
* Sign the given content using this Algorithm instance.
150150
*
151151
* @param contentBytes an array of bytes representing the base64 encoded content to be verified against the signature.
152+
* @return the signature in a base64 encoded array of bytes
152153
* @throws SignatureGenerationException if the Key is invalid.
153154
*/
154155
public abstract byte[] sign(byte[] contentBytes) throws SignatureGenerationException;

lib/src/main/java/com/auth0/jwt/algorithms/CryptoHelper.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66

77
class CryptoHelper {
88

9-
boolean verifyMacFor(String algorithm, byte[] secretBytes, byte[] contentBytes, byte[] signatureBytes) throws NoSuchAlgorithmException, InvalidKeyException {
10-
return MessageDigest.isEqual(createMacFor(algorithm, secretBytes, contentBytes), signatureBytes);
9+
boolean verifySignatureFor(String algorithm, byte[] secretBytes, byte[] contentBytes, byte[] signatureBytes) throws NoSuchAlgorithmException, InvalidKeyException {
10+
return MessageDigest.isEqual(createSignatureFor(algorithm, secretBytes, contentBytes), signatureBytes);
1111
}
1212

13-
byte[] createMacFor(String algorithm, byte[] secretBytes, byte[] contentBytes) throws NoSuchAlgorithmException, InvalidKeyException {
13+
byte[] createSignatureFor(String algorithm, byte[] secretBytes, byte[] contentBytes) throws NoSuchAlgorithmException, InvalidKeyException {
1414
final Mac mac = Mac.getInstance(algorithm);
1515
mac.init(new SecretKeySpec(secretBytes, algorithm));
1616
return mac.doFinal(contentBytes);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ ECKey getKey() {
3737

3838
@Override
3939
public void verify(byte[] contentBytes, byte[] signatureBytes) throws SignatureVerificationException {
40-
if (!(key instanceof ECPublicKey)) {
41-
throw new IllegalArgumentException("The given ECKey is not an ECPublicKey.");
42-
}
4340
try {
41+
if (!(key instanceof ECPublicKey)) {
42+
throw new IllegalArgumentException("The given ECKey is not an ECPublicKey.");
43+
}
4444
if (!isDERSignature(signatureBytes)) {
4545
signatureBytes = JOSEToDER(signatureBytes);
4646
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ String getSecret() {
3131
@Override
3232
public void verify(byte[] contentBytes, byte[] signatureBytes) throws SignatureVerificationException {
3333
try {
34-
boolean valid = crypto.verifyMacFor(getDescription(), secret.getBytes(), contentBytes, signatureBytes);
35-
34+
boolean valid = crypto.verifySignatureFor(getDescription(), secret.getBytes(), contentBytes, signatureBytes);
3635
if (!valid) {
3736
throw new SignatureVerificationException(this);
3837
}
@@ -44,7 +43,7 @@ public void verify(byte[] contentBytes, byte[] signatureBytes) throws SignatureV
4443
@Override
4544
public byte[] sign(byte[] contentBytes) throws SignatureGenerationException {
4645
try {
47-
return crypto.createMacFor(getDescription(), secret.getBytes(), contentBytes);
46+
return crypto.createSignatureFor(getDescription(), secret.getBytes(), contentBytes);
4847
} catch (NoSuchAlgorithmException | InvalidKeyException e) {
4948
throw new SignatureGenerationException(this, e);
5049
}

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
class RSAAlgorithm extends Algorithm {
1212

1313
private final RSAKey key;
14-
private CryptoHelper crypto;
14+
private final CryptoHelper crypto;
1515

1616
RSAAlgorithm(CryptoHelper crypto, String id, String algorithm, RSAKey key) {
1717
super(id, algorithm);
@@ -32,12 +32,11 @@ RSAKey getKey() {
3232

3333
@Override
3434
public void verify(byte[] contentBytes, byte[] signatureBytes) throws SignatureVerificationException {
35-
if (!(key instanceof PublicKey)) {
36-
throw new IllegalArgumentException("The given RSAKey is not a RSAPublicKey.");
37-
}
3835
try {
36+
if (!(key instanceof PublicKey)) {
37+
throw new IllegalArgumentException("The given RSAKey is not a RSAPublicKey.");
38+
}
3939
boolean valid = crypto.verifySignatureFor(getDescription(), (RSAPublicKey) key, contentBytes, signatureBytes);
40-
4140
if (!valid) {
4241
throw new SignatureVerificationException(this);
4342
}

lib/src/test/java/com/auth0/jwt/JWTDecoderTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,8 @@ public void shouldGetNullClaimIfClaimValueIsNull() throws Exception {
200200
//Helper Methods
201201

202202
private JWT customJWT(String jsonHeader, String jsonPayload, String signature) {
203-
String header = base64Encode(jsonHeader);
204-
String body = base64Encode(jsonPayload);
203+
String header = base64Encode(jsonHeader.getBytes());
204+
String body = base64Encode(jsonPayload.getBytes());
205205
return JWTDecoder.decode(String.format("%s.%s.%s", header, body, signature));
206206
}
207207

0 commit comments

Comments
 (0)