1 28 29 package com.caucho.server.security; 30 31 import com.caucho.config.Config; 32 import com.caucho.config.ConfigException; 33 import com.caucho.config.types.InitParam; 34 35 import javax.annotation.PostConstruct; 36 import javax.security.auth.Subject ; 37 import javax.security.auth.callback.Callback ; 38 import javax.security.auth.callback.CallbackHandler ; 39 import javax.security.auth.callback.NameCallback ; 40 import javax.security.auth.callback.PasswordCallback ; 41 import javax.security.auth.callback.UnsupportedCallbackException ; 42 import javax.security.auth.login.LoginException ; 43 import javax.security.auth.spi.LoginModule ; 44 import javax.servlet.ServletContext ; 45 import javax.servlet.ServletException ; 46 import javax.servlet.http.HttpServletRequest ; 47 import javax.servlet.http.HttpServletResponse ; 48 import java.io.IOException ; 49 import java.lang.reflect.Method ; 50 import java.security.Principal ; 51 import java.util.HashMap ; 52 import java.util.Iterator ; 53 import java.util.Set ; 54 import java.util.logging.Level ; 55 56 59 public class JaasAuthenticator extends AbstractAuthenticator { 60 private Class _loginModuleClass; 61 62 private HashMap <String ,String > _options = 63 new HashMap <String ,String >(); 64 65 68 public void setLoginModule(Class loginModuleClass) 69 throws ConfigException 70 { 71 _loginModuleClass = loginModuleClass; 72 73 Config.checkCanInstantiate(loginModuleClass); 74 75 if (! LoginModule .class.isAssignableFrom(loginModuleClass)) 76 throw new ConfigException(L.l("`{0}' must implement javax.security.auth.spi.LoginModule", 77 loginModuleClass.getName())); 78 } 79 80 public void setInitParam(InitParam init) 81 { 82 _options.putAll(init.getParameters()); 83 } 84 85 public void setOptions(InitParam init) 86 { 87 _options.putAll(init.getParameters()); 88 } 89 90 93 @PostConstruct 94 public synchronized void init() 95 throws ServletException 96 { 97 super.init(); 98 99 if (_loginModuleClass == null) 100 throw new ServletException (L.l("JaasAuthenticator requires login-module")); 101 } 102 103 106 protected Principal loginImpl(HttpServletRequest request, 107 HttpServletResponse response, 108 ServletContext application, 109 String userName, String password) 110 throws ServletException 111 { 112 try { 113 LoginModule login = (LoginModule ) _loginModuleClass.newInstance(); 114 Subject subject = new Subject (); 115 116 HashMap <String ,String > state = new HashMap <String ,String >(); 117 118 state.put("javax.security.auth.login.name", userName); 119 state.put("javax.security.auth.login.password", password); 120 121 login.initialize(subject, 122 new Handler (userName, password), 123 state, _options); 124 125 try { 126 login.login(); 127 } catch (Exception e) { 128 login.abort(); 129 } 130 131 login.commit(); 132 133 Set principals = subject.getPrincipals(); 134 135 if (principals == null || principals.size() == 0) 136 return null; 137 138 Iterator iter = principals.iterator(); 139 if (iter.hasNext()) 140 return (Principal ) iter.next(); 141 142 return null; 143 } catch (LoginException e) { 144 log.log(Level.FINE, e.toString(), e); 145 146 return null; 147 } catch (Throwable e) { 148 log.log(Level.WARNING, e.toString(), e); 149 150 return null; 151 } 152 } 153 154 161 public boolean isUserInRole(HttpServletRequest request, 162 HttpServletResponse response, 163 ServletContext application, 164 Principal principal, String role) 165 throws ServletException 166 { 167 if (principal == null) 168 return false; 169 170 Class principalCl = principal.getClass(); 171 172 try { 173 Method isUserInRole = principalCl.getMethod("isUserInRole", 174 new Class [] { String .class }); 175 176 if (isUserInRole != null) 177 return Boolean.TRUE.equals(isUserInRole.invoke(principal, role)); 178 } catch (Throwable e) { 179 log.log(Level.FINER, e.toString(), e); 180 } 181 182 try { 183 Method getRoles = principalCl.getMethod("getRoles", new Class [] { }); 184 185 if (getRoles != null) { 186 Set roles = (Set ) getRoles.invoke(principal); 187 188 return roles != null && roles.contains(role); 189 } 190 } catch (Throwable e) { 191 log.log(Level.FINER, e.toString(), e); 192 } 193 194 return principal != null; 195 } 196 197 static class Handler implements CallbackHandler { 198 private String _userName; 199 private String _password; 200 201 Handler(String userName, String password) 202 { 203 _userName = userName; 204 _password = password; 205 } 206 207 public void handle(Callback []callbacks) 208 throws IOException , UnsupportedCallbackException 209 { 210 for (int i = 0; i < callbacks.length; i++) { 211 Callback cb = callbacks[i]; 212 213 if (cb instanceof NameCallback ) { 214 NameCallback name = (NameCallback ) cb; 215 216 name.setName(_userName); 217 } 218 else if (cb instanceof PasswordCallback ) { 219 PasswordCallback password = (PasswordCallback ) cb; 220 221 password.setPassword(_password.toCharArray()); 222 } 223 } 224 } 225 } 226 } 227 | Popular Tags |