1 23 24 package com.sun.enterprise.security.jauth; 25 26 import java.util.*; 27 import java.lang.reflect.Method ; 28 import java.lang.reflect.InvocationTargetException ; 29 30 import javax.security.auth.Subject ; 31 import javax.security.auth.callback.CallbackHandler ; 32 import javax.security.auth.login.AppConfigurationEntry ; 33 34 import sun.security.util.Debug; 35 36 39 final class AuthContext { 40 41 static final String INIT = "initialize"; 42 static final String DISPOSE_SUBJECT = "disposeSubject"; 43 44 static final String SECURE_REQUEST = "secureRequest"; 45 static final String VALIDATE_RESPONSE = "validateResponse"; 46 47 static final String VALIDATE_REQUEST = "validateRequest"; 48 static final String SECURE_RESPONSE = "secureResponse"; 49 50 static final String MANAGES_SESSIONS = "managesSessions"; 53 static final String MANAGES_SESSIONS_OPTION = "managessessions"; 54 55 private ConfigFile.Entry[] entries; 56 private Debug debug; 57 58 AuthContext(ConfigFile.Entry[] entries, 59 Debug debug) throws AuthException { 60 61 this.entries = entries; 62 this.debug = debug; 63 } 64 65 68 Object [] invoke(final String methodName, final Object [] args) 69 throws AuthException { 70 71 final Object rValues[] = new Object [entries.length]; 73 74 try { 75 java.security.AccessController.doPrivileged 76 (new java.security.PrivilegedExceptionAction () { 77 public Object run() throws AuthException { 78 invokePriv(methodName, args, rValues); 79 return null; 80 } 81 }); 82 } catch (java.security.PrivilegedActionException pae) { 83 if (pae.getException() instanceof AuthException) { 84 throw (AuthException)pae.getException(); 85 } else { 86 AuthException ae = new AuthException(); 87 ae.initCause(pae.getException()); 88 throw ae; 89 } 90 } 91 return rValues; 92 } 93 94 void invokePriv(String methodName, Object [] args, Object [] rValues) 95 throws AuthException { 96 97 if (methodName.equals(AuthContext.MANAGES_SESSIONS)) { 100 for (int i = 0; i < entries.length; i++) { 101 Map options = entries[i].getOptions(); 102 String mS = (String ) options.get(AuthContext.MANAGES_SESSIONS_OPTION); 103 rValues[i] = Boolean.valueOf(mS); 104 } 105 return; 106 } 107 108 boolean success = false; 109 AuthException firstRequiredError = null; 110 AuthException firstError = null; 111 112 114 for (int i = 0; i < entries.length; i++) { 115 116 118 Object module = entries[i].module; 119 120 122 try { 123 Method [] mArray = module.getClass().getMethods(); 124 for (int j = 0; j < mArray.length; j++) { 125 if (mArray[j].getName().equals(methodName)) { 126 127 rValues[i] = mArray[j].invoke(module, args); 129 130 133 if (firstRequiredError == null && 134 entries[i].getControlFlag() == 135 AppConfigurationEntry.LoginModuleControlFlag.SUFFICIENT) { 136 137 if (debug != null) { 138 debug.println(entries[i].getLoginModuleName() + 139 "." + 140 methodName + 141 " SUFFICIENT success"); 142 } 143 144 return; 145 } 146 147 if (debug != null) { 148 debug.println(entries[i].getLoginModuleName() + 149 "." + 150 methodName + 151 " success"); 152 } 153 154 success = true; 155 break; 156 } 157 } 158 159 if (!success) { 160 NoSuchMethodException nsme = 164 new NoSuchMethodException ("module " + 165 module.getClass().getName() + 166 " does not implement " + 167 methodName); 168 AuthException ae = new AuthException(); 169 ae.initCause(nsme); 170 throw ae; 171 } 172 } catch (IllegalAccessException iae) { 173 AuthException ae = new AuthException(); 174 ae.initCause(iae); 175 throw ae; 176 } catch (InvocationTargetException ite) { 177 178 180 AuthException ae; 181 182 if (ite.getCause() instanceof AuthException) { 183 ae = (AuthException)ite.getCause(); 184 } else { 185 ae = new AuthException(); 186 ae.initCause(ite.getCause()); 187 } 188 189 if (entries[i].getControlFlag() == 190 AppConfigurationEntry.LoginModuleControlFlag.REQUISITE) { 191 192 if (debug != null) { 193 debug.println(entries[i].getLoginModuleName() + 194 "." + 195 methodName + 196 " REQUISITE failure"); 197 } 198 199 201 if (firstRequiredError != null) { 202 throw firstRequiredError; 203 } else { 204 throw ae; 205 } 206 207 } else if (entries[i].getControlFlag() == 208 AppConfigurationEntry.LoginModuleControlFlag.REQUIRED) { 209 210 if (debug != null) { 211 debug.println(entries[i].getLoginModuleName() + 212 "." + 213 methodName + 214 " REQUIRED failure"); 215 } 216 217 219 if (firstRequiredError == null) { 220 firstRequiredError = ae; 221 } 222 223 } else { 224 225 if (debug != null) { 226 debug.println(entries[i].getLoginModuleName() + 227 "." + 228 methodName + 229 " OPTIONAL failure"); 230 } 231 232 234 if (firstError == null) { 235 firstError = ae; 236 } 237 } 238 } 239 } 240 241 243 if (firstRequiredError != null) { 244 throw firstRequiredError; 245 } else if (firstError != null && !success) { 246 throw firstError; 247 } 248 249 if (debug != null) { 251 debug.println("overall " + methodName + " success"); 252 } 253 } 254 } 255 | Popular Tags |