-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathAttestationMiddleware.java
More file actions
162 lines (137 loc) · 7.59 KB
/
AttestationMiddleware.java
File metadata and controls
162 lines (137 loc) · 7.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package com.uid2.shared.middleware;
import com.uid2.shared.Const;
import com.uid2.shared.Utils;
import com.uid2.shared.attest.IAttestationTokenService;
import com.uid2.shared.attest.JwtService;
import com.uid2.shared.attest.JwtValidationResponse;
import com.uid2.shared.attest.RoleBasedJwtClaimValidator;
import com.uid2.shared.auth.*;
import io.vertx.core.Handler;
import io.vertx.ext.web.RoutingContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.security.MessageDigest;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import static com.uid2.shared.Utils.createMessageDigestSHA512;
public class AttestationMiddleware {
private final IAttestationTokenService tokenService;
private final JwtService jwtService;
private final String jwtAudience;
private final String jwtIssuer;
private final boolean enforceJwt;
public AttestationMiddleware(IAttestationTokenService tokenService, JwtService jwtService, String jwtAudience, String jwtIssuer, boolean enforceJwt) {
this.tokenService = tokenService;
this.jwtService = jwtService;
this.jwtAudience = jwtAudience;
this.jwtIssuer = jwtIssuer;
this.enforceJwt = enforceJwt;
}
//region RequestHandler
public Handler<RoutingContext> handle(Handler<RoutingContext> handler, com.uid2.shared.auth.Role... roles) {
final RoleBasedJwtClaimValidator validator = new RoleBasedJwtClaimValidator(Collections.unmodifiableSet(new HashSet<>(Arrays.asList(roles))));
final AttestationHandler wrapper = new AttestationHandler(handler, this.tokenService, this.jwtService, this.jwtAudience, this.jwtIssuer, this.enforceJwt, validator);
return wrapper::handle;
}
private static class AttestationHandler {
private final static Logger LOGGER = LoggerFactory.getLogger(AttestationHandler.class);
private final Handler<RoutingContext> next;
private final IAttestationTokenService attestor;
private final JwtService jwtService;
private final String jwtAudience;
private final String jwtIssuer;
private final boolean enforceJwt;
private final RoleBasedJwtClaimValidator roleBasedJwtClaimValidator;
AttestationHandler(Handler<RoutingContext> next, IAttestationTokenService attestor, JwtService jwtService, String jwtAudience, String jwtIssuer, boolean enforceJwt, RoleBasedJwtClaimValidator roleBasedJwtClaimValidator) {
this.next = next;
this.attestor = attestor;
this.jwtService = jwtService;
this.jwtAudience = jwtAudience;
this.jwtIssuer = jwtIssuer;
this.enforceJwt = enforceJwt;
this.roleBasedJwtClaimValidator = roleBasedJwtClaimValidator;
}
public void handle(RoutingContext rc) {
boolean success = false;
boolean isJwtValid = false;
final IAuthorizable profile = AuthMiddleware.getAuthClient(rc);
if (profile instanceof OperatorKey) {
OperatorKey operatorKey = (OperatorKey) profile;
final String protocol = operatorKey.getProtocol();
final String userToken = AuthMiddleware.getAuthToken(rc);
final String jwt = getAttestationJWT(rc);
final String encryptedToken = getAttestationToken(rc);
if ("trusted".equals(protocol)) {
// (pre-)trusted operator requires no-attestation
success = true;
} else if (encryptedToken != null && userToken != null) {
success = attestor.validateToken(userToken, encryptedToken);
}
if (success) {
if (jwt != null && !jwt.isBlank()) {
try {
JwtValidationResponse response = jwtService.validateJwt(jwt, this.jwtAudience, this.jwtIssuer);
isJwtValid = response.getIsValid();
if (isJwtValid) {
if (!this.roleBasedJwtClaimValidator.hasRequiredRoles(response)) {
isJwtValid = false;
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());
}
String subject = calculateSubject(operatorKey);
if (!validateSubject(response, subject)) {
isJwtValid = false;
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);
}
}
} catch (JwtService.ValidationException e) {
LOGGER.info("Error validating JWT. Attestation validation failed. SiteId: {}, Name: {}, Contact: {}. Error: {}", operatorKey.getSiteId(), operatorKey.getName(), operatorKey.getContact(), e.toString());
}
} else {
if (this.enforceJwt) {
LOGGER.info("JWT is required, but was not received. Attestation validation failed. SiteId: {}, Name: {}, Contact: {}", operatorKey.getSiteId(), operatorKey.getName(), operatorKey.getContact());
} else {
LOGGER.info("JWT is {} but is not required. SiteId: {}, Name: {}, Url: {}", jwt == null ? "null" : "blank", operatorKey.getSiteId(), operatorKey.getName(), rc.request().uri());
}
}
}
}
if (success && !isJwtValid && this.enforceJwt) {
LOGGER.error("JWT validation has failed.");
success = false;
} else if (success && !isJwtValid && !this.enforceJwt) {
LOGGER.info("JWT validation has failed, but JWTs are not being enforced.");
}
if (success) {
next.handle(rc);
} else {
onFailedAttestation(rc);
}
}
private void onFailedAttestation(RoutingContext rc) {
rc.fail(401);
}
private String getAttestationToken(RoutingContext rc) {
return rc.request().getHeader(Const.Attestation.AttestationTokenHeader);
}
private String getAttestationJWT(RoutingContext rc) {
return rc.request().getHeader(Const.Attestation.AttestationJWTHeader);
}
private static String calculateSubject(OperatorKey operatorKey) {
if (operatorKey.getKeyHash() == null || operatorKey.getKeyHash().isBlank()) {
return "";
}
byte[] keyBytes = operatorKey.getKeyHash().getBytes();
MessageDigest md = createMessageDigestSHA512();
byte[] hashBytes = md.digest(keyBytes);
return Utils.toBase64String(hashBytes);
}
private static Boolean validateSubject(JwtValidationResponse response, String subject) {
if (response.getSubject() == null || response.getSubject().isBlank()) {
return false;
}
return subject.equals(response.getSubject());
}
}
//endregion RequestHandler
}