Salome HOME
0a8eeb7f1b97787303577eab4e541fa3c5ee8df7
[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
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 public class RealmLoginModule implements LoginModule {
28
29         // Initial state
30         private transient Subject _subject;
31         private transient CallbackHandler _callbackHandler;
32         // private Map sharedState;
33         // private Map options;
34
35         // Authentication status
36         private transient boolean _succeeded = false;
37         private transient boolean _commit = false;
38
39         // Principal
40         private transient User _identity = null;
41
42         private static final Logger LOG = Logger.getLogger(RealmLoginModule.class);
43
44         // ==============================================================================================================================
45         // Constructor
46         // ==============================================================================================================================
47
48         public void initialize(final Subject user, final CallbackHandler handler,
49                         final Map<String, ?> state, final Map<String, ?> opts) {
50                 // --------------------------------------------------------------------------------------------------------
51                 _subject = user;
52                 _callbackHandler = handler;
53                 // sharedState = state;
54                 // options = opts;
55                 // debug = "true".equalsIgnoreCase((String)options.get("debug"));
56                 //_logger = Logger.getLogger(RealmLoginModule.class);
57         }
58
59         // ==============================================================================================================================
60         // Public services
61         // ==============================================================================================================================
62
63         public boolean login() throws LoginException {
64                 boolean res = false;
65                 try {
66                         // Ask for username password
67                         Callback[] callbacks = new Callback[2];
68                         callbacks[0] = new NameCallback("username");
69                         callbacks[1] = new PasswordCallback("password", false);
70
71                         _callbackHandler.handle(callbacks);
72
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();
79                         }
80
81                         // Authentication
82                         User found = ServiceLocatorImpl.getInstance().getUserService()
83                                         .selectUser(username, password);
84                         _identity = found;
85                         _succeeded = (found != null);
86                         if (_succeeded) {
87                                 Calendar today = java.util.Calendar.getInstance();
88                                 Date datime = today.getTime();
89                                 LOG.info("Connection of " + _identity.toString() + " "
90                                                 + datime.toString() + ".");
91                                 res = true;
92                         } else {
93                                 found = ServiceLocatorImpl.getInstance().getUserService()
94                                                 .selectUser(username);
95                                 String reason = "password";
96                                 if (found == null) {
97                                         reason = "username";
98                                 }
99                                 LOG.info("Connection attempt as " + username + ".");
100                                 throw new FailedLoginException(reason);
101                         }
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"
107                                         + " from the user");
108                 }
109                 return res;
110         }
111
112         public boolean commit() throws LoginException {
113                 boolean res = _succeeded;
114                 if (res) {
115                         if (!_subject.getPrincipals().contains(_identity)) {
116                                 _subject.getPrincipals().add(_identity);
117                         }
118                         _identity = null;
119                         _commit = true;
120                 }
121                 return res;
122         }
123
124         public boolean abort() throws LoginException {
125                 boolean res = _succeeded;
126                 if (res) {
127                         if (_succeeded && !_commit) {
128                                 _identity = null;
129                                 _succeeded = false;
130                         } else {
131                                 logout();
132                         }
133                 }
134                 return res;
135         }
136
137         public boolean logout() throws LoginException {
138                 _subject.getPrincipals().remove(_identity);
139                 _identity = null;
140                 _succeeded = false;
141                 _commit = false; // To be validated
142                 return true;
143         }
144 }