|
1 | 1 | package com.auth0.jwt; |
2 | 2 |
|
3 | 3 | 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.*; |
9 | 5 | import com.auth0.jwt.impl.PublicClaims; |
10 | 6 | import com.auth0.jwt.interfaces.Claim; |
11 | 7 | import com.auth0.jwt.interfaces.Clock; |
12 | 8 | import com.auth0.jwt.interfaces.DecodedJWT; |
13 | 9 | import com.auth0.jwt.interfaces.Verification; |
14 | | -import org.apache.commons.codec.binary.Base64; |
15 | 10 |
|
16 | | -import java.nio.charset.StandardCharsets; |
17 | 11 | import java.util.*; |
18 | 12 |
|
19 | 13 | /** |
@@ -352,19 +346,13 @@ private void requireClaim(String name, Object value) { |
352 | 346 | * @throws JWTVerificationException if any of the required contents inside the JWT is invalid. |
353 | 347 | */ |
354 | 348 | public DecodedJWT verify(String token) throws JWTVerificationException { |
355 | | - DecodedJWT jwt = JWTDecoder.decode(token); |
| 349 | + DecodedJWT jwt = JWT.decode(token); |
356 | 350 | verifyAlgorithm(jwt, algorithm); |
357 | | - verifySignature(TokenUtils.splitToken(token)); |
| 351 | + algorithm.verify(jwt); |
358 | 352 | verifyClaims(jwt, claims); |
359 | 353 | return jwt; |
360 | 354 | } |
361 | 355 |
|
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 | | - |
368 | 356 | private void verifyAlgorithm(DecodedJWT jwt, Algorithm expectedAlgorithm) throws AlgorithmMismatchException { |
369 | 357 | if (!expectedAlgorithm.getName().equals(jwt.getAlgorithm())) { |
370 | 358 | 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 |
435 | 423 | } |
436 | 424 |
|
437 | 425 | 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 | + } |
463 | 448 |
|
464 | 449 | private void assertValidAudienceClaim(List<String> audience, List<String> value) { |
465 | 450 | if (audience == null || !audience.containsAll(value)) { |
|
0 commit comments