1 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 54 public class NTLMScheme extends AuthSchemeBase { 55 56 57 private static final Log LOG = LogFactory.getLog(NTLMScheme.class); 58 59 60 private String ntlmchallenge = null; 61 62 70 public NTLMScheme(final String challenge) throws MalformedChallengeException { 71 super(challenge); 72 String 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 90 public String getSchemeName() { 91 return "ntlm"; 92 } 93 94 100 public String getRealm() { 101 return null; 102 } 103 104 120 public String getID() { 121 return ntlmchallenge; 122 } 123 124 125 135 public String getParameter(String name) { 136 if (name == null) { 137 throw new IllegalArgumentException ("Parameter name may not be null"); 138 } 139 return null; 140 } 141 142 152 public static String authenticate( 153 final NTCredentials credentials, final String challenge) 154 throws AuthenticationException { 155 156 LOG.trace("enter NTLMScheme.authenticate(NTCredentials, String)"); 157 158 if (credentials == null) { 159 throw new IllegalArgumentException ("Credentials may not be null"); 160 } 161 162 NTLM ntlm = new NTLM(); 163 String 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 186 public String authenticate(Credentials credentials, String method, String 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 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 |