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;
20 public class RealmLoginModule implements LoginModule {
23 private Subject subject;
24 private CallbackHandler callbackHandler;
25 // private Map sharedState;
26 // private Map options;
28 // Authentication status
29 private boolean succeeded = false;
30 private boolean commit = false;
33 private User identity = null;
35 private Logger logger = null;
37 // ==============================================================================================================================
39 // ==============================================================================================================================
41 public void initialize(Subject user, CallbackHandler handler, Map<String, ?> state, Map<String, ?> opts) {
42 // --------------------------------------------------------------------------------------------------------
44 callbackHandler = handler;
45 // sharedState = state;
47 // debug = "true".equalsIgnoreCase((String)options.get("debug"));
48 logger = Logger.getLogger(Database.class);
51 // ==============================================================================================================================
53 // ==============================================================================================================================
55 public boolean login() throws LoginException {
56 // ----------------------
58 // Ask for username password
59 Callback[] callbacks = new Callback[2];
60 callbacks[0] = new NameCallback("username");
61 callbacks[1] = new PasswordCallback("password", false);
63 callbackHandler.handle(callbacks);
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();
74 User found = UserDirectory.selectUser(username, password);
78 Calendar today = java.util.Calendar.getInstance();
79 Date datime = today.getTime();
80 logger.info("RKV:Connection of " + identity.toString() + " " + datime.toString() + ".");
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);
92 catch (java.io.IOException ioe) {
93 throw new LoginException(ioe.toString());
95 catch (UnsupportedCallbackException uce) {
96 throw new LoginException("Error: " + uce.getCallback().toString() +
97 " not available to garner authentication information" +
102 public boolean commit() throws LoginException {
103 // -----------------------
104 if (!succeeded) return false;
106 if (!subject.getPrincipals().contains(identity)) subject.getPrincipals().add(identity);
112 public boolean abort() throws LoginException {
113 // ----------------------
117 if (succeeded && !commit) {
126 public boolean logout() throws LoginException {
127 // -----------------------
128 subject.getPrincipals().remove(identity);
131 commit = false; // To be validated