Skip to content

Commit 95929e0

Browse files
Merge pull request #427 from IABTechLab/sch-UID2-5355-fix-jwt-token-validation
sch-UID2-5355 cleaning up logs for JWT validation
2 parents c51dfbf + 6fd055b commit 95929e0

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

src/main/java/com/uid2/shared/attest/JwtService.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ public JwtValidationResponse validateJwt(String jwt, String audience, String iss
6060
throw new ValidationException(Optional.of("Unable to get public keys. Validation can not continue"));
6161
}
6262

63+
Exception lastException = null;
64+
6365
for (PublicKey key : this.publicKeys) {
6466
var tokenVerifier = TokenVerifier.newBuilder()
6567
.setPublicKey(key)
@@ -83,10 +85,14 @@ public JwtValidationResponse validateJwt(String jwt, String audience, String iss
8385
// return the first verified response
8486
return response;
8587
} catch (Exception e) {
86-
LOGGER.error("Error validating JWT", e);
87-
throw new ValidationException(Optional.ofNullable(e.getMessage()));
88+
lastException = e;
8889
}
8990
}
91+
92+
if (!response.getIsValid()) {
93+
throw new ValidationException(Optional.ofNullable(lastException.getMessage()), lastException);
94+
}
95+
9096
return response;
9197
}
9298

src/main/java/com/uid2/shared/middleware/AttestationMiddleware.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,10 @@ public void handle(RoutingContext rc) {
110110
}
111111
}
112112

113-
if (!isJwtValid && this.enforceJwt) {
113+
if (success && !isJwtValid && this.enforceJwt) {
114114
LOGGER.info("JWT validation has failed.");
115115
success = false;
116-
} else if (!isJwtValid && !this.enforceJwt) {
116+
} else if (success && !isJwtValid && !this.enforceJwt) {
117117
LOGGER.info("JWT validation has failed, but JWTs are not being enforced.");
118118
}
119119

0 commit comments

Comments
 (0)