1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.codelibs.fess.es.config.exentity;
17
18 import java.util.Map;
19 import java.util.Properties;
20
21 import org.apache.http.auth.AuthScheme;
22 import org.apache.http.auth.AuthScope;
23 import org.apache.http.auth.Credentials;
24 import org.apache.http.auth.NTCredentials;
25 import org.apache.http.auth.UsernamePasswordCredentials;
26 import org.apache.http.impl.auth.BasicScheme;
27 import org.apache.http.impl.auth.DigestScheme;
28 import org.apache.http.impl.auth.NTLMScheme;
29 import org.apache.logging.log4j.LogManager;
30 import org.apache.logging.log4j.Logger;
31 import org.codelibs.core.lang.StringUtil;
32 import org.codelibs.fess.Constants;
33 import org.codelibs.fess.app.service.WebConfigService;
34 import org.codelibs.fess.crawler.client.http.Authentication;
35 import org.codelibs.fess.crawler.client.http.form.FormScheme;
36 import org.codelibs.fess.crawler.client.http.impl.AuthenticationImpl;
37 import org.codelibs.fess.crawler.client.http.ntlm.JcifsEngine;
38 import org.codelibs.fess.crawler.exception.CrawlerSystemException;
39 import org.codelibs.fess.es.config.bsentity.BsWebAuthentication;
40 import org.codelibs.fess.es.config.exentity.CrawlingConfig.ConfigName;
41 import org.codelibs.fess.es.config.exentity.CrawlingConfig.Param.Config;
42 import org.codelibs.fess.util.ComponentUtil;
43 import org.codelibs.fess.util.ParameterUtil;
44
45
46
47
48 public class WebAuthentication extends BsWebAuthentication {
49
50 private static final long serialVersionUID = 1L;
51
52 private static final Logger logger = LogManager.getLogger(WebAuthentication.class);
53
54 private WebConfig webConfig;
55
56 public Authentication getAuthentication() {
57 return new AuthenticationImpl(getAuthScope(), getCredentials(), getAuthScheme());
58 }
59
60 private AuthScheme getAuthScheme() {
61 final String scheme = getProtocolScheme();
62 if (Constants.BASIC.equals(scheme)) {
63 return new BasicScheme();
64 }
65 if (Constants.DIGEST.equals(scheme)) {
66 return new DigestScheme();
67 }
68 if (Constants.NTLM.equals(scheme)) {
69 final Properties props = new Properties();
70 getWebConfig().getConfigParameterMap(ConfigName.CONFIG).entrySet().stream()
71 .filter(e -> e.getKey().startsWith(Config.JCIFS_PREFIX)).forEach(e -> {
72 props.setProperty(e.getKey(), e.getValue());
73 });
74 return new NTLMScheme(new JcifsEngine(props));
75 } else if (Constants.FORM.equals(scheme)) {
76 final Map<String, String> parameterMap = ParameterUtil.parse(getParameters());
77 return new FormScheme(parameterMap);
78 }
79 return null;
80 }
81
82 private AuthScope getAuthScope() {
83 if (StringUtil.isBlank(getHostname())) {
84 return AuthScope.ANY;
85 }
86
87 int p;
88 if (getPort() == null) {
89 p = AuthScope.ANY_PORT;
90 } else {
91 p = getPort();
92 }
93
94 String r = getAuthRealm();
95 if (StringUtil.isBlank(r)) {
96 r = AuthScope.ANY_REALM;
97 }
98
99 String s = getProtocolScheme();
100 if (StringUtil.isBlank(s) || Constants.NTLM.equals(s)) {
101 s = AuthScope.ANY_SCHEME;
102 }
103
104 return new AuthScope(getHostname(), p, r, s);
105 }
106
107 private Credentials getCredentials() {
108 if (StringUtil.isEmpty(getUsername())) {
109 throw new CrawlerSystemException("username is empty.");
110 }
111
112 if (Constants.NTLM.equals(getProtocolScheme())) {
113 final Map<String, String> parameterMap = ParameterUtil.parse(getParameters());
114 final String workstation = parameterMap.get("workstation");
115 final String domain = parameterMap.get("domain");
116 return new NTCredentials(getUsername(), getPassword(), workstation == null ? StringUtil.EMPTY : workstation,
117 domain == null ? StringUtil.EMPTY : domain);
118 }
119
120 return new UsernamePasswordCredentials(getUsername(), getPassword() == null ? StringUtil.EMPTY : getPassword());
121 }
122
123 public WebConfig getWebConfig() {
124 if (webConfig == null) {
125 final WebConfigService webConfigService = ComponentUtil.getComponent(WebConfigService.class);
126 try {
127 webConfig = webConfigService.getWebConfig(getWebConfigId()).get();
128 } catch (final Exception e) {
129 logger.warn("Web Config {} does not exist.", getWebConfigId(), e);
130 }
131 }
132 return webConfig;
133 }
134
135 public String getId() {
136 return asDocMeta().id();
137 }
138
139 public void setId(final String id) {
140 asDocMeta().id(id);
141 }
142
143 public Long getVersionNo() {
144 return asDocMeta().version();
145 }
146
147 public void setVersionNo(final Long version) {
148 asDocMeta().version(version);
149 }
150
151 @Override
152 public String toString() {
153 return "WebAuthentication [webConfig=" + webConfig + ", authRealm=" + authRealm + ", createdBy=" + createdBy + ", createdTime="
154 + createdTime + ", hostname=" + hostname + ", parameters=" + parameters + ", port=" + port + ", protocolScheme="
155 + protocolScheme + ", updatedBy=" + updatedBy + ", updatedTime=" + updatedTime + ", username=" + username + ", webConfigId="
156 + webConfigId + ", docMeta=" + docMeta + "]";
157 }
158 }