1 22 package org.jboss.resource.security; 23 24 import java.security.AccessController ; 25 import java.security.PrivilegedAction ; 26 import java.security.Principal ; 27 import java.io.UnsupportedEncodingException ; 28 29 import org.jboss.security.SecurityAssociation; 30 import org.jboss.security.RunAsIdentity; 31 32 37 class GetPrincipalInfoAction 38 { 39 42 private static char[] getPassword() 43 { 44 Object credential = SecurityAssociation.getCredential(); 45 char[] password = null; 46 if( credential instanceof char[] ) 47 { 48 password = (char[]) credential; 49 } 50 else if( credential instanceof byte[] ) 51 { 52 try 53 { 54 String tmp = new String ((byte[]) credential, "UTF-8"); 55 password = tmp.toCharArray(); 56 } 57 catch (UnsupportedEncodingException e) 58 { 59 throw new SecurityException (e.getMessage()); 60 } 61 } 62 else if( credential != null ) 63 { 64 String tmp = credential.toString(); 65 password = tmp.toCharArray(); 66 } 67 return password; 68 } 69 70 interface PrincipalActions 71 { 72 PrincipalActions PRIVILEGED = new PrincipalActions() 73 { 74 private final PrivilegedAction peekAction = new PrivilegedAction () 75 { 76 public Object run() 77 { 78 return SecurityAssociation.peekRunAsIdentity(); 79 } 80 }; 81 82 private final PrivilegedAction getPrincipalAction = new PrivilegedAction () 83 { 84 public Object run() 85 { 86 return SecurityAssociation.getPrincipal(); 87 } 88 }; 89 90 private final PrivilegedAction getCredentialAction = new PrivilegedAction () 91 { 92 public Object run() 93 { 94 return getPassword(); 95 } 96 }; 97 98 public RunAsIdentity peek() 99 { 100 return (RunAsIdentity)AccessController.doPrivileged(peekAction); 101 } 102 103 public Principal getPrincipal() 104 { 105 return (Principal )AccessController.doPrivileged(getPrincipalAction); 106 } 107 108 public char[] getCredential() 109 { 110 return (char[]) AccessController.doPrivileged(getCredentialAction); 111 } 112 }; 113 114 PrincipalActions NON_PRIVILEGED = new PrincipalActions() 115 { 116 public RunAsIdentity peek() 117 { 118 return SecurityAssociation.peekRunAsIdentity(); 119 } 120 121 public Principal getPrincipal() 122 { 123 return SecurityAssociation.getPrincipal(); 124 } 125 126 public char[] getCredential() 127 { 128 return getPassword(); 129 } 130 }; 131 132 Principal getPrincipal(); 133 char[] getCredential(); 134 RunAsIdentity peek(); 135 } 136 137 static Principal getPrincipal() 138 { 139 Principal principal; 140 if(System.getSecurityManager() == null) 141 { 142 principal = PrincipalActions.NON_PRIVILEGED.getPrincipal(); 143 } 144 else 145 { 146 principal = PrincipalActions.PRIVILEGED.getPrincipal(); 147 } 148 return principal; 149 } 150 static char[] getCredential() 151 { 152 char[] credential; 153 if(System.getSecurityManager() == null) 154 { 155 credential = PrincipalActions.NON_PRIVILEGED.getCredential(); 156 } 157 else 158 { 159 credential = PrincipalActions.PRIVILEGED.getCredential(); 160 } 161 return credential; 162 } 163 static RunAsIdentity peekRunAsIdentity() 164 { 165 if(System.getSecurityManager() == null) 166 { 167 return PrincipalActions.NON_PRIVILEGED.peek(); 168 } 169 else 170 { 171 return PrincipalActions.PRIVILEGED.peek(); 172 } 173 } 174 175 } 176 | Popular Tags |