Salome HOME
fcc0b288d3e44d9aa95697c13ef736d0672a7ea2
[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
19
20 public class RealmLoginModule implements LoginModule {
21         
22 //  Initial state
23     private Subject         subject;
24     private CallbackHandler callbackHandler;
25 //  private Map             sharedState;
26 //  private Map             options;
27
28 //  Authentication status
29     private boolean succeeded = false;
30     private boolean commit    = false;
31
32 //  Principal
33     private User    identity  = null;
34     
35     private Logger  logger    = null;
36
37 //  ==============================================================================================================================
38 //  Constructor 
39 //  ==============================================================================================================================
40
41     public void initialize(Subject user, CallbackHandler handler, Map<String, ?> state, Map<String, ?> opts) {
42 //  --------------------------------------------------------------------------------------------------------
43       subject         = user;
44           callbackHandler = handler;
45 //        sharedState     = state;
46 //        options         = opts;
47 //    debug           = "true".equalsIgnoreCase((String)options.get("debug"));
48           logger          = Logger.getLogger(Database.class);
49     }
50
51 //  ==============================================================================================================================
52 //  Public services
53 //  ==============================================================================================================================
54
55         public boolean login() throws LoginException {
56 //  ----------------------
57           try {           
58 //    Ask for username password   
59                 Callback[] callbacks = new Callback[2];
60                 callbacks[0] = new NameCallback("username");
61                 callbacks[1] = new PasswordCallback("password", false);
62
63             callbackHandler.handle(callbacks);
64
65             String username = ((NameCallback)callbacks[0]).getName();
66             String password = null;
67             char[] entered  = ((PasswordCallback)callbacks[1]).getPassword();
68             if (entered != null) {
69               password = new String(entered);
70               ((PasswordCallback)callbacks[1]).clearPassword();
71             }
72             
73 //    Authentication        
74             User found = UserDirectory.selectUser(username, password);
75             if (found != null) {
76               identity  = found;
77               succeeded = true;       
78               Calendar today  = java.util.Calendar.getInstance();
79               Date     datime = today.getTime();
80               logger.info("RKV:Connection of " + identity.toString() + " " + datime.toString() + ".");
81               return true;
82             } else {
83               identity  = null;
84                   succeeded = false;              
85                   found     = UserDirectory.selectUser(username);
86           String             reason = "password";
87                   if (found == null) reason = "username";
88               logger.info("Connection attempt as " + username + ".");
89                   throw new FailedLoginException(reason);
90             }
91           }
92           catch (java.io.IOException ioe) {
93                 throw new LoginException(ioe.toString());
94           }
95           catch (UnsupportedCallbackException uce) {
96                 throw new LoginException("Error: " + uce.getCallback().toString() +
97                         " not available to garner authentication information" +
98                         " from the user");
99           }
100         }
101
102         public boolean commit() throws LoginException {
103 //  -----------------------
104           if (!succeeded) return false;
105           
106           if (!subject.getPrincipals().contains(identity)) subject.getPrincipals().add(identity);
107           identity = null;
108           commit   = true;
109           return true;
110         }
111
112         public boolean abort() throws LoginException {
113 //  ----------------------
114           if (!succeeded) {
115                 return false;
116           } else
117           if (succeeded && !commit) {
118                 identity  = null;
119                 succeeded = false;
120           } else {
121                 logout();
122           }
123           return true;
124         }
125
126         public boolean logout() throws LoginException {
127 //  -----------------------
128           subject.getPrincipals().remove(identity);
129           identity  = null;
130           succeeded = false;
131           commit    = false;     // To be validated
132           return true;
133         }
134 }