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