Skip to content

Commit c557205

Browse files
Merge pull request #390 from IABTechLab/vse-UID2-5015-allow-jwt-validation-failures-when-not-enforcing-jwts
Allow JWT validation failures when not enforcing JWTs
2 parents fab9dee + 467a2b7 commit c557205

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ private static class AttestationHandler {
6565

6666
public void handle(RoutingContext rc) {
6767
boolean success = false;
68+
boolean isJwtValid = false;
6869

6970
final IAuthorizable profile = AuthMiddleware.getAuthClient(rc);
7071
if (profile instanceof OperatorKey) {
@@ -85,32 +86,37 @@ public void handle(RoutingContext rc) {
8586
if (jwt != null && !jwt.isBlank()) {
8687
try {
8788
JwtValidationResponse response = jwtService.validateJwt(jwt, this.jwtAudience, this.jwtIssuer);
88-
success = response.getIsValid();
89-
if (success) {
89+
isJwtValid = response.getIsValid();
90+
if (isJwtValid) {
9091
if (!this.roleBasedJwtClaimValidator.hasRequiredRoles(response)) {
91-
success = false;
92+
isJwtValid = false;
9293
LOGGER.info("JWT missing required role. Required roles: {}, JWT Presented Roles: {}, SiteId: {}, Name: {}, Contact: {}", this.roleBasedJwtClaimValidator.getRequiredRoles(), response.getRoles(), operatorKey.getSiteId(), operatorKey.getName(), operatorKey.getContact());
9394
}
9495

9596
String subject = calculateSubject(operatorKey);
9697
if (!validateSubject(response, subject)) {
97-
success = false;
98+
isJwtValid = false;
9899
LOGGER.info("JWT failed validation of Subject. JWT Presented Roles: {}, SiteId: {}, Name: {}, Contact: {}, JWT Subject: {}, Operator Subject: {}", response.getRoles(), operatorKey.getSiteId(), operatorKey.getName(), operatorKey.getContact(), response.getSubject(), subject);
99100
}
100101
}
101102
} catch (JwtService.ValidationException e) {
102103
LOGGER.info("Error validating JWT. Attestation validation failed. SiteId: {}, Name: {}, Contact: {}. Error: {}", operatorKey.getSiteId(), operatorKey.getName(), operatorKey.getContact(), e);
103-
success = false;
104104
}
105105
} else {
106106
if (this.enforceJwt) {
107107
LOGGER.info("JWT is required, but was not received. Attestation validation failed. SiteId: {}, Name: {}, Contact: {}", operatorKey.getSiteId(), operatorKey.getName(), operatorKey.getContact());
108-
success = false;
109108
}
110109
}
111110
}
112111
}
113112

113+
if (!isJwtValid && this.enforceJwt) {
114+
LOGGER.info("JWT validation has failed.");
115+
success = false;
116+
} else if (!isJwtValid && !this.enforceJwt) {
117+
LOGGER.info("JWT validation has failed, but JWTs are not being enforced.");
118+
}
119+
114120
if (success) {
115121
next.handle(rc);
116122
} else {

0 commit comments

Comments
 (0)