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: Configuration config = Configuration.getConfig();
86: AppConfigurationEntry[] entries = config.getAppConfigurationEntry (name);
87: if (entries == null)
88: entries = config.getAppConfigurationEntry (OTHER);
89: if (entries == null)
90: throw new LoginException ("no configured modules for application "
91: + name);
92: this.entries = entries;
93: modules = new LoginModule[entries.length];
94: sharedState = new HashMap();
95: for (int i = 0; i < entries.length; i++)
96: modules[i] = lookupModule (entries[i], subject, sharedState);
97: this.name = name;
98: this.subject = subject;
99: this.cbHandler = cbHandler;
100: }
101:
102:
109: public Subject getSubject()
110: {
111: return subject;
112: }
113:
114:
124: public void login() throws LoginException
125: {
126: boolean failure = false;
127: for (int i = 0; i < modules.length; i++)
128: {
129: try
130: {
131: boolean result = modules[i].login();
132: if (!result)
133: {
134: if (entries[i].getControlFlag() ==
135: AppConfigurationEntry.LoginModuleControlFlag.REQUISITE)
136: throw new LoginException ("REQUISITE module " + entries[i].getLoginModuleName()
137: + " failed");
138: else if (entries[i].getControlFlag() ==
139: AppConfigurationEntry.LoginModuleControlFlag.REQUIRED)
140: failure = true;
141: }
142: else
143: {
144: if (entries[i].getControlFlag() ==
145: AppConfigurationEntry.LoginModuleControlFlag.SUFFICIENT)
146: break;
147: }
148: }
149: catch (LoginException le)
150: {
151: if (entries[i].getControlFlag() !=
152: AppConfigurationEntry.LoginModuleControlFlag.REQUISITE)
153: continue;
154: for (int j = 0; j < modules.length; j++)
155: modules[i].abort();
156: throw le;
157: }
158: }
159: if (failure)
160: throw new LoginException ("not all REQUIRED modules succeeded");
161:
162: for (int i = 0; i < modules.length; i++)
163: modules[i].commit();
164: }
165:
166:
171: public void logout() throws LoginException
172: {
173: for (int i = 0; i < modules.length; i++)
174: modules[i].logout();
175: }
176:
177:
178:
179:
184: private static CallbackHandler defaultHandler()
185: {
186: GetSecurityPropertyAction act =
187: new GetSecurityPropertyAction ("auth.login.defaultCallbackHandler");
188: String classname = (String) AccessController.doPrivileged (act);
189: if (classname != null)
190: {
191: try
192: {
193: return (CallbackHandler) Class.forName (classname).newInstance();
194: }
195: catch (ClassNotFoundException cnfe)
196: {
197: return null;
198: }
199: catch (ClassCastException cce)
200: {
201: return null;
202: }
203: catch (IllegalAccessException iae)
204: {
205: return null;
206: }
207: catch (InstantiationException ie)
208: {
209: return null;
210: }
211: }
212: return null;
213: }
214:
215: private LoginModule lookupModule (AppConfigurationEntry entry,
216: Subject subject, Map sharedState)
217: throws LoginException
218: {
219: LoginModule module = null;
220: Exception cause = null;
221: try
222: {
223: ClassLoader cl = Thread.currentThread().getContextClassLoader();
224: Class c = Class.forName(entry.getLoginModuleName(), true, cl);
225: module = (LoginModule) c.newInstance();
226: }
227: catch (ClassNotFoundException cnfe)
228: {
229: cause = cnfe;
230: }
231: catch (ClassCastException cce)
232: {
233: cause = cce;
234: }
235: catch (IllegalAccessException iae)
236: {
237: cause = iae;
238: }
239: catch (InstantiationException ie)
240: {
241: cause = ie;
242: }
243:
244: if (cause != null)
245: {
246: LoginException le = new LoginException ("could not load module "
247: + entry.getLoginModuleName());
248: le.initCause (cause);
249: throw le;
250: }
251:
252: module.initialize (subject, cbHandler, sharedState, entry.getOptions());
253: return module;
254: }
255: }