-
Notifications
You must be signed in to change notification settings - Fork 3
/
BankIdConfigurationProperties.java
436 lines (378 loc) · 13.7 KB
/
BankIdConfigurationProperties.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/*
* Copyright 2023-2024 Sweden Connect
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package se.swedenconnect.bankid.idp.config;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import se.swedenconnect.bankid.idp.audit.AuditRepositoryConfiguration;
import se.swedenconnect.bankid.idp.authn.BankIdAuthenticationController;
import se.swedenconnect.bankid.idp.rp.RelyingPartyUiInfo;
import se.swedenconnect.bankid.rpapi.support.WebClientFactoryBean;
import se.swedenconnect.opensaml.sweid.saml2.authn.LevelOfAssuranceUris;
import se.swedenconnect.security.credential.PkiCredential;
import se.swedenconnect.security.credential.factory.PkiCredentialConfigurationProperties;
import se.swedenconnect.security.credential.factory.PkiCredentialFactoryBean;
/**
* BankID configuration properties.
*
* @author Martin Lindström
* @author Felix Hellman
*/
@ConfigurationProperties("bankid")
@Slf4j
public class BankIdConfigurationProperties implements InitializingBean {
/**
* The URL to the BankID API. Defaults to {@link WebClientFactoryBean#PRODUCTION_WEB_SERVICE_URL}.
*/
@Getter
@Setter
private String serviceUrl;
/**
* The root certificate of the BankID server TLS credential. Defaults to "classpath:trust/bankid-trust-prod.crt".
*/
@Getter
@Setter
private Resource serverRootCertificate;
/**
* Whether we are using a built-in frontend, i.e., if we are using the built in Vue frontend app, this controller
* redirects calls made from the underlying SAML IdP library to our frontend start page.
*/
@Getter
@Setter
private boolean builtInFrontend = true;
/**
* Should be set to 'true' if the BankID IdP is running in "test mode", i.e., if the test BankID RP API is used.
*/
@Getter
@Setter
private boolean testMode = false;
/**
* Duration from initial request to allow restart of the BankID session.
* <p>
* In practice this setting has effect on the time the user has to scan a QR-code, or to start his or her app.
* </p>
* <p>
* The BankID session will enter the state "startFailed" if no client application connects within 30 seconds. If the
* current time is between start and start + startRetryDuration the application will silently start a new
* session. If the current time is outside this duration the user will be presented with an error. The duration will
* only be checked on startFailed i.e. every 30 seconds. If you want to disable silent retries set the duration to
* something lower than 30 seconds, e.g., 0 seconds.
* </p>
*/
@Getter
@Setter
private Duration startRetryDuration = Duration.ofMinutes(3);
/**
* IdP Authentication configuration.
*/
@NestedConfigurationProperty
@Getter
private final IdpConfiguration authn = new IdpConfiguration();
/**
* Configuration for health endpoints.
*/
@NestedConfigurationProperty
@Getter
private final HealthConfiguration health = new HealthConfiguration();
/**
* Session module configuration.
*/
@NestedConfigurationProperty
@Getter
private final SessionConfiguration session = new SessionConfiguration();
/**
* Configuration for audit support.
*/
@NestedConfigurationProperty
@Getter
private final AuditConfiguration audit = new AuditConfiguration();
/**
* Configuration concerning the BankID IdP UI (including texts displayed in the BankID app).
*/
@NestedConfigurationProperty
@Getter
private final UiProperties ui = new UiProperties();
/**
* The relying parties handled by this IdP.
*/
@Getter
private final List<RelyingPartyConfiguration> relyingParties = new ArrayList<>();
/**
* {@inheritDoc}
*/
@Override
public void afterPropertiesSet() throws Exception {
if (!StringUtils.hasText(this.serviceUrl)) {
if (!this.testMode) {
this.serviceUrl = WebClientFactoryBean.TEST_WEB_SERVICE_URL;
}
else {
this.serviceUrl = WebClientFactoryBean.PRODUCTION_WEB_SERVICE_URL;
}
log.info("bankid.service-url was not assigned, defaulting to {}", this.serviceUrl);
}
if (this.serverRootCertificate == null) {
if (!this.testMode) {
this.serverRootCertificate = WebClientFactoryBean.PRODUCTION_ROOT_CERTIFICATE.get();
}
else {
this.serverRootCertificate = WebClientFactoryBean.TEST_ROOT_CERTIFICATE.get();
}
log.info("bankid.server-root-certificate was not assigned, defaulting to {}",
((ClassPathResource) this.serverRootCertificate).getPath());
}
this.authn.afterPropertiesSet();
this.health.afterPropertiesSet();
this.audit.afterPropertiesSet();
this.ui.afterPropertiesSet();
Assert.notEmpty(this.relyingParties, "bankid.relying-parties must contain at least one RP");
for (final RelyingPartyConfiguration rp : this.relyingParties) {
rp.afterPropertiesSet();
final RelyingPartyConfiguration.RpUserMessage msg = rp.getUserMessage();
if (msg.getFallbackSignText() == null) {
Assert.notNull(this.ui.getUserMessageDefaults().getFallbackSignText(),
"bankid.user-message-defaults.fallback-sign-text must be assigned");
msg.setFallbackSignText(this.ui.getUserMessageDefaults().getFallbackSignText());
}
if (msg.getLoginText() == null && msg.isInheritDefaultLoginText()
&& this.ui.getUserMessageDefaults().getLoginText() != null) {
msg.setLoginText(this.ui.getUserMessageDefaults().getLoginText());
}
}
}
/**
* Configuration for a relying party. A BankID Relying Party can serve any number of SAML SP:s (usually they are from
* the same organization).
*/
public static class RelyingPartyConfiguration implements InitializingBean {
/**
* The ID for the Relying Party. Used in logging and may be used for statistics.
*/
@Getter
@Setter
private String id;
/**
* The SAML entityID:s (SP:s) served by this Relying Party. If the IdP is in test mode this may be an empty list
* (meaning that all SP:s are served).
*/
@Getter
private final List<String> entityIds = new ArrayList<>();
/**
* The BankID relying party credential.
*/
@Getter
@Setter
private PkiCredentialConfigurationProperties credential;
// Internal use only ...
private PkiCredential _credential;
/**
* Relying Party specific display text for authentication (and signature). Overrides the default text.
*/
@Getter
@Setter
private RpUserMessage userMessage;
/**
* The UI info for a Relying Party is normally extracted from the SAML metadata, but there are cases where you may
* want to manually configure these data elements (for example if the metadata does not contain this information, or
* you simply want to override it). This element holds this information.
*/
@Getter
@Setter
private RelyingPartyUiInfo uiInfo;
/**
* Specific BankID requirements for this Relying Party.
*/
@Getter
@Setter
private BankIdRequirement bankidRequirements;
/**
* {@inheritDoc}
*/
@Override
public void afterPropertiesSet() throws Exception {
Assert.hasText(this.id, "bankid.relying-parties[].id must be set");
Assert.notNull(this.credential, "bankid.relying-parties[].credential.* must be set");
if (this.credential.isEmpty()) {
throw new IllegalArgumentException("bankid.relying-parties[].credential.* must be set");
}
if (this.userMessage == null) {
this.userMessage = new RpUserMessage();
}
this.userMessage.afterPropertiesSet();
}
/**
* Creates a {@link PkiCredential} given the {@link PkiCredentialConfigurationProperties}.
*
* @return a {@link PkiCredential}
* @throws Exception for errors creating the object
*/
public PkiCredential createCredential() throws Exception {
if (this._credential == null) {
final PkiCredentialFactoryBean factory = new PkiCredentialFactoryBean(this.credential);
factory.afterPropertiesSet();
this._credential = factory.getObject();
}
return this._credential;
}
/**
* For configuring user messages per RP.
*/
public static class RpUserMessage extends UiProperties.UserMessageProperties {
/**
* If the default user message login text has been assigned, and a specific RP wishes to not use login messages it
* should set this flag to 'false' (and not assign 'login-text').
*/
@Getter
@Setter
private boolean inheritDefaultLoginText = true;
}
}
/**
* Configuration of the IdP.
*/
public static class IdpConfiguration implements InitializingBean {
/**
* The name of the authentication provider.
*/
@Getter
@Setter
private String providerName;
/**
* The authentication path. Where the Spring Security flow directs the user for authentication by our
* implementation.
*/
@Getter
@Setter
private String authnPath;
/**
* The resume path. Where we redirect back the user after that we are done.
*/
@Getter
@Setter
private String resumePath;
/**
* The supported LoA:s.
*/
@Getter
private final List<String> supportedLoas = new ArrayList<>();
/**
* The SAML entity categories this IdP declares.
*/
@Getter
private final List<String> entityCategories = new ArrayList<>();
/** {@inheritDoc} */
@Override
public void afterPropertiesSet() throws Exception {
if (!StringUtils.hasText(this.providerName)) {
this.providerName = "BankID";
log.info("bankid.authn.provider-name is not assigned, defaulting to '{}'", this.providerName);
}
if (!StringUtils.hasText(this.authnPath)) {
this.authnPath = BankIdAuthenticationController.AUTHN_PATH;
}
if (!StringUtils.hasText(this.resumePath)) {
this.resumePath = "/resume";
}
if (this.supportedLoas.isEmpty()) {
this.supportedLoas.add(LevelOfAssuranceUris.AUTHN_CONTEXT_URI_UNCERTIFIED_LOA3);
log.info("bankid.authn.supported-loas has not been assigned, defaulting to {}", this.supportedLoas);
}
}
}
/**
* Configuration for health endpoints.
*/
public static class HealthConfiguration implements InitializingBean {
/**
* Default value for the setting that tells when the health endpoint should warn about Relying Party certificates
* that are about to expire.
*/
public static final Duration RP_CERTIFICATE_WARN_THRESHOLD_DEFAULT = Duration.ofDays(14);
/**
* Setting that tells when the health endpoint should warn about Relying Party certificates that are about to
* expire. The default is 14 days.
*/
@Getter
@Setter
private Duration rpCertificateWarnThreshold;
/** {@inheritDoc} */
@Override
public void afterPropertiesSet() throws Exception {
if (this.rpCertificateWarnThreshold == null) {
this.rpCertificateWarnThreshold = RP_CERTIFICATE_WARN_THRESHOLD_DEFAULT;
}
}
}
/**
* Session handling configuration.
*/
public static class SessionConfiguration {
/**
* The session module to use. Supported values are "memory" and "redis". Set to other value if you extend the BankID
* IdP with your own session handling.
*/
@Getter
@Setter
private String module;
}
/**
* Audit logging configuration.
*/
public static class AuditConfiguration implements InitializingBean {
/**
* The type of AuditEventRepository that should be used. Possible values are: "memory" for an in-memory repository,
* "redislist" for a Redis list implementation, "redistimeseries" for a Redis time series implementation or another
* value if you extend the BankID IdP with your own implementation.
*/
@Getter
@Setter
private String repository;
/**
* If assigned, the audit events will not only be stored according to the "repository" but also be written to the
* given log file. If set, a complete path must be given.
*/
@Getter
@Setter
private String logFile;
/**
* The supported events that will be logged to the given repository (and possibly the file). The default is
* {@link AuditRepositoryConfiguration#DEFAULT_SUPPORTED_EVENTS}.
*/
@Getter
private List<String> supportedEvents = new ArrayList<>();
/** {@inheritDoc} */
@Override
public void afterPropertiesSet() throws Exception {
if (!StringUtils.hasText(this.repository)) {
this.repository = "memory";
log.info("bankid.audit.repository has not been assigned, defaulting to '{}'", this.repository);
}
if (this.supportedEvents.isEmpty()) {
this.supportedEvents = AuditRepositoryConfiguration.DEFAULT_SUPPORTED_EVENTS;
}
}
}
}