1 21 22 package com.knowgate.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 com.knowgate.jcifs.smb.NtlmPasswordAuthentication; 32 33 import com.knowgate.misc.Base64Decoder; 34 import com.knowgate.misc.Base64Encoder; 35 36 import com.knowgate.jcifs.ntlmssp.NtlmFlags; 37 import com.knowgate.jcifs.ntlmssp.Type1Message; 38 import com.knowgate.jcifs.ntlmssp.Type2Message; 39 import com.knowgate.jcifs.ntlmssp.Type3Message; 40 41 54 55 public class NtlmSsp implements NtlmFlags { 56 57 68 public NtlmPasswordAuthentication doAuthentication( 69 HttpServletRequest req, HttpServletResponse resp, byte[] challenge) 70 throws IOException , ServletException { 71 return authenticate(req, resp, challenge); 72 } 73 74 83 public static NtlmPasswordAuthentication authenticate( 84 HttpServletRequest req, HttpServletResponse resp, byte[] challenge) 85 throws IOException , ServletException { 86 String msg = req.getHeader("Authorization"); 87 if (msg != null && msg.startsWith("NTLM ")) { 88 byte[] src = Base64Decoder.decodeToBytes(msg.substring(5)); 89 if (src[8] == 1) { 90 Type1Message type1 = new Type1Message(src); 91 Type2Message type2 = new Type2Message(type1, challenge, null); 92 msg = Base64Encoder.encode(type2.toByteArray()); 93 resp.setHeader( "WWW-Authenticate", "NTLM " + msg ); 94 } else if (src[8] == 3) { 95 Type3Message type3 = new Type3Message(src); 96 byte[] lmResponse = type3.getLMResponse(); 97 if (lmResponse == null) lmResponse = new byte[0]; 98 byte[] ntResponse = type3.getNTResponse(); 99 if (ntResponse == null) ntResponse = new byte[0]; 100 return new NtlmPasswordAuthentication(type3.getDomain(), 101 type3.getUser(), challenge, lmResponse, ntResponse); 102 } 103 } else { 104 resp.setHeader("WWW-Authenticate", "NTLM"); 105 resp.setHeader("Connection", "close"); 106 } 107 resp.setStatus(HttpServletResponse.SC_UNAUTHORIZED); 108 resp.setContentLength( 0 ); 109 resp.flushBuffer(); 110 return null; 111 } 112 113 } 114 115 | Popular Tags |