Skip to content

Commit c4f2094

Browse files
committed
chore cast and exception handling
1 parent bf7e7a7 commit c4f2094

5 files changed

Lines changed: 32 additions & 34 deletions

File tree

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

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
import com.auth0.jwt.exceptions.SignatureVerificationException;
55

66
import java.io.UnsupportedEncodingException;
7-
import java.security.PrivateKey;
8-
import java.security.PublicKey;
97
import java.security.interfaces.*;
108

119
/**
@@ -27,8 +25,8 @@ public abstract class Algorithm {
2725
*/
2826
@Deprecated
2927
public static Algorithm RSA256(RSAKey key) throws IllegalArgumentException {
30-
RSAPublicKey publicKey = key instanceof PublicKey ? (RSAPublicKey) key : null;
31-
RSAPrivateKey privateKey = key instanceof PrivateKey ? (RSAPrivateKey) key : null;
28+
RSAPublicKey publicKey = key instanceof RSAPublicKey ? (RSAPublicKey) key : null;
29+
RSAPrivateKey privateKey = key instanceof RSAPrivateKey ? (RSAPrivateKey) key : null;
3230
return RSA256(publicKey, privateKey);
3331
}
3432

@@ -42,8 +40,8 @@ public static Algorithm RSA256(RSAKey key) throws IllegalArgumentException {
4240
*/
4341
@Deprecated
4442
public static Algorithm RSA384(RSAKey key) throws IllegalArgumentException {
45-
RSAPublicKey publicKey = key instanceof PublicKey ? (RSAPublicKey) key : null;
46-
RSAPrivateKey privateKey = key instanceof PrivateKey ? (RSAPrivateKey) key : null;
43+
RSAPublicKey publicKey = key instanceof RSAPublicKey ? (RSAPublicKey) key : null;
44+
RSAPrivateKey privateKey = key instanceof RSAPrivateKey ? (RSAPrivateKey) key : null;
4745
return RSA384(publicKey, privateKey);
4846
}
4947

@@ -57,8 +55,8 @@ public static Algorithm RSA384(RSAKey key) throws IllegalArgumentException {
5755
*/
5856
@Deprecated
5957
public static Algorithm RSA512(RSAKey key) throws IllegalArgumentException {
60-
RSAPublicKey publicKey = key instanceof PublicKey ? (RSAPublicKey) key : null;
61-
RSAPrivateKey privateKey = key instanceof PrivateKey ? (RSAPrivateKey) key : null;
58+
RSAPublicKey publicKey = key instanceof RSAPublicKey ? (RSAPublicKey) key : null;
59+
RSAPrivateKey privateKey = key instanceof RSAPrivateKey ? (RSAPrivateKey) key : null;
6260
return RSA512(publicKey, privateKey);
6361
}
6462

@@ -177,8 +175,8 @@ public static Algorithm HMAC512(byte[] secret) throws IllegalArgumentException {
177175
*/
178176
@Deprecated
179177
public static Algorithm ECDSA256(ECKey key) throws IllegalArgumentException {
180-
ECPublicKey publicKey = key instanceof PublicKey ? (ECPublicKey) key : null;
181-
ECPrivateKey privateKey = key instanceof PrivateKey ? (ECPrivateKey) key : null;
178+
ECPublicKey publicKey = key instanceof ECPublicKey ? (ECPublicKey) key : null;
179+
ECPrivateKey privateKey = key instanceof ECPrivateKey ? (ECPrivateKey) key : null;
182180
return ECDSA256(publicKey, privateKey);
183181
}
184182

@@ -192,8 +190,8 @@ public static Algorithm ECDSA256(ECKey key) throws IllegalArgumentException {
192190
*/
193191
@Deprecated
194192
public static Algorithm ECDSA384(ECKey key) throws IllegalArgumentException {
195-
ECPublicKey publicKey = key instanceof PublicKey ? (ECPublicKey) key : null;
196-
ECPrivateKey privateKey = key instanceof PrivateKey ? (ECPrivateKey) key : null;
193+
ECPublicKey publicKey = key instanceof ECPublicKey ? (ECPublicKey) key : null;
194+
ECPrivateKey privateKey = key instanceof ECPrivateKey ? (ECPrivateKey) key : null;
197195
return ECDSA384(publicKey, privateKey);
198196
}
199197

@@ -207,8 +205,8 @@ public static Algorithm ECDSA384(ECKey key) throws IllegalArgumentException {
207205
*/
208206
@Deprecated
209207
public static Algorithm ECDSA512(ECKey key) throws IllegalArgumentException {
210-
ECPublicKey publicKey = key instanceof PublicKey ? (ECPublicKey) key : null;
211-
ECPrivateKey privateKey = key instanceof PrivateKey ? (ECPrivateKey) key : null;
208+
ECPublicKey publicKey = key instanceof ECPublicKey ? (ECPublicKey) key : null;
209+
ECPrivateKey privateKey = key instanceof ECPrivateKey ? (ECPrivateKey) key : null;
212210
return ECDSA512(publicKey, privateKey);
213211
}
214212

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ ECPrivateKey getPrivateKey() {
4444
public void verify(byte[] contentBytes, byte[] signatureBytes) throws SignatureVerificationException {
4545
try {
4646
if (publicKey == null) {
47-
throw new IllegalArgumentException("The given Public Key is null.");
47+
throw new IllegalStateException("The given Public Key is null.");
4848
}
4949
if (!isDERSignature(signatureBytes)) {
5050
signatureBytes = JOSEToDER(signatureBytes);
@@ -54,7 +54,7 @@ public void verify(byte[] contentBytes, byte[] signatureBytes) throws SignatureV
5454
if (!valid) {
5555
throw new SignatureVerificationException(this);
5656
}
57-
} catch (NoSuchAlgorithmException | SignatureException | InvalidKeyException | IllegalArgumentException e) {
57+
} catch (NoSuchAlgorithmException | SignatureException | InvalidKeyException | IllegalStateException e) {
5858
throw new SignatureVerificationException(this, e);
5959
}
6060
}
@@ -63,10 +63,10 @@ public void verify(byte[] contentBytes, byte[] signatureBytes) throws SignatureV
6363
public byte[] sign(byte[] contentBytes) throws SignatureGenerationException {
6464
try {
6565
if (privateKey == null) {
66-
throw new IllegalArgumentException("The given Private Key is null.");
66+
throw new IllegalStateException("The given Private Key is null.");
6767
}
6868
return crypto.createSignatureFor(getDescription(), privateKey, contentBytes);
69-
} catch (NoSuchAlgorithmException | SignatureException | InvalidKeyException | IllegalArgumentException e) {
69+
} catch (NoSuchAlgorithmException | SignatureException | InvalidKeyException | IllegalStateException e) {
7070
throw new SignatureGenerationException(this, e);
7171
}
7272
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ RSAPrivateKey getPrivateKey() {
4242
public void verify(byte[] contentBytes, byte[] signatureBytes) throws SignatureVerificationException {
4343
try {
4444
if (publicKey == null) {
45-
throw new IllegalArgumentException("The given Public Key is null.");
45+
throw new IllegalStateException("The given Public Key is null.");
4646
}
4747
boolean valid = crypto.verifySignatureFor(getDescription(), publicKey, contentBytes, signatureBytes);
4848
if (!valid) {
4949
throw new SignatureVerificationException(this);
5050
}
51-
} catch (NoSuchAlgorithmException | SignatureException | InvalidKeyException | IllegalArgumentException e) {
51+
} catch (NoSuchAlgorithmException | SignatureException | InvalidKeyException | IllegalStateException e) {
5252
throw new SignatureVerificationException(this, e);
5353
}
5454
}
@@ -57,10 +57,10 @@ public void verify(byte[] contentBytes, byte[] signatureBytes) throws SignatureV
5757
public byte[] sign(byte[] contentBytes) throws SignatureGenerationException {
5858
try {
5959
if (privateKey == null) {
60-
throw new IllegalArgumentException("The given Private Key is null.");
60+
throw new IllegalStateException("The given Private Key is null.");
6161
}
6262
return crypto.createSignatureFor(getDescription(), privateKey, contentBytes);
63-
} catch (NoSuchAlgorithmException | SignatureException | InvalidKeyException | IllegalArgumentException e) {
63+
} catch (NoSuchAlgorithmException | SignatureException | InvalidKeyException | IllegalStateException e) {
6464
throw new SignatureGenerationException(this, e);
6565
}
6666
}

lib/src/test/java/com/auth0/jwt/algorithms/ECDSAAlgorithmTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public void shouldFailECDSA256VerificationWithInvalidPublicKey() throws Exceptio
8888
public void shouldFailECDSA256VerificationWhenUsingPrivateKey() throws Exception {
8989
exception.expect(SignatureVerificationException.class);
9090
exception.expectMessage("The Token's Signature resulted invalid when verified using the Algorithm: SHA256withECDSA");
91-
exception.expectCause(isA(IllegalArgumentException.class));
91+
exception.expectCause(isA(IllegalStateException.class));
9292
exception.expectCause(hasMessage(is("The given Public Key is null.")));
9393
String jwt = "eyJhbGciOiJFUzI1NiJ9.eyJpc3MiOiJhdXRoMCJ9.W9qfN1b80B9hnMo49WL8THrOsf1vEjOhapeFemPMGySzxTcgfyudS5esgeBTO908X5SLdAr5jMwPUPBs9b6nNg";
9494
Algorithm algorithm = Algorithm.ECDSA256((ECKey) readPrivateKeyFromFile(PRIVATE_KEY_FILE_256, "EC"));
@@ -180,7 +180,7 @@ public void shouldFailECDSA384VerificationWithInvalidPublicKey() throws Exceptio
180180
public void shouldFailECDSA384VerificationWhenUsingPrivateKey() throws Exception {
181181
exception.expect(SignatureVerificationException.class);
182182
exception.expectMessage("The Token's Signature resulted invalid when verified using the Algorithm: SHA384withECDSA");
183-
exception.expectCause(isA(IllegalArgumentException.class));
183+
exception.expectCause(isA(IllegalStateException.class));
184184
exception.expectCause(hasMessage(is("The given Public Key is null.")));
185185
String jwt = "eyJhbGciOiJFUzM4NCJ9.eyJpc3MiOiJhdXRoMCJ9._k5h1KyO-NE0R2_HAw0-XEc0bGT5atv29SxHhOGC9JDqUHeUdptfCK_ljQ01nLVt2OQWT2SwGs-TuyHDFmhPmPGFZ9wboxvq_ieopmYqhQilNAu-WF-frioiRz9733fU";
186186
Algorithm algorithm = Algorithm.ECDSA384((ECKey) readPrivateKeyFromFile(PRIVATE_KEY_FILE_384, "EC"));
@@ -272,7 +272,7 @@ public void shouldFailECDSA512VerificationWithInvalidPublicKey() throws Exceptio
272272
public void shouldFailECDSA512VerificationWhenUsingPrivateKey() throws Exception {
273273
exception.expect(SignatureVerificationException.class);
274274
exception.expectMessage("The Token's Signature resulted invalid when verified using the Algorithm: SHA512withECDSA");
275-
exception.expectCause(isA(IllegalArgumentException.class));
275+
exception.expectCause(isA(IllegalStateException.class));
276276
exception.expectCause(hasMessage(is("The given Public Key is null.")));
277277
String jwt = "eyJhbGciOiJFUzUxMiJ9.eyJpc3MiOiJhdXRoMCJ9.AZgdopFFsN0amCSs2kOucXdpylD31DEm5ChK1PG0_gq5Mf47MrvVph8zHSVuvcrXzcE1U3VxeCg89mYW1H33Y-8iAF0QFkdfTUQIWKNObH543WNMYYssv3OtOj0znPv8atDbaF8DMYAtcT1qdmaSJRhx-egRE9HGZkinPh9CfLLLt58X";
278278
Algorithm algorithm = Algorithm.ECDSA512((ECKey) readPrivateKeyFromFile(PRIVATE_KEY_FILE_512, "EC"));
@@ -421,7 +421,7 @@ public void shouldDoECDSA256SigningWithBothKeys() throws Exception {
421421
public void shouldFailOnECDSA256SigningWhenUsingPublicKey() throws Exception {
422422
exception.expect(SignatureGenerationException.class);
423423
exception.expectMessage("The Token's Signature couldn't be generated when signing using the Algorithm: SHA256withECDSA");
424-
exception.expectCause(isA(IllegalArgumentException.class));
424+
exception.expectCause(isA(IllegalStateException.class));
425425
exception.expectCause(hasMessage(is("The given Private Key is null.")));
426426

427427
Algorithm algorithm = Algorithm.ECDSA256((ECKey) readPublicKeyFromFile(PUBLIC_KEY_FILE_256, "EC"));
@@ -453,7 +453,7 @@ public void shouldDoECDSA384SigningWithBothKeys() throws Exception {
453453
public void shouldFailOnECDSA384SigningWhenUsingPublicKey() throws Exception {
454454
exception.expect(SignatureGenerationException.class);
455455
exception.expectMessage("The Token's Signature couldn't be generated when signing using the Algorithm: SHA384withECDSA");
456-
exception.expectCause(isA(IllegalArgumentException.class));
456+
exception.expectCause(isA(IllegalStateException.class));
457457
exception.expectCause(hasMessage(is("The given Private Key is null.")));
458458

459459
Algorithm algorithm = Algorithm.ECDSA384((ECKey) readPublicKeyFromFile(PUBLIC_KEY_FILE_384, "EC"));
@@ -485,7 +485,7 @@ public void shouldDoECDSA512SigningWithBothKeys() throws Exception {
485485
public void shouldFailOnECDSA512SigningWhenUsingPublicKey() throws Exception {
486486
exception.expect(SignatureGenerationException.class);
487487
exception.expectMessage("The Token's Signature couldn't be generated when signing using the Algorithm: SHA512withECDSA");
488-
exception.expectCause(isA(IllegalArgumentException.class));
488+
exception.expectCause(isA(IllegalStateException.class));
489489
exception.expectCause(hasMessage(is("The given Private Key is null.")));
490490

491491
Algorithm algorithm = Algorithm.ECDSA512((ECKey) readPublicKeyFromFile(PUBLIC_KEY_FILE_512, "EC"));

lib/src/test/java/com/auth0/jwt/algorithms/RSAAlgorithmTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public void shouldFailRSA256VerificationWithInvalidPublicKey() throws Exception
6161
public void shouldFailRSA256VerificationWhenUsingPrivateKey() throws Exception {
6262
exception.expect(SignatureVerificationException.class);
6363
exception.expectMessage("The Token's Signature resulted invalid when verified using the Algorithm: SHA256withRSA");
64-
exception.expectCause(isA(IllegalArgumentException.class));
64+
exception.expectCause(isA(IllegalStateException.class));
6565
exception.expectCause(hasMessage(is("The given Public Key is null.")));
6666
String jwt = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJhdXRoMCJ9.dxXF3MdsyW-AuvwJpaQtrZ33fAde9xWxpLIg9cO2tMLH2GSRNuLAe61KsJusZhqZB9Iy7DvflcmRz-9OZndm6cj_ThGeJH2LLc90K83UEvvRPo8l85RrQb8PcanxCgIs2RcZOLygERizB3pr5icGkzR7R2y6zgNCjKJ5_NJ6EiZsGN6_nc2PRK_DbyY-Wn0QDxIxKoA5YgQJ9qafe7IN980pXvQv2Z62c3XR8dYuaXBqhthBj-AbaFHEpZapN-V-TmuLNzR2MCB6Xr7BYMuCaqWf_XU8og4XNe8f_8w9Wv5vvgqMM1KhqVpG5VdMJv4o_L4NoCROHhtUQSLRh2M9cA";
6767
Algorithm algorithm = Algorithm.RSA256((RSAKey) readPrivateKeyFromFile(PRIVATE_KEY_FILE, "RSA"));
@@ -95,7 +95,7 @@ public void shouldFailRSA384VerificationWithInvalidPublicKey() throws Exception
9595
public void shouldFailRSA384VerificationWhenUsingPrivateKey() throws Exception {
9696
exception.expect(SignatureVerificationException.class);
9797
exception.expectMessage("The Token's Signature resulted invalid when verified using the Algorithm: SHA384withRSA");
98-
exception.expectCause(isA(IllegalArgumentException.class));
98+
exception.expectCause(isA(IllegalStateException.class));
9999
exception.expectCause(hasMessage(is("The given Public Key is null.")));
100100
String jwt = "eyJhbGciOiJSUzM4NCIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJhdXRoMCJ9.TZlWjXObwGSQOiu2oMq8kiKz0_BR7bbBddNL6G8eZ_GoR82BXOZDqNrQr7lb_M-78XGBguWLWNIdYhzgxOUL9EoCJlrqVm9s9vo6G8T1sj1op-4TbjXZ61TwIvrJee9BvPLdKUJ9_fp1Js5kl6yXkst40Th8Auc5as4n49MLkipjpEhKDKaENKHpSubs1ripSz8SCQZSofeTM_EWVwSw7cpiM8Fy8jOPvWG8Xz4-e3ODFowvHVsDcONX_4FTMNbeRqDuHq2ZhCJnEfzcSJdrve_5VD5fM1LperBVslTrOxIgClOJ3RmM7-WnaizJrWP3D6Z9OLxPxLhM6-jx6tcxEw";
101101
Algorithm algorithm = Algorithm.RSA384((RSAKey) readPrivateKeyFromFile(PRIVATE_KEY_FILE, "RSA"));
@@ -129,7 +129,7 @@ public void shouldFailRSA512VerificationWithInvalidPublicKey() throws Exception
129129
public void shouldFailRSA512VerificationWhenUsingPrivateKey() throws Exception {
130130
exception.expect(SignatureVerificationException.class);
131131
exception.expectMessage("The Token's Signature resulted invalid when verified using the Algorithm: SHA512withRSA");
132-
exception.expectCause(isA(IllegalArgumentException.class));
132+
exception.expectCause(isA(IllegalStateException.class));
133133
exception.expectCause(hasMessage(is("The given Public Key is null.")));
134134
String jwt = "eyJhbGciOiJSUzUxMiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJhdXRoMCJ9.mvL5LoMyIrWYjk5umEXZTmbyIrkbbcVPUkvdGZbu0qFBxGOf0nXP5PZBvPcOu084lvpwVox5n3VaD4iqzW-PsJyvKFgi5TnwmsbKchAp7JexQEsQOnTSGcfRqeUUiBZqRQdYsho71oAB3T4FnalDdFEpM-fztcZY9XqKyayqZLreTeBjqJm4jfOWH7KfGBHgZExQhe96NLq1UA9eUyQwdOA1Z0SgXe4Ja5PxZ6Fm37KnVDtDlNnY4JAAGFo6y74aGNnp_BKgpaVJCGFu1f1S5xCQ1HSvs8ZSdVWs5NgawW3wRd0kRt_GJ_Y3mIwiF4qUyHWGtsSHu_qjVdCTtbFyow";
135135
Algorithm algorithm = Algorithm.RSA512((RSAKey) readPrivateKeyFromFile(PRIVATE_KEY_FILE, "RSA"));
@@ -227,7 +227,7 @@ public void shouldDoRSA256SigningWithBothKeys() throws Exception {
227227
public void shouldFailOnRSA256SigningWhenUsingPublicKey() throws Exception {
228228
exception.expect(SignatureGenerationException.class);
229229
exception.expectMessage("The Token's Signature couldn't be generated when signing using the Algorithm: SHA256withRSA");
230-
exception.expectCause(isA(IllegalArgumentException.class));
230+
exception.expectCause(isA(IllegalStateException.class));
231231
exception.expectCause(hasMessage(is("The given Private Key is null.")));
232232

233233
Algorithm algorithm = Algorithm.RSA256((RSAKey) readPublicKeyFromFile(PUBLIC_KEY_FILE, "RSA"));
@@ -267,7 +267,7 @@ public void shouldDoRSA384SigningWithBothKeys() throws Exception {
267267
public void shouldFailOnRSA384SigningWhenUsingPublicKey() throws Exception {
268268
exception.expect(SignatureGenerationException.class);
269269
exception.expectMessage("The Token's Signature couldn't be generated when signing using the Algorithm: SHA384withRSA");
270-
exception.expectCause(isA(IllegalArgumentException.class));
270+
exception.expectCause(isA(IllegalStateException.class));
271271
exception.expectCause(hasMessage(is("The given Private Key is null.")));
272272

273273
Algorithm algorithm = Algorithm.RSA384((RSAKey) readPublicKeyFromFile(PUBLIC_KEY_FILE, "RSA"));
@@ -307,7 +307,7 @@ public void shouldDoRSA512SigningWithBothKeys() throws Exception {
307307
public void shouldFailOnRSA512SigningWhenUsingPublicKey() throws Exception {
308308
exception.expect(SignatureGenerationException.class);
309309
exception.expectMessage("The Token's Signature couldn't be generated when signing using the Algorithm: SHA512withRSA");
310-
exception.expectCause(isA(IllegalArgumentException.class));
310+
exception.expectCause(isA(IllegalStateException.class));
311311
exception.expectCause(hasMessage(is("The given Private Key is null.")));
312312

313313
Algorithm algorithm = Algorithm.RSA512((RSAKey) readPublicKeyFromFile(PUBLIC_KEY_FILE, "RSA"));

0 commit comments

Comments
 (0)