1 package org.splat.kernel;
5 * @author Daniel Brunier-Coulin
6 * @copyright OPEN CASCADE 2012
9 import java.util.Calendar;
10 import java.util.Date;
13 import javax.security.auth.Subject;
14 import javax.security.auth.callback.Callback;
15 import javax.security.auth.callback.CallbackHandler;
16 import javax.security.auth.callback.NameCallback;
17 import javax.security.auth.callback.PasswordCallback;
18 import javax.security.auth.callback.UnsupportedCallbackException;
19 import javax.security.auth.login.FailedLoginException;
20 import javax.security.auth.login.LoginException;
21 import javax.security.auth.spi.LoginModule;
23 import org.apache.log4j.Logger;
24 import org.splat.dal.bo.kernel.User;
25 import org.splat.service.ServiceLocatorImpl;
27 public class RealmLoginModule implements LoginModule {
30 private transient Subject _subject;
31 private transient CallbackHandler _callbackHandler;
32 // private Map sharedState;
33 // private Map options;
35 // Authentication status
36 private transient boolean _succeeded = false;
37 private transient boolean _commit = false;
40 private transient User _identity = null;
42 private static final Logger LOG = Logger.getLogger(RealmLoginModule.class);
44 // ==============================================================================================================================
46 // ==============================================================================================================================
48 public void initialize(final Subject user, final CallbackHandler handler,
49 final Map<String, ?> state, final Map<String, ?> opts) {
50 // --------------------------------------------------------------------------------------------------------
52 _callbackHandler = handler;
53 // sharedState = state;
55 // debug = "true".equalsIgnoreCase((String)options.get("debug"));
56 //_logger = Logger.getLogger(RealmLoginModule.class);
59 // ==============================================================================================================================
61 // ==============================================================================================================================
63 public boolean login() throws LoginException {
66 // Ask for username password
67 Callback[] callbacks = new Callback[2];
68 callbacks[0] = new NameCallback("username");
69 callbacks[1] = new PasswordCallback("password", false);
71 _callbackHandler.handle(callbacks);
73 String username = ((NameCallback) callbacks[0]).getName();
74 String password = null;
75 char[] entered = ((PasswordCallback) callbacks[1]).getPassword();
76 if (entered != null) {
77 password = new String(entered);
78 ((PasswordCallback) callbacks[1]).clearPassword();
82 User found = ServiceLocatorImpl.getInstance().getUserService()
83 .selectUser(username, password);
85 _succeeded = (found != null);
87 Calendar today = java.util.Calendar.getInstance();
88 Date datime = today.getTime();
89 LOG.info("Connection of " + _identity.toString() + " "
90 + datime.toString() + ".");
93 found = ServiceLocatorImpl.getInstance().getUserService()
94 .selectUser(username);
95 String reason = "password";
99 LOG.info("Connection attempt as " + username + ".");
100 throw new FailedLoginException(reason);
102 } catch (java.io.IOException ioe) {
103 throw new LoginException(ioe.getMessage()); // RKV: NOPMD: The message is sent into the constructor
104 } catch (UnsupportedCallbackException uce) {
105 throw new LoginException("Error: " + uce.getCallback().toString() // RKV: NOPMD: Stacktrace is printed
106 + " not available to garner authentication information"
112 public boolean commit() throws LoginException {
113 boolean res = _succeeded;
115 if (!_subject.getPrincipals().contains(_identity)) {
116 _subject.getPrincipals().add(_identity);
124 public boolean abort() throws LoginException {
125 boolean res = _succeeded;
127 if (_succeeded && !_commit) {
137 public boolean logout() throws LoginException {
138 _subject.getPrincipals().remove(_identity);
141 _commit = false; // To be validated