Salome HOME
Update copyrights 2014.
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / kernel / RealmLoginModule.java
1 package org.splat.kernel;
2
3 /**
4  * 
5  * @author    Daniel Brunier-Coulin
6  * @copyright OPEN CASCADE 2012-2014
7  */
8
9 import java.util.Calendar;
10 import java.util.Date;
11 import java.util.Map;
12
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;
22
23 import org.apache.log4j.Logger;
24 import org.splat.dal.bo.kernel.User;
25 import org.splat.service.ServiceLocatorImpl;
26
27 /**
28  * Implementation of login module for SIMAN.
29  */
30 public class RealmLoginModule implements LoginModule {
31
32         /**
33          * The logger.
34          */
35         private static final Logger LOG = Logger.getLogger(RealmLoginModule.class);
36
37         /**
38          * Initial state.
39          */
40         private transient Subject _subject;
41         private transient CallbackHandler _callbackHandler;
42         // private Map sharedState;
43         // private Map options;
44
45         /**
46          * Authentication status.
47          */
48         private transient boolean _succeeded = false;
49         private transient boolean _commit = false;
50
51         /**
52          * Principal.
53          */
54         private transient User _identity = null;
55
56         // ==============================================================================================================================
57         // Constructor
58         // ==============================================================================================================================
59
60         /**
61          * {@inheritDoc}
62          * 
63          * @see javax.security.auth.spi.LoginModule#initialize(javax.security.auth.Subject, javax.security.auth.callback.CallbackHandler,
64          *      java.util.Map, java.util.Map)
65          */
66         public void initialize(final Subject user, final CallbackHandler handler,
67                         final Map<String, ?> state, final Map<String, ?> opts) {
68                 _subject = user;
69                 _callbackHandler = handler;
70                 // sharedState = state;
71                 // options = opts;
72                 // debug = "true".equalsIgnoreCase((String)options.get("debug"));
73                 // _logger = Logger.getLogger(RealmLoginModule.class);
74         }
75
76         // ==============================================================================================================================
77         // Public services
78         // ==============================================================================================================================
79
80         /**
81          * {@inheritDoc}
82          * 
83          * @see javax.security.auth.spi.LoginModule#login()
84          */
85         public boolean login() throws LoginException {
86                 boolean res = false;
87                 try {
88                         // Ask for username password
89                         Callback[] callbacks = new Callback[2];
90                         callbacks[0] = new NameCallback("username");
91                         callbacks[1] = new PasswordCallback("password", false);
92
93                         _callbackHandler.handle(callbacks);
94
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();
101                         }
102
103                         // Authentication
104                         User found = ServiceLocatorImpl.getInstance().getUserService()
105                                         .selectUser(username, password);
106                         _identity = found;
107                         _succeeded = (found != null);
108                         if (_succeeded) {
109                                 Calendar today = java.util.Calendar.getInstance();
110                                 Date datime = today.getTime();
111                                 LOG.info("Connection of " + _identity.toString() + " "
112                                                 + datime.toString() + ".");
113                                 res = true;
114                         } else {
115                                 found = ServiceLocatorImpl.getInstance().getUserService()
116                                                 .selectUser(username);
117                                 String reason = "password";
118                                 if (found == null) {
119                                         reason = "username";
120                                 }
121                                 LOG.info("Connection attempt as " + username + ".");
122                                 throw new FailedLoginException(reason);
123                         }
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"
130                                         + " from the user");
131                 }
132                 return res;
133         }
134
135         /**
136          * {@inheritDoc}
137          * 
138          * @see javax.security.auth.spi.LoginModule#commit()
139          */
140         public boolean commit() throws LoginException {
141                 boolean res = _succeeded;
142                 if (res) {
143                         if (!_subject.getPrincipals().contains(_identity)) {
144                                 _subject.getPrincipals().add(_identity);
145                         }
146                         _identity = null;
147                         _commit = true;
148                 }
149                 return res;
150         }
151
152         /**
153          * {@inheritDoc}
154          * 
155          * @see javax.security.auth.spi.LoginModule#abort()
156          */
157         public boolean abort() throws LoginException {
158                 boolean res = _succeeded;
159                 if (res) {
160                         if (_commit) {
161                                 logout();
162                         } else {
163                                 _identity = null;
164                                 _succeeded = false;
165                         }
166                 }
167                 return res;
168         }
169
170         /**
171          * {@inheritDoc}
172          * 
173          * @see javax.security.auth.spi.LoginModule#logout()
174          */
175         public boolean logout() throws LoginException {
176                 _subject.getPrincipals().remove(_identity);
177                 _identity = null;
178                 _succeeded = false;
179                 _commit = false; // To be validated
180                 return true;
181         }
182 }