00001
00026 package org.objectweb.jonas.security.realm.web.catalina50;
00027
00028 import java.security.Principal;
00029 import java.security.cert.X509Certificate;
00030 import java.util.ArrayList;
00031
00032 import org.apache.catalina.LifecycleException;
00033 import org.apache.catalina.realm.GenericPrincipal;
00034 import org.apache.catalina.realm.RealmBase;
00035
00036 import org.objectweb.jonas.common.Log;
00037 import org.objectweb.jonas.security.SecurityService;
00038 import org.objectweb.jonas.security.realm.factory.JResource;
00039 import org.objectweb.jonas.security.realm.factory.JResourceException;
00040 import org.objectweb.jonas.security.realm.principals.User;
00041 import org.objectweb.jonas.service.ServiceManager;
00042
00043 import org.objectweb.security.context.SecurityContext;
00044 import org.objectweb.security.context.SecurityCurrent;
00045
00046 import org.objectweb.util.monolog.api.BasicLevel;
00047 import org.objectweb.util.monolog.api.Logger;
00048
00056 public class Standard extends RealmBase {
00057
00061 private static final String NAME = "JRealmCatalina50";
00062
00066 private static final String INFO = "org.objectweb.jonas.security.realm.JRealmCatalina50/1.0";
00067
00071 private static Logger logger = null;
00072
00077 private JResource jResource = null;
00078
00082 private String resourceName = null;
00083
00087 private SecurityService securityService = null;
00088
00096 public String getInfo() {
00097 return INFO;
00098 }
00099
00105 public String getResourceName() {
00106 return resourceName;
00107 }
00108
00114 public void setResourceName(String resourceName) {
00115 this.resourceName = resourceName;
00116
00117 }
00118
00128 public Principal authenticate(String username, String credentials) {
00129
00130
00131 if (username == null) {
00132 logger.log(BasicLevel.DEBUG, "No username so no authentication");
00133 return null;
00134 }
00135
00136
00137 User user = null;
00138 try {
00139 user = jResource.findUser(username);
00140 } catch (Exception jre) {
00141
00142 logger.log(BasicLevel.ERROR, "Can not find the user : " + jre.getMessage());
00143 return null;
00144 }
00145
00146
00147 if (user == null) {
00148 logger.log(BasicLevel.DEBUG, "User " + username + " not found.");
00149 return null;
00150 }
00151
00152 boolean validated = jResource.isValidUser(user, credentials);
00153 if (!validated) {
00154 logger.log(BasicLevel.ERROR, "The password for the user " + username + " is not valid");
00155 return null;
00156 }
00157
00158 ArrayList combinedRoles = null;
00159 try {
00160 combinedRoles = jResource.getArrayListCombinedRoles(user);
00161 } catch (JResourceException jre) {
00162 logger.log(BasicLevel.ERROR, jre.getMessage());
00163 return null;
00164 }
00165
00166 GenericPrincipal principal = new GenericPrincipal(this, user.getName(), user.getPassword(), combinedRoles);
00167 SecurityContext ctx = new SecurityContext(principal.getName(), combinedRoles);
00168 SecurityCurrent current = SecurityCurrent.getCurrent();
00169 current.setSecurityContext(ctx);
00170
00171 return principal;
00172 }
00173
00182 public Principal authenticate(X509Certificate[] cert) {
00183 String dn = cert[0].getSubjectDN().getName();
00184 return authenticate(dn, "tomcat");
00185 }
00186
00192 protected String getName() {
00193 return NAME;
00194 }
00195
00202 protected String getPassword(String username) {
00203 return null;
00204 }
00205
00212 protected Principal getPrincipal(String username) {
00213 return null;
00214 }
00215
00222 public synchronized void start() throws LifecycleException {
00223
00224 if (logger == null) {
00225 logger = Log.getLogger(Log.JONAS_SECURITY_PREFIX);
00226 }
00227
00228
00229 try {
00230 securityService = (SecurityService) ServiceManager.getInstance().getSecurityService();
00231 } catch (Exception e) {
00232
00233 throw new LifecycleException("can't retrieve Security service");
00234 }
00235
00236
00237 jResource = securityService.getJResource(resourceName);
00238 if (jResource == null) {
00239 throw new LifecycleException("Can't retrieve resource '" + resourceName + "' from the security service");
00240 }
00241
00242
00243 super.start();
00244
00245 }
00246
00253 public synchronized void stop() throws LifecycleException {
00254
00255 super.stop();
00256
00257
00258 jResource = null;
00259 }
00260
00266 protected void log(String message) {
00267 logger.log(BasicLevel.DEBUG, message);
00268 }
00269
00270 }