Skip to content

Commit bd2b873

Browse files
committed
refactor SignUtils
1 parent 64f7252 commit bd2b873

11 files changed

Lines changed: 75 additions & 119 deletions

File tree

‎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/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
}

‎lib/src/test/java/com/auth0/jwt/ConcurrentVerifyTest.java‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
import com.auth0.jwt.algorithms.Algorithm;
44
import net.jodah.concurrentunit.Waiter;
5-
import org.junit.*;
5+
import org.junit.AfterClass;
6+
import org.junit.BeforeClass;
7+
import org.junit.Rule;
8+
import org.junit.Test;
69
import org.junit.rules.ExpectedException;
710

811
import java.security.interfaces.ECKey;
@@ -13,7 +16,7 @@
1316

1417
import static com.auth0.jwt.PemUtils.readPublicKeyFromFile;
1518

16-
@Ignore("Skipping concurrency tests")
19+
//@Ignore("Skipping concurrency tests")
1720
public class ConcurrentVerifyTest {
1821

1922
private static final long TIMEOUT = 60 * 1000 * 1000; //1 min

‎lib/src/test/java/com/auth0/jwt/JWTCreatorTest.java‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public void shouldAddHeader() throws Exception {
3636
.sign(Algorithm.HMAC256("secret"));
3737

3838
assertThat(signed, is(notNullValue()));
39-
assertThat(SignUtils.splitToken(signed)[0], is("eyJhbGciOiJIUzI1NiIsImFzZCI6MTIzfQ"));
39+
assertThat(TokenUtils.splitToken(signed)[0], is("eyJhbGciOiJIUzI1NiIsImFzZCI6MTIzfQ"));
4040
}
4141

4242
@Test
@@ -46,7 +46,7 @@ public void shouldAddIssuer() throws Exception {
4646
.sign(Algorithm.HMAC256("secret"));
4747

4848
assertThat(signed, is(notNullValue()));
49-
assertThat(SignUtils.splitToken(signed)[1], is("eyJpc3MiOiJhdXRoMCJ9"));
49+
assertThat(TokenUtils.splitToken(signed)[1], is("eyJpc3MiOiJhdXRoMCJ9"));
5050
}
5151

5252
@Test
@@ -56,7 +56,7 @@ public void shouldAddSubject() throws Exception {
5656
.sign(Algorithm.HMAC256("secret"));
5757

5858
assertThat(signed, is(notNullValue()));
59-
assertThat(SignUtils.splitToken(signed)[1], is("eyJzdWIiOiIxMjM0NTY3ODkwIn0"));
59+
assertThat(TokenUtils.splitToken(signed)[1], is("eyJzdWIiOiIxMjM0NTY3ODkwIn0"));
6060
}
6161

6262
@Test
@@ -66,15 +66,15 @@ public void shouldAddAudience() throws Exception {
6666
.sign(Algorithm.HMAC256("secret"));
6767

6868
assertThat(signed, is(notNullValue()));
69-
assertThat(SignUtils.splitToken(signed)[1], is("eyJhdWQiOiJNYXJrIn0"));
69+
assertThat(TokenUtils.splitToken(signed)[1], is("eyJhdWQiOiJNYXJrIn0"));
7070

7171

7272
String signedArr = JWTCreator.init()
7373
.withAudience(new String[]{"Mark", "David"})
7474
.sign(Algorithm.HMAC256("secret"));
7575

7676
assertThat(signedArr, is(notNullValue()));
77-
assertThat(SignUtils.splitToken(signedArr)[1], is("eyJhdWQiOlsiTWFyayIsIkRhdmlkIl19"));
77+
assertThat(TokenUtils.splitToken(signedArr)[1], is("eyJhdWQiOlsiTWFyayIsIkRhdmlkIl19"));
7878
}
7979

8080
@Test
@@ -84,7 +84,7 @@ public void shouldAddExpiresAt() throws Exception {
8484
.sign(Algorithm.HMAC256("secret"));
8585

8686
assertThat(signed, is(notNullValue()));
87-
assertThat(SignUtils.splitToken(signed)[1], is("eyJleHAiOjE0Nzc1OTJ9"));
87+
assertThat(TokenUtils.splitToken(signed)[1], is("eyJleHAiOjE0Nzc1OTJ9"));
8888
}
8989

9090
@Test
@@ -94,7 +94,7 @@ public void shouldAddNotBefore() throws Exception {
9494
.sign(Algorithm.HMAC256("secret"));
9595

9696
assertThat(signed, is(notNullValue()));
97-
assertThat(SignUtils.splitToken(signed)[1], is("eyJuYmYiOjE0Nzc1OTJ9"));
97+
assertThat(TokenUtils.splitToken(signed)[1], is("eyJuYmYiOjE0Nzc1OTJ9"));
9898
}
9999

100100
@Test
@@ -104,7 +104,7 @@ public void shouldAddIssuedAt() throws Exception {
104104
.sign(Algorithm.HMAC256("secret"));
105105

106106
assertThat(signed, is(notNullValue()));
107-
assertThat(SignUtils.splitToken(signed)[1], is("eyJpYXQiOjE0Nzc1OTJ9"));
107+
assertThat(TokenUtils.splitToken(signed)[1], is("eyJpYXQiOjE0Nzc1OTJ9"));
108108
}
109109

110110
@Test
@@ -114,7 +114,7 @@ public void shouldAddJWTId() throws Exception {
114114
.sign(Algorithm.HMAC256("secret"));
115115

116116
assertThat(signed, is(notNullValue()));
117-
assertThat(SignUtils.splitToken(signed)[1], is("eyJqdGkiOiJqd3RfaWRfMTIzIn0"));
117+
assertThat(TokenUtils.splitToken(signed)[1], is("eyJqdGkiOiJqd3RfaWRfMTIzIn0"));
118118
}
119119

120120

@@ -126,7 +126,7 @@ public void shouldRemoveClaimWhenPassingNull() throws Exception {
126126
.sign(Algorithm.HMAC256("secret"));
127127

128128
assertThat(signed, is(notNullValue()));
129-
assertThat(SignUtils.splitToken(signed)[1], is("e30"));
129+
assertThat(TokenUtils.splitToken(signed)[1], is("e30"));
130130
}
131131

132132
@Test
@@ -135,15 +135,15 @@ public void shouldSetCorrectAlgorithmInTheHeader() throws Exception {
135135
.sign(Algorithm.HMAC256("secret"));
136136

137137
assertThat(signed, is(notNullValue()));
138-
assertThat(SignUtils.splitToken(signed)[0], is("eyJhbGciOiJIUzI1NiJ9"));
138+
assertThat(TokenUtils.splitToken(signed)[0], is("eyJhbGciOiJIUzI1NiJ9"));
139139
}
140140

141141
@Test
142142
public void shouldSetEmptySignatureIfAlgorithmIsNone() throws Exception {
143143
String signed = JWTCreator.init()
144144
.sign(Algorithm.none());
145145
assertThat(signed, is(notNullValue()));
146-
assertThat(SignUtils.splitToken(signed)[2], is(""));
146+
assertThat(TokenUtils.splitToken(signed)[2], is(""));
147147
}
148148

149149
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
import com.auth0.jwt.impl.BaseClaim;
55
import com.auth0.jwt.interfaces.Claim;
66
import com.auth0.jwt.interfaces.JWT;
7+
import org.apache.commons.codec.binary.Base64;
78
import org.junit.Ignore;
89
import org.junit.Rule;
910
import org.junit.Test;
1011
import org.junit.rules.ExpectedException;
1112

1213
import java.util.Date;
1314

14-
import static com.auth0.jwt.SignUtils.base64Encode;
1515
import static org.hamcrest.MatcherAssert.assertThat;
1616
import static org.hamcrest.Matchers.*;
1717

@@ -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.getBytes());
204-
String body = base64Encode(jsonPayload.getBytes());
203+
String header = Base64.encodeBase64URLSafeString(jsonHeader.getBytes());
204+
String body = Base64.encodeBase64URLSafeString(jsonPayload.getBytes());
205205
return JWTDecoder.decode(String.format("%s.%s.%s", header, body, signature));
206206
}
207207

