1 17 18 19 package org.apache.catalina.realm; 20 21 22 import java.security.Principal ; 23 import java.io.File ; 24 import java.util.ArrayList ; 25 import java.util.HashMap ; 26 import java.util.Map ; 27 import org.apache.catalina.Container; 28 import org.apache.catalina.LifecycleException; 29 import org.apache.catalina.util.StringManager; 30 import org.apache.commons.logging.Log; 31 import org.apache.commons.logging.LogFactory; 32 import org.apache.tomcat.util.digester.Digester; 33 34 35 48 49 public class MemoryRealm extends RealmBase { 50 51 private static Log log = LogFactory.getLog(MemoryRealm.class); 52 53 55 56 59 private Container container = null; 60 61 62 65 private static Digester digester = null; 66 67 68 71 protected final String info = 72 "org.apache.catalina.realm.MemoryRealm/1.0"; 73 74 75 78 79 protected static final String name = "MemoryRealm"; 80 81 82 86 private String pathname = "conf/tomcat-users.xml"; 87 88 89 92 private Map principals = new HashMap (); 93 94 95 98 private static StringManager sm = 99 StringManager.getManager(Constants.Package); 100 101 102 105 private boolean started = false; 106 107 108 110 111 116 public String getInfo() { 117 118 return info; 119 120 } 121 122 123 126 public String getPathname() { 127 128 return pathname; 129 130 } 131 132 133 139 public void setPathname(String pathname) { 140 141 this.pathname = pathname; 142 143 } 144 145 146 148 149 157 public Principal authenticate(String username, String credentials) { 158 159 GenericPrincipal principal = 160 (GenericPrincipal) principals.get(username); 161 162 boolean validated = false; 163 if (principal != null) { 164 if (hasMessageDigest()) { 165 validated = (digest(credentials) 167 .equalsIgnoreCase(principal.getPassword())); 168 } else { 169 validated = 170 (digest(credentials).equals(principal.getPassword())); 171 } 172 } 173 174 if (validated) { 175 if (log.isDebugEnabled()) 176 log.debug(sm.getString("memoryRealm.authenticateSuccess", username)); 177 return (principal); 178 } else { 179 if (log.isDebugEnabled()) 180 log.debug(sm.getString("memoryRealm.authenticateFailure", username)); 181 return (null); 182 } 183 184 } 185 186 187 189 190 197 void addUser(String username, String password, String roles) { 198 199 ArrayList list = new ArrayList (); 201 roles += ","; 202 while (true) { 203 int comma = roles.indexOf(','); 204 if (comma < 0) 205 break; 206 String role = roles.substring(0, comma).trim(); 207 list.add(role); 208 roles = roles.substring(comma + 1); 209 } 210 211 GenericPrincipal principal = 213 new GenericPrincipal(this, username, password, list); 214 principals.put(username, principal); 215 216 } 217 218 219 221 222 226 protected synchronized Digester getDigester() { 227 228 if (digester == null) { 229 digester = new Digester(); 230 digester.setValidating(false); 231 digester.addRuleSet(new MemoryRuleSet()); 232 } 233 return (digester); 234 235 } 236 237 238 241 protected String getName() { 242 243 return (name); 244 245 } 246 247 248 251 protected String getPassword(String username) { 252 253 GenericPrincipal principal = 254 (GenericPrincipal) principals.get(username); 255 if (principal != null) { 256 return (principal.getPassword()); 257 } else { 258 return (null); 259 } 260 261 } 262 263 264 267 protected Principal getPrincipal(String username) { 268 269 return (Principal ) principals.get(username); 270 271 } 272 273 278 protected Map getPrincipals() { 279 return principals; 280 } 281 282 283 285 286 292 public synchronized void start() throws LifecycleException { 293 294 super.start(); 296 297 File file = new File (pathname); 299 if (!file.isAbsolute()) 300 file = new File (System.getProperty("catalina.base"), pathname); 301 if (!file.exists() || !file.canRead()) 302 throw new LifecycleException 303 (sm.getString("memoryRealm.loadExist", 304 file.getAbsolutePath())); 305 306 if (log.isDebugEnabled()) 308 log.debug(sm.getString("memoryRealm.loadPath", 309 file.getAbsolutePath())); 310 Digester digester = getDigester(); 311 try { 312 synchronized (digester) { 313 digester.push(this); 314 digester.parse(file); 315 } 316 } catch (Exception e) { 317 throw new LifecycleException("memoryRealm.readXml", e); 318 } finally { 319 digester.reset(); 320 } 321 322 } 323 324 325 331 public synchronized void stop() throws LifecycleException { 332 333 super.stop(); 335 336 338 } 339 340 341 } 342 | Popular Tags |