1 25 26 package org.objectweb.security.context; 27 28 import java.io.Serializable ; 29 import java.security.Principal ; 30 import java.util.ArrayList ; 31 import java.util.Collections ; 32 import java.util.Iterator ; 33 import java.util.List ; 34 35 import org.objectweb.jonas.security.jacc.JPolicyUserRoleMapping; 36 37 43 44 public class SecurityContext implements Serializable { 45 46 49 private String principalName = null; 50 51 54 private final String [] roles; 55 56 60 private List runAsRoleStack = null; 61 62 65 private List runAsPrincipalStack = null; 66 67 71 private List runAsPrincipalRolesStack = null; 72 73 76 public SecurityContext() { 77 this("ANONYMOUS"); 78 } 79 80 85 public SecurityContext(String principalName, String [] roles) { 86 this.principalName = principalName; 87 this.roles = roles; 88 } 89 90 95 public SecurityContext(String principalName, List arrayRoles) { 96 this(principalName, arrayRoles, null, null, null); 97 } 98 99 108 protected SecurityContext(String principalName, List arrayRoles, List arrayRunas, List arrayRunasPrincipal, List arrayRunasPrincipalRoles) { 109 this.principalName = principalName; 110 String [] overridedRoles = JPolicyUserRoleMapping.getGlobalMappingForPrincipal(principalName); 111 if (overridedRoles != null) { 112 this.roles = overridedRoles; 113 } else { 114 if (arrayRoles != null) { 115 String [] r = new String [arrayRoles.size()]; 117 r = (String []) arrayRoles.toArray(r); 118 this.roles = r; 119 } else { 120 this.roles = null; 121 } 122 } 123 124 this.runAsRoleStack = arrayRunas; 125 this.runAsPrincipalStack = arrayRunasPrincipal; 126 this.runAsPrincipalRolesStack = arrayRunasPrincipalRoles; 127 } 128 129 133 public SecurityContext(String principalName) { 134 this.principalName = principalName; 135 this.roles = new String [] {"JOnAS"}; 136 } 137 138 145 public Principal getCallerPrincipal(boolean inRunAs) { 146 return new InternalPrincipal(inRunAs); 147 } 148 149 154 public String [] getCallerPrincipalRoles(boolean inRunAs) { 155 String [] runAsRoles = null; 156 if (inRunAs) { 157 runAsRoles = peekLastRunAsPrincipalRoles(); 158 } else { 159 runAsRoles = peekRunAsPrincipalRoles(); 160 } 161 if (runAsRoles != null) { 162 return runAsRoles; 163 } else { 164 return roles; 165 } 166 } 167 168 172 public synchronized void pushRunAsRole(String role) { 173 getRunAsRoleStack().add(role); 174 } 175 176 182 public synchronized void pushRunAsPrincipal(String principalName, String [] roles) { 183 getRunAsPrincipalStack().add(principalName); 184 getRunAsPrincipalRolesStack().add(roles); 185 } 186 187 190 public synchronized void popRunAs() { 191 if (!getRunAsRoleStack().isEmpty()) { 192 getRunAsRoleStack().remove(getRunAsRoleStack().size() - 1); 193 } 194 if (!getRunAsPrincipalStack().isEmpty()) { 195 getRunAsPrincipalStack().remove(getRunAsPrincipalStack().size() - 1); 196 } 197 if (!getRunAsPrincipalRolesStack().isEmpty()) { 198 getRunAsPrincipalRolesStack().remove(getRunAsPrincipalRolesStack().size() - 1); 199 } 200 } 201 202 207 public synchronized String peekRunAsRole() { 208 if (getRunAsRoleStack().isEmpty()) { 209 return null; 210 } else { 211 return (String ) getRunAsRoleStack().get(getRunAsRoleStack().size() - 1); 212 } 213 } 214 215 220 public synchronized String peekRunAsPrincipal() { 221 if (getRunAsPrincipalStack().isEmpty()) { 222 return null; 223 } else { 224 return (String ) getRunAsPrincipalStack().get(getRunAsPrincipalStack().size() - 1); 225 } 226 } 227 228 233 public synchronized String peekLastRunAsPrincipal() { 234 if (getRunAsPrincipalStack().size() < 2) { 235 return null; 236 } else { 237 return (String ) getRunAsPrincipalStack().get(getRunAsPrincipalStack().size() - 2); 238 } 239 } 240 241 246 public synchronized String [] peekRunAsPrincipalRoles() { 247 if (getRunAsPrincipalRolesStack().isEmpty()) { 248 return null; 249 } else { 250 return (String []) getRunAsPrincipalRolesStack().get(getRunAsPrincipalRolesStack().size() - 1); 251 } 252 } 253 254 259 public synchronized String [] peekLastRunAsPrincipalRoles() { 260 if (getRunAsPrincipalRolesStack().size() < 2) { 261 return null; 262 } else { 263 return (String []) getRunAsPrincipalRolesStack().get(getRunAsPrincipalRolesStack().size() - 2); 264 } 265 } 266 267 271 public synchronized List getRunAsRoleStack() { 272 if (runAsRoleStack == null) { 273 runAsRoleStack = Collections.synchronizedList(new ArrayList ()); 274 } 275 return runAsRoleStack; 276 } 277 278 282 public synchronized List getRunAsPrincipalRolesStack() { 283 if (runAsPrincipalRolesStack == null) { 284 runAsPrincipalRolesStack = Collections.synchronizedList(new ArrayList ()); 285 } 286 return runAsPrincipalRolesStack; 287 } 288 289 293 public synchronized List getRunAsPrincipalStack() { 294 if (runAsPrincipalStack == null) { 295 runAsPrincipalStack = Collections.synchronizedList(new ArrayList ()); 296 } 297 return runAsPrincipalStack; 298 } 299 300 304 public String toString() { 305 String txt = "principal : name = " + principalName + "\n"; 306 if (roles != null) { 307 for (int i = 0; i < roles.length; i++) { 308 txt += "role[" + i + "] = " + roles[i] + "\n"; 309 } 310 } 311 if (runAsRoleStack != null) { 312 Iterator iRunas = runAsRoleStack.iterator(); 313 int i = 0; 314 while (iRunas.hasNext()) { 315 txt += "runas[" + i + "] = " + ((String ) iRunas.next()) + "\n"; 316 } 317 } 318 return txt; 319 } 320 321 325 protected String getPrincipalName(boolean runningRunAs) { 326 String principal = null; 327 if (runningRunAs) { 328 principal = peekLastRunAsPrincipal(); 329 } else { 330 principal = peekRunAsPrincipal(); 331 } 332 if (principal != null) { 333 return principal; 334 } else { 335 return principalName; 336 } 337 } 338 339 342 public String getPrincipalName() { 343 return principalName; 344 } 345 346 349 protected String [] getRoles() { 350 return roles; 351 } 352 353 356 class InternalPrincipal implements Principal { 357 358 363 private boolean inRunAs = false; 364 365 369 public InternalPrincipal(boolean inRunAs) { 370 super(); 371 this.inRunAs = inRunAs; 372 373 } 374 375 378 public String getName() { 379 return getPrincipalName(inRunAs); 380 } 381 382 386 public boolean equals(Object o) { 387 if (o instanceof Principal ) { 388 return getPrincipalName(inRunAs).equals(((Principal ) o).getName()); 389 } 390 return false; 391 } 392 393 397 public int hashCode() { 398 return getPrincipalName(inRunAs).hashCode(); 399 } 400 401 405 public String toString() { 406 return "name = " + getPrincipalName(inRunAs); 407 } 408 } 409 410 } 411 412 | Popular Tags |