‎lib/src/test/java/com/auth0/jwt/JWTTest.java‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,18 @@ public class JWTTest {
3030
@Rule
3131
public ExpectedException exception = ExpectedException.none();
3232

33-
// Initiate with diff Algorithms
33+
// Decode
34+
35+
@Test
36+
public void shouldDecodeAStringToken() throws Exception {
37+
String token = "eyJhbGciOiJIUzI1NiIsImN0eSI6IkpXVCJ9.eyJpc3MiOiJhdXRoMCJ9.mZ0m_N1J4PgeqWmi903JuUoDRZDBPB7HwkS4nVyWH1M";
38+
JWT jwt = JWT.decode(token);
39+
40+
assertThat(jwt, is(notNullValue()));
41+
}
42+
43+
44+
// Verify
3445

3546
@Test
3647
public void shouldAcceptNoneAlgorithm() throws Exception {
@@ -166,8 +177,6 @@ public void shouldGetSignature() throws Exception {
166177
assertThat(jwt.getSignature(), is("XmNK3GpH3Ys_7wsYBfq4C3M6goz71I7dTgUkuIa5lyQ"));
167178
}
168179

169-
// Public PublicClaims
170-
171180
@Test
172181
public void shouldGetIssuer() throws Exception {
173182
String token = "eyJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJKb2huIERvZSJ9.SgXosfRR_IwCgHq5lF3tlM-JHtpucWCRSaVuoHTbWbQ";
@@ -299,9 +308,7 @@ public void shouldGetType() throws Exception {
299308
}
300309

301310

302-
// *********************************************** //
303-
// Creation / Signing
304-
// *********************************************** //
311+
// Sign
305312

306313
@Test
307314
public void shouldCreateAnEmptyHMAC256SignedToken() throws Exception {

0 commit comments

Comments
 (0)