1 package org.splat.kernel;
4 * @author Daniel Brunier-Coulin
5 * @copyright OPEN CASCADE 2012
8 import java.util.Calendar;
12 import javax.security.auth.*;
13 import javax.security.auth.callback.*;
14 import javax.security.auth.login.*;
15 import javax.security.auth.spi.*;
17 import org.apache.log4j.Logger;
18 import org.splat.dal.bo.kernel.User;
19 import org.splat.dal.dao.kernel.Database;
22 public class RealmLoginModule implements LoginModule {
25 private Subject subject;
26 private CallbackHandler callbackHandler;
27 // private Map sharedState;
28 // private Map options;
30 // Authentication status
31 private boolean succeeded = false;
32 private boolean commit = false;
35 private User identity = null;
37 private Logger logger = null;
39 // ==============================================================================================================================
41 // ==============================================================================================================================
43 public void initialize(Subject user, CallbackHandler handler, Map<String, ?> state, Map<String, ?> opts) {
44 // --------------------------------------------------------------------------------------------------------
46 callbackHandler = handler;
47 // sharedState = state;
49 // debug = "true".equalsIgnoreCase((String)options.get("debug"));
50 logger = Logger.getLogger(Database.class);
53 // ==============================================================================================================================
55 // ==============================================================================================================================
57 public boolean login() throws LoginException {
58 // ----------------------
60 // Ask for username password
61 Callback[] callbacks = new Callback[2];
62 callbacks[0] = new NameCallback("username");
63 callbacks[1] = new PasswordCallback("password", false);
65 callbackHandler.handle(callbacks);
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();
76 User found = UserDirectory.selectUser(username, password);
80 Calendar today = java.util.Calendar.getInstance();
81 Date datime = today.getTime();
82 logger.info("RKV:Connection of " + identity.toString() + " " + datime.toString() + ".");
87 found = UserDirectory.selectUser(username);
88 String reason = "password";
89 if (found == null) reason = "username";
90 logger.info("Connection attempt as " + username + ".");
91 throw new FailedLoginException(reason);
94 catch (java.io.IOException ioe) {
95 throw new LoginException(ioe.toString());
97 catch (UnsupportedCallbackException uce) {
98 throw new LoginException("Error: " + uce.getCallback().toString() +
99 " not available to garner authentication information" +
104 public boolean commit() throws LoginException {
105 // -----------------------
106 if (!succeeded) return false;
108 if (!subject.getPrincipals().contains(identity)) subject.getPrincipals().add(identity);
114 public boolean abort() throws LoginException {
115 // ----------------------
119 if (succeeded && !commit) {
128 public boolean logout() throws LoginException {
129 // -----------------------
130 subject.getPrincipals().remove(identity);
133 commit = false; // To be validated