1 25 26 package org.objectweb.easybeans.security.propagation.jonas; 27 28 import java.lang.reflect.InvocationTargetException ; 29 import java.lang.reflect.Method ; 30 import java.security.Principal ; 31 import java.security.acl.Group ; 32 33 import javax.security.auth.Subject ; 34 35 import org.objectweb.easybeans.security.api.EZBSecurityContext; 36 import org.objectweb.easybeans.security.struct.JPrincipal; 37 38 43 public class JOnASSecurityContext implements EZBSecurityContext { 44 45 48 private Object jonasSecurityContext = null; 49 50 54 public JOnASSecurityContext(final Object jonasSecurityContext) { 55 this.jonasSecurityContext = jonasSecurityContext; 56 } 57 58 63 public Principal getCallerPrincipal(final boolean runAsBean) { 64 Method m = null; 65 try { 66 m = jonasSecurityContext.getClass().getMethod("getCallerPrincipal", new Class [] {boolean.class}); 67 } catch (SecurityException e) { 68 throw new IllegalStateException ("Cannot get the method getCallerPrincipal on the JOnAS security context", e); 69 } catch (NoSuchMethodException e) { 70 throw new IllegalStateException ("Cannot get the method getCallerPrincipal on the JOnAS security context", e); 71 } 72 73 try { 74 return (Principal ) m.invoke(jonasSecurityContext, Boolean.valueOf(runAsBean)); 75 } catch (IllegalArgumentException e) { 76 throw new IllegalStateException ("Cannot call getCallerPrincipal method on the JOnAS security context", e); 77 } catch (IllegalAccessException e) { 78 throw new IllegalStateException ("Cannot call getCallerPrincipal method on the JOnAS security context", e); 79 } catch (InvocationTargetException e) { 80 throw new IllegalStateException ("Cannot call getCallerPrincipal method on the JOnAS security context", e); 81 } 82 } 83 84 89 public Principal [] getCallerRoles(final boolean runAsBean) { 90 Method m = null; 91 try { 92 m = jonasSecurityContext.getClass().getMethod("getCallerPrincipalRoles", new Class [] {boolean.class}); 93 } catch (SecurityException e) { 94 throw new IllegalStateException ("Cannot get the method getCallerPrincipalRoles on the JOnAS security context", e); 95 } catch (NoSuchMethodException e) { 96 throw new IllegalStateException ("Cannot get the method getCallerPrincipalRoles on the JOnAS security context", e); 97 } 98 99 String [] roles = null; 100 try { 101 roles = (String []) m.invoke(jonasSecurityContext, Boolean.valueOf(runAsBean)); 102 } catch (IllegalArgumentException e) { 103 throw new IllegalStateException ("Cannot call getCallerPrincipalRoles method on the JOnAS security context", e); 104 } catch (IllegalAccessException e) { 105 throw new IllegalStateException ("Cannot call getCallerPrincipalRoles method on the JOnAS security context", e); 106 } catch (InvocationTargetException e) { 107 throw new IllegalStateException ("Cannot call getCallerPrincipalRoles method on the JOnAS security context", e); 108 } 109 110 if (roles == null) { 111 throw new IllegalStateException ("No roles found on the JOnAS security context"); 112 } 113 114 Principal [] principals = new Principal [roles.length]; 115 int i = 0; 116 for (String role : roles) { 117 principals[i++] = new JPrincipal(role); 118 } 119 return principals; 120 } 121 122 129 public Subject enterRunAs(final Subject runAsSubject) { 130 131 Method m = null; 132 try { 133 m = jonasSecurityContext.getClass().getMethod("pushRunAs", new Class [] {String .class, String .class, String [].class}); 134 } catch (SecurityException e) { 135 throw new IllegalStateException ("Cannot get the method pushRunAs on the JOnAS security context", e); 136 } catch (NoSuchMethodException e) { 137 throw new IllegalStateException ("Cannot get the method pushRunAs on the JOnAS security context", e); 138 } 139 140 String principalName = null; 142 for (Principal principal : runAsSubject.getPrincipals(Principal .class)) { 143 if (!(principal instanceof Group )) { 144 principalName = principal.getName(); 145 break; 146 } 147 } 148 149 String role = null; 151 for (Principal principal : runAsSubject.getPrincipals(Principal .class)) { 152 if (principal instanceof Group ) { 153 role = ((Group ) principal).members().nextElement().getName(); 154 } 155 } 156 157 try { 158 m.invoke(jonasSecurityContext, role, principalName, new String [] {role}); 159 } catch (IllegalArgumentException e) { 160 throw new IllegalStateException ("Cannot call pushRunAs method on the JOnAS security context", e); 161 } catch (IllegalAccessException e) { 162 throw new IllegalStateException ("Cannot call pushRunAs method on the JOnAS security context", e); 163 } catch (InvocationTargetException e) { 164 throw new IllegalStateException ("Cannot call pushRunAs method on the JOnAS security context", e); 165 } 166 167 return null; 169 170 } 171 172 176 public void endsRunAs(final Subject oldSubject) { 177 Method m = null; 178 try { 179 m = jonasSecurityContext.getClass().getMethod("popRunAs"); 180 } catch (SecurityException e) { 181 throw new IllegalStateException ("Cannot get the method popRunAs on the JOnAS security context", e); 182 } catch (NoSuchMethodException e) { 183 throw new IllegalStateException ("Cannot get the method popRunAs on the JOnAS security context", e); 184 } 185 186 try { 187 m.invoke(jonasSecurityContext); 188 } catch (IllegalArgumentException e) { 189 throw new IllegalStateException ("Cannot call popRunAs method on the JOnAS security context", e); 190 } catch (IllegalAccessException e) { 191 throw new IllegalStateException ("Cannot call popRunAs method on the JOnAS security context", e); 192 } catch (InvocationTargetException e) { 193 throw new IllegalStateException ("Cannot call popRunAs method on the JOnAS security context", e); 194 } 195 } 196 197 198 199 } 200 | Popular Tags |