1 18 19 package org.objectweb.jac.aspects.authentication; 20 21 import org.aopalliance.intercept.ConstructorInvocation; 22 import org.aopalliance.intercept.MethodInvocation; 23 import org.objectweb.jac.aspects.gui.*; 24 import org.objectweb.jac.core.*; 25 import org.objectweb.jac.core.AspectComponent; 26 import org.objectweb.jac.core.rtti.*; 27 import org.objectweb.jac.util.*; 28 29 35 36 public class AuthenticationWrapper extends Wrapper { 37 38 Authenticator authenticator; 39 MethodItem controller; 40 41 public Object invoke(MethodInvocation invocation) throws Throwable { 42 return authenticateAndControl((Interaction) invocation); 43 } 44 45 public Object construct(ConstructorInvocation invocation) 46 throws Throwable { 47 return authenticateAndControl((Interaction) invocation); 48 } 49 50 57 public AuthenticationWrapper( 58 AspectComponent ac, 59 Authenticator authenticator, 60 MethodItem controller) 61 { 62 super(ac); 63 Log.trace( 64 "authentication", 65 "new authentication wrapper: " + authenticator + "," + controller); 66 this.authenticator = authenticator; 67 this.controller = controller; 68 } 69 70 78 public void setController(MethodItem controller) { 79 Log.trace( 80 "authentication", 81 "wrapper setController(" + controller + ")"); 82 this.controller = controller; 83 } 84 85 public void setAuthenticator(Authenticator authenticator) { 86 Log.trace( 87 "authentication", 88 "wrapper setAuthenticator(" + authenticator + ")"); 89 this.authenticator = authenticator; 90 } 91 92 99 public Object authenticateAndControl(Interaction interaction) 100 throws AuthenticationFailedException, AccessDeniedException, Throwable { 101 102 if (interaction.wrappee instanceof Display 103 && interaction.method.getName().equals("showCustomized")) { 104 CustomizedGUI cgui = (CustomizedGUI) interaction.args[1]; 105 if (cgui != null) { 106 Log.trace( 107 "application", 108 "auth sets application to " + cgui.getApplication()); 109 Collaboration.get().setCurApp(cgui.getApplication()); 110 } else { 111 Log.trace( 112 "application", 113 "auth cannot set the application since " 114 + "customized GUI is null"); 115 } 116 } 117 118 Log.trace( 119 "authentication", 120 "authenticate for method " 121 + interaction.method 122 + " on " 123 + interaction.wrappee); 124 Log.trace( 125 "authentication", 126 "name is: " + (String ) attr(AuthenticationAC.USER)); 127 String name = (String ) attr(AuthenticationAC.USER); 128 String password = null; 129 if (name == null) { 130 try { 131 name = authenticator.authenticate(); 132 Log.trace("authentication", "authenticated " + name); 133 attrdef(AuthenticationAC.USER, name); 134 } catch (Exception e) { 135 Log.trace( 136 "authentication", 137 "user authentication failed for " 138 + interaction.method 139 + " because of exception: " 140 + e); 141 e.printStackTrace(); 142 } 143 } 144 try { 145 Boolean allowed = 146 (Boolean ) controller.invokeStatic( 147 new Object [] { 148 name, 149 interaction.wrappee, 150 interaction.method }); 151 if (allowed.booleanValue()) { 152 Log.trace( 153 "authentication", 154 "accesses granted to " 155 + name 156 + " for " 157 + interaction.method); 158 return proceed(interaction); 159 } else { 160 Log.trace( 161 "authentication", 162 "accesses denied to " 163 + name 164 + " for " 165 + interaction.method); 166 throw new AccessDeniedException( 167 accessDeniedMessage != null 168 ? accessDeniedMessage 169 : "you are not allowed to call " 170 + interaction.method 171 + " on " 172 + interaction.wrappee); 173 } 174 } catch (Exception e) { 175 Log.trace( 176 "authentication", 177 "accesses denied to " 178 + name 179 + " for " 180 + interaction.method 181 + " because of exception: " 182 + e); 183 Log.trace("authentication", 2, e); 184 throw new AccessDeniedException( 185 "you are not allowed to call " 186 + interaction.method 187 + " on " 188 + interaction.wrappee 189 + ": " 190 + e); 191 } 192 } 193 194 200 public boolean isTrustedUser(String username) { 201 Log.trace( 202 "authentication", 203 "is trusted user: " + username + ":" + getAspectComponent()); 204 return ((AuthenticationAC) getAspectComponent()).isTrustedUser( 205 username); 206 } 207 208 String accessDeniedMessage = "Access denied"; 209 210 215 public void setAccessDeniedMessage(String message) { 216 this.accessDeniedMessage = message; 217 } 218 219 224 public void catchAccessDenied(AccessDeniedException e) { 225 Log.trace("authentication", "catching " + e.toString()); 226 DisplayContext context = 227 (DisplayContext) Collaboration.get().getAttribute( 228 GuiAC.DISPLAY_CONTEXT); 229 if (context != null) { 230 Display display = context.getDisplay(); 231 if (display != null) { 232 display.showMessage("Error", accessDeniedMessage); 233 return; 234 } 235 } 236 Log.error("no display available"); 237 } 238 239 246 public static boolean dummyController( 247 String username, 248 Object wrappee, 249 MethodItem method) { 250 return true; 251 } 252 253 } 254 | Popular Tags |