1 22 package org.jboss.ejb.plugins; 23 24 import java.security.Principal ; 25 import java.util.Map ; 26 import java.util.Set ; 27 import java.lang.reflect.Method ; 28 import javax.security.auth.Subject ; 29 import javax.ejb.TimedObject ; 30 import javax.ejb.Timer ; 31 32 import org.jboss.ejb.Container; 33 import org.jboss.invocation.Invocation; 34 import org.jboss.invocation.PayloadKey; 35 import org.jboss.metadata.ApplicationMetaData; 36 import org.jboss.metadata.AssemblyDescriptorMetaData; 37 import org.jboss.metadata.BeanMetaData; 38 import org.jboss.metadata.SecurityIdentityMetaData; 39 import org.jboss.security.AuthenticationManager; 40 import org.jboss.security.RunAsIdentity; 41 import org.jboss.security.SecurityRolesAssociation; 42 import org.jboss.security.SecurityAssociation; 43 44 51 public class JaasAuthenticationInterceptor extends AbstractInterceptor 52 { 53 55 protected AuthenticationManager securityManager; 56 57 58 protected Map securityRoles; 59 60 protected RunAsIdentity runAsIdentity; 61 62 protected Method ejbTimeout; 63 64 67 public void setContainer(Container container) 68 { 69 super.setContainer(container); 70 if (container != null) 71 { 72 BeanMetaData beanMetaData = container.getBeanMetaData(); 73 ApplicationMetaData applicationMetaData = beanMetaData.getApplicationMetaData(); 74 AssemblyDescriptorMetaData assemblyDescriptor = applicationMetaData.getAssemblyDescriptor(); 75 76 SecurityIdentityMetaData secMetaData = beanMetaData.getSecurityIdentityMetaData(); 77 if (secMetaData != null && secMetaData.getUseCallerIdentity() == false) 78 { 79 String roleName = secMetaData.getRunAsRoleName(); 80 String principalName = secMetaData.getRunAsPrincipalName(); 81 82 Set extraRoleNames = assemblyDescriptor.getSecurityRoleNamesByPrincipal(principalName); 84 runAsIdentity = new RunAsIdentity(roleName, principalName, extraRoleNames); 85 } 86 87 securityManager = container.getSecurityManager(); 88 try 89 { 90 ejbTimeout = TimedObject .class.getMethod("ejbTimeout", new Class []{Timer .class}); 92 } 93 catch (NoSuchMethodException ignore) 94 { 95 } 96 } 97 } 98 99 public void start() throws Exception 101 { 102 super.start(); 103 } 104 105 public Object invokeHome(Invocation mi) throws Exception 106 { 107 checkSecurityAssociation(mi); 109 110 114 SecurityActions.pushRunAsIdentity(runAsIdentity); 115 116 try 117 { 118 Object returnValue = getNext().invokeHome(mi); 119 return returnValue; 120 } 121 finally 122 { 123 SecurityActions.popRunAsIdentity(); 124 SecurityActions.popSubjectContext(); 125 } 126 } 127 128 public Object invoke(Invocation mi) throws Exception 129 { 130 checkSecurityAssociation(mi); 132 133 RunAsIdentity callerRunAsIdentity = SecurityActions.peekRunAsIdentity(); 135 if( callerRunAsIdentity != null ) 136 mi.setValue("RunAsIdentity", callerRunAsIdentity, PayloadKey.TRANSIENT); 137 138 142 SecurityActions.pushRunAsIdentity(runAsIdentity); 143 144 try 145 { 146 Object returnValue = getNext().invoke(mi); 147 return returnValue; 148 } 149 finally 150 { 151 SecurityActions.popRunAsIdentity(); 152 SecurityActions.popSubjectContext(); 153 } 154 } 155 156 159 private void checkSecurityAssociation(Invocation mi) 160 throws Exception 161 { 162 Principal principal = mi.getPrincipal(); 163 Object credential = mi.getCredential(); 164 boolean trace = log.isTraceEnabled(); 165 166 Method m = mi.getMethod(); 168 boolean containerMethod = m == null || m.equals(ejbTimeout); 169 if ( containerMethod == true || securityManager == null || container == null ) 170 { 171 SecurityActions.pushSubjectContext(principal, credential, null); 173 return; 174 } 175 176 RunAsIdentity callerRunAsIdentity = SecurityAssociation.peekRunAsIdentity(); 178 if (callerRunAsIdentity == null) 179 { 180 184 SecurityRolesAssociation.setSecurityRoles(securityRoles); 185 Subject subject = new Subject (); 186 if (securityManager.isValid(principal, credential, subject) == false) 187 { 188 Exception ex = SecurityActions.getContextException(); 190 if( ex != null ) 191 throw ex; 192 String msg = "Authentication exception, principal=" + principal; 194 SecurityException e = new SecurityException (msg); 195 throw e; 196 } 197 else 198 { 199 SecurityActions.pushSubjectContext(principal, credential, subject); 200 if (trace) 201 { 202 log.trace("Authenticated principal=" + principal); 203 } 204 } 205 } 206 else 207 { 208 SecurityActions.dupSubjectContext(); 210 } 211 212 } 213 } 214 | Popular Tags |