-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDPoPProofValidator.java
More file actions
207 lines (158 loc) · 7.55 KB
/
DPoPProofValidator.java
File metadata and controls
207 lines (158 loc) · 7.55 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package com.auth0;
import com.auth0.exception.BaseAuthException;
import com.auth0.exception.InvalidDpopProofException;
import com.auth0.exception.VerifyAccessTokenException;
import com.auth0.jwk.Jwk;
import com.auth0.jwk.JwkException;
import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.auth0.jwt.interfaces.JWTVerifier;
import com.auth0.models.AuthOptions;
import com.auth0.models.HttpRequestInfo;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.interfaces.ECPublicKey;
import java.time.Instant;
import java.util.*;
class DPoPProofValidator {
private final AuthOptions options;
private final ObjectMapper objectMapper = new ObjectMapper();;
DPoPProofValidator(AuthOptions options) {
this.options = options;
}
/**
* Validates the DPoP proof.
*
* @param dpopProof The raw DPoP JWT from the DPoP header.
* @param decodedJwtToken The access token being bound.
* @param requestInfo HTTP request info: method and URL
* @throws BaseAuthException if the DPoP proof is invalid.
*/
void validate(String dpopProof, DecodedJWT decodedJwtToken, HttpRequestInfo requestInfo)
throws BaseAuthException {
DecodedJWT proofJwt = decodeDPoP(dpopProof);
validateHeader(proofJwt);
validateSignatureAndTokenBinding(proofJwt, dpopProof, decodedJwtToken);
validateClaims(proofJwt, requestInfo);
}
DecodedJWT decodeDPoP(String dpopProof) throws InvalidDpopProofException {
try {
return JWT.decode(dpopProof);
} catch (Exception e) {
throw new InvalidDpopProofException("Failed to verify DPoP proof");
}
}
private void validateHeader(DecodedJWT proof) throws BaseAuthException {
if (!"dpop+jwt".equalsIgnoreCase(proof.getType())) {
throw new InvalidDpopProofException("Unexpected JWT 'typ' header parameter value");
}
if (!"ES256".equalsIgnoreCase(proof.getAlgorithm())) {
throw new InvalidDpopProofException("Unsupported algorithm in DPoP proof");
}
}
protected void validateSignatureAndTokenBinding(DecodedJWT dpopProof, String rawProof, DecodedJWT accessToken) throws BaseAuthException {
Map<String, Object> cnf = accessToken.getClaim("cnf").asMap();
if (cnf == null || cnf.get("jkt") == null) {
throw new VerifyAccessTokenException("JWT Access Token has no jkt confirmation claim");
}
Map<String, Object> jwkMap = dpopProof.getHeaderClaim("jwk").asMap();
if (jwkMap == null || jwkMap.isEmpty()) {
throw new InvalidDpopProofException("Missing or invalid jwk in header");
}
String expectedJkt = cnf.get("jkt").toString();
String thumbprint = calculateJwkThumbprint(jwkMap);
// Use constant-time comparison for thumbprint validation
if (!MessageDigest.isEqual(expectedJkt.getBytes(StandardCharsets.UTF_8), thumbprint.getBytes(StandardCharsets.UTF_8))) {
throw new InvalidDpopProofException("DPoP proof cnf.jkt mismatch");
}
String athClaim = dpopProof.getClaim("ath").asString();
if (athClaim == null || athClaim.isEmpty()) {
throw new InvalidDpopProofException("DPoP proof missing ath claim");
}
String accessTokenHash = sha256Base64Url(accessToken.getToken());
// Use constant-time comparison for access token hash validation
if (!MessageDigest.isEqual(athClaim.getBytes(StandardCharsets.UTF_8), accessTokenHash.getBytes(StandardCharsets.UTF_8))) {
throw new InvalidDpopProofException("DPoP Proof ath mismatch");
}
if (jwkMap.containsKey("d") || jwkMap.containsKey("p") || jwkMap.containsKey("q")) {
throw new InvalidDpopProofException("Private key material found in jwk header");
}
if (!"EC".equals(jwkMap.get("kty"))) {
throw new InvalidDpopProofException("Only EC keys are supported for DPoP");
}
if (!"P-256".equals(jwkMap.get("crv"))) {
throw new InvalidDpopProofException("Only P-256 curve is supported");
}
try {
ECPublicKey ecPublicKey = convertJwkToEcPublicKey(jwkMap);
Algorithm alg = Algorithm.ECDSA256(ecPublicKey, null);
JWTVerifier verifier = JWT.require(alg).build();
verifier.verify(rawProof);
} catch (Exception e) {
throw new InvalidDpopProofException("JWT signature verification failed");
}
}
private void validateClaims(DecodedJWT proof, HttpRequestInfo httpRequestInfo) throws BaseAuthException {
Instant iat = proof.getClaim("iat").asInstant();
String jti = proof.getClaim("jti").asString();
if (!httpRequestInfo.getHttpMethod().equalsIgnoreCase(proof.getClaim("htm").asString())) {
throw new InvalidDpopProofException("DPoP Proof htm mismatch");
}
if (!httpRequestInfo.getHttpUrl().equals(proof.getClaim("htu").asString())) {
throw new InvalidDpopProofException("DPoP Proof htu mismatch");
}
if (jti == null || jti.trim().isEmpty()) {
throw new InvalidDpopProofException("jti claim must not be empty");
}
Instant now = Instant.now();
Instant earliestAllowed = now.minusSeconds(options.getDpopIatOffsetSeconds());
Instant latestAllowed = now.plusSeconds(options.getDpopIatLeewaySeconds());
if (iat.isBefore(earliestAllowed)) {
throw new InvalidDpopProofException("DPoP Proof iat is too old");
}
if (iat.isAfter(latestAllowed)) {
throw new InvalidDpopProofException("DPoP Proof iat is from the future");
}
}
/**
* Compute SHA-256 hash and encode in Base64URL
*/
String sha256Base64Url(String value) throws BaseAuthException {
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] hash = digest.digest(value.getBytes(StandardCharsets.UTF_8));
return Base64.getUrlEncoder().withoutPadding().encodeToString(hash);
} catch (NoSuchAlgorithmException e) {
throw new InvalidDpopProofException("Failed to hash access token for DPoP binding", e);
}
}
/**
* Compute JWK thumbprint (RFC 7638)
*/
String calculateJwkThumbprint(Map<String, Object> jwk) throws BaseAuthException {
try {
// RFC 7638: keys in lexicographic order
Map<String, String> ordered = new TreeMap<>();
if (jwk.get("crv") == null || jwk.get("kty") == null ||
jwk.get("x") == null || jwk.get("y") == null) {
throw new InvalidDpopProofException("Malformed JWK: missing required fields");
}
ordered.put("crv", jwk.get("crv").toString());
ordered.put("kty", jwk.get("kty").toString());
ordered.put("x", jwk.get("x").toString());
ordered.put("y", jwk.get("y").toString());
String serialized = objectMapper.writeValueAsString(ordered);
return sha256Base64Url(serialized);
} catch (Exception e) {
throw new InvalidDpopProofException("Failed to compute JWK thumbprint");
}
}
static ECPublicKey convertJwkToEcPublicKey(Map<String, Object> jwkMap)
throws JwkException {
Jwk jwk = Jwk.fromValues(jwkMap);
return (ECPublicKey) jwk.getPublicKey();
}
}