lundi 2 octobre 2017

Java callback handler for Remember Me checkbox in Shibboleth using JAAS configuration

Hi I working on application where we are using Shibboleth for SSO implementation. I am not much familiar with java and Shibboleth so reading online and the Shibboleth documentation I implemented the idp login page with java callbackhandler demo here. The problem I am having is that Shibboleth idp has a "Don't Remember Me" instead of traditional "Remember Me" checkbox and that is what I am trying to reverse. So in my callbacks I am trying to grab the values of username, password and checkbox and work with them. As per the demo, I am able to grab the username and password values using NameCallback and PasswordCallback handlers, but I am not able to get the value for the "Don't Remember Me" or donotcache flag.

I tried to get the value of donotcache flag using different callbackhandlers as specified here, but still I don't get the selected checkbox value.

Below is the code snipped of callback handler code I wrote:

public boolean login() throws LoginException
{
    Logger logger = Logger.getLogger(SimpleLogin.class.getName());
    FileHandler fh = null;

    try {
        fh = new FileHandler("C:/temp/Puneet/MyLogFile.txt");
        logger.addHandler(fh);
        SimpleFormatter formatter = new SimpleFormatter();
        fh.setFormatter(formatter);
    } catch(SecurityException e) {
        e.printStackTrace();
    } catch(IOException e) {
        e.printStackTrace();
    }

    // username and password
    String  username;
    char    password[] = null;
    int donotcache = 0;

    try {
        // prompt for a username and password
        if (callbackHandler == null) {
            throw new LoginException("Error: no CallbackHandler available to garner authentication information from the user");
        }

        Callback[] callbacks = new Callback[3];
        callbacks[0] = new NameCallback("Username: ");
        callbacks[1] = new PasswordCallback("Password: ", false);

        //Remember login options
        /*String prompt = "Remember Login";
        String[] choices = {"true", "false"};
        int defaultChoice = 1, selectedChoice = 0;
        boolean multipleSelectionsAllowed = false;

        callbacks[2] = new ChoiceCallback(prompt, choices, defaultChoice, multipleSelectionsAllowed);*/

        String prompt = "Don't Remember Login";
        String defaultText = "0";
        callbacks[2] = new TextInputCallback(prompt, defaultText);

        try {
            callbackHandler.handle(callbacks);

            // Get username...
            username = ((NameCallback) callbacks[0]).getName();
            logger.info("username : " + username);  **// 1**

            // ...password...
            password = ((PasswordCallback) callbacks[1]).getPassword();
            ((PasswordCallback)callbacks[1]).clearPassword();
            logger.info("password : " + password);  **// 2**

            // ...remember login value...
            String enteredText = (((TextInputCallback) callbacks[2]).getText());
            logger.info("enteredText : " + enteredText);  **// 3**

            if (enteredText != "" && enteredText == "1") {
                donotcache = 1;
            }
            logger.info("donotcache : " + donotcache);  **// 4**
        } catch (java.io.IOException ioe) {
            throw new LoginException(ioe.toString());
        } catch (UnsupportedCallbackException uce) {
            throw new LoginException("Error: " + uce.getCallback().toString());
        }

        // Attempt to logon using the supplied credentials
        pending = null;
        pending = validateUser(username, password, donotcache, logger);     // may throw

        createSession(username, donotcache, logger);

        System.out.println("Finished Login");
        logger.info("Finished Login");
    } finally {
        Utils.smudge(password);
        if (fh != null) {
            fh.close();
        }
    }
    return true;
}

When I look at the logger, I am getting proper values of username and password at statement //1 & //2, but at statement //3, I get null as entered. I tried using ChoiceCallback but same result. I am not very familiar with java and thus not sure if there is a different way of handling callbacks for checkbox.

Any help would be appreciated.




Aucun commentaire:

Enregistrer un commentaire