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;
28 * Implementation of login module for SIMAN.
30 public class RealmLoginModule implements LoginModule {
35 private static final Logger LOG = Logger.getLogger(RealmLoginModule.class);
40 private transient Subject _subject;
41 private transient CallbackHandler _callbackHandler;
42 // private Map sharedState;
43 // private Map options;
46 * Authentication status.
48 private transient boolean _succeeded = false;
49 private transient boolean _commit = false;
54 private transient User _identity = null;
56 // ==============================================================================================================================
58 // ==============================================================================================================================
63 * @see javax.security.auth.spi.LoginModule#initialize(javax.security.auth.Subject, javax.security.auth.callback.CallbackHandler,
64 * java.util.Map, java.util.Map)
66 public void initialize(final Subject user, final CallbackHandler handler,
67 final Map<String, ?> state, final Map<String, ?> opts) {
69 _callbackHandler = handler;
70 // sharedState = state;
72 // debug = "true".equalsIgnoreCase((String)options.get("debug"));
73 // _logger = Logger.getLogger(RealmLoginModule.class);
76 // ==============================================================================================================================
78 // ==============================================================================================================================
83 * @see javax.security.auth.spi.LoginModule#login()
85 public boolean login() throws LoginException {
88 // Ask for username password
89 Callback[] callbacks = new Callback[2];
90 callbacks[0] = new NameCallback("username");
91 callbacks[1] = new PasswordCallback("password", false);
93 _callbackHandler.handle(callbacks);
95 String username = ((NameCallback) callbacks[0]).getName();
96 String password = null;
97 char[] entered = ((PasswordCallback) callbacks[1]).getPassword();
98 if (entered != null) {
99 password = new String(entered);
100 ((PasswordCallback) callbacks[1]).clearPassword();
104 User found = ServiceLocatorImpl.getInstance().getUserService()
105 .selectUser(username, password);
107 _succeeded = (found != null);
109 Calendar today = java.util.Calendar.getInstance();
110 Date datime = today.getTime();
111 LOG.info("Connection of " + _identity.toString() + " "
112 + datime.toString() + ".");
115 found = ServiceLocatorImpl.getInstance().getUserService()
116 .selectUser(username);
117 String reason = "password";
121 LOG.info("Connection attempt as " + username + ".");
122 throw new FailedLoginException(reason);
124 } catch (java.io.IOException ioe) {
125 throw new LoginException(ioe.getMessage()); // RKV: NOPMD: The message is sent into the constructor
126 } catch (UnsupportedCallbackException uce) {
127 throw new LoginException("Error: " // RKV: NOPMD: Stacktrace is printed
128 + uce.getCallback().toString()
129 + " not available to garner authentication information"
138 * @see javax.security.auth.spi.LoginModule#commit()
140 public boolean commit() throws LoginException {
141 boolean res = _succeeded;
143 if (!_subject.getPrincipals().contains(_identity)) {
144 _subject.getPrincipals().add(_identity);
155 * @see javax.security.auth.spi.LoginModule#abort()
157 public boolean abort() throws LoginException {
158 boolean res = _succeeded;
173 * @see javax.security.auth.spi.LoginModule#logout()
175 public boolean logout() throws LoginException {
176 _subject.getPrincipals().remove(_identity);
179 _commit = false; // To be validated