annotate libs/httpcomponents-client-4.0-beta1/NTLM_SUPPORT.txt @ 5:0be9d53a6967

editor for annotations
author dwinter
date Tue, 13 Dec 2011 17:43:46 +0100
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
1 NTLM support in HttpClient 4.x
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
2 ==============================
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
3
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
4 Currently HttpClient 4.0 does not provide support for the NTLM authentication
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
5 scheme out of the box and probably never will. The reasons for that are legal
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
6 rather than technical.
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
7
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
8 Background
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
9 ==========
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
10 NTLM is a proprietary authentication scheme developed by Microsoft and
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
11 optimized for Windows operating system.
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
12
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
13 Until year 2008 there was no official, publicly available, complete
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
14 documentation of the protocol. Unofficial 3rd party protocol descriptions
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
15 existed [1] as a result of reverse-engineering efforts. It was not really
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
16 known whether the protocol based on the reverse-engineering were complete
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
17 or even correct.
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
18
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
19 Microsoft published MS-NLMP [2] and MS-NTHT [3] specifications in February
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
20 2008 as a part of its Interoperability Principles initiative [4].
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
21 Unfortunately, it is still not entirely clear whether NTLM encryption
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
22 algorithms are covered by any patents held by Microsoft, which would make
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
23 commercial users of open-source NTLM implementations liable for the use of
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
24 Microsoft intellectual property.
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
25
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
26 Enabling NTLM support in HttpClient 4.x
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
27 =======================================
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
28 The good news is HttpClient is fully NTLM capable right out of the box.
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
29 HttpClient ships with the NTLM authentication scheme, which, if configured
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
30 to use an external NTLM engine, can handle NTLM challenges and authenticate
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
31 against NTLM servers.
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
32
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
33 -----------------------------------------------------------
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
34 public interface NTLMEngine {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
35
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
36 String generateType1Msg(
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
37 String domain,
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
38 String workstation) throws NTLMEngineException;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
39
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
40 String generateType3Msg(
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
41 String username,
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
42 String password,
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
43 String domain,
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
44 String workstation,
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
45 String challenge) throws NTLMEngineException;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
46
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
47 }
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
48 -----------------------------------------------------------
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
49
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
50 Using Samba JCIFS as an NTLM engine
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
51 ===================================
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
52 Follow these instructions to build an NTLMEngine implementation using JCIFS
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
53 library
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
54
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
55 =========== !!!! DISCLAIMER !!!! ===========
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
56 HttpComponents project DOES _NOT_ SUPPORT the code provided below. Use it as
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
57 is at your own discretion.
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
58
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
59 * Download the latest jcifs library from the Samba web site [5]
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
60 * Implement NTLMEngine interface
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
61 -----------------------------------------------------------
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
62 import jcifs.ntlmssp.Type1Message;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
63 import jcifs.ntlmssp.Type2Message;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
64 import jcifs.ntlmssp.Type3Message;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
65 import jcifs.util.Base64;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
66
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
67 import org.apache.http.impl.auth.NTLMEngine;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
68 import org.apache.http.impl.auth.NTLMEngineException;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
69
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
70 public class JCIFSEngine implements NTLMEngine {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
71
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
72 public String generateType1Msg(
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
73 String domain,
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
74 String workstation) throws NTLMEngineException {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
75
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
76 Type1Message t1m = new Type1Message(
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
77 Type1Message.getDefaultFlags(),
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
78 domain,
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
79 workstation);
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
80 return Base64.encode(t1m.toByteArray());
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
81 }
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
82
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
83 public String generateType3Msg(
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
84 String username,
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
85 String password,
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
86 String domain,
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
87 String workstation,
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
88 String challenge) throws NTLMEngineException {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
89 Type2Message t2m;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
90 try {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
91 t2m = new Type2Message(Base64.decode(challenge));
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
92 } catch (IOException ex) {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
93 throw new NTLMEngineException("Invalid Type2 message", ex);
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
94 }
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
95 Type3Message t3m = new Type3Message(
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
96 t2m,
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
97 password,
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
98 domain,
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
99 username,
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
100 workstation);
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
101 return Base64.encode(t3m.toByteArray());
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
102 }
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
103
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
104 }
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
105 -----------------------------------------------------------
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
106 * Implement AuthSchemeFactory interface
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
107 -----------------------------------------------------------
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
108 import org.apache.http.auth.AuthScheme;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
109 import org.apache.http.auth.AuthSchemeFactory;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
110 import org.apache.http.impl.auth.NTLMScheme;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
111 import org.apache.http.params.HttpParams;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
112
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
113 public class NTLMSchemeFactory implements AuthSchemeFactory {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
114
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
115 public AuthScheme newInstance(final HttpParams params) {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
116 return new NTLMScheme(new JCIFSEngine());
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
117 }
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
118
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
119 }
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
120 -----------------------------------------------------------
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
121 * Register NTLMSchemeFactory with the HttpClient instance you want to NTLM
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
122 enable.
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
123 -----------------------------------------------------------
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
124 httpclient.getAuthSchemes().register("ntlm", new NTLMSchemeFactory());
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
125 -----------------------------------------------------------
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
126 * Set NTCredentials for the web server you are going to access.
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
127 -----------------------------------------------------------
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
128 httpclient.getCredentialsProvider().setCredentials(
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
129 new AuthScope("myserver", -1),
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
130 new NTCredentials("username", "password", "MYSERVER", "MYDOMAIN"));
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
131 -----------------------------------------------------------
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
132 * You are done.
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
133
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
134
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
135 Why this code is not distributed with HttpClient
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
136 ================================================
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
137 JCIFS is licensed under the Lesser General Public License (LGPL). This license
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
138 is not compatible with the Apache Licenses under which all Apache Software is
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
139 released. Lawyers of the Apache Software Foundation are currently investigating
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
140 under which conditions Apache software is allowed to make use of LGPL software.
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
141
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
142 -----------------------------------------------------------
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
143 [1] http://davenport.sourceforge.net/ntlm.html
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
144 [2] http://download.microsoft.com/download/a/e/6/ae6e4142-aa58-45c6-8dcf-a657e5900cd3/%5BMS-NLMP%5D.pdf
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
145 [3] http://download.microsoft.com/download/a/e/6/ae6e4142-aa58-45c6-8dcf-a657e5900cd3/%5BMS-NTHT%5D.pdf
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
146 [4] http://www.microsoft.com/interop/principles/default.mspx
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
147 [5] http://jcifs.samba.org/