1 8 package org.jboss.security.jacc; 9 10 import java.security.AccessController ; 11 import java.security.PrivilegedAction ; 12 import java.util.Set ; 13 import java.util.HashSet ; 14 import javax.security.auth.Subject ; 15 import javax.security.jacc.PolicyContextException ; 16 import javax.security.jacc.PolicyContextHandler ; 17 18 import org.jboss.security.RunAsIdentity; 19 import org.jboss.security.SecurityAssociation; 20 21 25 public class SubjectPolicyContextHandler implements PolicyContextHandler 26 { 27 public static final String SUBJECT_CONTEXT_KEY = "javax.security.auth.Subject.container"; 28 public static final HashSet EMPTY_SET = new HashSet (); 29 30 private static class GetSubjectAction implements PrivilegedAction 31 { 32 static PrivilegedAction ACTION = new GetSubjectAction(); 33 public Object run() 34 { 35 Subject theSubject = null; 36 Subject activeSubject = SecurityAssociation.getSubject(); 37 if( activeSubject != null ) 38 { 39 Set principalsSet = null; 40 RunAsIdentity callerRunAsIdentity = (RunAsIdentity) 41 SecurityAssociation.peekRunAsIdentity(1); 42 if( callerRunAsIdentity == null ) 43 { 44 principalsSet = activeSubject.getPrincipals(); 45 } 46 else 47 { 48 principalsSet = callerRunAsIdentity.getRunAsRoles(); 49 } 50 51 theSubject = new Subject (true, principalsSet, 52 activeSubject.getPublicCredentials(), 53 activeSubject.getPrivateCredentials()); 54 } 55 else 56 { 57 RunAsIdentity callerRunAsIdentity = (RunAsIdentity) 58 SecurityAssociation.peekRunAsIdentity(1); 59 if( callerRunAsIdentity != null ) 60 { 61 Set principalsSet = callerRunAsIdentity.getRunAsRoles(); 62 theSubject = new Subject (true, principalsSet, EMPTY_SET, EMPTY_SET); 63 } 64 } 65 return theSubject; 66 } 67 } 68 69 public Object getContext(String key, Object data) 70 throws PolicyContextException 71 { 72 if( key.equalsIgnoreCase(SUBJECT_CONTEXT_KEY) == false ) 73 return null; 74 75 Subject subject = (Subject ) AccessController.doPrivileged(GetSubjectAction.ACTION); 76 return subject; 77 } 78 79 public String [] getKeys() 80 throws PolicyContextException 81 { 82 String [] keys = {SUBJECT_CONTEXT_KEY}; 83 return keys; 84 } 85 86 public boolean supports(String key) 87 throws PolicyContextException 88 { 89 return key.equalsIgnoreCase(SUBJECT_CONTEXT_KEY); 90 } 91 } 92 | Popular Tags |