1:
37:
38:
39: package ;
40:
41: import ;
42:
43: import ;
44:
45: import ;
46: import ;
47:
48: import ;
49: import ;
50: import ;
51:
52: public class LoginContext
53: {
54:
55: private static final String OTHER = "other";
56:
57: private final String name;
58: private final CallbackHandler cbHandler;
59: private final Subject subject;
60: private final AppConfigurationEntry[] entries;
61: private final LoginModule[] modules;
62: private final Map sharedState;
63:
64: public LoginContext (final String name) throws LoginException
65: {
66: this (name, new Subject(), defaultHandler());
67: }
68:
69: public LoginContext (final String name, final CallbackHandler cbHandler)
70: throws LoginException
71: {
72: this (name, new Subject(), cbHandler);
73: }
74:
75: public LoginContext (final String name, final Subject subject)
76: throws LoginException
77: {
78: this (name, subject, defaultHandler());
79: }
80:
81: public LoginContext (final String name, final Subject subject,
82: final CallbackHandler cbHandler)
83: throws LoginException
84: {
85: this (name, subject, cbHandler, null);
86: }
87:
88:
89: public LoginContext (final String name, final Subject subject,
90: final CallbackHandler cbHandler,
91: Configuration config)
92: throws LoginException
93: {
94: this.name = name;
95: this.subject = subject;
96: this.cbHandler = cbHandler;
97: if (config == null)
98: config = Configuration.getConfig();
99: AppConfigurationEntry[] entries = config.getAppConfigurationEntry (name);
100: if (entries == null)
101: entries = config.getAppConfigurationEntry (OTHER);
102: if (entries == null)
103: throw new LoginException ("no configured modules for application "
104: + name);
105: this.entries = entries;
106: modules = new LoginModule[entries.length];
107: sharedState = new HashMap();
108: for (int i = 0; i < entries.length; i++)
109: modules[i] = lookupModule (entries[i], subject, sharedState);
110: }
111:
112:
119: public Subject getSubject()
120: {
121: return subject;
122: }
123:
124:
134: public void login() throws LoginException
135: {
136: boolean failure = false;
137: for (int i = 0; i < modules.length; i++)
138: {
139: try
140: {
141: boolean result = modules[i].login();
142: if (!result)
143: {
144: if (entries[i].getControlFlag() ==
145: AppConfigurationEntry.LoginModuleControlFlag.REQUISITE)
146: throw new LoginException ("REQUISITE module " + entries[i].getLoginModuleName()
147: + " failed");
148: else if (entries[i].getControlFlag() ==
149: AppConfigurationEntry.LoginModuleControlFlag.REQUIRED)
150: failure = true;
151: }
152: else
153: {
154: if (entries[i].getControlFlag() ==
155: AppConfigurationEntry.LoginModuleControlFlag.SUFFICIENT)
156: break;
157: }
158: }
159: catch (LoginException le)
160: {
161: if (entries[i].getControlFlag() !=
162: AppConfigurationEntry.LoginModuleControlFlag.REQUISITE)
163: continue;
164: for (int j = 0; j < modules.length; j++)
165: modules[i].abort();
166: throw le;
167: }
168: }
169: if (failure)
170: throw new LoginException ("not all REQUIRED modules succeeded");
171:
172: for (int i = 0; i < modules.length; i++)
173: modules[i].commit();
174: }
175:
176:
181: public void logout() throws LoginException
182: {
183: for (int i = 0; i < modules.length; i++)
184: modules[i].logout();
185: }
186:
187:
188:
189:
194: private static CallbackHandler defaultHandler()
195: {
196: GetSecurityPropertyAction act =
197: new GetSecurityPropertyAction ("auth.login.defaultCallbackHandler");
198: String classname = (String) AccessController.doPrivileged (act);
199: if (classname != null)
200: {
201: try
202: {
203: return (CallbackHandler) Class.forName (classname).newInstance();
204: }
205: catch (ClassNotFoundException cnfe)
206: {
207: return null;
208: }
209: catch (ClassCastException cce)
210: {
211: return null;
212: }
213: catch (IllegalAccessException iae)
214: {
215: return null;
216: }
217: catch (InstantiationException ie)
218: {
219: return null;
220: }
221: }
222: return null;
223: }
224:
225: private LoginModule lookupModule (AppConfigurationEntry entry,
226: Subject subject, Map sharedState)
227: throws LoginException
228: {
229: LoginModule module = null;
230: Exception cause = null;
231: try
232: {
233: ClassLoader cl = Thread.currentThread().getContextClassLoader();
234: Class c = Class.forName(entry.getLoginModuleName(), true, cl);
235: module = (LoginModule) c.newInstance();
236: }
237: catch (ClassNotFoundException cnfe)
238: {
239: cause = cnfe;
240: }
241: catch (ClassCastException cce)
242: {
243: cause = cce;
244: }
245: catch (IllegalAccessException iae)
246: {
247: cause = iae;
248: }
249: catch (InstantiationException ie)
250: {
251: cause = ie;
252: }
253:
254: if (cause != null)
255: {
256: LoginException le = new LoginException ("could not load module "
257: + entry.getLoginModuleName());
258: le.initCause (cause);
259: throw le;
260: }
261:
262: module.initialize (subject, cbHandler, sharedState, entry.getOptions());
263: return module;
264: }
265: }