Search in sources :

Example 21 with TextOutputCallback

use of javax.security.auth.callback.TextOutputCallback in project OpenAM by OpenRock.

the class LDAPCallbacks method messageCallback.

private void messageCallback(int msgType, String msg) throws LoginException {
    if (callbackHandler == null) {
        throw new LoginException(bundle.getString("NoCallbackHandler"));
    }
    try {
        Callback[] callbacks = new Callback[1];
        callbacks[0] = new TextOutputCallback(msgType, msg);
        callbackHandler.handle(callbacks);
    } catch (java.io.IOException ioe) {
        throw new LoginException(ioe.toString());
    } catch (UnsupportedCallbackException uce) {
        throw new LoginException(bundle.getString("NoCallbackHandler"));
    } catch (IllegalArgumentException ill) {
        debug.message("message type missing");
        throw new LoginException(bundle.getString("IllegalArgs"));
    }
}
Also used : PasswordCallback(javax.security.auth.callback.PasswordCallback) NameCallback(javax.security.auth.callback.NameCallback) TextOutputCallback(javax.security.auth.callback.TextOutputCallback) Callback(javax.security.auth.callback.Callback) LoginException(javax.security.auth.login.LoginException) TextOutputCallback(javax.security.auth.callback.TextOutputCallback) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException)

Example 22 with TextOutputCallback

use of javax.security.auth.callback.TextOutputCallback in project OpenAM by OpenRock.

the class LDAPCallbacks method loginCallbacks.

private void loginCallbacks() throws LoginException {
    if (callbackHandler == null) {
        throw new LoginException(bundle.getString("NoCallbackHandler"));
    }
    Callback[] callbacks = new Callback[3];
    callbacks[0] = new TextOutputCallback(TextOutputCallback.INFORMATION, "LDAP Authentication");
    callbacks[1] = new NameCallback("Enter Username :");
    callbacks[2] = new PasswordCallback("Enter Password :", false);
    try {
        callbackHandler.handle(callbacks);
        username = ((NameCallback) callbacks[1]).getName();
        passwd = charToString(((PasswordCallback) callbacks[2]).getPassword(), callbacks[2]);
    } catch (java.io.IOException ioe) {
        throw new LoginException(bundle.getString("NoCallbackHandler"));
    } catch (UnsupportedCallbackException uce) {
        throw new LoginException();
    }
}
Also used : PasswordCallback(javax.security.auth.callback.PasswordCallback) NameCallback(javax.security.auth.callback.NameCallback) TextOutputCallback(javax.security.auth.callback.TextOutputCallback) Callback(javax.security.auth.callback.Callback) NameCallback(javax.security.auth.callback.NameCallback) LoginException(javax.security.auth.login.LoginException) PasswordCallback(javax.security.auth.callback.PasswordCallback) TextOutputCallback(javax.security.auth.callback.TextOutputCallback) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException)

Example 23 with TextOutputCallback

use of javax.security.auth.callback.TextOutputCallback in project jdk8u_jdk by JetBrains.

the class DialogCallbackHandler method handle.

/**
     * Handles the specified set of callbacks.
     *
     * @param callbacks the callbacks to handle
     * @throws UnsupportedCallbackException if the callback is not an
     * instance  of NameCallback or PasswordCallback
     */
