1 17 18 19 package org.apache.catalina.authenticator; 20 21 22 import java.io.IOException ; 23 import java.security.Principal ; 24 25 import javax.servlet.http.HttpServletResponse ; 26 27 import org.apache.catalina.connector.Request; 28 import org.apache.catalina.connector.Response; 29 import org.apache.catalina.deploy.LoginConfig; 30 import org.apache.catalina.util.Base64; 31 import org.apache.commons.logging.Log; 32 import org.apache.commons.logging.LogFactory; 33 import org.apache.tomcat.util.buf.ByteChunk; 34 import org.apache.tomcat.util.buf.CharChunk; 35 import org.apache.tomcat.util.buf.MessageBytes; 36 37 38 39 47 48 public class BasicAuthenticator 49 extends AuthenticatorBase { 50 private static Log log = LogFactory.getLog(BasicAuthenticator.class); 51 52 53 54 57 public static final byte[] AUTHENTICATE_BYTES = { 58 (byte) 'W', 59 (byte) 'W', 60 (byte) 'W', 61 (byte) '-', 62 (byte) 'A', 63 (byte) 'u', 64 (byte) 't', 65 (byte) 'h', 66 (byte) 'e', 67 (byte) 'n', 68 (byte) 't', 69 (byte) 'i', 70 (byte) 'c', 71 (byte) 'a', 72 (byte) 't', 73 (byte) 'e' 74 }; 75 76 77 79 80 83 protected static final String info = 84 "org.apache.catalina.authenticator.BasicAuthenticator/1.0"; 85 86 87 89 90 93 public String getInfo() { 94 95 return (info); 96 97 } 98 99 100 102 103 116 public boolean authenticate(Request request, 117 Response response, 118 LoginConfig config) 119 throws IOException { 120 121 Principal principal = request.getUserPrincipal(); 123 String ssoId = (String ) request.getNote(Constants.REQ_SSOID_NOTE); 124 if (principal != null) { 125 if (log.isDebugEnabled()) 126 log.debug("Already authenticated '" + principal.getName() + "'"); 127 if (ssoId != null) 129 associate(ssoId, request.getSessionInternal(true)); 130 return (true); 131 } 132 133 if (ssoId != null) { 135 if (log.isDebugEnabled()) 136 log.debug("SSO Id " + ssoId + " set; attempting " + 137 "reauthentication"); 138 144 if (reauthenticateFromSSO(ssoId, request)) 145 return true; 146 } 147 148 String username = null; 150 String password = null; 151 152 MessageBytes authorization = 153 request.getCoyoteRequest().getMimeHeaders() 154 .getValue("authorization"); 155 156 if (authorization != null) { 157 authorization.toBytes(); 158 ByteChunk authorizationBC = authorization.getByteChunk(); 159 if (authorizationBC.startsWithIgnoreCase("basic ", 0)) { 160 authorizationBC.setOffset(authorizationBC.getOffset() + 6); 161 164 CharChunk authorizationCC = authorization.getCharChunk(); 165 Base64.decode(authorizationBC, authorizationCC); 166 167 int colon = authorizationCC.indexOf(':'); 169 if (colon < 0) { 170 username = authorizationCC.toString(); 171 } else { 172 char[] buf = authorizationCC.getBuffer(); 173 username = new String (buf, 0, colon); 174 password = new String (buf, colon + 1, 175 authorizationCC.getEnd() - colon - 1); 176 } 177 178 authorizationBC.setOffset(authorizationBC.getOffset() - 6); 179 } 180 181 principal = context.getRealm().authenticate(username, password); 182 if (principal != null) { 183 register(request, response, principal, Constants.BASIC_METHOD, 184 username, password); 185 return (true); 186 } 187 } 188 189 190 MessageBytes authenticate = 192 response.getCoyoteResponse().getMimeHeaders() 193 .addValue(AUTHENTICATE_BYTES, 0, AUTHENTICATE_BYTES.length); 194 CharChunk authenticateCC = authenticate.getCharChunk(); 195 authenticateCC.append("Basic realm=\""); 196 if (config.getRealmName() == null) { 197 authenticateCC.append(request.getServerName()); 198 authenticateCC.append(':'); 199 authenticateCC.append(Integer.toString(request.getServerPort())); 200 } else { 201 authenticateCC.append(config.getRealmName()); 202 } 203 authenticateCC.append('\"'); 204 authenticate.toChars(); 205 response.sendError(HttpServletResponse.SC_UNAUTHORIZED); 206 return (false); 208 209 } 210 211 212 } 213 | Popular Tags |