blob: 75c61622f7727638ae1fe61cb35ec4f92fc669bc [file] [log] [blame]
margaretha56e8e552017-12-05 16:31:21 +01001package de.ids_mannheim.korap.config;
2
margaretha5225ed02018-06-25 18:38:40 +02003import java.net.MalformedURLException;
margaretha5225ed02018-06-25 18:38:40 +02004import java.net.URL;
margarethadfecb4b2017-12-12 19:32:30 +01005import java.util.ArrayList;
margarethab4ce6602018-04-26 20:23:57 +02006import java.util.Arrays;
margarethadfecb4b2017-12-12 19:32:30 +01007import java.util.List;
margaretha56e8e552017-12-05 16:31:21 +01008import java.util.Properties;
margarethab4ce6602018-04-26 20:23:57 +02009import java.util.Set;
margaretha56e8e552017-12-05 16:31:21 +010010import java.util.regex.Pattern;
margarethab4ce6602018-04-26 20:23:57 +020011import java.util.stream.Collectors;
margaretha56e8e552017-12-05 16:31:21 +010012
margaretha14686b52021-07-26 15:24:16 +020013import org.apache.logging.log4j.LogManager;
14import org.apache.logging.log4j.Logger;
15
margaretha6374f722018-04-17 18:45:57 +020016import de.ids_mannheim.korap.constant.AuthenticationMethod;
margaretha6d61a552018-04-10 19:26:44 +020017import de.ids_mannheim.korap.interfaces.EncryptionIface;
margaretha064eb6f2018-07-10 18:33:01 +020018import de.ids_mannheim.korap.utils.TimeUtils;
margaretha6d61a552018-04-10 19:26:44 +020019
margarethab4ce6602018-04-26 20:23:57 +020020/**
21 * Configuration for Kustvakt full version including properties
margaretha19295962018-06-26 16:00:47 +020022 * concerning authentication and licenses.
margaretha56e8e552017-12-05 16:31:21 +010023 *
24 * @author margaretha
25 *
26 */
margarethadfecb4b2017-12-12 19:32:30 +010027
margaretha2afb97d2017-12-07 19:18:44 +010028public class FullConfiguration extends KustvaktConfiguration {
margaretha35e1ca22023-11-16 22:00:01 +010029 public static Logger jlog = LogManager.getLogger(FullConfiguration.class);
margaretha6b3ecdd2018-03-01 18:23:56 +010030
margaretha56e8e552017-12-05 16:31:21 +010031 private String ldapConfig;
32
33 private String freeOnlyRegex;
34 private String publicOnlyRegex;
35 private String allOnlyRegex;
36
margarethadfecb4b2017-12-12 19:32:30 +010037 private List<String> freeRegexList;
38 private List<String> publicRegexList;
39 private List<String> allRegexList;
40
margaretha56e8e552017-12-05 16:31:21 +010041 private String authenticationScheme;
42
margaretha33fa3d92018-07-26 13:50:17 +020043 private EncryptionIface.Encryption secureHashAlgorithm;
margaretha6374f722018-04-17 18:45:57 +020044
45 private AuthenticationMethod OAuth2passwordAuthentication;
46 private String nativeClientHost;
margarethabe4c5c92018-05-03 18:55:49 +020047 private Set<String> clientCredentialsScopes;
margarethab4ce6602018-04-26 20:23:57 +020048 private int maxAuthenticationAttempts;
margaretha6374f722018-04-17 18:45:57 +020049
margaretha0afd44a2020-02-05 10:49:21 +010050 private int accessTokenLongExpiry;
margaretha064eb6f2018-07-10 18:33:01 +020051 private int accessTokenExpiry;
margarethad7163122022-04-11 09:42:41 +020052 private int refreshTokenLongExpiry;
margaretha064eb6f2018-07-10 18:33:01 +020053 private int refreshTokenExpiry;
54 private int authorizationCodeExpiry;
margaretha35e1ca22023-11-16 22:00:01 +010055
margaretha9e73c0e2023-05-05 16:51:49 +020056 private int maxNumberOfUserQueries;
margaretha35e1ca22023-11-16 22:00:01 +010057
margaretha5225ed02018-06-25 18:38:40 +020058 private URL issuer;
margaretha339fd2e2018-11-13 12:14:53 +010059
margaretha1b320452018-08-02 16:56:25 +020060 private String namedVCPath;
margaretha35e1ca22023-11-16 22:00:01 +010061
margaretha3181b8d2022-05-31 11:51:47 +020062 private boolean createInitialSuperClient;
margaretha5225ed02018-06-25 18:38:40 +020063
margaretha19295962018-06-26 16:00:47 +020064 public FullConfiguration (Properties properties) throws Exception {
margaretha56e8e552017-12-05 16:31:21 +010065 super(properties);
66 }
margaretha35e1ca22023-11-16 22:00:01 +010067
margaretha3d55b002019-03-19 12:00:44 +010068 public FullConfiguration () {
69 super();
70 }
margaretha56e8e552017-12-05 16:31:21 +010071
72 @Override
margaretha19295962018-06-26 16:00:47 +020073 public void load (Properties properties) throws Exception {
margaretha56e8e552017-12-05 16:31:21 +010074
75 super.load(properties);
76 // EM: regex used for storing vc
77 setLicenseRegex(properties);
78
79 // EM: pattern for matching availability in Krill matches
80 setLicensePatterns(properties);
margaretha2afb97d2017-12-07 19:18:44 +010081 ldapConfig = properties.getProperty("ldap.config");
margaretha58e18632018-02-15 13:04:42 +010082
margaretha33fa3d92018-07-26 13:50:17 +020083 setSecurityConfiguration(properties);
margaretha6374f722018-04-17 18:45:57 +020084 setOAuth2Configuration(properties);
margaretha339fd2e2018-11-13 12:14:53 +010085
86 setNamedVCPath(properties.getProperty("krill.namedVC", ""));
margaretha35e1ca22023-11-16 22:00:01 +010087
88 // Cache cache = CacheManager.newInstance().getCache("named_vc");
89 // CacheConfiguration config = cache.getCacheConfiguration();
90 // config.setMaxBytesLocalHeap(properties.getProperty("cache.max.bytes.local.heap", "256m"));
91 // config.setMaxBytesLocalDisk(properties.getProperty("cache.max.bytes.local.disk", "2G"));
92 // jlog.info("max local heap:"+config.getMaxBytesLocalHeapAsString());
93 // jlog.info("max local disk:"+config.getMaxBytesLocalDiskAsString());
94
margaretha9e73c0e2023-05-05 16:51:49 +020095 setMaxNumberOfUserQueries(Integer.parseInt(
96 properties.getProperty("max.user.persistent.queries", "20")));
margaretha19295962018-06-26 16:00:47 +020097 }
98
margaretha35e1ca22023-11-16 22:00:01 +010099 private void setSecurityConfiguration (Properties properties)
100 throws MalformedURLException {
margaretha33fa3d92018-07-26 13:50:17 +0200101 setSecureHashAlgorithm(Enum.valueOf(EncryptionIface.Encryption.class,
102 properties.getProperty("security.secure.hash.algorithm",
103 "BCRYPT")));
margaretha35e1ca22023-11-16 22:00:01 +0100104
margaretha9c78e1a2018-06-27 14:12:35 +0200105 String issuerStr = properties.getProperty("security.jwt.issuer",
106 "https://korap.ids-mannheim.de");
107
108 if (!issuerStr.startsWith("http")) {
109 issuerStr = "http://" + issuerStr;
110 }
111 setIssuer(new URL(issuerStr));
margaretha5225ed02018-06-25 18:38:40 +0200112 }
113
margaretha6374f722018-04-17 18:45:57 +0200114 private void setOAuth2Configuration (Properties properties) {
115 setOAuth2passwordAuthentication(
margarethab4ce6602018-04-26 20:23:57 +0200116 Enum.valueOf(AuthenticationMethod.class, properties.getProperty(
117 "oauth2.password.authentication", "TEST")));
118 setNativeClientHost(properties.getProperty("oauth2.native.client.host",
margaretha6374f722018-04-17 18:45:57 +0200119 "korap.ids-mannheim.de"));
margaretha35e1ca22023-11-16 22:00:01 +0100120 setCreateInitialSuperClient(Boolean.valueOf(properties
121 .getProperty("oauth2.initial.super.client", "false")));
margarethab4ce6602018-04-26 20:23:57 +0200122
123 setMaxAuthenticationAttempts(Integer
margaretha064eb6f2018-07-10 18:33:01 +0200124 .parseInt(properties.getProperty("oauth2.max.attempts", "1")));
margarethab4ce6602018-04-26 20:23:57 +0200125
margarethad7cab212018-07-02 19:01:43 +0200126 String clientScopes = properties
127 .getProperty("oauth2.client.credentials.scopes", "client_info");
margarethabe4c5c92018-05-03 18:55:49 +0200128 setClientCredentialsScopes(Arrays.stream(clientScopes.split(" "))
129 .collect(Collectors.toSet()));
margaretha064eb6f2018-07-10 18:33:01 +0200130
131 accessTokenExpiry = TimeUtils.convertTimeToSeconds(
132 properties.getProperty("oauth2.access.token.expiry", "1D"));
133 refreshTokenExpiry = TimeUtils.convertTimeToSeconds(
134 properties.getProperty("oauth2.refresh.token.expiry", "90D"));
135 authorizationCodeExpiry = TimeUtils.convertTimeToSeconds(properties
136 .getProperty("oauth2.authorization.code.expiry", "10M"));
margaretha35e1ca22023-11-16 22:00:01 +0100137
138 setAccessTokenLongExpiry(TimeUtils.convertTimeToSeconds(properties
139 .getProperty("oauth2.access.token.long.expiry", "365D")));
140 setRefreshTokenLongExpiry(TimeUtils.convertTimeToSeconds(properties
141 .getProperty("oauth2.refresh.token.long.expiry", "365D")));
margaretha58e18632018-02-15 13:04:42 +0100142 }
143
margaretha56e8e552017-12-05 16:31:21 +0100144 private void setLicensePatterns (Properties properties) {
margaretha979a2e62017-12-12 19:47:04 +0100145 setFreeLicensePattern(compilePattern(getFreeOnlyRegex()));
margaretha2c019fa2018-02-01 19:50:51 +0100146 setPublicLicensePattern(compilePattern(
147 getFreeOnlyRegex() + "|" + getPublicOnlyRegex()));
148 setAllLicensePattern(compilePattern(getFreeOnlyRegex() + "|"
149 + getPublicOnlyRegex() + "|" + getAllOnlyRegex()));
margaretha56e8e552017-12-05 16:31:21 +0100150 }
151
152 private void setLicenseRegex (Properties properties) {
margaretha979a2e62017-12-12 19:47:04 +0100153 setFreeOnlyRegex(properties.getProperty("availability.regex.free", ""));
154 freeRegexList = splitAndAddToList(getFreeOnlyRegex());
margarethadfecb4b2017-12-12 19:32:30 +0100155
margaretha2c019fa2018-02-01 19:50:51 +0100156 setPublicOnlyRegex(
157 properties.getProperty("availability.regex.public", ""));
margaretha979a2e62017-12-12 19:47:04 +0100158 publicRegexList = splitAndAddToList(getPublicOnlyRegex());
margarethadfecb4b2017-12-12 19:32:30 +0100159
margaretha979a2e62017-12-12 19:47:04 +0100160 setAllOnlyRegex(properties.getProperty("availability.regex.all", ""));
161 allRegexList = splitAndAddToList(getAllOnlyRegex());
margarethadfecb4b2017-12-12 19:32:30 +0100162 }
163
164 private List<String> splitAndAddToList (String regex) {
165 List<String> list;
166 if (regex.contains("|")) {
167 String[] regexes = regex.split("\\|");
168 list = new ArrayList<>(regexes.length);
169 for (String s : regexes) {
170 list.add(s.trim());
171 }
172 }
margaretha2c019fa2018-02-01 19:50:51 +0100173 else {
margarethadfecb4b2017-12-12 19:32:30 +0100174 list = new ArrayList<>(1);
175 list.add(regex);
176 }
177 return list;
margaretha56e8e552017-12-05 16:31:21 +0100178 }
179
margaretha56e8e552017-12-05 16:31:21 +0100180 private Pattern compilePattern (String patternStr) {
181 if (!patternStr.isEmpty()) {
182 return Pattern.compile(patternStr);
183 }
184 else {
185 return null;
186 }
187 }
188
margarethadfecb4b2017-12-12 19:32:30 +0100189 public String getLdapConfig () {
190 return ldapConfig;
191 }
192
193 public Pattern getPublicLicensePattern () {
194 return publicLicensePattern;
195 }
196
197 public void setPublicLicensePattern (Pattern publicLicensePattern) {
198 this.publicLicensePattern = publicLicensePattern;
199 }
200
201 public Pattern getFreeLicensePattern () {
202 return freeLicensePattern;
203 }
204
205 public void setFreeLicensePattern (Pattern freeLicensePattern) {
206 this.freeLicensePattern = freeLicensePattern;
207 }
208
209 public Pattern getAllLicensePattern () {
210 return allLicensePattern;
211 }
212
213 public void setAllLicensePattern (Pattern allLicensePattern) {
214 this.allLicensePattern = allLicensePattern;
215 }
216
217 public String getAuthenticationScheme () {
218 return authenticationScheme;
219 }
220
221 public void setAuthenticationScheme (String authenticationScheme) {
222 this.authenticationScheme = authenticationScheme;
223 }
224
225 public List<String> getFreeRegexList () {
226 return freeRegexList;
227 }
228
229 public void setFreeRegexList (List<String> freeRegexList) {
230 this.freeRegexList = freeRegexList;
231 }
232
233 public List<String> getPublicRegexList () {
234 return publicRegexList;
235 }
236
237 public void setPublicRegexList (List<String> publicRegexList) {
238 this.publicRegexList = publicRegexList;
239 }
240
241 public List<String> getAllRegexList () {
242 return allRegexList;
243 }
244
245 public void setAllRegexList (List<String> allRegexList) {
246 this.allRegexList = allRegexList;
247 }
248
margaretha979a2e62017-12-12 19:47:04 +0100249 public String getFreeOnlyRegex () {
250 return freeOnlyRegex;
251 }
252
253 public void setFreeOnlyRegex (String freeOnlyRegex) {
254 this.freeOnlyRegex = freeOnlyRegex;
255 }
256
257 public String getPublicOnlyRegex () {
258 return publicOnlyRegex;
259 }
260
261 public void setPublicOnlyRegex (String publicOnlyRegex) {
262 this.publicOnlyRegex = publicOnlyRegex;
263 }
264
265 public String getAllOnlyRegex () {
266 return allOnlyRegex;
267 }
268
269 public void setAllOnlyRegex (String allOnlyRegex) {
270 this.allOnlyRegex = allOnlyRegex;
271 }
272
margaretha33fa3d92018-07-26 13:50:17 +0200273 public EncryptionIface.Encryption getSecureHashAlgorithm () {
274 return secureHashAlgorithm;
margaretha6d61a552018-04-10 19:26:44 +0200275 }
276
margaretha33fa3d92018-07-26 13:50:17 +0200277 public void setSecureHashAlgorithm (
278 EncryptionIface.Encryption secureHashAlgorithm) {
279 this.secureHashAlgorithm = secureHashAlgorithm;
margaretha6d61a552018-04-10 19:26:44 +0200280 }
281
margaretha6374f722018-04-17 18:45:57 +0200282 public AuthenticationMethod getOAuth2passwordAuthentication () {
283 return OAuth2passwordAuthentication;
284 }
285
286 public void setOAuth2passwordAuthentication (
287 AuthenticationMethod oAuth2passwordAuthentication) {
288 OAuth2passwordAuthentication = oAuth2passwordAuthentication;
289 }
290
291 public String getNativeClientHost () {
292 return nativeClientHost;
293 }
294
295 public void setNativeClientHost (String nativeClientHost) {
296 this.nativeClientHost = nativeClientHost;
297 }
298
margarethab4ce6602018-04-26 20:23:57 +0200299 public int getMaxAuthenticationAttempts () {
300 return maxAuthenticationAttempts;
301 }
302
303 public void setMaxAuthenticationAttempts (int maxAuthenticationAttempts) {
304 this.maxAuthenticationAttempts = maxAuthenticationAttempts;
305 }
306
margarethabe4c5c92018-05-03 18:55:49 +0200307 public Set<String> getClientCredentialsScopes () {
308 return clientCredentialsScopes;
309 }
310
311 public void setClientCredentialsScopes (
312 Set<String> clientCredentialsScopes) {
313 this.clientCredentialsScopes = clientCredentialsScopes;
margarethab4ce6602018-04-26 20:23:57 +0200314 }
315
margaretha5225ed02018-06-25 18:38:40 +0200316 public URL getIssuer () {
317 return issuer;
318 }
319
320 public void setIssuer (URL issuer) {
321 this.issuer = issuer;
322 }
323
margaretha064eb6f2018-07-10 18:33:01 +0200324 public int getAccessTokenExpiry () {
325 return accessTokenExpiry;
326 }
327
328 public void setAccessTokenExpiry (int accessTokenExpiry) {
329 this.accessTokenExpiry = accessTokenExpiry;
330 }
331
332 public int getRefreshTokenExpiry () {
333 return refreshTokenExpiry;
334 }
335
336 public void setRefreshTokenExpiry (int refreshTokenExpiry) {
337 this.refreshTokenExpiry = refreshTokenExpiry;
338 }
339
340 public int getAuthorizationCodeExpiry () {
341 return authorizationCodeExpiry;
342 }
343
344 public void setAuthorizationCodeExpiry (int authorizationCodeExpiry) {
345 this.authorizationCodeExpiry = authorizationCodeExpiry;
346 }
margaretha33fa3d92018-07-26 13:50:17 +0200347
margaretha1b320452018-08-02 16:56:25 +0200348 public String getNamedVCPath () {
349 return namedVCPath;
350 }
351
352 public void setNamedVCPath (String namedVCPath) {
353 this.namedVCPath = namedVCPath;
354 }
margaretha0afd44a2020-02-05 10:49:21 +0100355
356 public int getAccessTokenLongExpiry () {
357 return accessTokenLongExpiry;
358 }
359
360 public void setAccessTokenLongExpiry (int accessTokenLongExpiry) {
361 this.accessTokenLongExpiry = accessTokenLongExpiry;
362 }
margarethad7163122022-04-11 09:42:41 +0200363
364 public int getRefreshTokenLongExpiry () {
365 return refreshTokenLongExpiry;
366 }
367
368 public void setRefreshTokenLongExpiry (int refreshTokenLongExpiry) {
369 this.refreshTokenLongExpiry = refreshTokenLongExpiry;
370 }
margaretha3181b8d2022-05-31 11:51:47 +0200371
372 public boolean createInitialSuperClient () {
373 return createInitialSuperClient;
374 }
margaretha35e1ca22023-11-16 22:00:01 +0100375
margaretha3181b8d2022-05-31 11:51:47 +0200376 public void setCreateInitialSuperClient (boolean initialSuperClient) {
377 this.createInitialSuperClient = initialSuperClient;
378 }
margaretha9e73c0e2023-05-05 16:51:49 +0200379
380 public int getMaxNumberOfUserQueries () {
381 return maxNumberOfUserQueries;
382 }
383
384 public void setMaxNumberOfUserQueries (int maxNumberOfUserQueries) {
385 this.maxNumberOfUserQueries = maxNumberOfUserQueries;
386 }
margaretha35e1ca22023-11-16 22:00:01 +0100387
margaretha56e8e552017-12-05 16:31:21 +0100388}