-
Notifications
You must be signed in to change notification settings - Fork 0
/
LollipopConsumerRequestValidationServiceImpl.java
267 lines (232 loc) · 11.4 KB
/
LollipopConsumerRequestValidationServiceImpl.java
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/* (C)2023 */
package it.pagopa.tech.lollipop.consumer.service.impl;
import com.nimbusds.jose.JOSEException;
import com.nimbusds.jose.jwk.JWK;
import com.nimbusds.jose.jwk.KeyType;
import it.pagopa.tech.lollipop.consumer.config.LollipopConsumerRequestConfig;
import it.pagopa.tech.lollipop.consumer.enumeration.AssertionRefAlgorithms;
import it.pagopa.tech.lollipop.consumer.enumeration.AssertionType;
import it.pagopa.tech.lollipop.consumer.enumeration.LollipopRequestMethod;
import it.pagopa.tech.lollipop.consumer.exception.LollipopRequestContentValidationException;
import it.pagopa.tech.lollipop.consumer.model.LollipopConsumerRequest;
import it.pagopa.tech.lollipop.consumer.service.LollipopConsumerRequestValidationService;
import java.text.ParseException;
import java.util.Base64;
import java.util.Map;
import java.util.logging.Level;
import java.util.regex.Pattern;
import lombok.extern.java.Log;
@Log
public class LollipopConsumerRequestValidationServiceImpl
implements LollipopConsumerRequestValidationService {
private final LollipopConsumerRequestConfig config;
public LollipopConsumerRequestValidationServiceImpl(LollipopConsumerRequestConfig config) {
this.config = config;
}
@Override
public void validateLollipopRequest(LollipopConsumerRequest request)
throws LollipopRequestContentValidationException {
Map<String, String> headerParams = request.getHeaderParams();
validatePublicKey(headerParams.get(this.config.getPublicKeyHeader()));
validateAssertionRefHeader(headerParams.get(this.config.getAssertionRefHeader()));
validateAssertionTypeHeader(headerParams.get(this.config.getAssertionTypeHeader()));
validateUserIdHeader(headerParams.get(this.config.getUserIdHeader()));
validateAuthJWTHeader(headerParams.get(this.config.getAuthJWTHeader()));
validateOriginalMethodHeader(headerParams.get(this.config.getOriginalMethodHeader()));
validateOriginalURLHeader(headerParams.get(this.config.getOriginalURLHeader()));
validateSignatureInputHeader(headerParams.get(this.config.getSignatureInputHeader()));
validateSignatureHeader(headerParams.get(this.config.getSignatureHeader()));
}
private void validatePublicKey(String publicKey)
throws LollipopRequestContentValidationException {
if (publicKey == null) {
throw new LollipopRequestContentValidationException(
LollipopRequestContentValidationException.ErrorCode.MISSING_PUBLIC_KEY,
"Missing Public Key Header");
}
try {
publicKey = new String(Base64.getDecoder().decode(publicKey.getBytes()));
} catch (Exception e) {
log.log(Level.FINE, "Key not in Base64");
}
if (isNotValidPublicKey(publicKey)) {
throw new LollipopRequestContentValidationException(
LollipopRequestContentValidationException.ErrorCode.INVALID_PUBLIC_KEY,
"Invalid Public Key Header value");
}
}
private boolean isNotValidPublicKey(String publicKey) {
try {
JWK jwk = JWK.parse(publicKey);
KeyType keyType = jwk.getKeyType();
if (KeyType.EC.equals(keyType)) {
jwk.toECKey().toECPublicKey();
} else if (KeyType.RSA.equals(keyType)) {
jwk.toRSAKey().toRSAPublicKey();
}
} catch (JOSEException | ParseException e) {
return true;
}
return false;
}
private void validateAssertionRefHeader(String assertionRef)
throws LollipopRequestContentValidationException {
if (assertionRef == null) {
throw new LollipopRequestContentValidationException(
LollipopRequestContentValidationException.ErrorCode.MISSING_ASSERTION_REF,
"Missing AssertionRef Header");
}
if (isNotValidAssertionRef(assertionRef)) {
throw new LollipopRequestContentValidationException(
LollipopRequestContentValidationException.ErrorCode.INVALID_ASSERTION_REF,
"Invalid AssertionRef Header value");
}
}
private boolean isNotValidAssertionRef(String signature) {
boolean matchesSHA256 =
AssertionRefAlgorithms.SHA256.getPattern().matcher(signature).matches();
boolean matchesSHA384 =
AssertionRefAlgorithms.SHA384.getPattern().matcher(signature).matches();
boolean matchesSHA512 =
AssertionRefAlgorithms.SHA512.getPattern().matcher(signature).matches();
return !matchesSHA256 && !matchesSHA384 && !matchesSHA512;
}
private void validateAssertionTypeHeader(String assertionType)
throws LollipopRequestContentValidationException {
if (assertionType == null) {
throw new LollipopRequestContentValidationException(
LollipopRequestContentValidationException.ErrorCode.MISSING_ASSERTION_TYPE,
"Missing Assertion Type Header");
}
if ((!isAssertionTypeSupported(assertionType))) {
throw new LollipopRequestContentValidationException(
LollipopRequestContentValidationException.ErrorCode.INVALID_ASSERTION_TYPE,
"Invalid Assertion Type Header value, type not supported");
}
}
private boolean isAssertionTypeSupported(String assertionType) {
for (AssertionType supportedType : AssertionType.values()) {
if (supportedType.name().equals(assertionType)) {
return true;
}
}
return false;
}
private void validateUserIdHeader(String userId)
throws LollipopRequestContentValidationException {
if (userId == null) {
throw new LollipopRequestContentValidationException(
LollipopRequestContentValidationException.ErrorCode.MISSING_USER_ID,
"Missing User Id Header");
}
if ((isNotValidFiscalCode(userId))) {
throw new LollipopRequestContentValidationException(
LollipopRequestContentValidationException.ErrorCode.INVALID_USER_ID,
"Invalid User Id Header value, type not supported");
}
}
private boolean isNotValidFiscalCode(String userId) {
return !Pattern.compile(
"^[A-Z]{6}[0-9LMNPQRSTUV]{2}[ABCDEHLMPRST][0-9LMNPQRSTUV]{2}[A-Z][0-9LMNPQRSTUV]{3}[A-Z]$")
.matcher(userId)
.matches();
}
private void validateAuthJWTHeader(String authJWT)
throws LollipopRequestContentValidationException {
if (authJWT == null) {
throw new LollipopRequestContentValidationException(
LollipopRequestContentValidationException.ErrorCode.MISSING_AUTH_JWT,
"Missing Auth JWT Header");
}
if (authJWT.isBlank()) {
throw new LollipopRequestContentValidationException(
LollipopRequestContentValidationException.ErrorCode.INVALID_AUTH_JWT,
"Invalid Auth JWT Header value, cannot be empty");
}
}
private void validateOriginalMethodHeader(String originalMethod)
throws LollipopRequestContentValidationException {
if (originalMethod == null) {
throw new LollipopRequestContentValidationException(
LollipopRequestContentValidationException.ErrorCode.MISSING_ORIGINAL_METHOD,
"Missing Original Method Header");
}
if (!isRequestMethodSupported(originalMethod)) {
throw new LollipopRequestContentValidationException(
LollipopRequestContentValidationException.ErrorCode.INVALID_ORIGINAL_METHOD,
"Invalid Original Method Header value, method not supported");
}
if (!originalMethod.equals(this.config.getExpectedFirstLcOriginalMethod())) {
String errMsg = String.format("Unexpected original method: %s", originalMethod);
throw new LollipopRequestContentValidationException(
LollipopRequestContentValidationException.ErrorCode.UNEXPECTED_ORIGINAL_METHOD,
errMsg);
}
}
private boolean isRequestMethodSupported(String originalMethod) {
for (LollipopRequestMethod supportedType : LollipopRequestMethod.values()) {
if (supportedType.name().equals(originalMethod)) {
return true;
}
}
return false;
}
private void validateOriginalURLHeader(String originalURL)
throws LollipopRequestContentValidationException {
if (originalURL == null) {
throw new LollipopRequestContentValidationException(
LollipopRequestContentValidationException.ErrorCode.MISSING_ORIGINAL_URL,
"Missing Original URL Header");
}
if (isNotValidOriginalURL(originalURL)) {
throw new LollipopRequestContentValidationException(
LollipopRequestContentValidationException.ErrorCode.INVALID_ORIGINAL_URL,
"Invalid Original URL Header value");
}
if (!originalURL.equals(this.config.getExpectedFirstLcOriginalUrl())) {
String errMsg = String.format("Unexpected original url: %s", originalURL);
throw new LollipopRequestContentValidationException(
LollipopRequestContentValidationException.ErrorCode.UNEXPECTED_ORIGINAL_URL,
errMsg);
}
}
private boolean isNotValidOriginalURL(String originalURL) {
return !Pattern.compile("^https://\\S+").matcher(originalURL).matches();
}
private void validateSignatureInputHeader(String signatureInput)
throws LollipopRequestContentValidationException {
if (signatureInput == null) {
throw new LollipopRequestContentValidationException(
LollipopRequestContentValidationException.ErrorCode.MISSING_SIGNATURE_INPUT,
"Missing Signature Input Header");
}
if (isNotValidSignatureInput(signatureInput)) {
throw new LollipopRequestContentValidationException(
LollipopRequestContentValidationException.ErrorCode.INVALID_SIGNATURE_INPUT,
"Invalid Signature Input Header value");
}
}
private boolean isNotValidSignatureInput(String signatureInput) {
return !Pattern.compile("^(((sig[\\d]++)=[^,]*+)(, ?+)?+)++$")
.matcher(signatureInput)
.matches();
}
private void validateSignatureHeader(String signature)
throws LollipopRequestContentValidationException {
if (signature == null) {
throw new LollipopRequestContentValidationException(
LollipopRequestContentValidationException.ErrorCode.MISSING_SIGNATURE,
"Missing Signature Header");
}
if (isNotValidSignature(signature)) {
throw new LollipopRequestContentValidationException(
LollipopRequestContentValidationException.ErrorCode.INVALID_SIGNATURE,
"Invalid Signature Header value");
}
}
private boolean isNotValidSignature(String signature) {
return !Pattern.compile("^((sig[\\d]+)=:[A-Za-z0-9+/=]*+:(, ?)?)+$")
.matcher(signature)
.matches();
}
}