1 23 24 28 29 package com.sun.enterprise.connectors.authentication; 30 31 import com.sun.enterprise.connectors.*; 32 import com.sun.logging.LogDomains; 33 import java.security.Principal ; 34 import java.util.logging.*; 35 import java.util.*; 36 import com.sun.enterprise.connectors.util.SecurityMapUtils; 37 import com.sun.enterprise.config.serverbeans.SecurityMap; 38 import javax.security.auth.Subject ; 39 import javax.ejb.EJBContext ; 40 41 import com.sun.enterprise.*; 42 import com.sun.enterprise.deployment.interfaces.*; 43 import com.sun.enterprise.security.factory.SecurityManagerFactory; 44 import com.sun.enterprise.web.*; 45 import com.sun.ejb.Container; 46 47 48 53 54 55 public class BasicPasswordAuthenticationService 56 implements AuthenticationService { 57 58 private String rarName_; 59 private String poolName_; 60 ConnectorRegistry connectorRegistry_ = ConnectorRegistry.getInstance(); 61 static Logger _logger = LogDomains.getLogger(LogDomains.RSR_LOGGER); 62 private Object containerContext = null; 63 64 69 70 public BasicPasswordAuthenticationService(String rarName,String poolName) { 71 rarName_ = rarName; 72 poolName_ = poolName; 73 _logger.log(Level.FINE,"Contructor:BasicPasswordAuthenticationService"); 74 } 75 76 81 82 public Principal mapPrincipal(Principal callerPrincipal, Set principalSet) { 83 84 RuntimeSecurityMap runtimeSecurityMap = 86 connectorRegistry_.getRuntimeSecurityMap(poolName_); 87 if(runtimeSecurityMap == null) { 88 return null; 89 } 90 91 String principalName = callerPrincipal.getName(); 92 93 List<String > groupNames = new ArrayList(); 95 Iterator iter = principalSet.iterator(); 96 while (iter.hasNext()){ 97 Principal p = (Principal )iter.next(); 98 if (p.equals(callerPrincipal)){ 100 continue; 101 } 102 String groupName = p.getName(); 103 groupNames.add(groupName); 104 } 105 106 if (isContainerContextAWebModuleObject()){ 108 String roleName = getRoleName(callerPrincipal); 109 return doMap(principalName, groupNames, roleName, runtimeSecurityMap); 110 } else { 111 return doMap(principalName, groupNames, null, runtimeSecurityMap); 112 } 113 } 114 115 121 122 private Principal doMap(String principalName, List groupNames, 123 String roleName, RuntimeSecurityMap runtimeSecurityMap) { 124 125 131 HashMap userNameSecurityMap = (HashMap)runtimeSecurityMap.getUserMap(); 132 HashMap groupNameSecurityMap = (HashMap)runtimeSecurityMap.getGroupMap(); 133 134 if (userNameSecurityMap.containsKey(principalName)){ 136 return (Principal )userNameSecurityMap.get(principalName); 137 } 138 139 if (isContainerContextAWebModuleObject() && roleName != null ){ 141 if (groupNameSecurityMap.containsKey(roleName)){ 142 return (Principal )groupNameSecurityMap.get(roleName); 143 } 144 } 145 146 if (isContainerContextAContainerObject() && roleName == null){ 148 ComponentInvocation componentInvocation = 149 Switch.getSwitch().getInvocationManager().getCurrentInvocation(); 150 EJBContext ejbcontext = (EJBContext )componentInvocation.context; 151 Set s = groupNameSecurityMap.keySet(); 152 Iterator i = s.iterator(); 153 while (i.hasNext()){ 154 String entry = (String )i.next(); 155 boolean isInRole = false; 156 try{ 157 isInRole = ejbcontext.isCallerInRole(entry); 158 } catch (Exception ex){ 159 _logger.log(Level.FINE,"asciPasswordAuthentication::caller not in role "+entry); 160 } 161 if (isInRole){ 162 return (Principal )groupNameSecurityMap.get(entry); 163 } 164 } 165 } 166 167 for (int j=0; j<groupNames.size(); j++){ 169 String groupName = (String )groupNames.get(j); 170 if (groupNameSecurityMap.containsKey(groupName)){ 171 return (Principal )groupNameSecurityMap.get(groupName); 172 } 173 } 174 175 if (userNameSecurityMap.containsKey(ConnectorConstants.SECURITYMAPMETACHAR)){ 177 return (Principal )userNameSecurityMap.get(ConnectorConstants.SECURITYMAPMETACHAR); 178 } 179 180 if (groupNameSecurityMap.containsKey(ConnectorConstants.SECURITYMAPMETACHAR)){ 182 return (Principal )groupNameSecurityMap.get(ConnectorConstants.SECURITYMAPMETACHAR); 183 } 184 185 return null; 186 } 187 188 private String getRoleName(Principal callerPrincipal){ 189 190 String roleName = null; 191 WebModule _webmodule = (WebModule)getContainerContext(); 192 193 SecurityRoleMapperFactory securityRoleMapperFactory = 194 SecurityRoleMapperFactoryMgr.getFactory(); 195 SecurityRoleMapper securityRoleMapper= 196 securityRoleMapperFactory.getRoleMapper(_webmodule.getID()); 197 198 Map<String , Subject > map = securityRoleMapper.getRoleToSubjectMapping(); 199 Set<String > roleSet = map.keySet(); 200 Iterator iter = roleSet.iterator(); 201 while (iter.hasNext()){ 202 roleName = (String )iter.next(); 203 Subject subject = (Subject )map.get(roleName); 204 Set principalSet = subject.getPrincipals(); 205 if (principalSet.contains(callerPrincipal)){ 206 return roleName; 207 } 208 } 209 return ""; 210 } 211 212 214 private Object getContainerContext(){ 215 if (this.containerContext == null){ 216 ComponentInvocation componentInvocation = 217 Switch.getSwitch().getInvocationManager().getCurrentInvocation(); 218 this.containerContext = componentInvocation.getContainerContext(); 219 } 220 return this.containerContext; 221 } 222 223 private boolean isContainerContextAContainerObject(){ 224 if (getContainerContext() instanceof Container){ 225 return true; 226 } 227 return false; 228 } 229 230 private boolean isContainerContextAWebModuleObject(){ 231 if (getContainerContext() instanceof WebModule){ 232 return true; 233 } 234 return false; 235 236 } 237 } 238 | Popular Tags |