1 22 package org.jboss.web.tomcat.security; 23 24 import java.security.PrivilegedAction ; 25 import java.security.Principal ; 26 import java.security.AccessController ; 27 import java.util.HashMap ; 28 29 import javax.security.auth.Subject ; 30 31 import org.jboss.security.SecurityAssociation; 32 import org.jboss.security.RunAsIdentity; 33 import org.jboss.security.SecurityConstants; 34 import org.jboss.security.SecurityContext; 35 36 42 class SecurityAssociationActions 43 { 44 public static final String AUTH_EXCEPTION_KEY = "org.jboss.security.exception"; 45 46 private static class SetPrincipalInfoAction implements PrivilegedAction 47 { 48 Principal principal; 49 Object credential; 50 Subject subject; 51 SetPrincipalInfoAction(Principal principal, Object credential, Subject subject) 52 { 53 this.principal = principal; 54 this.credential = credential; 55 this.subject = subject; 56 } 57 58 public Object run() 59 { 60 SecurityAssociation.pushSubjectContext(subject, principal, credential); 61 credential = null; 62 principal = null; 63 subject = null; 64 return null; 65 } 66 } 67 private static class SetServerAction implements PrivilegedAction 68 { 69 static PrivilegedAction ACTION = new SetServerAction(); 70 public Object run() 71 { 72 SecurityAssociation.setServer(); 73 return null; 74 } 75 } 76 private static class ClearAction implements PrivilegedAction 77 { 78 static PrivilegedAction ACTION = new ClearAction(); 79 public Object run() 80 { 81 SecurityAssociation.clear(); 82 return null; 83 } 84 } 85 private static class GetSubjectAction implements PrivilegedAction 86 { 87 static PrivilegedAction ACTION = new GetSubjectAction(); 88 public Object run() 89 { 90 Subject subject = SecurityAssociation.getSubject(); 91 return subject; 92 } 93 } 94 private static class GetPrincipalAction implements PrivilegedAction 95 { 96 static PrivilegedAction ACTION = new GetPrincipalAction(); 97 public Object run() 98 { 99 Principal principal = SecurityAssociation.getPrincipal(); 100 return principal; 101 } 102 } 103 private static class GetCredentialAction implements PrivilegedAction 104 { 105 static PrivilegedAction ACTION = new GetCredentialAction(); 106 public Object run() 107 { 108 Object credential = SecurityAssociation.getCredential(); 109 return credential; 110 } 111 } 112 private static class GetSecurityContextAction implements PrivilegedAction 113 { 114 private String securityDomain; 115 GetSecurityContextAction(String sd) 116 { 117 this.securityDomain = sd; 118 } 119 120 public Object run() 121 { 122 String sc = SecurityConstants.SECURITY_CONTEXT; 123 HashMap map = (HashMap )SecurityAssociation.getContextInfo(sc); 124 if(map == null) 125 { 126 map = new HashMap (); 127 SecurityAssociation.setContextInfo(sc, map); 128 } 129 SecurityAssociation.setContextInfo(sc, map); 130 return map.get(this.securityDomain); 131 } 132 } 133 134 private static class SetSecurityContextAction implements PrivilegedAction 135 { 136 private SecurityContext securityContext; 137 private String securityDomain; 138 SetSecurityContextAction(SecurityContext sc, String sd) 139 { 140 this.securityContext = sc; 141 this.securityDomain = sd; 142 } 143 144 public Object run() 145 { 146 String sc = SecurityConstants.SECURITY_CONTEXT; 147 HashMap map = (HashMap )SecurityAssociation.getContextInfo(sc); 148 if(map == null) 149 { 150 map = new HashMap (); 151 SecurityAssociation.setContextInfo(sc, map); 152 } 153 map.put(securityDomain, securityContext); 154 SecurityAssociation.setContextInfo(sc, map); 155 return null; 156 } 157 } 158 159 private static class PushRunAsRoleAction implements PrivilegedAction 160 { 161 RunAsIdentity principal; 162 PushRunAsRoleAction(RunAsIdentity principal) 163 { 164 this.principal = principal; 165 } 166 public Object run() 167 { 168 SecurityAssociation.pushRunAsIdentity(principal); 169 return null; 170 } 171 } 172 173 private static class PopRunAsRoleAction implements PrivilegedAction 174 { 175 static PrivilegedAction ACTION = new PopRunAsRoleAction(); 176 public Object run() 177 { 178 RunAsIdentity principal = SecurityAssociation.popRunAsIdentity(); 179 return principal; 180 } 181 } 182 private static class GetAuthExceptionAction implements PrivilegedAction 183 { 184 static PrivilegedAction ACTION = new GetAuthExceptionAction(); 185 public Object run() 186 { 187 Object exception = SecurityAssociation.getContextInfo(AUTH_EXCEPTION_KEY); 188 return exception; 189 } 190 } 191 private static class ClearAuthExceptionAction implements PrivilegedAction 192 { 193 static PrivilegedAction ACTION = new ClearAuthExceptionAction(); 194 public Object run() 195 { 196 Object exception = SecurityAssociation.setContextInfo(AUTH_EXCEPTION_KEY, null); 197 return exception; 198 } 199 } 200 private static class ClearSecurityContextAction implements PrivilegedAction 201 { 202 private String securityDomain; 203 204 public ClearSecurityContextAction(String securityDomain) 205 { 206 this.securityDomain = securityDomain; 207 } 208 209 public Object run() 210 { 211 String sc = SecurityConstants.SECURITY_CONTEXT; 212 HashMap map = (HashMap )SecurityAssociation.getContextInfo(sc); 213 if(map == null) 214 { 215 map = new HashMap (); 216 SecurityAssociation.setContextInfo(sc, map); 217 } 218 if(map.containsKey(securityDomain)) 219 map.remove(securityDomain); 220 221 SecurityAssociation.setContextInfo(sc, map); 222 return null; 223 } 224 } 225 226 static void clearSecurityContext(String securityDomain) 227 { 228 ClearSecurityContextAction action = new ClearSecurityContextAction(securityDomain); 229 AccessController.doPrivileged(action); 230 } 231 232 static SecurityContext getSecurityContext(String securityDomain) 233 { 234 GetSecurityContextAction action = new GetSecurityContextAction(securityDomain); 235 return (SecurityContext)AccessController.doPrivileged(action); 236 } 237 238 static void setSecurityContext(SecurityContext sc, String securityDomain) 239 { 240 SetSecurityContextAction action = new SetSecurityContextAction(sc,securityDomain); 241 AccessController.doPrivileged(action); 242 } 243 244 static void setPrincipalInfo(Principal principal, Object credential, Subject subject) 245 { 246 SetPrincipalInfoAction action = new SetPrincipalInfoAction(principal, credential, subject); 247 AccessController.doPrivileged(action); 248 } 249 static void setServer() 250 { 251 AccessController.doPrivileged(SetServerAction.ACTION); 252 } 253 static void clear() 254 { 255 AccessController.doPrivileged(ClearAction.ACTION); 256 } 257 static Subject getSubject() 258 { 259 Subject subject = (Subject ) AccessController.doPrivileged(GetSubjectAction.ACTION); 260 return subject; 261 } 262 static Principal getPrincipal() 263 { 264 Principal principal = (Principal ) AccessController.doPrivileged(GetPrincipalAction.ACTION); 265 return principal; 266 } 267 static Object getCredential() 268 { 269 Object credential = AccessController.doPrivileged(GetCredentialAction.ACTION); 270 return credential; 271 } 272 static void pushRunAsIdentity(RunAsIdentity principal) 273 { 274 PushRunAsRoleAction action = new PushRunAsRoleAction(principal); 275 AccessController.doPrivileged(action); 276 } 277 static RunAsIdentity popRunAsIdentity() 278 { 279 RunAsIdentity principal = (RunAsIdentity) AccessController.doPrivileged(PopRunAsRoleAction.ACTION); 280 return principal; 281 } 282 283 static Throwable getAuthException() 284 { 285 Throwable ex = (Throwable ) AccessController.doPrivileged(GetAuthExceptionAction.ACTION); 286 return ex; 287 } 288 static void clearAuthException() 289 { 290 AccessController.doPrivileged(ClearAuthExceptionAction.ACTION); 291 } 292 } 293 | Popular Tags |