1 26 27 package org.objectweb.jonas.security.realm.web.jetty50; 28 29 import java.security.Principal ; 30 import java.security.acl.Group ; 31 import java.security.cert.X509Certificate ; 32 import java.util.ArrayList ; 33 import java.util.Enumeration ; 34 import java.util.Iterator ; 35 36 import javax.security.auth.Subject ; 37 import javax.security.auth.login.AccountExpiredException ; 38 import javax.security.auth.login.CredentialExpiredException ; 39 import javax.security.auth.login.FailedLoginException ; 40 import javax.security.auth.login.LoginContext ; 41 import javax.security.auth.login.LoginException ; 42 43 import org.mortbay.http.HttpRequest; 44 45 import org.objectweb.jonas.security.auth.callback.NoInputCallbackHandler; 46 47 import org.objectweb.security.context.SecurityContext; 48 import org.objectweb.security.context.SecurityCurrent; 49 50 import org.objectweb.util.monolog.api.BasicLevel; 51 52 60 public class JAAS extends Standard { 61 62 65 private static final String JAAS_CONFIG_NAME = "jetty"; 66 67 70 public JAAS() { 71 super(); 72 } 73 74 78 public JAAS(String name) { 79 super(); 80 setName(name); 81 } 82 83 90 public Principal authenticate(String username, Object credentials, HttpRequest request) { 91 92 if (username == null) { 94 return null; 95 } 96 97 Principal jettyPrincipal = (Principal ) getUsers().get(username); 98 if (jettyPrincipal != null) { 100 removeUser(username); 101 } 102 103 NoInputCallbackHandler noInputCH = null; 104 LoginContext loginContext = null; 105 if (credentials instanceof X509Certificate []) { 106 String headerCertificate = "##DN##"; 108 X509Certificate [] certs = (X509Certificate []) credentials; 109 110 username = certs[0].getSubjectDN().getName(); 111 String usernameCert = headerCertificate.concat(username.replace('=', '#').replace(',', '%').replace(' ', 112 '$')); 113 noInputCH = new NoInputCallbackHandler(usernameCert, "", certs[0]); 116 } else { 117 noInputCH = new NoInputCallbackHandler(username, (String ) credentials, null); 120 } 121 122 try { 124 loginContext = new LoginContext (JAAS_CONFIG_NAME, noInputCH); 125 } catch (LoginException e) { 126 getLogger().log(BasicLevel.WARN, "loginException : unable to create a LoginContext for : '" + username + "'. Error : " + e.getMessage()); 127 return null; 128 } 129 130 Subject subject = null; 132 try { 133 loginContext.login(); 134 subject = loginContext.getSubject(); 135 if (subject == null) { 136 getLogger().log(BasicLevel.ERROR, "failedLogin for user :" + username); 137 return null; 138 } 139 } catch (AccountExpiredException e) { 140 if (getLogger().isLoggable(BasicLevel.ERROR)) { 141 getLogger().log(BasicLevel.ERROR, "accountExpired for user :" + username); 142 } 143 return null; 144 } catch (CredentialExpiredException e) { 145 if (getLogger().isLoggable(BasicLevel.ERROR)) { 146 getLogger().log(BasicLevel.ERROR, "credentialExpired for user :" + username); 147 } 148 return null; 149 } catch (FailedLoginException e) { 150 if (getLogger().isLoggable(BasicLevel.ERROR)) { 151 getLogger().log(BasicLevel.ERROR, "failedLogin for user :" + username); 152 } 153 return null; 154 } catch (LoginException e) { 155 if (getLogger().isLoggable(BasicLevel.ERROR)) { 156 getLogger().log(BasicLevel.ERROR, "loginException for user :" + username); 157 } 158 return null; 159 } 160 161 166 Iterator iterator = subject.getPrincipals(Principal .class).iterator(); 168 String userName = null; 169 while (iterator.hasNext() && (userName == null)) { 170 Principal principal = (Principal ) iterator.next(); 171 if (!(principal instanceof Group )) { 172 userName = principal.getName(); 173 } 174 } 175 176 if (userName == null) { 178 getLogger().log(BasicLevel.ERROR, "No Username found in the subject"); 179 return null; 180 } 181 182 iterator = subject.getPrincipals(Group .class).iterator(); 184 ArrayList roles = new ArrayList (); 185 while (iterator.hasNext()) { 186 Group group = (Group ) iterator.next(); 187 Enumeration e = group.members(); 188 while (e.hasMoreElements()) { 189 Principal p = (Principal ) e.nextElement(); 190 roles.add(p.getName()); 191 } 192 } 193 194 JettyPrincipal principal = new JettyPrincipal(userName, roles); 196 197 SecurityContext ctx = new SecurityContext(userName, roles); 200 SecurityCurrent current = SecurityCurrent.getCurrent(); 201 current.setSecurityContext(ctx); 202 203 addUser(username, principal); 205 206 return principal; 207 } 208 209 } | Popular Tags |