Skip to content

Commit 97cde74

Browse files
committed
refactor verify method signature
1 parent 351cda2 commit 97cde74

11 files changed

Lines changed: 96 additions & 81 deletions

File tree

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

Lines changed: 4 additions & 1 deletion
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

@@ -213,7 +214,9 @@ public JWT verify(String token) throws JWTDecodeException, JWTVerificationExcept
213214
}
214215

215216
private void verifySignature(String[] parts) throws SignatureVerificationException {
216-
algorithm.verify(parts);
217+
byte[] content = String.format("%s.%s", parts[0], parts[1]).getBytes();
218+
byte[] signature = Base64.decodeBase64(parts[2]);
219+
algorithm.verify(content, signature);
217220
}
218221

219222
private void verifyAlgorithm(JWT jwt, Algorithm expectedAlgorithm) throws AlgorithmMismatchException {

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,19 @@ public String toString() {
137137
}
138138

139139
/**
140-
* Verify the given JWT parts using this Algorithm instance.
140+
* Verify the given content using this Algorithm instance.
141141
*
142-
* @param jwtParts a valid array of size 3 representing the JWT parts.
143-
* @throws SignatureVerificationException if the Token's Signature is invalid.
142+
* @param contentBytes an array of bytes representing the base64 encoded content to be verified against the signature.
143+
* @param signatureBytes an array of bytes representing the base64 encoded signature to compare the content against.
144+
* @throws SignatureVerificationException if the Token's Signature is invalid, meaning that it doesn't match the signatureBytes, or if the Key is invalid.
144145
*/
145-
public abstract void verify(String[] jwtParts) throws SignatureVerificationException;
146+
public abstract void verify(byte[] contentBytes, byte[] signatureBytes) throws SignatureVerificationException;
146147

147-
public abstract byte[] sign(byte[] headerAndPayloadBytes) throws SignatureGenerationException;
148+
/**
149+
* Sign the given content using this Algorithm instance.
150+
*
151+
* @param contentBytes an array of bytes representing the base64 encoded content to be verified against the signature.
152+
* @throws SignatureGenerationException if the Key is invalid.
153+
*/
154+
public abstract byte[] sign(byte[] contentBytes) throws SignatureGenerationException;
148155
}

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

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

33
import com.auth0.jwt.exceptions.SignatureGenerationException;
44
import com.auth0.jwt.exceptions.SignatureVerificationException;
5-
import org.apache.commons.codec.binary.Base64;
65

7-
import java.security.*;
6+
import java.security.InvalidKeyException;
7+
import java.security.NoSuchAlgorithmException;
8+
import java.security.PrivateKey;
9+
import java.security.SignatureException;
810
import java.security.interfaces.ECKey;
911
import java.security.interfaces.ECPrivateKey;
1012
import java.security.interfaces.ECPublicKey;
@@ -34,17 +36,15 @@ ECKey getKey() {
3436
}
3537