public void handle(Callback[] callbacks) throws UnsupportedCallbackException {
    /* Collect messages to display in the dialog */
    final List<Object> messages = new ArrayList<>(3);
    /* Collection actions to perform if the user clicks OK */
    final List<Action> okActions = new ArrayList<>(2);
    ConfirmationInfo confirmation = new ConfirmationInfo();
    for (int i = 0; i < callbacks.length; i++) {
        if (callbacks[i] instanceof TextOutputCallback) {
            TextOutputCallback tc = (TextOutputCallback) callbacks[i];
            switch(tc.getMessageType()) {
                case TextOutputCallback.INFORMATION:
                    confirmation.messageType = JOptionPane.INFORMATION_MESSAGE;
                    break;
                case TextOutputCallback.WARNING:
                    confirmation.messageType = JOptionPane.WARNING_MESSAGE;
                    break;
                case TextOutputCallback.ERROR:
                    confirmation.messageType = JOptionPane.ERROR_MESSAGE;
                    break;
                default:
                    throw new UnsupportedCallbackException(callbacks[i], "Unrecognized message type");
            }
            messages.add(tc.getMessage());
        } else if (callbacks[i] instanceof NameCallback) {
            final NameCallback nc = (NameCallback) callbacks[i];
            JLabel prompt = new JLabel(nc.getPrompt());
            final JTextField name = new JTextField(JTextFieldLen);
            String defaultName = nc.getDefaultName();
            if (defaultName != null) {
                name.setText(defaultName);
            }
            /*
                 * Put the prompt and name in a horizontal box,
                 * and add that to the set of messages.
                 */
            Box namePanel = Box.createHorizontalBox();
            namePanel.add(prompt);
            namePanel.add(name);
            messages.add(namePanel);
            /* Store the name back into the callback if OK */
            okActions.add(new Action() {

                public void perform() {
                    nc.setName(name.getText());
                }
            });
        } else if (callbacks[i] instanceof PasswordCallback) {
            final PasswordCallback pc = (PasswordCallback) callbacks[i];
            JLabel prompt = new JLabel(pc.getPrompt());
            final JPasswordField password = new JPasswordField(JPasswordFieldLen);
            if (!pc.isEchoOn()) {
                password.setEchoChar('*');
            }
            Box passwordPanel = Box.createHorizontalBox();
            passwordPanel.add(prompt);
            passwordPanel.add(password);
            messages.add(passwordPanel);
            okActions.add(new Action() {

                public void perform() {
                    pc.setPassword(password.getPassword());
                }
            });
        } else if (callbacks[i] instanceof ConfirmationCallback) {
            ConfirmationCallback cc = (ConfirmationCallback) callbacks[i];
            confirmation.setCallback(cc);
            if (cc.getPrompt() != null) {
                messages.add(cc.getPrompt());
            }
        } else {
            throw new UnsupportedCallbackException(callbacks[i], "Unrecognized Callback");
        }
    }
    /* Display the dialog */
    int result = JOptionPane.showOptionDialog(parentComponent, messages.toArray(), "Confirmation", /* title */
    confirmation.optionType, confirmation.messageType, null, /* icon */
    confirmation.options, /* options */
    confirmation.initialValue);
    /* Perform the OK actions */
    if (result == JOptionPane.OK_OPTION || result == JOptionPane.YES_OPTION) {
        Iterator<Action> iterator = okActions.iterator();
        while (iterator.hasNext()) {
            iterator.next().perform();
        }
    }
    confirmation.handleResult(result);
}
Also used : ConfirmationCallback(javax.security.auth.callback.ConfirmationCallback) ArrayList(java.util.ArrayList) JLabel(javax.swing.JLabel) Box(javax.swing.Box) TextOutputCallback(javax.security.auth.callback.TextOutputCallback) JTextField(javax.swing.JTextField) NameCallback(javax.security.auth.callback.NameCallback) JPasswordField(javax.swing.JPasswordField) PasswordCallback(javax.security.auth.callback.PasswordCallback) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException)

Example 24 with TextOutputCallback

use of javax.security.auth.callback.TextOutputCallback in project jdk8u_jdk by JetBrains.

the class TextCallbackHandler method handle.

/**
     * Handles the specified set of callbacks.
     *
     * @param callbacks the callbacks to handle
     * @throws IOException if an input or output error occurs.
     * @throws UnsupportedCallbackException if the callback is not an
     * instance of NameCallback or PasswordCallback
     */
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
    ConfirmationCallback confirmation = null;
    for (int i = 0; i < callbacks.length; i++) {
        if (callbacks[i] instanceof TextOutputCallback) {
            TextOutputCallback tc = (TextOutputCallback) callbacks[i];
            String text;
            switch(tc.getMessageType()) {
                case TextOutputCallback.INFORMATION:
                    text = "";
                    break;
                case TextOutputCallback.WARNING:
                    text = "Warning: ";
                    break;
                case TextOutputCallback.ERROR:
                    text = "Error: ";
                    break;
                default:
                    throw new UnsupportedCallbackException(callbacks[i], "Unrecognized message type");
            }
            String message = tc.getMessage();
            if (message != null) {
                text += message;
            }
            if (text != null) {
                System.err.println(text);
            }
        } else if (callbacks[i] instanceof NameCallback) {
            NameCallback nc = (NameCallback) callbacks[i];
            if (nc.getDefaultName() == null) {
                System.err.print(nc.getPrompt());
            } else {
                System.err.print(nc.getPrompt() + " [" + nc.getDefaultName() + "] ");
            }
            System.err.flush();
            String result = readLine();
            if (result.equals("")) {
                result = nc.getDefaultName();
            }
            nc.setName(result);
        } else if (callbacks[i] instanceof PasswordCallback) {
            PasswordCallback pc = (PasswordCallback) callbacks[i];
            System.err.print(pc.getPrompt());
            System.err.flush();
            pc.setPassword(Password.readPassword(System.in, pc.isEchoOn()));
        } else if (callbacks[i] instanceof ConfirmationCallback) {
            confirmation = (ConfirmationCallback) callbacks[i];
        } else {
            throw new UnsupportedCallbackException(callbacks[i], "Unrecognized Callback");
        }
    }
    /* Do the confirmation callback last. */
    if (confirmation != null) {
        doConfirmation(confirmation);
    }
}
Also used : ConfirmationCallback(javax.security.auth.callback.ConfirmationCallback) NameCallback(javax.security.auth.callback.NameCallback) PasswordCallback(javax.security.auth.callback.PasswordCallback) TextOutputCallback(javax.security.auth.callback.TextOutputCallback) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException)

