View Javadoc
1   /*
2    * Copyright 2012-2025 CodeLibs Project and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.codelibs.fess.opensearch.config.exentity;
17  
18  import java.util.HashMap;
19  import java.util.Map;
20  
21  import org.apache.logging.log4j.LogManager;
22  import org.apache.logging.log4j.Logger;
23  import org.codelibs.core.lang.StringUtil;
24  import org.codelibs.fess.Constants;
25  import org.codelibs.fess.app.service.WebConfigService;
26  import org.codelibs.fess.crawler.client.http.config.CredentialsConfig;
27  import org.codelibs.fess.crawler.client.http.config.CredentialsConfig.CredentialsType;
28  import org.codelibs.fess.crawler.client.http.config.WebAuthenticationConfig;
29  import org.codelibs.fess.crawler.client.http.config.WebAuthenticationConfig.AuthSchemeType;
30  import org.codelibs.fess.crawler.exception.CrawlerSystemException;
31  import org.codelibs.fess.opensearch.config.bsentity.BsWebAuthentication;
32  import org.codelibs.fess.opensearch.config.exentity.CrawlingConfig.ConfigName;
33  import org.codelibs.fess.opensearch.config.exentity.CrawlingConfig.Param.Config;
34  import org.codelibs.fess.util.ComponentUtil;
35  import org.codelibs.fess.util.ParameterUtil;
36  
37  /**
38   * @author FreeGen
39   */
40  public class WebAuthentication extends BsWebAuthentication {
41  
42      private static final long serialVersionUID = 1L;
43  
44      private static final Logger logger = LogManager.getLogger(WebAuthentication.class);
45  
46      private WebConfig webConfig;
47  
48      public WebAuthenticationConfig getWebAuthenticationConfig() {
49          if (StringUtil.isEmpty(getUsername())) {
50              throw new CrawlerSystemException(
51                      "Username is empty in WebAuthentication configuration. A valid username must be provided for authentication.");
52          }
53  
54          final WebAuthenticationConfig config = new WebAuthenticationConfig();
55  
56          // host/port/realm: only set if not blank (null means "any" - AuthScope.ANY equivalent)
57          if (StringUtil.isNotBlank(getHostname())) {
58              config.setHost(getHostname());
59          }
60          if (getPort() != null) {
61              config.setPort(getPort());
62          }
63          if (StringUtil.isNotBlank(getAuthRealm())) {
64              config.setRealm(getAuthRealm());
65          }
66  
67          // AuthSchemeType の設定
68          final String scheme = getProtocolScheme();
69          if (Constants.BASIC.equals(scheme)) {
70              config.setAuthSchemeType(AuthSchemeType.BASIC);
71          } else if (Constants.DIGEST.equals(scheme)) {
72              config.setAuthSchemeType(AuthSchemeType.DIGEST);
73          } else if (Constants.NTLM.equals(scheme)) {
74              config.setAuthSchemeType(AuthSchemeType.NTLM);
75              // Pass jcifs.* properties via formParameters for NTLM configuration
76              final Map<String, String> jcifsParams = new HashMap<>();
77              getWebConfig().getConfigParameterMap(ConfigName.CONFIG)
78                      .entrySet()
79                      .stream()
80                      .filter(e -> e.getKey().startsWith(Config.JCIFS_PREFIX))
81                      .forEach(e -> jcifsParams.put(e.getKey(), e.getValue()));
82              if (!jcifsParams.isEmpty()) {
83                  config.setNtlmParameters(jcifsParams);
84              }
85          } else if (Constants.FORM.equals(scheme)) {
86              config.setAuthSchemeType(AuthSchemeType.FORM);
87              config.setFormParameters(ParameterUtil.parse(getParameters()));
88          }
89  
90          // Credentials の設定
91          final CredentialsConfig credentials = new CredentialsConfig();
92          credentials.setUsername(getUsername());
93          credentials.setPassword(getPassword() == null ? StringUtil.EMPTY : getPassword());
94          if (Constants.NTLM.equals(scheme)) {
95              credentials.setType(CredentialsType.NTLM);
96              final Map<String, String> parameterMap = ParameterUtil.parse(getParameters());
97              credentials.setDomain(parameterMap.get("domain"));
98              credentials.setWorkstation(parameterMap.get("workstation"));
99          }
100         config.setCredentials(credentials);
101 
102         return config;
103     }
104 
105     public WebConfig getWebConfig() {
106         if (webConfig == null) {
107             final WebConfigService webConfigService = ComponentUtil.getComponent(WebConfigService.class);
108             try {
109                 webConfig = webConfigService.getWebConfig(getWebConfigId()).get();
110             } catch (final Exception e) {
111                 logger.warn("Web Config {} does not exist.", getWebConfigId(), e);
112             }
113         }
114         return webConfig;
115     }
116 
117     public String getId() {
118         return asDocMeta().id();
119     }
120 
121     public void setId(final String id) {
122         asDocMeta().id(id);
123     }
124 
125     public Long getVersionNo() {
126         return asDocMeta().version();
127     }
128 
129     public void setVersionNo(final Long version) {
130         asDocMeta().version(version);
131     }
132 
133     @Override
134     public String toString() {
135         return "WebAuthentication [webConfig=" + webConfig + ", authRealm=" + authRealm + ", createdBy=" + createdBy + ", createdTime="
136                 + createdTime + ", hostname=" + hostname + ", parameters=" + parameters + ", port=" + port + ", protocolScheme="
137                 + protocolScheme + ", updatedBy=" + updatedBy + ", updatedTime=" + updatedTime + ", username=" + username + ", webConfigId="
138                 + webConfigId + ", docMeta=" + docMeta + "]";
139     }
140 }