Skip to content

Commit 3135718

Browse files
authored
Merge pull request auth0#114 from auth0/force-bytes-charset
Make getBytes use UTF-8 charset
2 parents 8d70e3b + fea96e8 commit 3135718

File tree

16 files changed

+78
-69
lines changed

16 files changed

+78
-69
lines changed

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@
1111
import com.fasterxml.jackson.databind.module.SimpleModule;
1212
import org.apache.commons.codec.binary.Base64;
1313

14+
import java.nio.charset.StandardCharsets;
1415
import java.util.Date;
1516
import java.util.HashMap;
1617
import java.util.Map;
1718

1819
/**
19-
* The JWTCreator class holds the sign method to generate a complete DecodedJWT (with Signature) from a given Header and Payload content.
20+
* The JWTCreator class holds the sign method to generate a complete JWT (with Signature) from a given Header and Payload content.
2021
*/
2122
@SuppressWarnings("WeakerAccess")
2223
public final class JWTCreator {
@@ -50,7 +51,7 @@ static JWTCreator.Builder init() {
5051
}
5152

5253
/**
53-
* The Builder class holds the Claims that defines the DecodedJWT to be created.
54+
* The Builder class holds the Claims that defines the JWT to be created.
5455
*/
5556
public static class Builder {
5657
private final Map<String, Object> payloadClaims;
@@ -139,7 +140,7 @@ public Builder withIssuedAt(Date issuedAt) {
139140
}
140141

141142
/**
142-
* Add a specific DecodedJWT Id ("jti") claim.
143+
* Add a specific JWT Id ("jti") claim.
143144
*
144145
* @param jwtId the Token Id value.
145146
* @return this same Builder instance.
@@ -172,12 +173,12 @@ public Builder withClaim(String name, Object value) throws IllegalArgumentExcept
172173
}
173174

174175
/**
175-
* Creates a new instance of the DecodedJWT with the specified payloadClaims.
176+
* Creates a new JWT and signs is with the given algorithm
176177
*
177-
* @param algorithm the Algorithm to use on the DecodedJWT signing.
178-
* @return a new DecodedJWT instance.
178+
* @param algorithm used to sign the JWT
179+
* @return a new JWT token
179180
* @throws IllegalArgumentException if the provided algorithm is null.
180-
* @throws JWTCreationException if the Claims coudln't be converted to a valid JSON or there was a problem with the signing key.
181+
* @throws JWTCreationException if the claims could not be converted to a valid JSON or there was a problem with the signing key.
181182
*/
182183
public String sign(Algorithm algorithm) throws IllegalArgumentException, JWTCreationException {
183184
if (algorithm == null) {
@@ -197,11 +198,11 @@ private void addClaim(String name, Object value) {
197198
}
198199

199200
private String sign() throws SignatureGenerationException {
200-
String header = Base64.encodeBase64URLSafeString((headerJson.getBytes()));
201-
String payload = Base64.encodeBase64URLSafeString((payloadJson.getBytes()));
201+
String header = Base64.encodeBase64URLSafeString((headerJson.getBytes(StandardCharsets.UTF_8)));
202+
String payload = Base64.encodeBase64URLSafeString((payloadJson.getBytes(StandardCharsets.UTF_8)));
202203
String content = String.format("%s.%s", header, payload);
203204

204-
byte[] signatureBytes = algorithm.sign(content.getBytes());
205+
byte[] signatureBytes = algorithm.sign(content.getBytes(StandardCharsets.UTF_8));
205206
String signature = Base64.encodeBase64URLSafeString((signatureBytes));
206207

207208
return String.format("%s.%s", content, signature);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import java.util.List;
1414

1515
/**
16-
* The JWTDecoder class holds the decode method to parse a given Token into it's DecodedJWT representation.
16+
* The JWTDecoder class holds the decode method to parse a given JWT token into it's JWT representation.
1717
*/
1818
@SuppressWarnings("WeakerAccess")
1919
final class JWTDecoder extends JWT {

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010
import com.auth0.jwt.interfaces.DecodedJWT;
1111
import org.apache.commons.codec.binary.Base64;
1212

13+
import java.nio.charset.StandardCharsets;
1314
import java.util.*;
1415

1516
/**
16-
* The JWTVerifier class holds the verify method to assert that a given Token has not only a proper DecodedJWT format, but also it's signature matches.
17+
* The JWTVerifier class holds the verify method to assert that a given Token has not only a proper JWT format, but also it's signature matches.
1718
*/
1819
@SuppressWarnings("WeakerAccess")
1920
public final class JWTVerifier {
@@ -30,7 +31,7 @@ public final class JWTVerifier {
3031
/**
3132
* Initialize a JWTVerifier instance using the given Algorithm.
3233
*
33-
* @param algorithm the Algorithm to use on the DecodedJWT verification.
34+
* @param algorithm the Algorithm to use on the JWT verification.
3435
* @return a JWTVerifier.Verification instance to configure.
3536
* @throws IllegalArgumentException if the provided algorithm is null.
3637
*/
@@ -39,7 +40,7 @@ static JWTVerifier.Verification init(Algorithm algorithm) throws IllegalArgument
3940
}
4041

4142
/**
42-
* The Verification class holds the Claims required by a DecodedJWT to be valid.
43+
* The Verification class holds the Claims required by a JWT to be valid.
4344
*/
4445
public static class Verification {
4546
private final Algorithm algorithm;
@@ -154,7 +155,7 @@ public Verification acceptIssuedAt(long leeway) throws IllegalArgumentException
154155
}
155156

156157
/**
157-
* Require a specific DecodedJWT Id ("jti") claim.
158+
* Require a specific JWT Id ("jti") claim.
158159
*
159160
* @param jwtId the required Id value
160161
* @return this same Verification instance.
@@ -232,9 +233,9 @@ private void requireClaim(String name, Object value) {
232233
/**
233234
* Perform the verification against the given Token, using any previous configured options.
234235
*
235-
* @param token the String representation of the DecodedJWT.
236-
* @return a verified DecodedJWT.
237-
* @throws JWTVerificationException if any of the required contents inside the DecodedJWT is invalid.
236+
* @param token to verify.
237+
* @return a verified and decoded JWT.
238+
* @throws JWTVerificationException if any of the required contents inside the JWT is invalid.
238239
*/
239240
public DecodedJWT verify(String token) throws JWTVerificationException {
240241
DecodedJWT jwt = JWTDecoder.decode(token);
@@ -245,14 +246,14 @@ public DecodedJWT verify(String token) throws JWTVerificationException {
245246
}
246247

247248
private void verifySignature(String[] parts) throws SignatureVerificationException {
248-
byte[] content = String.format("%s.%s", parts[0], parts[1]).getBytes();
249+
byte[] content = String.format("%s.%s", parts[0], parts[1]).getBytes(StandardCharsets.UTF_8);
249250
byte[] signature = Base64.decodeBase64(parts[2]);
250251
algorithm.verify(content, signature);
251252
}
252253

253254
private void verifyAlgorithm(DecodedJWT jwt, Algorithm expectedAlgorithm) throws AlgorithmMismatchException {
254255
if (!expectedAlgorithm.getName().equals(jwt.getAlgorithm())) {
255-
throw new AlgorithmMismatchException("The provided Algorithm doesn't match the one defined in the DecodedJWT's Header.");
256+
throw new AlgorithmMismatchException("The provided Algorithm doesn't match the one defined in the JWT's Header.");
256257
}
257258
}
258259

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ protected Algorithm(String name, String description) {
160160
}
161161

162162
/**
163-
* Getter for the name of this Algorithm, as defined in the DecodedJWT Standard. i.e. "HS256"
163+
* Getter for the name of this Algorithm, as defined in the JWT Standard. i.e. "HS256"
164164
*
165165
* @return the algorithm name.
166166
*/

lib/src/main/java/com/auth0/jwt/interfaces/DecodedJWT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.auth0.jwt.interfaces;
22

33
/**
4-
* The DecodedJWT class represents a Json Web Token.
4+
* Class that represents a Json Web Token that was decoded from it's string representation.
55
*/
66
public interface DecodedJWT extends Payload, Header, Signature {
77
String getToken();

lib/src/main/java/com/auth0/jwt/interfaces/Header.java

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

33
/**
4-
* The Header class represents the 1st part of the DecodedJWT, where the Header value is hold.
4+
* The Header class represents the 1st part of the JWT, where the Header value is hold.
55
*/
66
public interface Header {
77

88
/**
9-
* Getter for the Algorithm "alg" claim defined in the DecodedJWT's Header. If the claim is missing it will return null.
9+
* Getter for the Algorithm "alg" claim defined in the JWT's Header. If the claim is missing it will return null.
1010
*
1111
* @return the Algorithm defined or null.
1212
*/
1313
String getAlgorithm();
1414

1515
/**
16-
* Getter for the Type "typ" claim defined in the DecodedJWT's Header. If the claim is missing it will return null.
16+
* Getter for the Type "typ" claim defined in the JWT's Header. If the claim is missing it will return null.
1717
*
1818
* @return the Type defined or null.
1919
*/
2020
String getType();
2121

2222
/**
23-
* Getter for the Content Type "cty" claim defined in the DecodedJWT's Header. If the claim is missing it will return null.
23+
* Getter for the Content Type "cty" claim defined in the JWT's Header. If the claim is missing it will return null.
2424
*
2525
* @return the Content Type defined or null.
2626
*/

lib/src/main/java/com/auth0/jwt/interfaces/JWTPartsParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import com.auth0.jwt.exceptions.JWTDecodeException;
44

55
/**
6-
* The JWTPartsParser class defines which parts of the DecodedJWT should be converted to it's specific Object representation instance.
6+
* The JWTPartsParser class defines which parts of the JWT should be converted to it's specific Object representation instance.
77
*/
88
public interface JWTPartsParser {
99

lib/src/main/java/com/auth0/jwt/interfaces/Payload.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import java.util.List;
55

66
/**
7-
* The Payload class represents the 2nd part of the DecodedJWT, where the Payload value is hold.
7+
* The Payload class represents the 2nd part of the JWT, where the Payload value is hold.
88
*/
99
public interface Payload {
1010

@@ -53,7 +53,7 @@ public interface Payload {
5353
/**
5454
* Get the value of the "jti" claim, or null if it's not available.
5555
*
56-
* @return the DecodedJWT ID value or null.
56+
* @return the JWT ID value or null.
5757
*/
5858
String getId();
5959

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package com.auth0.jwt.interfaces;
22

33
/**
4-
* The Signature class represents the 3rd part of the DecodedJWT, where the Signature value is hold.
4+
* The Signature class represents the 3rd part of the JWT, where the Signature value is hold.
55
*/
66
public interface Signature {
77

88
/**
9-
* Getter for the Signature contained in the DecodedJWT as a Base64 encoded String.
9+
* Getter for the Signature contained in the JWT as a Base64 encoded String.
1010
*
11-
* @return the Signature of the DecodedJWT.
11+
* @return the Signature of the JWT.
1212
*/
1313
String getSignature();
1414
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.junit.Test;
1212
import org.junit.rules.ExpectedException;
1313

14+
import java.nio.charset.StandardCharsets;
1415
import java.util.Date;
1516

1617
import static org.hamcrest.MatcherAssert.assertThat;
@@ -200,8 +201,8 @@ public void shouldGetNullClaimIfClaimValueIsNull() throws Exception {
200201
//Helper Methods
201202

202203
private DecodedJWT customJWT(String jsonHeader, String jsonPayload, String signature) {
203-
String header = Base64.encodeBase64URLSafeString(jsonHeader.getBytes());
204-
String body = Base64.encodeBase64URLSafeString(jsonPayload.getBytes());
204+
String header = Base64.encodeBase64URLSafeString(jsonHeader.getBytes(StandardCharsets.UTF_8));
205+
String body = Base64.encodeBase64URLSafeString(jsonPayload.getBytes(StandardCharsets.UTF_8));
205206
return JWTDecoder.decode(String.format("%s.%s.%s", header, body, signature));
206207
}
207208

0 commit comments

Comments
 (0)