KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > httpclient > auth > NTLMScheme


1 /*
2  * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/auth/NTLMScheme.java,v 1.6.2.3 2004/02/22 18:21:15 olegk Exp $
3  * $Revision: 1.6.2.3 $
4  * $Date: 2004/02/22 18:21:15 $
5  *
6  * ====================================================================
7  *
8  * Copyright 2002-2004 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  * ====================================================================
22  *
23  * This software consists of voluntary contributions made by many
24  * individuals on behalf of the Apache Software Foundation. For more
25  * information on the Apache Software Foundation, please see
26  * <http://www.apache.org/>.
27  *
28  * [Additional notices, if required by prior licensing conditions]
29  *
30  */

31
32 package org.apache.commons.httpclient.auth;
33
34 import org.apache.commons.httpclient.HttpException;
35 import org.apache.commons.httpclient.NTLM;
36 import org.apache.commons.httpclient.Credentials;
37 import org.apache.commons.httpclient.NTCredentials;
38 import org.apache.commons.logging.Log;
39 import org.apache.commons.logging.LogFactory;
40
41 /** An implementation of the Microsoft proprietary NTLM authentication scheme. For a detailed
42  * explanation of the NTLM scheme please see <a HREF="http://davenport.sourceforge.net/ntlm.html">
43  * http://davenport.sourceforge.net/ntlm.html</a>.
44  *
45  * @author <a HREF="mailto:remm@apache.org">Remy Maucherat</a>
46  * @author Rodney Waldhoff
47  * @author <a HREF="mailto:jsdever@apache.org">Jeff Dever</a>
48  * @author Ortwin Glück
49  * @author Sean C. Sullivan
50  * @author <a HREF="mailto:adrian@ephox.com">Adrian Sutton</a>
51  * @author <a HREF="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
52  * @author <a HREF="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
53  */

54 public class NTLMScheme extends AuthSchemeBase {
55
56     /** Log object for this class. */
57     private static final Log LOG = LogFactory.getLog(NTLMScheme.class);
58
59     /** NTLM challenge string. */
60     private String JavaDoc ntlmchallenge = null;
61
62     /**
63      * Constructor for the NTLM authentication scheme.
64      *
65      * @param challenge The authentication challenge
66      *
67      * @throws MalformedChallengeException is thrown if the authentication challenge
68      * is malformed
69      */

70     public NTLMScheme(final String JavaDoc challenge) throws MalformedChallengeException {
71         super(challenge);
72         String JavaDoc s = AuthChallengeParser.extractScheme(challenge);
73         if (!s.equalsIgnoreCase(getSchemeName())) {
74             throw new MalformedChallengeException("Invalid NTLM challenge: " + challenge);
75         }
76         int i = challenge.indexOf(' ');
77         if (i != -1) {
78             s = challenge.substring(i, challenge.length());
79             this.ntlmchallenge = s.trim();
80         } else {
81             this.ntlmchallenge = "";
82         }
83     }
84
85     /**
86      * Returns textual designation of the NTLM authentication scheme.
87      *
88      * @return <code>ntlm</code>
89      */

90     public String JavaDoc getSchemeName() {
91         return "ntlm";
92     }
93
94     /**
95      * The concept of an authentication realm is not supported by the NTLM
96      * authentication scheme. Always returns <code>null</code>.
97      *
98      * @return <code>null</code>
99      */

100     public String JavaDoc getRealm() {
101         return null;
102     }
103     
104     /**
105      * Returns a String identifying the authentication challenge. This is
106      * used, in combination with the host and port to determine if
107      * authorization has already been attempted or not. Schemes which
108      * require multiple requests to complete the authentication should
109      * return a different value for each stage in the request.
110      *
111      * <p>Additionally, the ID should take into account any changes to the
112      * authentication challenge and return a different value when appropriate.
113      * For example when the realm changes in basic authentication it should be
114      * considered a different authentication attempt and a different value should
115      * be returned.</p>
116      *
117      * @return String a String identifying the authentication challenge. The
118      * returned value may be null.
119      */

120     public String JavaDoc getID() {
121         return ntlmchallenge;
122     }
123     
124
125     /**
126      * Returns the authentication parameter with the given name, if available.
127      *
128      * <p>There are no valid parameters for NTLM authentication so this method always returns
129      * <tt>null</tt>.</p>
130      *
131      * @param name The name of the parameter to be returned
132      *
133      * @return the parameter with the given name
134      */

135     public String JavaDoc getParameter(String JavaDoc name) {
136         if (name == null) {
137             throw new IllegalArgumentException JavaDoc("Parameter name may not be null");
138         }
139         return null;
140     }
141
142     /**
143      * Create a NTLM authorization string for the given
144      * challenge and NT credentials.
145      *
146      * @param challenge The challenge.
147      * @param credentials {@link NTCredentials}
148      *
149      * @return a ntlm authorization string
150      * @throws AuthenticationException is thrown if authentication fails
151      */

152     public static String JavaDoc authenticate(
153      final NTCredentials credentials, final String JavaDoc challenge)
154       throws AuthenticationException {
155
156         LOG.trace("enter NTLMScheme.authenticate(NTCredentials, String)");
157
158         if (credentials == null) {
159             throw new IllegalArgumentException JavaDoc("Credentials may not be null");
160         }
161         
162         NTLM ntlm = new NTLM();
163         String JavaDoc s = null;
164         try {
165             s = ntlm.getResponseFor(challenge,
166               credentials.getUserName(), credentials.getPassword(),
167               credentials.getHost(), credentials.getDomain());
168         } catch (HttpException e) {
169             throw new AuthenticationException(e.getMessage());
170         }
171         return "NTLM " + s;
172     }
173     
174     /**
175      * Produces NTLM authorization string for the given set of
176      * {@link Credentials}.
177      *
178      * @param credentials The set of credentials to be used for athentication
179      * @param method Method name is ignored by the NTLM authentication scheme
180      * @param uri URI is ignored by the NTLM authentication scheme
181      * @throws AuthenticationException if authorization string cannot
182      * be generated due to an authentication failure
183      *
184      * @return an NTLM authorization string
185      */

186     public String JavaDoc authenticate(Credentials credentials, String JavaDoc method, String JavaDoc uri)
187       throws AuthenticationException {
188         LOG.trace("enter NTLMScheme.authenticate(Credentials, String, String)");
189
190         NTCredentials ntcredentials = null;
191         try {
192             ntcredentials = (NTCredentials) credentials;
193         } catch (ClassCastException JavaDoc e) {
194             throw new AuthenticationException(
195              "Credentials cannot be used for NTLM authentication: "
196               + credentials.getClass().getName());
197         }
198         return NTLMScheme.authenticate(ntcredentials, this.ntlmchallenge);
199     }
200 }
201
Popular Tags