Salome HOME
Refactoring continues: UserService is created instead of UserDirectory. Database...
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / kernel / RealmLoginModule.java
1 package org.splat.kernel;
2 /**
3  * 
4  * @author    Daniel Brunier-Coulin
5  * @copyright OPEN CASCADE 2012
6  */
7
8 import java.util.Calendar;
9 import java.util.Date;
10 import java.util.Map;
11
12 import javax.security.auth.*;
13 import javax.security.auth.callback.*;
14 import javax.security.auth.login.*;
15 import javax.security.auth.spi.*;
16
17 import org.apache.log4j.Logger;
18 import org.splat.dal.bo.kernel.User;
19 import org.splat.service.ServiceLocatorImpl;
20
21
22 public class RealmLoginModule implements LoginModule {
23         
24 //  Initial state
25     private Subject         subject;
26     private CallbackHandler callbackHandler;
27 //  private Map             sharedState;
28 //  private Map             options;
29
30 //  Authentication status
31     private boolean succeeded = false;
32     private boolean commit    = false;
33
34 //  Principal
35     private User    identity  = null;
36     
37     private Logger  logger    = null;
38
39 //  ==============================================================================================================================
40 //  Constructor 
41 //  ==============================================================================================================================
42
43     public void initialize(Subject user, CallbackHandler handler, Map<String, ?> state, Map<String, ?> opts) {
44 //  --------------------------------------------------------------------------------------------------------
45       subject         = user;
46           callbackHandler = handler;
47 //        sharedState     = state;
48 //        options         = opts;
49 //    debug           = "true".equalsIgnoreCase((String)options.get("debug"));
50           logger          = Logger.getLogger(RealmLoginModule.class);
51     }
52
53 //  ==============================================================================================================================
54 //  Public services
55 //  ==============================================================================================================================
56
57         public boolean login() throws LoginException {
58 //  ----------------------
59           try {           
60 //    Ask for username password   
61                 Callback[] callbacks = new Callback[2];
62                 callbacks[0] = new NameCallback("username");
63                 callbacks[1] = new PasswordCallback("password", false);
64
65             callbackHandler.handle(callbacks);
66
67             String username = ((NameCallback)callbacks[0]).getName();
68             String password = null;
69             char[] entered  = ((PasswordCallback)callbacks[1]).getPassword();
70             if (entered != null) {
71               password = new String(entered);
72               ((PasswordCallback)callbacks[1]).clearPassword();
73             }
74             
75 //    Authentication        
76             User found = ServiceLocatorImpl.getInstance().getUserService().selectUser(username, password);
77             if (found != null) {
78               identity  = found;
79               succeeded = true;       
80               Calendar today  = java.util.Calendar.getInstance();
81               Date     datime = today.getTime();
82               logger.info("Connection of " + identity.toString() + " " + datime.toString() + ".");
83               return true;
84             } else {
85               identity  = null;
86                   succeeded = false;              
87                   found     = ServiceLocatorImpl.getInstance().getUserService().selectUser(username);
88           String             reason = "password";
89                   if (found == null) reason = "username";
90               logger.info("Connection attempt as " + username + ".");
91                   throw new FailedLoginException(reason);
92             }
93           }
94           catch (java.io.IOException ioe) {
95                 throw new LoginException(ioe.toString());
96           }
97           catch (UnsupportedCallbackException uce) {
98                 throw new LoginException("Error: " + uce.getCallback().toString() +
99                         " not available to garner authentication information" +
100                         " from the user");
101           }
102         }
103
104         public boolean commit() throws LoginException {
105 //  -----------------------
106           if (!succeeded) return false;
107           
108           if (!subject.getPrincipals().contains(identity)) subject.getPrincipals().add(identity);
109           identity = null;
110           commit   = true;
111           return true;
112         }
113
114         public boolean abort() throws LoginException {
115 //  ----------------------
116           if (!succeeded) {
117                 return false;
118           } else
119           if (succeeded && !commit) {
120                 identity  = null;
121                 succeeded = false;
122           } else {
123                 logout();
124           }
125           return true;
126         }
127
128         public boolean logout() throws LoginException {
129 //  -----------------------
130           subject.getPrincipals().remove(identity);
131           identity  = null;
132           succeeded = false;
133           commit    = false;     // To be validated
134           return true;
135         }
136 }