1 25 26 package org.objectweb.jonas.security.realm.web.catalina55; 27 28 import java.security.Principal ; 29 import java.security.cert.X509Certificate ; 30 import java.util.ArrayList ; 31 32 import org.apache.catalina.LifecycleException; 33 import org.apache.catalina.realm.GenericPrincipal; 34 import org.apache.catalina.realm.RealmBase; 35 36 import org.objectweb.jonas.common.Log; 37 import org.objectweb.jonas.security.SecurityService; 38 import org.objectweb.jonas.security.realm.factory.JResource; 39 import org.objectweb.jonas.security.realm.factory.JResourceException; 40 import org.objectweb.jonas.security.realm.principals.User; 41 import org.objectweb.jonas.service.ServiceManager; 42 43 import org.objectweb.security.context.SecurityContext; 44 import org.objectweb.security.context.SecurityCurrent; 45 46 import org.objectweb.util.monolog.api.BasicLevel; 47 import org.objectweb.util.monolog.api.Logger; 48 49 56 public class Standard extends RealmBase { 57 58 61 private static final String NAME = "JRealmCatalina50"; 62 63 66 private static final String INFO = "org.objectweb.jonas.security.realm.JRealmCatalina50/1.0"; 67 68 71 private static Logger logger = null; 72 73 77 private JResource jResource = null; 78 79 82 private String resourceName = null; 83 84 87 private SecurityService securityService = null; 88 89 96 public String getInfo() { 97 return INFO; 98 } 99 100 105 public String getResourceName() { 106 return resourceName; 107 } 108 109 114 public void setResourceName(String resourceName) { 115 this.resourceName = resourceName; 116 117 } 118 119 128 public Principal authenticate(String username, String credentials) { 129 130 if (username == null) { 132 if (logger.isLoggable(BasicLevel.DEBUG)) { 133 logger.log(BasicLevel.DEBUG, "No username so no authentication"); 134 } 135 return null; 136 } 137 138 User user = null; 140 try { 141 user = jResource.findUser(username); 142 } catch (Exception jre) { 143 logger.log(BasicLevel.ERROR, "Can not find the user : " + jre.getMessage()); 145 return null; 146 } 147 148 if (user == null) { 150 if (logger.isLoggable(BasicLevel.DEBUG)) { 151 logger.log(BasicLevel.DEBUG, "User " + username + " not found."); 152 } 153 return null; 154 } 155 156 boolean validated = jResource.isValidUser(user, credentials); 157 if (!validated) { 158 logger.log(BasicLevel.ERROR, "The password for the user " + username + " is not valid"); 159 return null; 160 } 161 162 ArrayList combinedRoles = null; 163 try { 164 combinedRoles = jResource.getArrayListCombinedRoles(user); 165 } catch (JResourceException jre) { 166 logger.log(BasicLevel.ERROR, jre.getMessage()); 167 return null; 168 } 169 170 GenericPrincipal principal = new GenericPrincipal(this, user.getName(), user.getPassword(), combinedRoles); 171 SecurityContext ctx = new SecurityContext(principal.getName(), combinedRoles); 172 SecurityCurrent current = SecurityCurrent.getCurrent(); 173 current.setSecurityContext(ctx); 174 175 return principal; 176 } 177 178 186 public Principal authenticate(X509Certificate [] cert) { 187 String dn = cert[0].getSubjectDN().getName(); 188 return authenticate(dn, "tomcat"); 189 } 190 191 196 protected String getName() { 197 return NAME; 198 } 199 200 206 protected String getPassword(String username) { 207 return null; 208 } 209 210 216 protected Principal getPrincipal(String username) { 217 return null; 218 } 219 220 226 public synchronized void start() throws LifecycleException { 227 228 if (logger == null) { 229 logger = Log.getLogger(Log.JONAS_SECURITY_PREFIX); 230 } 231 232 try { 234 securityService = (SecurityService) ServiceManager.getInstance().getSecurityService(); 235 } catch (Exception e) { 236 throw new LifecycleException("can't retrieve Security service"); 238 } 239 240 jResource = securityService.getJResource(resourceName); 242 if (jResource == null) { 243 throw new LifecycleException("Can't retrieve resource '" + resourceName + "' from the security service"); 244 } 245 246 super.start(); 248 249 } 250 251 257 public synchronized void stop() throws LifecycleException { 258 super.stop(); 260 261 jResource = null; 263 } 264 265 270 protected void log(String message) { 271 if (logger.isLoggable(BasicLevel.DEBUG)) { 272 logger.log(BasicLevel.DEBUG, message); 273 } 274 } 275 276 } | Popular Tags |