1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 package org.apache.commons.httpclient.auth;
32
33 import org.apache.commons.httpclient.Credentials;
34 import org.apache.commons.httpclient.HttpMethod;
35 import org.apache.commons.httpclient.NTCredentials;
36 import org.apache.commons.logging.Log;
37 import org.apache.commons.logging.LogFactory;
38
39 /** An implementation of the Microsoft proprietary NTLM authentication scheme. For a detailed
40 * explanation of the NTLM scheme please see <a href="http://davenport.sourceforge.net/ntlm.html">
41 * http://davenport.sourceforge.net/ntlm.html</a>.
42 *
43 * @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
44 * @author Rodney Waldhoff
45 * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
46 * @author Ortwin Gl???ck
47 * @author Sean C. Sullivan
48 * @author <a href="mailto:adrian@ephox.com">Adrian Sutton</a>
49 * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
50 * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
51 */
52 public class NTLMScheme implements AuthScheme {
53
54 /** Log object for this class. */
55 private static final Log LOG = LogFactory.getLog(NTLMScheme.class);
56
57 /** NTLM challenge string. */
58 private String ntlmchallenge = null;
59
60 private static final int UNINITIATED = 0;
61 private static final int INITIATED = 1;
62 private static final int TYPE1_MSG_GENERATED = 2;
63 private static final int TYPE2_MSG_RECEIVED = 3;
64 private static final int TYPE3_MSG_GENERATED = 4;
65 private static final int FAILED = Integer.MAX_VALUE;
66
67 /** Authentication process state */
68 private int state;
69
70 /**
71 * Default constructor for the NTLM authentication scheme.
72 *
73 * @since 3.0
74 */
75 public NTLMScheme() {
76 super();
77 this.state = UNINITIATED;
78 }
79
80 /**
81 * Constructor for the NTLM authentication scheme.
82 *
83 * @param challenge The authentication challenge
84 *
85 * @throws MalformedChallengeException is thrown if the authentication challenge
86 * is malformed
87 */
88 public NTLMScheme(final String challenge) throws MalformedChallengeException {
89 super();
90 processChallenge(challenge);
91 }
92
93 /**
94 * Processes the NTLM challenge.
95 *
96 * @param challenge the challenge string
97 *
98 * @throws MalformedChallengeException is thrown if the authentication challenge
99 * is malformed
100 *
101 * @since 3.0
102 */
103 public void processChallenge(final String challenge) throws MalformedChallengeException {
104 String s = AuthChallengeParser.extractScheme(challenge);
105 if (!s.equalsIgnoreCase(getSchemeName())) {
106 throw new MalformedChallengeException("Invalid NTLM challenge: " + challenge);
107 }
108 int i = challenge.indexOf(' ');
109 if (i != -1) {
110 s = challenge.substring(i, challenge.length());
111 this.ntlmchallenge = s.trim();
112 this.state = TYPE2_MSG_RECEIVED;
113 } else {
114 this.ntlmchallenge = "";
115 if (this.state == UNINITIATED) {
116 this.state = INITIATED;
117 } else {
118 this.state = FAILED;
119 }
120 }
121 }
122
123 /**
124 * Tests if the NTLM authentication process has been completed.
125 *
126 * @return <tt>true</tt> if Basic authorization has been processed,
127 * <tt>false</tt> otherwise.
128 *
129 * @since 3.0
130 */
131 public boolean isComplete() {
132 return this.state == TYPE3_MSG_GENERATED || this.state == FAILED;
133 }
134
135 /**
136 * Returns textual designation of the NTLM authentication scheme.
137 *
138 * @return <code>ntlm</code>
139 */
140 public String getSchemeName() {
141 return "ntlm";
142 }
143
144 /**
145 * The concept of an authentication realm is not supported by the NTLM
146 * authentication scheme. Always returns <code>null</code>.
147 *
148 * @return <code>null</code>
149 */
150 public String getRealm() {
151 return null;
152 }
153
154 /**
155 * Returns a String identifying the authentication challenge. This is
156 * used, in combination with the host and port to determine if
157 * authorization has already been attempted or not. Schemes which
158 * require multiple requests to complete the authentication should
159 * return a different value for each stage in the request.
160 *
161 * <p>Additionally, the ID should take into account any changes to the
162 * authentication challenge and return a different value when appropriate.
163 * For example when the realm changes in basic authentication it should be
164 * considered a different authentication attempt and a different value should
165 * be returned.</p>
166 *
167 * @return String a String identifying the authentication challenge. The
168 * returned value may be null.
169 *
170 * @deprecated no longer used
171 */
172 public String getID() {
173 return ntlmchallenge;
174 }
175
176 /**
177 * Returns the authentication parameter with the given name, if available.
178 *
179 * <p>There are no valid parameters for NTLM authentication so this method always returns
180 * <tt>null</tt>.</p>
181 *
182 * @param name The name of the parameter to be returned
183 *
184 * @return the parameter with the given name
185 */
186 public String getParameter(String name) {
187 if (name == null) {
188 throw new IllegalArgumentException("Parameter name may not be null");
189 }
190 return null;