1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.efaps.jaas;
22
23 import java.lang.reflect.InvocationTargetException;
24 import java.lang.reflect.Method;
25 import java.util.HashSet;
26 import java.util.Map;
27 import java.util.Set;
28
29 import javax.security.auth.callback.Callback;
30 import javax.security.auth.callback.CallbackHandler;
31 import javax.security.auth.callback.NameCallback;
32 import javax.security.auth.callback.PasswordCallback;
33 import javax.security.auth.callback.TextOutputCallback;
34 import javax.security.auth.callback.UnsupportedCallbackException;
35 import javax.security.auth.login.LoginContext;
36 import javax.security.auth.login.LoginException;
37
38 import org.efaps.admin.user.Group;
39 import org.efaps.admin.user.JAASSystem;
40 import org.efaps.admin.user.Person;
41 import org.efaps.admin.user.Role;
42 import org.efaps.util.EFapsException;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46
47
48
49
50
51
52
53
54
55
56 public class LoginHandler
57 {
58
59
60
61 private static Logger LOG = LoggerFactory.getLogger(LoginHandler.class);
62
63
64
65
66
67
68
69 private String applicationName = "eFaps";
70
71
72
73
74
75
76
77
78 public LoginHandler(final String _application)
79 {
80 if (_application != null) {
81 this.applicationName = _application;
82 }
83 }
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100 public Person checkLogin(final String _name,
101 final String _passwd)
102 {
103 Person person = null;
104 try {
105 final LoginCallbackHandler callback = new LoginCallbackHandler(ActionCallback.Mode.LOGIN, _name, _passwd);
106 final LoginContext login = new LoginContext(getApplicationName(), callback);
107 login.login();
108
109 person = getPerson(login);
110
111 if (person == null) {
112 person = createPerson(login);
113 }
114
115 if (person != null) {
116 updatePerson(login, person);
117
118 person.cleanUp();
119
120 updateRoles(login, person);
121 updateGroups(login, person);
122 updateCompanies(login, person);
123
124 person.updateLastLogin();
125 }
126 } catch (final EFapsException e) {
127 LoginHandler.LOG.error("login failed for '" + _name + "'", e);
128 } catch (final LoginException e) {
129 LoginHandler.LOG.error("login failed for '" + _name + "'", e);
130 }
131 return person;
132 }
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147 protected Person getPerson(final LoginContext _login)
148 throws EFapsException
149 {
150 Person person = null;
151 for (final JAASSystem system : JAASSystem.getAllJAASSystems()) {
152 final Set<?> users = _login.getSubject().getPrincipals(system.getPersonJAASPrincipleClass());
153
154 for (final Object persObj : users) {
155 try {
156 final String persKey = (String) system.getPersonMethodKey().invoke(persObj);
157
158 final Person foundPerson = Person.getWithJAASKey(system, persKey);
159 if (foundPerson == null) {
160 person.assignToJAASSystem(system, persKey);
161 } else if (person == null) {
162 person = foundPerson;
163 } else if (person.getId() != foundPerson.getId()) {
164 LoginHandler.LOG.error("For JAAS system " + system.getName() + " "
165 + "person with key '" + persKey + "' is not unique!"
166 + "Have found person '" + person.getName() + "' " + "(id = "
167 + person.getId() + ") and person " + "'"
168 + foundPerson.getName() + "' " + "(id = " + foundPerson.getId()
169 + ").");
170 throw new EFapsException(LoginHandler.class, "notFound", persKey);
171 }
172 } catch (final IllegalAccessException e) {
173 LoginHandler.LOG.error("could not execute person key method for system " + system.getName(), e);
174 throw new EFapsException(LoginHandler.class, "IllegalAccessException", e);
175 } catch (final IllegalArgumentException e) {
176 LoginHandler.LOG.error("could not execute person key method for system " + system.getName(), e);
177 throw new EFapsException(LoginHandler.class, "IllegalArgumentException", e);
178 } catch (final InvocationTargetException e) {
179 LoginHandler.LOG.error("could not execute person key method for system " + system.getName(), e);
180 throw new EFapsException(LoginHandler.class, "InvocationTargetException", e);
181 }
182 }
183 }
184 return person;
185 }
186
187
188
189
190
191
192
193
194
195
196
197 protected Person createPerson(final LoginContext _login)
198 throws EFapsException
199 {
200 Person person = null;
201
202 for (final JAASSystem system : JAASSystem.getAllJAASSystems()) {
203 final Set<?> users = _login.getSubject().getPrincipals(system.getPersonJAASPrincipleClass());
204 for (final Object persObj : users) {
205 try {
206 final String persKey = (String) system.getPersonMethodKey().invoke(persObj);
207 final String persName = (String) system.getPersonMethodName().invoke(persObj);
208
209 if (person == null) {
210 person = Person.createPerson(system, persKey, persName);
211 } else {
212 person.assignToJAASSystem(system, persKey);
213 }
214 } catch (final IllegalAccessException e) {
215 LoginHandler.LOG.error("could not execute a person method for system " + system.getName(), e);
216 throw new EFapsException(LoginHandler.class, "IllegalAccessException", e);
217 } catch (final IllegalArgumentException e) {
218 LoginHandler.LOG.error("could not execute a person method for system " + system.getName(), e);
219 throw new EFapsException(LoginHandler.class, "IllegalArgumentException", e);
220 } catch (final InvocationTargetException e) {
221 LoginHandler.LOG.error("could not execute a person method for system " + system.getName(), e);
222 throw new EFapsException(LoginHandler.class, "InvocationTargetException", e);
223 }
224 }
225 }
226 return person;
227 }
228
229
230
231
232
233
234
235
236
237
238
239
240 protected void updatePerson(final LoginContext _login,
241 final Person _person)
242 throws EFapsException
243 {
244 for (final JAASSystem system : JAASSystem.getAllJAASSystems()) {
245 final Set<?> users = _login.getSubject().getPrincipals(system.getPersonJAASPrincipleClass());
246 for (final Object persObj : users) {
247 try {
248 for (final Map.Entry<Person.AttrName, Method> entry
249 : system.getPersonMethodAttributes().entrySet()) {
250 _person.updateAttrValue(entry.getKey(), (String) entry.getValue().invoke(persObj));
251 }
252 } catch (final IllegalAccessException e) {
253 LoginHandler.LOG.error("could not execute a person method for system " + system.getName(), e);
254 throw new EFapsException(LoginHandler.class, "IllegalAccessException", e);
255 } catch (final IllegalArgumentException e) {
256 LoginHandler.LOG.error("could not execute a person method for system " + system.getName(), e);
257 throw new EFapsException(LoginHandler.class, "IllegalArgumentException", e);
258 } catch (final InvocationTargetException e) {
259 LoginHandler.LOG.error("could not execute a person method for system " + system.getName(), e);
260 throw new EFapsException(LoginHandler.class, "InvocationTargetException", e);
261 }
262 }
263 }
264 _person.commitAttrValuesInDB();
265 }
266
267
268
269
270
271
272
273
274
275
276
277 protected void updateRoles(final LoginContext _login,
278 final Person _person)
279 throws EFapsException
280 {
281 for (final JAASSystem system : JAASSystem.getAllJAASSystems()) {
282 if (system.getRoleJAASPrincipleClass() != null) {
283 final Set<?> rolesJaas = _login.getSubject().getPrincipals(system.getRoleJAASPrincipleClass());
284 final Set<Role> rolesEfaps = new HashSet<Role>();
285 for (final Object roleObj : rolesJaas) {
286 try {
287 final String roleKey = (String) system.getRoleMethodKey().invoke(roleObj);
288 final Role roleEfaps = Role.getWithJAASKey(system, roleKey);
289 if (roleEfaps != null) {
290 rolesEfaps.add(roleEfaps);
291 }
292 } catch (final IllegalAccessException e) {
293 LoginHandler.LOG.error("could not execute role key method for system " + system.getName(), e);
294 } catch (final IllegalArgumentException e) {
295 LoginHandler.LOG.error("could not execute role key method for system " + system.getName(), e);
296 } catch (final InvocationTargetException e) {
297 LoginHandler.LOG.error("could not execute role key method for system " + system.getName(), e);
298 }
299 }
300 _person.setRoles(system, rolesEfaps);
301 }
302 }
303 }
304
305
306
307
308
309
310
311
312
313
314
315 protected void updateGroups(final LoginContext _login,
316 final Person _person)
317 throws EFapsException
318 {
319 for (final JAASSystem system : JAASSystem.getAllJAASSystems()) {
320 if (system.getGroupJAASPrincipleClass() != null) {
321 final Set<?> groupsJaas = _login.getSubject().getPrincipals(system.getGroupJAASPrincipleClass());
322 final Set<Group> groupsEfaps = new HashSet<Group>();
323 for (final Object groupObj : groupsJaas) {
324 try {
325 final String groupKey = (String) system.getGroupMethodKey().invoke(groupObj);
326 final Group groupEfaps = Group.getWithJAASKey(system, groupKey);
327 if (groupEfaps != null) {
328 groupsEfaps.add(groupEfaps);
329 }
330 } catch (final IllegalAccessException e) {
331 LoginHandler.LOG.error("could not execute group key method for system " + system.getName(), e);
332 } catch (final IllegalArgumentException e) {
333 LoginHandler.LOG.error("could not execute group key method for system " + system.getName(), e);
334 } catch (final InvocationTargetException e) {
335 LoginHandler.LOG.error("could not execute group key method for system " + system.getName(), e);
336 }
337 }
338 _person.setGroups(system, groupsEfaps);
339 }
340 }
341 }
342
343
344
345
346
347
348
349
350
351
352
353 protected void updateCompanies(final LoginContext _login,
354 final Person _person)
355 throws EFapsException
356 {
357 if (!JAASSystem.getAllJAASSystems().isEmpty()) {
358 _person.setCompanies(JAASSystem.getAllJAASSystems().iterator().next(), _person.getCompaniesFromDB(null));
359 }
360 }
361
362
363
364
365
366
367
368 public String getApplicationName()
369 {
370 return this.applicationName;
371 }
372
373
374
375
376
377
378 protected class LoginCallbackHandler
379 implements CallbackHandler
380 {
381
382
383
384 private final String name;
385
386
387
388
389 private final String password;
390
391
392
393
394
395 private final ActionCallback.Mode mode;
396
397
398
399
400
401
402
403
404
405
406
407
408 protected LoginCallbackHandler(final ActionCallback.Mode _mode,
409 final String _name,
410 final String _passwd)
411 {
412 this.mode = _mode;
413 this.name = _name;
414 this.password = _passwd;
415 }
416
417
418
419
420
421
422
423
424
425
426
427
428 public void handle(final Callback[] _callbacks)
429 throws UnsupportedCallbackException
430 {
431 for (int i = 0; i < _callbacks.length; i++) {
432 if (_callbacks[i] instanceof ActionCallback) {
433 final ActionCallback ac = (ActionCallback) _callbacks[i];
434 ac.setMode(this.mode);
435 } else if (_callbacks[i] instanceof NameCallback) {
436 final NameCallback nc = (NameCallback) _callbacks[i];
437 nc.setName(this.name);
438 } else if (_callbacks[i] instanceof PasswordCallback) {
439 if (this.password != null) {
440 final PasswordCallback pc = (PasswordCallback) _callbacks[i];
441 pc.setPassword(this.password.toCharArray());
442 }
443 } else if (!(_callbacks[i] instanceof TextOutputCallback)) {
444 throw new UnsupportedCallbackException(_callbacks[i], "Unrecognized Callback");
445 }
446 }
447 }
448 }
449 }