-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathAuthTokenValidationConfiguration.java
More file actions
222 lines (188 loc) · 9.42 KB
/
AuthTokenValidationConfiguration.java
File metadata and controls
222 lines (188 loc) · 9.42 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
* Copyright (c) 2020-2025 Estonian Information System Authority
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package eu.webeid.security.validator;
import eu.webeid.security.certificate.SubjectCertificatePolicies;
import eu.webeid.security.validator.ocsp.service.DesignatedOcspServiceConfiguration;
import eu.webeid.security.validator.ocsp.service.FallbackOcspServiceConfiguration;
import io.github.resilience4j.circuitbreaker.CircuitBreakerConfig;
import io.github.resilience4j.retry.RetryConfig;
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.security.cert.X509Certificate;
import java.time.Duration;
import java.util.Collection;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import static eu.webeid.security.util.Collections.newHashSet;
import static eu.webeid.security.util.DateAndTime.requirePositiveDuration;
/**
* Stores configuration parameters for {@link AuthTokenValidatorImpl}.
*/
public final class AuthTokenValidationConfiguration {
private URI siteOrigin;
private Collection<X509Certificate> trustedCACertificates = new HashSet<>();
private boolean isUserCertificateRevocationCheckWithOcspEnabled = true;
private Duration ocspRequestTimeout = Duration.ofSeconds(5);
private Duration allowedOcspResponseTimeSkew = Duration.ofMinutes(15);
private Duration maxOcspResponseThisUpdateAge = Duration.ofMinutes(2);
private boolean rejectUnknownOcspResponseStatus;
private DesignatedOcspServiceConfiguration designatedOcspServiceConfiguration;
private Collection<FallbackOcspServiceConfiguration> fallbackOcspServiceConfigurations = new HashSet<>();
private CircuitBreakerConfig circuitBreakerConfig;
private RetryConfig circuitBreakerRetryConfig;
// Don't allow Estonian Mobile-ID policy by default.
private Collection<ASN1ObjectIdentifier> disallowedSubjectCertificatePolicies = newHashSet(
SubjectCertificatePolicies.ESTEID_SK_2015_MOBILE_ID_POLICY_V1,
SubjectCertificatePolicies.ESTEID_SK_2015_MOBILE_ID_POLICY_V2,
SubjectCertificatePolicies.ESTEID_SK_2015_MOBILE_ID_POLICY_V3,
SubjectCertificatePolicies.ESTEID_SK_2015_MOBILE_ID_POLICY
);
private Collection<URI> nonceDisabledOcspUrls = new HashSet<>();
AuthTokenValidationConfiguration() {
}
private AuthTokenValidationConfiguration(AuthTokenValidationConfiguration other) {
this.siteOrigin = other.siteOrigin;
this.trustedCACertificates = Set.copyOf(other.trustedCACertificates);
this.isUserCertificateRevocationCheckWithOcspEnabled = other.isUserCertificateRevocationCheckWithOcspEnabled;
this.ocspRequestTimeout = other.ocspRequestTimeout;
this.allowedOcspResponseTimeSkew = other.allowedOcspResponseTimeSkew;
this.maxOcspResponseThisUpdateAge = other.maxOcspResponseThisUpdateAge;
this.rejectUnknownOcspResponseStatus = other.rejectUnknownOcspResponseStatus;
this.designatedOcspServiceConfiguration = other.designatedOcspServiceConfiguration;
this.fallbackOcspServiceConfigurations = Set.copyOf(other.fallbackOcspServiceConfigurations);
this.circuitBreakerConfig = other.circuitBreakerConfig;
this.circuitBreakerRetryConfig = other.circuitBreakerRetryConfig;
this.disallowedSubjectCertificatePolicies = Set.copyOf(other.disallowedSubjectCertificatePolicies);
this.nonceDisabledOcspUrls = Set.copyOf(other.nonceDisabledOcspUrls);
}
void setSiteOrigin(URI siteOrigin) {
this.siteOrigin = siteOrigin;
}
URI getSiteOrigin() {
return siteOrigin;
}
Collection<X509Certificate> getTrustedCACertificates() {
return trustedCACertificates;
}
boolean isUserCertificateRevocationCheckWithOcspEnabled() {
return isUserCertificateRevocationCheckWithOcspEnabled;
}
void setUserCertificateRevocationCheckWithOcspDisabled() {
isUserCertificateRevocationCheckWithOcspEnabled = false;
}
public Duration getOcspRequestTimeout() {
return ocspRequestTimeout;
}
void setOcspRequestTimeout(Duration ocspRequestTimeout) {
this.ocspRequestTimeout = ocspRequestTimeout;
}
public Duration getAllowedOcspResponseTimeSkew() {
return allowedOcspResponseTimeSkew;
}
public void setAllowedOcspResponseTimeSkew(Duration allowedOcspResponseTimeSkew) {
this.allowedOcspResponseTimeSkew = allowedOcspResponseTimeSkew;
}
public Duration getMaxOcspResponseThisUpdateAge() {
return maxOcspResponseThisUpdateAge;
}
public void setMaxOcspResponseThisUpdateAge(Duration maxOcspResponseThisUpdateAge) {
this.maxOcspResponseThisUpdateAge = maxOcspResponseThisUpdateAge;
}
public DesignatedOcspServiceConfiguration getDesignatedOcspServiceConfiguration() {
return designatedOcspServiceConfiguration;
}
public void setDesignatedOcspServiceConfiguration(DesignatedOcspServiceConfiguration designatedOcspServiceConfiguration) {
this.designatedOcspServiceConfiguration = designatedOcspServiceConfiguration;
}
public Collection<ASN1ObjectIdentifier> getDisallowedSubjectCertificatePolicies() {
return disallowedSubjectCertificatePolicies;
}
public Collection<URI> getNonceDisabledOcspUrls() {
return nonceDisabledOcspUrls;
}
public Collection<FallbackOcspServiceConfiguration> getFallbackOcspServiceConfigurations() {
return fallbackOcspServiceConfigurations;
}
public CircuitBreakerConfig getCircuitBreakerConfig() {
return circuitBreakerConfig;
}
public void setCircuitBreakerConfig(CircuitBreakerConfig circuitBreakerConfig) {
this.circuitBreakerConfig = circuitBreakerConfig;
}
public RetryConfig getCircuitBreakerRetryConfig() {
return circuitBreakerRetryConfig;
}
public void setCircuitBreakerRetryConfig(RetryConfig circuitBreakerRetryConfig) {
this.circuitBreakerRetryConfig = circuitBreakerRetryConfig;
}
public boolean isRejectUnknownOcspResponseStatus() {
return rejectUnknownOcspResponseStatus;
}
public void setRejectUnknownOcspResponseStatus(boolean rejectUnknownOcspResponseStatus) {
this.rejectUnknownOcspResponseStatus = rejectUnknownOcspResponseStatus;
}
/**
* Checks that the configuration parameters are valid.
*
* @throws NullPointerException when required parameters are null
* @throws IllegalArgumentException when any parameter is invalid
*/
void validate() {
Objects.requireNonNull(siteOrigin, "Origin URI must not be null");
validateIsOriginURL(siteOrigin);
if (trustedCACertificates.isEmpty()) {
throw new IllegalArgumentException("At least one trusted certificate authority must be provided");
}
requirePositiveDuration(ocspRequestTimeout, "OCSP request timeout");
requirePositiveDuration(allowedOcspResponseTimeSkew, "Allowed OCSP response time-skew");
requirePositiveDuration(maxOcspResponseThisUpdateAge, "Max OCSP response thisUpdate age");
// TODO: Add OCSP fallback/response validation
}
AuthTokenValidationConfiguration copy() {
return new AuthTokenValidationConfiguration(this);
}
/**
* Validates that the given URI is an origin URL as defined in <a href="https://developer.mozilla.org/en-US/docs/Web/API/Location/origin">MDN</a>,
* in the form of {@code <scheme> "://" <hostname> [ ":" <port> ]}.
*
* @param uri URI with origin URL
* @throws IllegalArgumentException when the URI is not in the form of origin URL
*/
public static void validateIsOriginURL(URI uri) throws IllegalArgumentException {
try {
// 1. Verify that the URI can be converted to absolute URL.
uri.toURL();
// 2. Verify that the URI contains only HTTPS scheme, host and optional port components.
if (!new URI("https", null, uri.getHost(), uri.getPort(), null, null, null)
.equals(uri)) {
throw new IllegalArgumentException("Origin URI must only contain the HTTPS scheme, host and optional port component");
}
} catch (MalformedURLException e) {
throw new IllegalArgumentException("Provided URI is not a valid URL");
} catch (URISyntaxException e) {
throw new IllegalArgumentException("An URI syntax exception occurred");
}
}
}