1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.chemistry.opencmis.client.bindings.spi;
20
21 import java.net.Authenticator;
22 import java.net.PasswordAuthentication;
23 import java.util.List;
24 import java.util.Map;
25
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import org.w3c.dom.Element;
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 public class NTLMAuthenticationProvider extends AbstractAuthenticationProvider {
56
57 private static final long serialVersionUID = 1L;
58
59 private static final Logger LOG = LoggerFactory.getLogger(NTLMAuthenticationProvider.class);
60
61
62 private static final OpenCMISAuthenticator AUTHENTICATOR = new OpenCMISAuthenticator();
63 static {
64 Authenticator.setDefault(AUTHENTICATOR);
65 }
66
67 @Override
68 public void setSession(BindingSession session) {
69 super.setSession(session);
70 LOG.warn("Use NTLM only for testing! Choose a different authentication method in production.");
71 }
72
73 @Override
74 public Map<String, List<String>> getHTTPHeaders(String url) {
75
76 String user = getUser();
77 String password = getPassword();
78
79
80 if (user == null) {
81 AUTHENTICATOR.reset();
82 return null;
83 }
84
85 if (password == null) {
86 password = "";
87 }
88
89
90 AUTHENTICATOR.setPasswordAuthentication(user, password);
91
92
93
94 return null;
95 }
96
97 @Override
98 public Element getSOAPHeaders(Object portObject) {
99
100 return null;
101 }
102
103
104
105
106 static class OpenCMISAuthenticator extends Authenticator {
107
108 private PasswordAuthentication passwordAuthentication;
109
110
111
112
113
114 public synchronized void reset() {
115 passwordAuthentication = null;
116 }
117
118
119
120
121 public synchronized void setPasswordAuthentication(String user, String password) {
122 passwordAuthentication = new PasswordAuthentication(user, password.toCharArray());
123 }
124
125 @Override
126 protected synchronized PasswordAuthentication getPasswordAuthentication() {
127 return passwordAuthentication;
128 }
129 }
130 }