1 16 package org.mortbay.http; 17 18 import java.io.Externalizable ; 19 import java.io.IOException ; 20 import java.io.PrintStream ; 21 import java.security.Principal ; 22 import java.util.HashMap ; 23 import java.util.HashSet ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 import java.util.Map ; 27 import java.util.Properties ; 28 import java.util.StringTokenizer ; 29 30 import org.apache.commons.logging.Log; 31 import org.mortbay.log.LogFactory; 32 import org.mortbay.util.Credential; 33 import org.mortbay.util.Password; 34 import org.mortbay.util.Resource; 35 36 37 62 public class HashUserRealm 63 extends HashMap 64 implements UserRealm, SSORealm, Externalizable 65 { 66 private static Log log = LogFactory.getLog(HashUserRealm.class); 67 68 70 public static final String __SSO = "org.mortbay.http.SSO"; 71 72 73 private String _realmName; 74 private String _config; 75 protected HashMap _roles=new HashMap (7); 76 private SSORealm _ssoRealm; 77 78 79 80 82 public HashUserRealm() 83 {} 84 85 86 89 public HashUserRealm(String name) 90 { 91 _realmName=name; 92 } 93 94 95 99 public HashUserRealm(String name, String config) 100 throws IOException 101 { 102 _realmName=name; 103 load(config); 104 } 105 106 107 public void writeExternal(java.io.ObjectOutput out) 108 throws java.io.IOException 109 { 110 out.writeObject(_realmName); 111 out.writeObject(_config); 112 } 113 114 115 public void readExternal(java.io.ObjectInput in) 116 throws java.io.IOException , ClassNotFoundException 117 { 118 _realmName= (String )in.readObject(); 119 _config=(String )in.readObject(); 120 if (_config!=null) 121 load(_config); 122 } 123 124 125 126 133 public void load(String config) 134 throws IOException 135 { 136 _config=config; 137 if(log.isDebugEnabled())log.debug("Load "+this+" from "+config); 138 Properties properties = new Properties (); 139 Resource resource=Resource.newResource(config); 140 properties.load(resource.getInputStream()); 141 142 Iterator iter = properties.entrySet().iterator(); 143 while(iter.hasNext()) 144 { 145 Map.Entry entry = (Map.Entry )iter.next(); 146 147 String username=entry.getKey().toString().trim(); 148 String credentials=entry.getValue().toString().trim(); 149 String roles=null; 150 int c=credentials.indexOf(','); 151 if (c>0) 152 { 153 roles=credentials.substring(c+1).trim(); 154 credentials=credentials.substring(0,c).trim(); 155 } 156 157 if (username!=null && username.length()>0 && 158 credentials!=null && credentials.length()>0) 159 { 160 put(username,credentials); 161 if(roles!=null && roles.length()>0) 162 { 163 StringTokenizer tok = new StringTokenizer (roles,", "); 164 while (tok.hasMoreTokens()) 165 addUserToRole(username,tok.nextToken()); 166 } 167 } 168 } 169 } 170 171 172 175 public void setName(String name) 176 { 177 _realmName=name; 178 } 179 180 181 184 public String getName() 185 { 186 return _realmName; 187 } 188 189 190 public Principal getPrincipal(String username) 191 { 192 return (Principal )super.get(username); 193 } 194 195 196 public Principal authenticate(String username, 197 Object credentials, 198 HttpRequest request) 199 { 200 KnownUser user; 201 synchronized (this) 202 { 203 user = (KnownUser)super.get(username); 204 } 205 if (user==null) 206 return null; 207 208 if (user.authenticate(credentials)) 209 return user; 210 211 return null; 212 } 213 214 215 public void disassociate(Principal user) 216 { 217 } 218 219 220 public Principal pushRole(Principal user, String role) 221 { 222 if (user==null) 223 user=new User(); 224 225 return new WrappedUser(user,role); 226 } 227 228 229 public Principal popRole(Principal user) 230 { 231 WrappedUser wu = (WrappedUser)user; 232 return wu.getUserPrincipal(); 233 } 234 235 236 242 public synchronized Object put(Object name, Object credentials) 243 { 244 if (credentials instanceof Principal ) 245 return super.put(name.toString(), 246 credentials); 247 248 if (credentials instanceof Password) 249 return super.put(name, 250 new KnownUser(name.toString(), 251 (Password)credentials)); 252 if (credentials != null) 253 return super 254 .put(name, 255 new KnownUser(name.toString(), 256 Credential.getCredential(credentials.toString()))); 257 return null; 258 } 259 260 261 265 public synchronized void addUserToRole(String userName, String roleName) 266 { 267 HashSet userSet = (HashSet )_roles.get(roleName); 268 if (userSet==null) 269 { 270 userSet=new HashSet (11); 271 _roles.put(roleName,userSet); 272 } 273 userSet.add(userName); 274 } 275 276 277 public boolean reauthenticate(Principal user) 278 { 279 return ((User)user).isAuthenticated(); 280 } 281 282 283 288 public synchronized boolean isUserInRole(Principal user, String roleName) 289 { 290 if (user instanceof WrappedUser) 291 return ((WrappedUser)user).isUserInRole(roleName); 292 293 if (user==null || ((User)user).getUserRealm()!=this) 294 return false; 295 296 HashSet userSet = (HashSet )_roles.get(roleName); 297 return userSet!=null && userSet.contains(user.getName()); 298 } 299 300 301 public void logout(Principal user) 302 {} 303 304 305 public String toString() 306 { 307 return "Realm["+_realmName+"]"; 308 } 309 310 311 public void dump(PrintStream out) 312 { 313 out.println(this+":"); 314 out.println(super.toString()); 315 out.println(_roles); 316 } 317 318 319 322 public SSORealm getSSORealm() 323 { 324 return _ssoRealm; 325 } 326 327 328 332 public void setSSORealm(SSORealm ssoRealm) 333 { 334 _ssoRealm = ssoRealm; 335 } 336 337 338 public Credential getSingleSignOn(HttpRequest request, 339 HttpResponse response) 340 { 341 if (_ssoRealm!=null) 342 return _ssoRealm.getSingleSignOn(request,response); 343 return null; 344 } 345 346 347 348 public void setSingleSignOn(HttpRequest request, 349 HttpResponse response, 350 Principal principal, 351 Credential credential) 352 { 353 if (_ssoRealm!=null) 354 _ssoRealm.setSingleSignOn(request,response,principal,credential); 355 } 356 357 358 public void clearSingleSignOn(String username) 359 { 360 if (_ssoRealm!=null) 361 _ssoRealm.clearSingleSignOn(username); 362 } 363 364 365 366 367 private class User implements Principal 368 { 369 List roles=null; 370 371 372 private UserRealm getUserRealm() 373 { 374 return HashUserRealm.this; 375 } 376 377 public String getName() 378 { 379 return "Anonymous"; 380 } 381 382 public boolean isAuthenticated() 383 { 384 return false; 385 } 386 387 public String toString() 388 { 389 return getName(); 390 } 391 } 392 393 394 395 396 private class KnownUser extends User 397 { 398 private String _userName; 399 private Credential _cred; 400 401 402 KnownUser(String name,Credential credential) 403 { 404 _userName=name; 405 _cred=credential; 406 } 407 408 409 boolean authenticate(Object credentials) 410 { 411 return _cred!=null && _cred.check(credentials); 412 } 413 414 415 public String getName() 416 { 417 return _userName; 418 } 419 420 421 public boolean isAuthenticated() 422 { 423 return true; 424 } 425 } 426 427 428 429 430 private class WrappedUser extends User 431 { 432 private Principal user; 433 private String role; 434 435 WrappedUser(Principal user, String role) 436 { 437 this.user=user; 438 this.role=role; 439 } 440 441 Principal getUserPrincipal() 442 { 443 return user; 444 } 445 446 public String getName() 447 { 448 return "role:"+role; 449 } 450 451 public boolean isAuthenticated() 452 { 453 return true; 454 } 455 456 public boolean isUserInRole(String role) 457 { 458 return this.role.equals(role); 459 } 460 } 461 } 462 | Popular Tags |