1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
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
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
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
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 }