KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > maverick > http > NTLMAuthentication


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.maverick.http;
21
22 import java.io.IOException JavaDoc;
23
24 /**
25  *
26  * @author Lee David Painter <a HREF="mailto:lee@3sp.com">&lt;lee@3sp.com&gt;</a>
27  */

28 public class NTLMAuthentication extends HttpAuthenticator {
29
30     NTLM ntlm = new NTLM();
31     String JavaDoc host = ""; //$NON-NLS-1$
32
String JavaDoc domain = ""; //$NON-NLS-1$
33

34     String JavaDoc challenge = null;
35
36     private static final int INITIATED = 1;
37     private static final int TYPE1_MSG_GENERATED = 2;
38     private static final int TYPE2_MSG_RECEIVED = 3;
39     private static final int TYPE3_MSG_GENERATED = 4;
40     private static final int FAILED = Integer.MAX_VALUE;
41
42     int state;
43
44     boolean isAuthenticated = false;
45
46     public NTLMAuthentication(String JavaDoc uri, String JavaDoc host, int port, boolean secure) {
47         super("NTLM", uri, host, port, secure); //$NON-NLS-1$
48
this.state = INITIATED;
49     }
50
51     public boolean isStateless() {
52         return false;
53     }
54
55     public void setChallenge(String JavaDoc challenge) {
56
57     }
58
59     public void setDomain(String JavaDoc domain) {
60         this.domain = domain;
61     }
62
63     public void setCredentials(PasswordCredentials credentials) {
64
65         if (credentials != null && credentials.getUsername() != null && credentials.getUsername().indexOf('\\') > -1) {
66             int idx = credentials.getUsername().indexOf('\\');
67             domain = credentials.getUsername().substring(0, idx);
68
69             this.credentials = new PasswordCredentials(credentials.getUsername().substring(idx + 1), credentials.getPassword());
70
71         } else
72             this.credentials = credentials;
73     }
74
75     /**
76      * authenticate
77      *
78      * @param request HttpRequest
79      * @return boolean
80      * @throws ProxyException
81      * @todo Implement this com.maverick.proxy.http.HttpAuthenticator method
82      */

83     public void authenticate(HttpRequest request, HttpMethod method) throws IOException JavaDoc {
84
85         switch (state) {
86             case INITIATED:
87             {
88                 request.setHeaderField(authorizationHeader, "NTLM " + //$NON-NLS-1$
89
ntlm.getResponseFor(challenge,
90                         credentials.getUsername(),
91                         credentials.getPassword(),
92                         connection.getHost(),
93                         domain));
94                 this.state = TYPE1_MSG_GENERATED;
95                 break;
96             }
97             case TYPE2_MSG_RECEIVED:
98             {
99                 request.setHeaderField(authorizationHeader, "NTLM " + //$NON-NLS-1$
100
ntlm.getResponseFor(challenge,
101                         credentials.getUsername(),
102                         credentials.getPassword(),
103                         connection.getHost(),
104                         domain));
105                 this.state = TYPE3_MSG_GENERATED;
106                 break;
107             }
108             case TYPE3_MSG_GENERATED:
109             case TYPE1_MSG_GENERATED:
110             default:
111                 throw new IOException JavaDoc(Messages.getString("NTLMAuthentication.invalidState")); //$NON-NLS-1$
112

113         }
114     }
115
116     private void reset() {
117         state = INITIATED;
118         challenge = null;
119         ntlm = new NTLM();
120         domain = ""; //$NON-NLS-1$
121
}
122
123     public boolean wantsPrompt() {
124         return state == INITIATED && super.wantsPrompt();
125     }
126
127     public boolean canAuthenticate() {
128         return state == INITIATED || state == TYPE2_MSG_RECEIVED;
129     }
130
131     public int processResponse(HttpResponse response) {
132
133         if (response.getStatus() >= 200 && response.getStatus() < 400) {
134             reset();
135             return AUTHENTICATION_COMPLETED;
136         }
137         String JavaDoc[] challenges = response.getHeaderFields(authenticationHeader);
138
139         challenge = null;
140
141         for (int i = 0; i < challenges.length; i++) {
142             if (challenges[i].startsWith("NTLM")) { //$NON-NLS-1$
143
challenge = challenges[i];
144                 break;
145             }
146         }
147
148         if (challenge == null || challenge.equals("NTLM")) { //$NON-NLS-1$
149
reset();
150             return AUTHENTICATION_FAILED;
151         } else {
152             challenge = challenge.substring(5).trim();
153             this.state = TYPE2_MSG_RECEIVED;
154             return AUTHENTICATION_IN_PROGRESS;
155         }
156     }
157
158     public boolean isAuthenticated() {
159         return isAuthenticated;
160     }
161
162     void complete() {
163         reset();
164         super.complete();
165     }
166
167     public String JavaDoc getInformation() {
168         return domain;
169     }
170
171 }
172
Popular Tags