]> SALOME platform Git repositories - tools/siman.git/blobdiff - Workspace/Siman-Common/src/org/splat/kernel/RealmLoginModule.java
Salome HOME
Refactoring: kernel and som are moved to Siman-Common.
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / kernel / RealmLoginModule.java
diff --git a/Workspace/Siman-Common/src/org/splat/kernel/RealmLoginModule.java b/Workspace/Siman-Common/src/org/splat/kernel/RealmLoginModule.java
new file mode 100644 (file)
index 0000000..fcc0b28
--- /dev/null
@@ -0,0 +1,134 @@
+package org.splat.kernel;
+/**
+ * 
+ * @author    Daniel Brunier-Coulin
+ * @copyright OPEN CASCADE 2012
+ */
+
+import java.util.Calendar;
+import java.util.Date;
+import java.util.Map;
+
+import javax.security.auth.*;
+import javax.security.auth.callback.*;
+import javax.security.auth.login.*;
+import javax.security.auth.spi.*;
+
+import org.apache.log4j.Logger;
+
+
+public class RealmLoginModule implements LoginModule {
+       
+//  Initial state
+    private Subject         subject;
+    private CallbackHandler callbackHandler;
+//  private Map             sharedState;
+//  private Map             options;
+
+//  Authentication status
+    private boolean succeeded = false;
+    private boolean commit    = false;
+
+//  Principal
+    private User    identity  = null;
+    
+    private Logger  logger    = null;
+
+//  ==============================================================================================================================
+//  Constructor        
+//  ==============================================================================================================================
+
+    public void initialize(Subject user, CallbackHandler handler, Map<String, ?> state, Map<String, ?> opts) {
+//  --------------------------------------------------------------------------------------------------------
+      subject         = user;
+         callbackHandler = handler;
+//       sharedState     = state;
+//       options         = opts;
+//    debug           = "true".equalsIgnoreCase((String)options.get("debug"));
+         logger          = Logger.getLogger(Database.class);
+    }
+
+//  ==============================================================================================================================
+//  Public services
+//  ==============================================================================================================================
+
+       public boolean login() throws LoginException {
+//  ----------------------
+         try {           
+//    Ask for username password          
+               Callback[] callbacks = new Callback[2];
+               callbacks[0] = new NameCallback("username");
+               callbacks[1] = new PasswordCallback("password", false);
+
+           callbackHandler.handle(callbacks);
+
+           String username = ((NameCallback)callbacks[0]).getName();
+           String password = null;
+           char[] entered  = ((PasswordCallback)callbacks[1]).getPassword();
+           if (entered != null) {
+             password = new String(entered);
+             ((PasswordCallback)callbacks[1]).clearPassword();
+           }
+           
+//    Authentication       
+           User found = UserDirectory.selectUser(username, password);
+           if (found != null) {
+             identity  = found;
+             succeeded = true;       
+             Calendar today  = java.util.Calendar.getInstance();
+             Date     datime = today.getTime();
+             logger.info("RKV:Connection of " + identity.toString() + " " + datime.toString() + ".");
+             return true;
+           } else {
+             identity  = null;
+                 succeeded = false;              
+                 found     = UserDirectory.selectUser(username);
+          String             reason = "password";
+                 if (found == null) reason = "username";
+             logger.info("Connection attempt as " + username + ".");
+                 throw new FailedLoginException(reason);
+           }
+         }
+         catch (java.io.IOException ioe) {
+               throw new LoginException(ioe.toString());
+         }
+         catch (UnsupportedCallbackException uce) {
+               throw new LoginException("Error: " + uce.getCallback().toString() +
+                       " not available to garner authentication information" +
+                       " from the user");
+         }
+       }
+
+       public boolean commit() throws LoginException {
+//  -----------------------
+         if (!succeeded) return false;
+         
+         if (!subject.getPrincipals().contains(identity)) subject.getPrincipals().add(identity);
+         identity = null;
+         commit   = true;
+         return true;
+       }
+
+       public boolean abort() throws LoginException {
+//  ----------------------
+         if (!succeeded) {
+               return false;
+         } else
+         if (succeeded && !commit) {
+               identity  = null;
+               succeeded = false;
+         } else {
+               logout();
+         }
+         return true;
+       }
+
+       public boolean logout() throws LoginException {
+//  -----------------------
+         subject.getPrincipals().remove(identity);
+         identity  = null;
+         succeeded = false;
+         commit    = false;     // To be validated
+         return true;
+       }
+}
\ No newline at end of file