1 17 package org.apache.geronimo.jetty6; 18 19 import java.security.AccessControlContext ; 20 import java.security.AccessControlException ; 21 import java.security.Principal ; 22 import java.security.cert.X509Certificate ; 23 import java.util.HashMap ; 24 25 import javax.security.auth.Subject ; 26 import javax.security.auth.login.LoginContext ; 27 import javax.security.auth.login.LoginException ; 28 import javax.security.jacc.WebRoleRefPermission ; 29 30 import org.apache.commons.logging.Log; 31 import org.apache.commons.logging.LogFactory; 32 import org.apache.geronimo.security.ContextManager; 33 import org.apache.geronimo.security.realm.providers.CertificateCallbackHandler; 34 import org.apache.geronimo.security.realm.providers.ClearableCallbackHandler; 35 import org.apache.geronimo.security.realm.providers.PasswordCallbackHandler; 36 import org.mortbay.jetty.Request; 37 38 39 42 public class InternalJAASJettyRealm { 43 private static Log log = LogFactory.getLog(InternalJAASJettyRealm.class); 44 45 private final String securityRealmName; 46 private final HashMap <String , Principal > userMap = new HashMap <String , Principal >(); 47 private int count = 1; 48 49 public InternalJAASJettyRealm(String geronimoRealmName) { 50 this.securityRealmName = geronimoRealmName; 51 } 52 53 public String getSecurityRealmName() { 54 return securityRealmName; 55 } 56 57 public Principal getPrincipal(String username) { 58 return userMap.get(username); 59 } 60 61 public Principal authenticate(String username, Object credentials, Request request) { 62 try { 63 if ((username != null) && (!username.equals(""))) { 64 65 JAASJettyPrincipal userPrincipal = (JAASJettyPrincipal) userMap.get(username); 66 67 if (userPrincipal != null) { 70 userMap.remove(username); 71 } 72 73 ClearableCallbackHandler callbackHandler; 74 if (credentials instanceof char[]) { 75 char[] password = (char[]) credentials; 76 callbackHandler = new PasswordCallbackHandler(username, password); 77 } else if (credentials instanceof String ) { 78 char[] password = ((String ) credentials).toCharArray(); 79 callbackHandler = new PasswordCallbackHandler(username, password); 80 } else if (credentials instanceof X509Certificate []) { 81 X509Certificate [] certs = (X509Certificate []) credentials; 82 if (certs.length < 1) { 83 throw new LoginException ("no certificates supplied"); 84 } 85 callbackHandler = new CertificateCallbackHandler(certs[0]); 86 } else { 87 throw new LoginException ("Cannot extract credentials from class: " + credentials.getClass().getName()); 88 } 89 90 LoginContext loginContext = new LoginContext (securityRealmName, callbackHandler); 92 loginContext.login(); 93 callbackHandler.clear(); 94 95 Subject subject = ContextManager.getServerSideSubject(loginContext.getSubject()); 96 ContextManager.setCallers(subject, subject); 98 ContextManager.setNextCaller(subject); 99 100 userPrincipal = new JAASJettyPrincipal(username); 102 userPrincipal.setSubject(subject); 103 104 userMap.put(username, userPrincipal); 105 106 return userPrincipal; 107 } else { 108 log.debug("Login Failed - null userID"); 109 return null; 110 } 111 112 } catch (LoginException e) { 113 log.debug("Login Failed", e); 115 return null; 116 } 117 } 118 119 public void logout(Principal user) { 120 JAASJettyPrincipal principal = (JAASJettyPrincipal) user; 121 122 userMap.remove(principal.getName()); 123 ContextManager.unregisterSubject(principal.getSubject()); 124 } 125 126 public boolean reauthenticate(Principal user) { 127 129 Subject subject = ((JAASJettyPrincipal) user).getSubject(); 130 ContextManager.setCallers(subject, subject); 131 132 return (userMap.get(user.getName()) != null); 134 } 135 136 public void disassociate(Principal user) { 137 } 139 140 public boolean isUserInRole(Principal user, String role) { 141 if (user == null || role == null) { 142 return false; 143 } 144 145 AccessControlContext acc = ContextManager.getCurrentContext(); 146 try { 147 String servletName = InternalJettyServletHolder.getCurrentServletName(); 149 if (servletName.equals("jsp")) { 150 servletName = ""; 151 } 152 acc.checkPermission(new WebRoleRefPermission (servletName, role)); 153 } catch (AccessControlException e) { 154 return false; 155 } 156 return true; 157 } 158 159 public Principal pushRole(Principal user, String role) { 160 return user; 162 } 163 164 public Principal popRole(Principal user) { 165 return user; 166 } 167 168 public void addUse() { 169 count++; 170 } 171 172 public int removeUse() { 173 return count--; 174 } 175 176 } 177 | Popular Tags |