Example 25 with TextOutputCallback

use of javax.security.auth.callback.TextOutputCallback in project polymap4-core by Polymap4.

the class DummyLoginModule method login.

public boolean login() throws LoginException {
    // check if there is a user with "login" password
    for (DummyUserPrincipal candidate : users.values()) {
        if (candidate.getPassword().equals("login")) {
            principal = candidate;
            return loggedIn = true;
        }
    }
    try {
        Callback label = new TextOutputCallback(TextOutputCallback.INFORMATION, // empty if service login
        StringUtils.defaultIfEmpty(dialogTitle, "POLYMAP3 Workbench"));
        NameCallback nameCallback = new NameCallback(StringUtils.defaultIfEmpty(i18n.get("username"), "Username"), "default");
        PasswordCallback passwordCallback = new PasswordCallback(StringUtils.defaultIfEmpty(i18n.get("password"), "Password"), false);
        callbackHandler.handle(new Callback[] { label, nameCallback, passwordCallback });
        String username = nameCallback.getName();
        String password = "";
        if (passwordCallback.getPassword() != null) {
            password = String.valueOf(passwordCallback.getPassword());
        }
        DummyUserPrincipal candidate = userForName(username);
        if (candidate.getPassword().equals(password)) {
            principal = candidate;
            loggedIn = true;
            return true;
        }
        return false;
    } catch (Exception e) {
        log.warn("", e);
        throw new LoginException(e.getLocalizedMessage());
    }
}
Also used : PasswordCallback(javax.security.auth.callback.PasswordCallback) TextOutputCallback(javax.security.auth.callback.TextOutputCallback) NameCallback(javax.security.auth.callback.NameCallback) Callback(javax.security.auth.callback.Callback) NameCallback(javax.security.auth.callback.NameCallback) PasswordCallback(javax.security.auth.callback.PasswordCallback) LoginException(javax.security.auth.login.LoginException) TextOutputCallback(javax.security.auth.callback.TextOutputCallback) LoginException(javax.security.auth.login.LoginException)

Aggregations

TextOutputCallback (javax.security.auth.callback.TextOutputCallback)31 NameCallback (javax.security.auth.callback.NameCallback)23 PasswordCallback (javax.security.auth.callback.PasswordCallback)23 UnsupportedCallbackException (javax.security.auth.callback.UnsupportedCallbackException)17 Callback (javax.security.auth.callback.Callback)12 ConfirmationCallback (javax.security.auth.callback.ConfirmationCallback)12 Test (org.testng.annotations.Test)7 TextInputCallback (javax.security.auth.callback.TextInputCallback)6 LoginException (javax.security.auth.login.LoginException)6 JsonValue (org.forgerock.json.JsonValue)6 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)5 ChoiceCallback (javax.security.auth.callback.ChoiceCallback)5 LanguageCallback (javax.security.auth.callback.LanguageCallback)5 ScriptTextOutputCallback (com.sun.identity.authentication.callbacks.ScriptTextOutputCallback)4 HiddenValueCallback (com.sun.identity.authentication.callbacks.HiddenValueCallback)3 Box (javax.swing.Box)3 JLabel (javax.swing.JLabel)3 JPasswordField (javax.swing.JPasswordField)3 JTextField (javax.swing.JTextField)3