001: /*
002: * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/test/org/apache/commons/httpclient/auth/TestNTLMAuth.java,v 1.2 2004/11/07 12:31:42 olegk Exp $
003: * $Revision: 480424 $
004: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005: * ====================================================================
006: *
007: * Licensed to the Apache Software Foundation (ASF) under one or more
008: * contributor license agreements. See the NOTICE file distributed with
009: * this work for additional information regarding copyright ownership.
010: * The ASF licenses this file to You under the Apache License, Version 2.0
011: * (the "License"); you may not use this file except in compliance with
012: * the License. You may obtain a copy of the License at
013: *
014: * http://www.apache.org/licenses/LICENSE-2.0
015: *
016: * Unless required by applicable law or agreed to in writing, software
017: * distributed under the License is distributed on an "AS IS" BASIS,
018: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019: * See the License for the specific language governing permissions and
020: * limitations under the License.
021: * ====================================================================
022: *
023: * This software consists of voluntary contributions made by many
024: * individuals on behalf of the Apache Software Foundation. For more
025: * information on the Apache Software Foundation, please see
026: * <http://www.apache.org/>.
027: *
028: */
029:
030: package org.apache.commons.httpclient.auth;
031:
032: import java.io.IOException;
033:
034: import junit.framework.Test;
035: import junit.framework.TestSuite;
036:
037: import org.apache.commons.httpclient.FakeHttpMethod;
038: import org.apache.commons.httpclient.Header;
039: import org.apache.commons.httpclient.HttpClientTestBase;
040: import org.apache.commons.httpclient.HttpState;
041: import org.apache.commons.httpclient.HttpStatus;
042: import org.apache.commons.httpclient.HttpVersion;
043: import org.apache.commons.httpclient.NTCredentials;
044: import org.apache.commons.httpclient.methods.GetMethod;
045: import org.apache.commons.httpclient.protocol.Protocol;
046: import org.apache.commons.httpclient.server.HttpService;
047: import org.apache.commons.httpclient.server.RequestLine;
048: import org.apache.commons.httpclient.server.SimpleRequest;
049: import org.apache.commons.httpclient.server.SimpleResponse;
050:
051: /**
052: * Test Methods for NTLM Authentication.
053: *
054: * @author Rodney Waldhoff
055: * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
056: * @version $Id: TestNTLMAuth.java 480424 2006-11-29 05:56:49Z bayard $
057: */
058: public class TestNTLMAuth extends HttpClientTestBase {
059:
060: // ------------------------------------------------------------ Constructor
061: public TestNTLMAuth(String testName) throws IOException {
062: super (testName);
063: }
064:
065: // ------------------------------------------------------------------- Main
066: public static void main(String args[]) {
067: String[] testCaseName = { TestNTLMAuth.class.getName() };
068: junit.textui.TestRunner.main(testCaseName);
069: }
070:
071: // ------------------------------------------------------- TestCase Methods
072:
073: public static Test suite() {
074: return new TestSuite(TestNTLMAuth.class);
075: }
076:
077: // ---------------------------------
078:
079: public void testNTLMAuthenticationResponse1() throws Exception {
080: String challenge = "NTLM";
081: String expected = "NTLM TlRMTVNTUAABAAAABlIAAAYABgAkAAAABAAEACAAAABIT"
082: + "1NURE9NQUlO";
083: NTCredentials cred = new NTCredentials("username", "password",
084: "host", "domain");
085: FakeHttpMethod method = new FakeHttpMethod();
086: AuthScheme authscheme = new NTLMScheme(challenge);
087: authscheme.processChallenge(challenge);
088: String response = authscheme.authenticate(cred, method);
089: assertEquals(expected, response);
090: assertFalse(authscheme.isComplete());
091: }
092:
093: public void testNTLMAuthenticationResponse2() throws Exception {
094: String challenge = "NTLM TlRMTVNTUAACAAAACgAKADAAAAAGgoEAPc4kP4LtCV8AAAAAAAAAAJ4AngA"
095: + "6AAAASU5UUkFFUEhPWAIAFABJAE4AVABSAEEARQBQAEgATwBYAAEAEgBCAE8AQQB"
096: + "SAEQAUgBPAE8ATQAEACgAaQBuAHQAcgBhAGUAcABoAG8AeAAuAGUAcABoAG8AeAA"
097: + "uAGMAbwBtAAMAPABCAG8AYQByAGQAcgBvAG8AbQAuAGkAbgB0AHIAYQBlAHAAaAB"
098: + "vAHgALgBlAHAAaABvAHgALgBjAG8AbQAAAAAA";
099:
100: String expected = "NTLM TlRMTVNTUAADAAAAGAAYAFIAAAAAAAAAagAAAAYABgB"
101: + "AAAAACAAIAEYAAAAEAAQATgAAAAAAAABqAAAABlIAAERPTUFJTlVTRVJOQU1FSE"
102: + "9TVAaC+vLxUEHnUtpItj9Dp4kzwQfd61Lztg==";
103: NTCredentials cred = new NTCredentials("username", "password",
104: "host", "domain");
105: FakeHttpMethod method = new FakeHttpMethod();
106: AuthScheme authscheme = new NTLMScheme(challenge);
107: authscheme.processChallenge(challenge);
108: String response = authscheme.authenticate(cred, method);
109: assertEquals(expected, response);
110: assertTrue(authscheme.isComplete());
111: }
112:
113: private class NTLMAuthService implements HttpService {
114:
115: public NTLMAuthService() {
116: super ();
117: }
118:
119: public boolean process(final SimpleRequest request,
120: final SimpleResponse response) throws IOException {
121: RequestLine requestLine = request.getRequestLine();
122: HttpVersion ver = requestLine.getHttpVersion();
123: Header auth = request.getFirstHeader("Authorization");
124: if (auth == null) {
125: response.setStatusLine(ver, HttpStatus.SC_UNAUTHORIZED);
126: response.addHeader(new Header("WWW-Authenticate",
127: "NTLM"));
128: response.setBodyString("Authorization required");
129: return true;
130: } else {
131: String authstr = auth.getValue();
132:
133: if (authstr
134: .equals("NTLM TlRMTVNTUAABAAAABlIAAAYABgAkAAAABAAEACAAAABIT1NURE9NQUlO")) {
135: response.setStatusLine(ver,
136: HttpStatus.SC_UNAUTHORIZED);
137: response
138: .addHeader(new Header("WWW-Authenticate",
139: "NTLM TlRMTVNTUAACAAAAAAAAACgAAAABggAAU3J2Tm9uY2UAAAAAAAAAAA=="));
140: response.setBodyString("Authorization required");
141: return true;
142: }
143: if (authstr
144: .equals("NTLM TlRMTVNTUAADAAAAGAAYAFIAAAAAAAAAagAAAAYABgBAAAAACAAIAEYAAAAEAAQATgAAAAAAAABqAAAABlIAAERPTUFJTlVTRVJOQU1FSE9TVJxndWIt46bHm11TPrt5Z6wrz7ziq04yRA==")) {
145: response.setStatusLine(ver, HttpStatus.SC_OK);
146: response.setBodyString("Authorization successful");
147: return true;
148: } else {
149: response.setStatusLine(ver,
150: HttpStatus.SC_UNAUTHORIZED);
151: response.addHeader(new Header("WWW-Authenticate",
152: "NTLM"));
153: response.setBodyString("Authorization required");
154: return true;
155: }
156: }
157: }
158: }
159:
160: public void testNTLMAuthenticationRetry() throws Exception {
161:
162: this .server.setHttpService(new NTLMAuthService());
163:
164: // configure the client
165: this .client.getHostConfiguration().setHost(
166: server.getLocalAddress(), server.getLocalPort(),
167: Protocol.getProtocol("http"));
168:
169: this .client.getState().setCredentials(
170: AuthScope.ANY,
171: new NTCredentials("username", "password", "host",
172: "domain"));
173:
174: FakeHttpMethod httpget = new FakeHttpMethod("/");
175: try {
176: client.executeMethod(httpget);
177: } finally {
178: httpget.releaseConnection();
179: }
180: assertNull(httpget.getResponseHeader("WWW-Authenticate"));
181: assertEquals(200, httpget.getStatusCode());
182: }
183:
184: private class PreemptiveNTLMAuthService implements HttpService {
185:
186: public PreemptiveNTLMAuthService() {
187: super ();
188: }
189:
190: public boolean process(final SimpleRequest request,
191: final SimpleResponse response) throws IOException {
192: RequestLine requestLine = request.getRequestLine();
193: HttpVersion ver = requestLine.getHttpVersion();
194: Header auth = request.getFirstHeader("Authorization");
195: if (auth == null) {
196: response.setStatusLine(ver, HttpStatus.SC_BAD_REQUEST);
197: response.setBodyString("Authorization header missing");
198: return true;
199: } else {
200: String authstr = auth.getValue();
201:
202: if (authstr.indexOf("NTLM") != -1) {
203: response.setStatusLine(ver, HttpStatus.SC_OK);
204: return true;
205: } else if (authstr.indexOf("Basic") != -1) {
206: response.setStatusLine(ver,
207: HttpStatus.SC_UNAUTHORIZED);
208: response.addHeader(new Header("WWW-Authenticate",
209: "Negotiate"));
210: response.addHeader(new Header("WWW-Authenticate",
211: "NTLM"));
212: response.setBodyString("Authorization required");
213: return true;
214: } else {
215: response.setStatusLine(ver,
216: HttpStatus.SC_BAD_REQUEST);
217: response.setBodyString("Unknown auth type: "
218: + authstr);
219: return true;
220: }
221: }
222: }
223: }
224:
225: /**
226: * Make sure preemptive authorization works when the server requires NLM.
227: * @throws Exception
228: */
229: public void testPreemptiveAuthorization() throws Exception {
230:
231: NTCredentials creds = new NTCredentials("testuser", "testpass",
232: "host", "domain");
233:
234: HttpState state = new HttpState();
235: state.setCredentials(AuthScope.ANY, creds);
236: this .client.setState(state);
237: this .client.getParams().setAuthenticationPreemptive(true);
238:
239: this .server.setHttpService(new PreemptiveNTLMAuthService());
240:
241: GetMethod httpget = new GetMethod("/test/");
242: try {
243: this.client.executeMethod(httpget);
244: } finally {
245: httpget.releaseConnection();
246: }
247: assertNotNull(httpget.getStatusLine());
248: assertEquals(HttpStatus.SC_OK, httpget.getStatusLine()
249: .getStatusCode());
250: }
251:
252: }
|