Skip to content

Commit cf123a6

Browse files
committed
improve token parts handling. Remove Signature interface.
1 parent 72bceb9 commit cf123a6

File tree

4 files changed

+66
-55
lines changed

4 files changed

+66
-55
lines changed

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

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,12 @@
1919
@SuppressWarnings("WeakerAccess")
2020
final class JWTDecoder implements DecodedJWT {
2121

22-
private final String token;
23-
private Header header;
24-
private Payload payload;
25-
private String signature;
22+
private final String[] parts;
23+
private final Header header;
24+
private final Payload payload;
2625

2726
JWTDecoder(String jwt) throws JWTDecodeException {
28-
this.token = jwt;
29-
parseToken(jwt);
30-
}
31-
32-
private void parseToken(String token) throws JWTDecodeException {
33-
final String[] parts = TokenUtils.splitToken(token);
27+
parts = TokenUtils.splitToken(jwt);
3428
final JWTParser converter = new JWTParser();
3529
String headerJson;
3630
String payloadJson;
@@ -42,7 +36,6 @@ private void parseToken(String token) throws JWTDecodeException {
4236
}
4337
header = converter.parseHeader(headerJson);
4438
payload = converter.parsePayload(payloadJson);
45-
signature = parts[2];
4639
}
4740

4841
@Override
@@ -115,13 +108,23 @@ public Map<String, Claim> getClaims() {
115108
return payload.getClaims();
116109
}
117110

111+
@Override
112+
public String getHeader() {
113+
return parts[0];
114+
}
115+
116+
@Override
117+
public String getPayload() {
118+
return parts[1];
119+
}
120+
118121
@Override
119122
public String getSignature() {
120-
return signature;
123+
return parts[2];
121124
}
122125

123126
@Override
124127
public String getToken() {
125-
return token;
128+
return String.format("%s.%s.%s", parts[0], parts[1], parts[2]);
126129
}
127130
}

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

Lines changed: 25 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
package com.auth0.jwt;
22

33
import com.auth0.jwt.algorithms.Algorithm;
4-
import com.auth0.jwt.exceptions.AlgorithmMismatchException;
5-
import com.auth0.jwt.exceptions.InvalidClaimException;
6-
import com.auth0.jwt.exceptions.JWTVerificationException;
7-
import com.auth0.jwt.exceptions.SignatureVerificationException;
8-
import com.auth0.jwt.exceptions.TokenExpiredException;
4+
import com.auth0.jwt.exceptions.*;
95
import com.auth0.jwt.impl.PublicClaims;
106
import com.auth0.jwt.interfaces.Claim;
117
import com.auth0.jwt.interfaces.Clock;
128
import com.auth0.jwt.interfaces.DecodedJWT;
139
import com.auth0.jwt.interfaces.Verification;
14-
import org.apache.commons.codec.binary.Base64;
1510

16-
import java.nio.charset.StandardCharsets;
1711
import java.util.*;
1812

1913
/**
@@ -352,19 +346,13 @@ private void requireClaim(String name, Object value) {
352346
* @throws JWTVerificationException if any of the required contents inside the JWT is invalid.
353347
*/
354348
public DecodedJWT verify(String token) throws JWTVerificationException {
355-
DecodedJWT jwt = JWTDecoder.decode(token);
349+
DecodedJWT jwt = JWT.decode(token);
356350
verifyAlgorithm(jwt, algorithm);
357-
verifySignature(TokenUtils.splitToken(token));
351+
algorithm.verify(jwt);
358352
verifyClaims(jwt, claims);
359353
return jwt;
360354
}
361355

