1 19 20 package com.maverick.http; 21 22 import java.io.IOException ; 23 24 28 public class NTLMAuthentication extends HttpAuthenticator { 29 30 NTLM ntlm = new NTLM(); 31 String host = ""; String domain = ""; 34 String 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 uri, String host, int port, boolean secure) { 47 super("NTLM", uri, host, port, secure); this.state = INITIATED; 49 } 50 51 public boolean isStateless() { 52 return false; 53 } 54 55 public void setChallenge(String challenge) { 56 57 } 58 59 public void setDomain(String 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 83 public void authenticate(HttpRequest request, HttpMethod method) throws IOException { 84 85 switch (state) { 86 case INITIATED: 87 { 88 request.setHeaderField(authorizationHeader, "NTLM " + 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 " + 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 (Messages.getString("NTLMAuthentication.invalidState")); 113 } 114 } 115 116 private void reset() { 117 state = INITIATED; 118 challenge = null; 119 ntlm = new NTLM(); 120 domain = ""; } 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 [] challenges = response.getHeaderFields(authenticationHeader); 138 139 challenge = null; 140 141 for (int i = 0; i < challenges.length; i++) { 142 if (challenges[i].startsWith("NTLM")) { challenge = challenges[i]; 144 break; 145 } 146 } 147 148 if (challenge == null || challenge.equals("NTLM")) { 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 getInformation() { 168 return domain; 169 } 170 171 } 172 | Popular Tags |