1 25 26 package org.objectweb.jonas.security.realm.web.catalina55; 27 28 import java.security.Principal ; 29 import java.security.acl.Group ; 30 import java.security.cert.X509Certificate ; 31 import java.util.ArrayList ; 32 import java.util.Enumeration ; 33 import java.util.Iterator ; 34 35 import javax.security.auth.Subject ; 36 import javax.security.auth.login.AccountExpiredException ; 37 import javax.security.auth.login.CredentialExpiredException ; 38 import javax.security.auth.login.FailedLoginException ; 39 import javax.security.auth.login.LoginContext ; 40 import javax.security.auth.login.LoginException ; 41 42 import org.objectweb.util.monolog.api.BasicLevel; 43 import org.objectweb.util.monolog.api.Logger; 44 45 import org.objectweb.jonas.common.Log; 46 import org.objectweb.jonas.security.auth.callback.NoInputCallbackHandler; 47 48 import org.apache.catalina.LifecycleException; 49 import org.apache.catalina.realm.GenericPrincipal; 50 import org.apache.catalina.realm.RealmBase; 51 52 import org.objectweb.security.context.SecurityContext; 53 import org.objectweb.security.context.SecurityCurrent; 54 55 56 63 public class JAAS extends RealmBase { 64 65 68 private static final String NAME = "JRealmJAASCatalina50"; 69 70 73 private static final String INFO = "org.objectweb.jonas.security.realm.JRealmJAASCatalina50/1.0"; 74 75 78 private static final String JAAS_CONFIG_NAME = "tomcat"; 79 80 83 private static Logger logger = null; 84 85 88 private X509Certificate cert = null; 89 90 91 97 public String getInfo() { 98 return INFO; 99 } 100 101 107 public Principal authenticate(X509Certificate [] cert) { 108 String headerCertificate = "##DN##"; 109 String dn = headerCertificate.concat(cert[0].getSubjectDN().getName().replace('=', '#').replace(',', '%').replace(' ', '$')); 111 this.cert = cert[0]; 112 return authenticate(dn, ""); 113 } 114 115 124 public Principal authenticate(String username, String credentials) { 125 126 if (username == null) { 128 log("No username so no authentication"); 129 return null; 130 } 131 LoginContext loginContext = null; 133 try { 134 loginContext = new LoginContext (JAAS_CONFIG_NAME, new NoInputCallbackHandler(username, credentials, this.cert)); 135 } catch (LoginException e) { 136 logger.log(BasicLevel.ERROR, "loginException for user :" + username); 137 return null; 138 } 139 Subject subject = null; 141 try { 142 loginContext.login(); 143 subject = loginContext.getSubject(); 144 if (subject == null) { 145 if (logger.isLoggable(BasicLevel.ERROR)) { 146 logger.log(BasicLevel.ERROR, "failedLoginlogin for user :" + username); 147 } 148 return null; 149 } 150 } catch (AccountExpiredException e) { 151 if (logger.isLoggable(BasicLevel.ERROR)) { 152 logger.log(BasicLevel.ERROR, "accountExpired for user :" + username); 153 } 154 return null; 155 } catch (CredentialExpiredException e) { 156 if (logger.isLoggable(BasicLevel.ERROR)) { 157 logger.log(BasicLevel.ERROR, "credentialExpired for user :" + username); 158 } 159 return null; 160 } catch (FailedLoginException e) { 161 if (logger.isLoggable(BasicLevel.ERROR)) { 162 logger.log(BasicLevel.ERROR, "failedLogin for user :" + username); 163 } 164 return null; 165 } catch (LoginException e) { 166 if (logger.isLoggable(BasicLevel.ERROR)) { 167 logger.log(BasicLevel.ERROR, "loginException for user :" + username); 168 } 169 return null; 170 } 171 172 Iterator credentialsIterator = subject.getPrivateCredentials().iterator(); 174 String credential = (String ) credentialsIterator.next(); 175 176 Iterator iterator = subject.getPrincipals(Principal .class).iterator(); 178 String userName = null; 179 while (iterator.hasNext() && (userName == null)) { 180 Principal principal = (Principal ) iterator.next(); 181 if (!(principal instanceof Group )) { 182 userName = principal.getName(); 183 } 184 } 185 186 if (userName == null) { 188 logger.log(BasicLevel.ERROR, "No Username found in the subject"); 189 return null; 190 } 191 192 iterator = subject.getPrincipals(Group .class).iterator(); 194 ArrayList roles = new ArrayList (); 195 while (iterator.hasNext()) { 196 Group group = (Group ) iterator.next(); 197 Enumeration e = group.members(); 198 while (e.hasMoreElements()) { 199 Principal p = (Principal ) e.nextElement(); 200 roles.add(p.getName()); 201 } 202 } 203 204 GenericPrincipal principal = new GenericPrincipal(this, userName, credential, roles); 205 SecurityContext ctx = new SecurityContext(userName, roles); 207 SecurityCurrent current = SecurityCurrent.getCurrent(); 208 current.setSecurityContext(ctx); 209 210 return principal; 211 } 212 213 214 218 protected String getName() { 219 return NAME; 220 } 221 222 223 228 protected String getPassword(String username) { 229 return null; 230 } 231 232 233 238 protected Principal getPrincipal(String username) { 239 return null; 240 } 241 242 243 249 public synchronized void start() throws LifecycleException { 250 251 if (logger == null) { 252 logger = Log.getLogger(Log.JONAS_SECURITY_PREFIX); 253 } 254 255 super.start(); 257 258 } 259 260 261 267 public synchronized void stop() throws LifecycleException { 268 super.stop(); 270 } 271 272 273 278 protected void log(String message) { 279 if (logger.isLoggable(BasicLevel.DEBUG)) { 280 logger.log(BasicLevel.DEBUG, message); 281 } 282 } 283 284 285 } 286 | Popular Tags |