1 27 28 package org.objectweb.jonas.security.realm.web.jetty50; 29 30 import java.security.Principal ; 31 import java.util.ArrayList ; 32 import java.util.HashMap ; 33 import java.util.Map ; 34 35 import org.mortbay.http.HttpRequest; 36 import org.mortbay.http.UserRealm; 37 38 import org.objectweb.jonas.common.Log; 39 import org.objectweb.jonas.security.SecurityService; 40 import org.objectweb.jonas.security.realm.factory.JResource; 41 import org.objectweb.jonas.security.realm.principals.User; 42 import org.objectweb.jonas.service.ServiceManager; 43 44 import org.objectweb.security.context.SecurityContext; 45 import org.objectweb.security.context.SecurityCurrent; 46 47 import org.objectweb.util.monolog.api.BasicLevel; 48 import org.objectweb.util.monolog.api.Logger; 49 50 57 public class Standard implements UserRealm { 58 59 62 private static Logger logger = null; 63 64 67 private String name; 68 69 73 private JResource jResource = null; 74 75 78 private SecurityService securityService = null; 79 80 83 private Map users = null; 84 85 88 protected Standard() { 89 users = new HashMap (); 90 91 if (logger == null) { 92 logger = Log.getLogger(Log.JONAS_SECURITY_PREFIX); 93 } 94 } 95 96 101 public Standard(String resourceName) throws Exception { 102 this(); 103 104 try { 106 securityService = (SecurityService) ServiceManager.getInstance().getSecurityService(); 107 } catch (Exception e) { 108 throw new Exception ("can't retrieve Security service", e); 110 } 111 112 jResource = securityService.getJResource(resourceName); 114 if (jResource == null) { 115 throw new Exception ("Can't retrieve resource " + resourceName + "from the security service"); 116 } 117 } 118 119 125 public Standard(String name, String resourceName) throws Exception { 126 this(resourceName); 127 this.name = name; 128 } 129 130 133 public String getName() { 134 return name; 135 } 136 137 144 public Principal authenticate(String username, Object credentials, HttpRequest request) { 145 146 if (username == null) { 148 return null; 149 } 150 151 Principal jettyPrincipal = (Principal ) users.get(username); 152 if (jettyPrincipal != null) { 154 users.remove(username); 155 } 156 157 User user = null; 159 try { 160 user = jResource.findUser(username); 161 } catch (Exception jre) { 162 logger.log(BasicLevel.INFO, jre.getMessage()); 164 return null; 165 } 166 167 if (user == null) { 169 if (logger.isLoggable(BasicLevel.DEBUG)) { 170 logger.log(BasicLevel.DEBUG, "User " + username + " not found."); 171 } 172 return null; 173 } 174 175 if (!(credentials instanceof String )) { 176 logger.log(BasicLevel.ERROR, "Allow only string type as credentials"); 177 return null; 178 } 179 180 boolean validated = jResource.isValidUser(user, (String ) credentials); 181 182 if (!validated) { 183 logger.log(BasicLevel.INFO, "The password for the user " + username + " is not valid"); 184 return null; 185 } 186 187 ArrayList combinedRoles = null; 188 try { 189 combinedRoles = jResource.getArrayListCombinedRoles(user); 190 } catch (Exception jre) { 191 logger.log(BasicLevel.ERROR, jre.getMessage()); 192 return null; 193 } 194 195 Principal principal = new JettyPrincipal(user.getName(), combinedRoles); 196 SecurityContext ctx = new SecurityContext(principal.getName(), combinedRoles); 197 SecurityCurrent current = SecurityCurrent.getCurrent(); 198 current.setSecurityContext(ctx); 199 200 users.put(username, principal); 202 203 return principal; 204 } 205 206 212 public synchronized boolean isUserInRole(Principal user, String roleName) { 213 if (user == null) { 214 return false; 215 } 216 217 if (user instanceof JettyPrincipal) { 218 return ((JettyPrincipal) user).isUserInRole(roleName); 219 } else { 220 logger.log(BasicLevel.ERROR, "The user '" + user + "' is not instance of JettyPrincipal"); 221 return false; 222 } 223 } 224 225 230 public boolean isAuthenticated(Principal user) { 231 if (user == null) { 232 return false; 233 } 234 235 if (user instanceof JettyPrincipal) { 236 return ((JettyPrincipal) user).isAuthenticated(); 237 } else { 238 logger.log(BasicLevel.ERROR, "The user '" + user + "' is not instance of JettyPrincipal"); 239 return false; 240 } 241 } 242 243 248 public Principal getPrincipal(String username) { 249 if (logger.isLoggable(BasicLevel.DEBUG)) { 250 logger.log(BasicLevel.DEBUG, "Get principal with username '" + username + "'."); 251 } 252 253 JettyPrincipal principal = (JettyPrincipal) users.get(username); 254 SecurityContext ctx = new SecurityContext(principal.getName(), principal.getRoles()); 255 SecurityCurrent current = SecurityCurrent.getCurrent(); 256 current.setSecurityContext(ctx); 257 return principal; 258 } 259 260 264 public void disassociate(Principal user) { 265 } 266 267 273 public Principal pushRole(Principal user, String role) { 274 return user; 275 } 276 277 282 public Principal popRole(Principal user) { 283 return user; 284 } 285 286 290 public void logout(Principal user) { 291 } 292 293 298 public boolean reauthenticate(Principal user) { 299 if (user instanceof JettyPrincipal) { 300 return ((JettyPrincipal) user).isAuthenticated(); 301 } else { 302 return false; 303 } 304 } 305 306 309 protected static Logger getLogger() { 310 return logger; 311 } 312 313 316 protected Map getUsers() { 317 return users; 318 } 319 320 324 protected void removeUser(String username) { 325 users.remove(username); 326 } 327 328 333 protected void addUser(String username, Principal principal) { 334 users.put(username, principal); 335 } 336 337 341 protected void setName(String name) { 342 this.name = name; 343 } 344 } | Popular Tags |