3638
@Override
37-
public void verify(String[] jwtParts) throws SignatureVerificationException {
39+
public void verify(byte[] contentBytes, byte[] signatureBytes) throws SignatureVerificationException {
3840
if (!(key instanceof ECPublicKey)) {
3941
throw new IllegalArgumentException("The given ECKey is not an ECPublicKey.");
4042
}
4143
try {
42-
String content = String.format("%s.%s", jwtParts[0], jwtParts[1]);
43-
byte[] signature = Base64.decodeBase64(jwtParts[2]);
44-
if (!isDERSignature(signature)) {
45-
signature = JOSEToDER(signature);
44+
if (!isDERSignature(signatureBytes)) {
45+
signatureBytes = JOSEToDER(signatureBytes);
4646
}
47-
boolean valid = crypto.verifySignatureFor(getDescription(), (ECPublicKey) key, content.getBytes(), signature);
47+
boolean valid = crypto.verifySignatureFor(getDescription(), (ECPublicKey) key, contentBytes, signatureBytes);
4848

4949
if (!valid) {
5050
throw new SignatureVerificationException(this);
@@ -55,12 +55,12 @@ public void verify(String[] jwtParts) throws SignatureVerificationException {
5555
}
5656

5757
@Override
58-
public byte[] sign(byte[] headerAndPayloadBytes) throws SignatureGenerationException {
58+
public byte[] sign(byte[] contentBytes) throws SignatureGenerationException {
5959
try {
6060
if (!(key instanceof ECPrivateKey)) {
6161
throw new IllegalArgumentException("The given ECKey is not a ECPrivateKey.");
6262
}
63-
return crypto.createSignatureFor(getDescription(), (PrivateKey) key, headerAndPayloadBytes);
63+
return crypto.createSignatureFor(getDescription(), (PrivateKey) key, contentBytes);
6464
} catch (NoSuchAlgorithmException | SignatureException | InvalidKeyException | IllegalArgumentException e) {
6565
throw new SignatureGenerationException(this, e);
6666
}

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

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

33
import com.auth0.jwt.exceptions.SignatureGenerationException;
44
import com.auth0.jwt.exceptions.SignatureVerificationException;
5-
import org.apache.commons.codec.binary.Base64;
65

76
import java.security.InvalidKeyException;
87
import java.security.NoSuchAlgorithmException;
@@ -30,11 +29,9 @@ String getSecret() {
3029
}
3130

3231
@Override
33-
public void verify(String[] jwtParts) throws SignatureVerificationException {
32+
public void verify(byte[] contentBytes, byte[] signatureBytes) throws SignatureVerificationException {
3433
try {
35-
String message = String.format("%s.%s", jwtParts[0], jwtParts[1]);
36-
byte[] signature = Base64.decodeBase64(jwtParts[2]);
37-
boolean valid = crypto.verifyMacFor(getDescription(), secret.getBytes(), message.getBytes(), signature);
34+
boolean valid = crypto.verifyMacFor(getDescription(), secret.getBytes(), contentBytes, signatureBytes);
3835

3936
if (!valid) {
4037
throw new SignatureVerificationException(this);
@@ -45,9 +42,9 @@ public void verify(String[] jwtParts) throws SignatureVerificationException {
4542
}
4643

4744
@Override
48-
public byte[] sign(byte[] headerAndPayloadBytes) throws SignatureGenerationException {
45+
public byte[] sign(byte[] contentBytes) throws SignatureGenerationException {
4946
try {
50-
return crypto.createMacFor(getDescription(), secret.getBytes(), headerAndPayloadBytes);
47+
return crypto.createMacFor(getDescription(), secret.getBytes(), contentBytes);
5148
} catch (NoSuchAlgorithmException | InvalidKeyException e) {
5249
throw new SignatureGenerationException(this, e);
5350
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ class NoneAlgorithm extends Algorithm {
1010
}
1111

1212
@Override
13-
public void verify(String[] jwtParts) throws SignatureVerificationException {
14-
if (!jwtParts[2].isEmpty()) {
13+
public void verify(byte[] contentBytes, byte[] signatureBytes) throws SignatureVerificationException {
14+
if (signatureBytes.length > 0) {
1515
throw new SignatureVerificationException(this);
1616
}
1717
}
1818

1919
@Override
20-
public byte[] sign(byte[] headerAndPayloadBytes) throws SignatureGenerationException {
20+
public byte[] sign(byte[] contentBytes) throws SignatureGenerationException {
2121
return new byte[0];
2222
}
2323
}

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

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

33
import com.auth0.jwt.exceptions.SignatureGenerationException;
44
import com.auth0.jwt.exceptions.SignatureVerificationException;
5-
import org.apache.commons.codec.binary.Base64;
65

76
import java.security.*;
87
import java.security.interfaces.RSAKey;
@@ -32,14 +31,12 @@ RSAKey getKey() {
3231
}
3332

3433
@Override
35-
public void verify(String[] jwtParts) throws SignatureVerificationException {
34+
public void verify(byte[] contentBytes, byte[] signatureBytes) throws SignatureVerificationException {
3635
if (!(key instanceof PublicKey)) {
3736
throw new IllegalArgumentException("The given RSAKey is not a RSAPublicKey.");
3837
}
3938
try {
40-
String content = String.format("%s.%s", jwtParts[0], jwtParts[1]);
41-
byte[] signature = Base64.decodeBase64(jwtParts[2]);
42-
boolean valid = crypto.verifySignatureFor(getDescription(), (RSAPublicKey) key, content.getBytes(), signature);
39+
boolean valid = crypto.verifySignatureFor(getDescription(), (RSAPublicKey) key, contentBytes, signatureBytes);
4340

4441
if (!valid) {
4542
throw new SignatureVerificationException(this);
@@ -50,12 +47,12 @@ public void verify(String[] jwtParts) throws SignatureVerificationException {
5047
}
5148

5249
@Override
53-
public byte[] sign(byte[] headerAndPayloadBytes) throws SignatureGenerationException {
50+
public byte[] sign(byte[] contentBytes) throws SignatureGenerationException {
5451
try {
5552
if (!(key instanceof PrivateKey)) {
5653
throw new IllegalArgumentException("The given RSAKey is not a RSAPrivateKey.");
5754
}
58-
return crypto.createSignatureFor(getDescription(), (RSAPrivateKey) key, headerAndPayloadBytes);
55+
return crypto.createSignatureFor(getDescription(), (RSAPrivateKey) key, contentBytes);
5956
} catch (NoSuchAlgorithmException | SignatureException | InvalidKeyException | IllegalArgumentException e) {
6057
throw new SignatureGenerationException(this, e);
6158
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.auth0.jwt.algorithms;
2+
3+
import org.apache.commons.codec.binary.Base64;
4+
5+
class AlgorithmUtils {
6+
7+
static void verify(Algorithm algorithm, String jwt) {
8+
String[] parts = jwt.split("\\.");
9+
byte[] content = String.format("%s.%s", parts[0], parts[1]).getBytes();
10+
byte[] signature = new byte[0];
11+
if (parts.length == 3) {
12+
signature = Base64.decodeBase64(parts[2]);
13+
}
14+
algorithm.verify(content, signature);
15+
}
16+
}

0 commit comments

Comments
 (0)