Skip to content

Commit d45f972

Browse files
authored
Merge pull request auth0#242 from obecker/fix-issue-236-UnsupportedEncodingException
Refactored HMACAlgorithm so that it doesn't throw an UnsupportedEncodingException
2 parents 7dcea42 + f229898 commit d45f972

File tree

3 files changed

+6
-16
lines changed

3 files changed

+6
-16
lines changed

README.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,6 @@ try {
120120
String token = JWT.create()
121121
.withIssuer("auth0")
122122
.sign(algorithm);
123-
} catch (UnsupportedEncodingException exception){
124-
//UTF-8 encoding not supported
125123
} catch (JWTCreationException exception){
126124
//Invalid Signing configuration / Couldn't convert Claims.
127125
}
@@ -159,8 +157,6 @@ try {
159157
.withIssuer("auth0")
160158
.build(); //Reusable verifier instance
161159
DecodedJWT jwt = verifier.verify(token);
162-
} catch (UnsupportedEncodingException exception){
163-
//UTF-8 encoding not supported
164160
} catch (JWTVerificationException exception){
165161
//Invalid signature/claims
166162
}

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import com.auth0.jwt.interfaces.ECDSAKeyProvider;
77
import com.auth0.jwt.interfaces.RSAKeyProvider;
88

9-
import java.io.UnsupportedEncodingException;
109
import java.security.interfaces.*;
1110

1211
/**
@@ -138,9 +137,8 @@ public static Algorithm RSA512(RSAKey key) throws IllegalArgumentException {
138137
* @param secret the secret to use in the verify or signing instance.
139138
* @return a valid HMAC256 Algorithm.
140139
* @throws IllegalArgumentException if the provided Secret is null.
141-
* @throws UnsupportedEncodingException if the current Java platform implementation doesn't support the UTF-8 character encoding.
142140
*/
143-
public static Algorithm HMAC256(String secret) throws IllegalArgumentException, UnsupportedEncodingException {
141+
public static Algorithm HMAC256(String secret) throws IllegalArgumentException {
144142
return new HMACAlgorithm("HS256", "HmacSHA256", secret);
145143
}
146144

@@ -150,9 +148,8 @@ public static Algorithm HMAC256(String secret) throws IllegalArgumentException,
150148
* @param secret the secret to use in the verify or signing instance.
151149
* @return a valid HMAC384 Algorithm.
152150
* @throws IllegalArgumentException if the provided Secret is null.
153-
* @throws UnsupportedEncodingException if the current Java platform implementation doesn't support the UTF-8 character encoding.
154151
*/
155-
public static Algorithm HMAC384(String secret) throws IllegalArgumentException, UnsupportedEncodingException {
152+
public static Algorithm HMAC384(String secret) throws IllegalArgumentException {
156153
return new HMACAlgorithm("HS384", "HmacSHA384", secret);
157154
}
158155

@@ -162,9 +159,8 @@ public static Algorithm HMAC384(String secret) throws IllegalArgumentException,
162159
* @param secret the secret to use in the verify or signing instance.
163160
* @return a valid HMAC512 Algorithm.
164161
* @throws IllegalArgumentException if the provided Secret is null.
165-
* @throws UnsupportedEncodingException if the current Java platform implementation doesn't support the UTF-8 character encoding.
166162
*/
167-
public static Algorithm HMAC512(String secret) throws IllegalArgumentException, UnsupportedEncodingException {
163+
public static Algorithm HMAC512(String secret) throws IllegalArgumentException {
168164
return new HMACAlgorithm("HS512", "HmacSHA512", secret);
169165
}
170166

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33
import com.auth0.jwt.exceptions.SignatureGenerationException;
44
import com.auth0.jwt.exceptions.SignatureVerificationException;
55
import com.auth0.jwt.interfaces.DecodedJWT;
6-
import org.apache.commons.codec.CharEncoding;
76
import org.apache.commons.codec.binary.Base64;
87

9-
import java.io.UnsupportedEncodingException;
108
import java.nio.charset.StandardCharsets;
119
import java.security.InvalidKeyException;
1210
import java.security.NoSuchAlgorithmException;
@@ -30,16 +28,16 @@ class HMACAlgorithm extends Algorithm {
3028
this(new CryptoHelper(), id, algorithm, secretBytes);
3129
}
3230

33-
HMACAlgorithm(String id, String algorithm, String secret) throws IllegalArgumentException, UnsupportedEncodingException {
31+
HMACAlgorithm(String id, String algorithm, String secret) throws IllegalArgumentException {
3432
this(new CryptoHelper(), id, algorithm, getSecretBytes(secret));
3533
}
3634

3735
//Visible for testing
38-
static byte[] getSecretBytes(String secret) throws IllegalArgumentException, UnsupportedEncodingException {
36+
static byte[] getSecretBytes(String secret) throws IllegalArgumentException {
3937
if (secret == null) {
4038
throw new IllegalArgumentException("The Secret cannot be null");
4139
}
42-
return secret.getBytes(CharEncoding.UTF_8);
40+
return secret.getBytes(StandardCharsets.UTF_8);
4341
}
4442

4543
@Override

0 commit comments

Comments
 (0)