1 17 package org.apache.geronimo.jetty; 18 19 import java.security.AccessControlContext ; 20 import java.security.AccessControlException ; 21 import java.security.Principal ; 22 import java.util.HashMap ; 23 import javax.security.auth.Subject ; 24 import javax.security.auth.login.LoginContext ; 25 import javax.security.auth.login.LoginException ; 26 import javax.security.jacc.WebRoleRefPermission ; 27 28 import org.apache.commons.logging.Log; 29 import org.apache.commons.logging.LogFactory; 30 import org.apache.geronimo.security.ContextManager; 31 import org.apache.geronimo.jetty.interceptor.SecurityContextBeforeAfter; 32 import org.mortbay.http.HttpRequest; 33 import org.mortbay.http.UserRealm; 34 35 36 39 public class JAASJettyRealm implements UserRealm { 40 private static Log log = LogFactory.getLog(JAASJettyRealm.class); 41 42 private final String realmName; 43 private final String loginDomainName; 44 private final HashMap userMap = new HashMap (); 45 46 public JAASJettyRealm(String realmName, String loginDomainName) { 47 this.realmName = realmName; 48 this.loginDomainName = loginDomainName; 49 } 50 51 public String getName() { 52 return realmName; 53 } 54 55 public Principal getPrincipal(String username) { 56 return (Principal ) userMap.get(username); 57 } 58 59 public Principal authenticate(String username, Object credentials, HttpRequest request) { 60 try { 61 JAASJettyPrincipal userPrincipal = (JAASJettyPrincipal) userMap.get(username); 62 63 if (userPrincipal != null) { 66 userMap.remove(username); 67 } 68 69 70 char[] password; 71 if (credentials instanceof char[]) { 72 password = (char[]) credentials; 73 } else if (credentials instanceof String ) { 74 password = ((String ) credentials).toCharArray(); 75 } else { 76 throw new LoginException ("Cannot extract credentials from class: " + credentials.getClass().getName()); 77 } 78 PasswordCallbackHandler callbackHandler = new PasswordCallbackHandler(username, password); 79 80 LoginContext loginContext = new LoginContext (loginDomainName, callbackHandler); 82 loginContext.login(); 83 callbackHandler.clear(); 84 85 Subject subject = ContextManager.getServerSideSubject(loginContext.getSubject()); 86 ContextManager.setCurrentCaller(subject); 87 88 userPrincipal = new JAASJettyPrincipal(username); 90 userPrincipal.setSubject(subject); 91 92 userMap.put(username, userPrincipal); 93 94 return userPrincipal; 95 } catch (LoginException e) { 96 log.warn(e); 97 return null; 98 } 99 } 100 101 public void logout(Principal user) { 102 JAASJettyPrincipal principal = (JAASJettyPrincipal) user; 103 104 userMap.remove(principal.getName()); 105 ContextManager.unregisterSubject(principal.getSubject()); 106 } 107 108 public boolean reauthenticate(Principal user) { 109 111 ContextManager.setCurrentCaller(((JAASJettyPrincipal) user).getSubject()); 112 113 return (userMap.get(user.getName()) != null); 115 } 116 117 public void disassociate(Principal user) { 118 } 120 121 public boolean isUserInRole(Principal user, String role) { 122 AccessControlContext acc = ContextManager.getCurrentContext(); 123 try { 124 acc.checkPermission(new WebRoleRefPermission (JettyServletHolder.getCurrentServletName(), role)); 125 } catch (AccessControlException e) { 126 return false; 127 } 128 return true; 129 } 130 131 public Principal pushRole(Principal user, String role) { 132 ((JAASJettyPrincipal) user).push(ContextManager.getCurrentCaller()); 133 ContextManager.setCurrentCaller(SecurityContextBeforeAfter.getCurrentRoleDesignate(role)); 134 return user; 135 } 136 137 public Principal popRole(Principal user) { 138 ContextManager.setCurrentCaller(((JAASJettyPrincipal) user).pop()); 139 return user; 140 } 141 142 } 143 | Popular Tags |