362-
private void verifySignature(String[] parts) throws SignatureVerificationException {
363-
byte[] content = String.format("%s.%s", parts[0], parts[1]).getBytes(StandardCharsets.UTF_8);
364-
byte[] signature = Base64.decodeBase64(parts[2]);
365-
algorithm.verify(content, signature);
366-
}
367-
368356
private void verifyAlgorithm(DecodedJWT jwt, Algorithm expectedAlgorithm) throws AlgorithmMismatchException {
369357
if (!expectedAlgorithm.getName().equals(jwt.getAlgorithm())) {
370358
throw new AlgorithmMismatchException("The provided Algorithm doesn't match the one defined in the JWT's Header.");
@@ -435,31 +423,28 @@ private void assertValidStringClaim(String claimName, String value, String expec
435423
}
436424

437425
private void assertValidDateClaim(Date date, long leeway, boolean shouldBeFuture) {
438-
Date today = clock.getToday();
439-
today.setTime((long) Math.floor((today.getTime() / 1000) * 1000)); // truncate
440-
// millis
441-
if (shouldBeFuture) {
442-
assertDateIsFuture(date, leeway, today);
443-
} else {
444-
assertDateIsPast(date, leeway, today);
445-
}
446-
}
447-
448-
private void assertDateIsFuture(Date date, long leeway, Date today) {
449-
450-
today.setTime(today.getTime() - leeway * 1000);
451-
if (date != null && today.after(date)) {
452-
throw new TokenExpiredException(String.format("The Token has expired on %s.", date));
453-
}
454-
}
455-
456-
private void assertDateIsPast(Date date, long leeway, Date today) {
457-
today.setTime(today.getTime() + leeway * 1000);
458-
if(date!=null && today.before(date)) {
459-
throw new InvalidClaimException(String.format("The Token can't be used before %s.", date));
460-
}
461-
462-
}
426+
Date today = clock.getToday();
427+
today.setTime((long) Math.floor((today.getTime() / 1000) * 1000)); // truncate millis
428+
if (shouldBeFuture) {
429+
assertDateIsFuture(date, leeway, today);
430+
} else {
431+
assertDateIsPast(date, leeway, today);
432+
}
433+
}
434+
435+
private void assertDateIsFuture(Date date, long leeway, Date today) {
436+
today.setTime(today.getTime() - leeway * 1000);
437+
if (date != null && today.after(date)) {
438+
throw new TokenExpiredException(String.format("The Token has expired on %s.", date));
439+
}
440+
}
441+
442+
private void assertDateIsPast(Date date, long leeway, Date today) {
443+
today.setTime(today.getTime() + leeway * 1000);
444+
if (date != null && today.before(date)) {
445+
throw new InvalidClaimException(String.format("The Token can't be used before %s.", date));
446+
}
447+
}
463448

464449
private void assertValidAudienceClaim(List<String> audience, List<String> value) {
465450
if (audience == null || !audience.containsAll(value)) {

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,35 @@
33
/**
44
* Class that represents a Json Web Token that was decoded from it's string representation.
55
*/
6-
public interface DecodedJWT extends Payload, Header, Signature {
6+
public interface DecodedJWT extends Payload, Header {
77
/**
88
* Getter for the String Token used to create this JWT instance.
99
*
1010
* @return the String Token.
1111
*/
1212
String getToken();
13+
14+
/**
15+
* Getter for the Header contained in the JWT as a Base64 encoded String.
16+
* This represents the first part of the token.
17+
*
18+
* @return the Header of the JWT.
19+
*/
20+
String getHeader();
21+
22+
/**
23+
* Getter for the Payload contained in the JWT as a Base64 encoded String.
24+
* This represents the second part of the token.
25+
*
26+
* @return the Payload of the JWT.
27+
*/
28+
String getPayload();
29+
30+
/**
31+
* Getter for the Signature contained in the JWT as a Base64 encoded String.
32+
* This represents the third part of the token.
33+
*
34+
* @return the Signature of the JWT.
35+
*/
36+
String getSignature();
1337
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ public interface Header {
2626
*/
2727
String getContentType();
2828

29-
3029
/**
3130
* Get the value of the "kid" claim, or null if it's not available.
3231
*

0 commit comments

Comments
 (0)