1 17 package org.apache.servicemix.http.jetty; 18 19 import java.security.GeneralSecurityException ; 20 import java.security.Principal ; 21 import java.util.Map ; 22 23 import javax.security.auth.Subject ; 24 25 import org.apache.commons.logging.Log; 26 import org.apache.commons.logging.LogFactory; 27 import org.apache.servicemix.jbi.security.auth.AuthenticationService; 28 import org.apache.servicemix.jbi.security.auth.impl.JAASAuthenticationService; 29 import org.mortbay.jetty.Request; 30 import org.mortbay.jetty.security.UserRealm; 31 32 import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap; 33 34 39 public class JaasUserRealm implements UserRealm { 40 41 private static final Log log = LogFactory.getLog(JaasUserRealm.class); 42 43 private String name = getClass().getName(); 44 private String domain = "servicemix-domain"; 45 private AuthenticationService authenticationService = new JAASAuthenticationService(); 46 private final Map userMap = new ConcurrentHashMap(); 47 48 51 public AuthenticationService getAuthenticationService() { 52 return authenticationService; 53 } 54 55 58 public void setAuthenticationService(AuthenticationService authenticationService) { 59 this.authenticationService = authenticationService; 60 } 61 62 65 public String getDomain() { 66 return domain; 67 } 68 69 72 public void setDomain(String domain) { 73 this.domain = domain; 74 } 75 76 79 public String getName() { 80 return this.name; 81 } 82 83 86 public void setName(String name) { 87 this.name = name; 88 } 89 90 public Principal authenticate(final String username, final Object credentials, Request request) { 91 try { 92 if ((username != null) && (!username.equals(""))) { 93 94 JaasJettyPrincipal userPrincipal = (JaasJettyPrincipal) userMap.get(username); 95 96 if (userPrincipal != null) { 99 userMap.remove(username); 100 } 101 102 Subject subject = new Subject (); 104 authenticationService.authenticate(subject, domain, username, credentials); 105 userPrincipal = new JaasJettyPrincipal(username); 107 userPrincipal.setSubject(subject); 108 109 userMap.put(username, userPrincipal); 110 111 return userPrincipal; 112 } else { 113 log.debug("Login Failed - null userID"); 114 return null; 115 } 116 117 } catch (GeneralSecurityException e) { 118 log.debug("Login Failed", e); 119 return null; 120 } 121 } 122 123 public void disassociate(Principal user) { 124 } 125 126 public Principal getPrincipal(String username) { 127 return (Principal ) userMap.get(username); 128 } 129 130 public boolean isUserInRole(Principal user, String role) { 131 return false; 133 } 134 135 public void logout(Principal user) { 136 JaasJettyPrincipal principal = (JaasJettyPrincipal) user; 137 userMap.remove(principal.getName()); 138 } 139 140 public Principal popRole(Principal user) { 141 return null; 143 } 144 145 public Principal pushRole(Principal user, String role) { 146 return null; 148 } 149 150 public boolean reauthenticate(Principal user) { 151 return (userMap.get(user.getName()) != null); 153 } 154 155 } 156 | Popular Tags |