KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > security > jauth > AuthContext


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.security.jauth;
25
26 import java.util.*;
27 import java.lang.reflect.Method JavaDoc;
28 import java.lang.reflect.InvocationTargetException JavaDoc;
29
30 import javax.security.auth.Subject JavaDoc;
31 import javax.security.auth.callback.CallbackHandler JavaDoc;
32 import javax.security.auth.login.AppConfigurationEntry JavaDoc;
33
34 import sun.security.util.Debug;
35
36 /**
37  * Shared logic from Client and ServerAuthContext reside here.
38  */

39 final class AuthContext {
40
41     static final String JavaDoc INIT = "initialize";
42     static final String JavaDoc DISPOSE_SUBJECT = "disposeSubject";
43
44     static final String JavaDoc SECURE_REQUEST = "secureRequest";
45     static final String JavaDoc VALIDATE_RESPONSE = "validateResponse";
46
47     static final String JavaDoc VALIDATE_REQUEST = "validateRequest";
48     static final String JavaDoc SECURE_RESPONSE = "secureResponse";
49
50     // managesSessions method is implemented by looking for
51
// corresponding option value in module configuration
52
static final String JavaDoc MANAGES_SESSIONS = "managesSessions";
53     static final String JavaDoc 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     /**
66      * Invoke modules according to configuration
67      */

68     Object JavaDoc[] invoke(final String JavaDoc methodName, final Object JavaDoc[] args)
69         throws AuthException {
70
71     // invoke modules in a doPrivileged
72
final Object JavaDoc rValues[] = new Object JavaDoc[entries.length];
73
74     try {
75         java.security.AccessController.doPrivileged
76         (new java.security.PrivilegedExceptionAction JavaDoc() {
77         public Object JavaDoc run() throws AuthException {
78             invokePriv(methodName, args, rValues);
79             return null;
80         }
81         });
82     } catch (java.security.PrivilegedActionException JavaDoc 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 JavaDoc methodName, Object JavaDoc[] args, Object JavaDoc[] rValues)
95     throws AuthException {
96
97     // special treatment for managesSessions until the module
98
// interface can be extended.
99
if (methodName.equals(AuthContext.MANAGES_SESSIONS)) {
100         for (int i = 0; i < entries.length; i++) {
101         Map options = entries[i].getOptions();
102         String JavaDoc mS = (String JavaDoc) 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     // XXX no way to reverse module invocation
113

114     for (int i = 0; i < entries.length; i++) {
115
116         // get initialized module instance
117

118         Object JavaDoc module = entries[i].module;
119
120         // invoke the module
121

122         try {
123         Method JavaDoc[] mArray = module.getClass().getMethods();
124         for (int j = 0; j < mArray.length; j++) {
125             if (mArray[j].getName().equals(methodName)) {
126
127             // invoke module
128
rValues[i] = mArray[j].invoke(module, args);
129
130             // success -
131
// return if SUFFICIENT and no previous REQUIRED errors
132

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             // PLEASE NOTE:
161
// this exception will be thrown if any module
162
// in the context does not support the method.
163
NoSuchMethodException JavaDoc nsme =
164             new NoSuchMethodException JavaDoc("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 JavaDoc iae) {
173         AuthException ae = new AuthException();
174         ae.initCause(iae);
175         throw ae;
176         } catch (InvocationTargetException JavaDoc ite) {
177
178         // failure cases
179

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             // immediately throw exception
200

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             // save exception and continue
218

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             // save exception and continue
233

234             if (firstError == null) {
235             firstError = ae;
236             }
237         }
238         }
239     }
240
241     // done invoking entire stack of modules
242

243     if (firstRequiredError != null) {
244         throw firstRequiredError;
245     } else if (firstError != null && !success) {
246         throw firstError;
247     }
248
249     // if no errors, return gracefully
250
if (debug != null) {
251         debug.println("overall " + methodName + " success");
252     }
253     }
254 }
255
Popular Tags