1 21 22 package jcifs.http; 23 24 import java.io.IOException ; 25 26 import javax.servlet.ServletException ; 27 28 import javax.servlet.http.HttpServletRequest ; 29 import javax.servlet.http.HttpServletResponse ; 30 31 import jcifs.smb.NtlmPasswordAuthentication; 32 33 import jcifs.util.Base64; 34 35 import jcifs.ntlmssp.NtlmFlags; 36 import jcifs.ntlmssp.Type1Message; 37 import jcifs.ntlmssp.Type2Message; 38 import jcifs.ntlmssp.Type3Message; 39 40 52 53 public class NtlmSsp implements NtlmFlags { 54 55 66 public NtlmPasswordAuthentication doAuthentication( 67 HttpServletRequest req, HttpServletResponse resp, byte[] challenge) 68 throws IOException , ServletException { 69 return authenticate(req, resp, challenge); 70 } 71 72 81 public static NtlmPasswordAuthentication authenticate( 82 HttpServletRequest req, HttpServletResponse resp, byte[] challenge) 83 throws IOException , ServletException { 84 String msg = req.getHeader("Authorization"); 85 if (msg != null && msg.startsWith("NTLM ")) { 86 byte[] src = Base64.decode(msg.substring(5)); 87 if (src[8] == 1) { 88 Type1Message type1 = new Type1Message(src); 89 Type2Message type2 = new Type2Message(type1, challenge, null); 90 msg = Base64.encode(type2.toByteArray()); 91 resp.setHeader( "WWW-Authenticate", "NTLM " + msg ); 92 } else if (src[8] == 3) { 93 Type3Message type3 = new Type3Message(src); 94 byte[] lmResponse = type3.getLMResponse(); 95 if (lmResponse == null) lmResponse = new byte[0]; 96 byte[] ntResponse = type3.getNTResponse(); 97 if (ntResponse == null) ntResponse = new byte[0]; 98 return new NtlmPasswordAuthentication(type3.getDomain(), 99 type3.getUser(), challenge, lmResponse, ntResponse); 100 } 101 } else { 102 resp.setHeader("WWW-Authenticate", "NTLM"); 103 } 104 resp.setStatus(HttpServletResponse.SC_UNAUTHORIZED); 105 resp.flushBuffer(); 106 return null; 107 } 108 109 } 110 111 | Popular Tags |