Search in sources :

Example 26 with TextOutputCallback

use of javax.security.auth.callback.TextOutputCallback in project force-oneself by Force-oneself.

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 27 with TextOutputCallback

use of javax.security.auth.callback.TextOutputCallback in project servoy-client by Servoy.

the class J2DBClient method main.

/**
 * Starting point
 */
@SuppressWarnings("nls")
public static void main(final String[] args) {
    String userTimeZone = System.getProperty("user.timezone");
    // Bug around java when downloading the first time (pack or gzip seems to alter the timezone and don't set it back)
    if (userTimeZone != null && !userTimeZone.equals(TimeZone.getDefault().getID())) {
        TimeZone timeZone = TimeZone.getTimeZone(userTimeZone);
        if (userTimeZone.equals(timeZone.getID())) {
            TimeZone.setDefault(timeZone);
        }
    }
    String remoteRunnerClassName = System.getProperty("com.servoy.remote.checker");
    if (remoteRunnerClassName != null) {
        try {
            Class<?> clazz = Class.forName(remoteRunnerClassName);
            RemoteRunnerChecker.setInstance((IRemoteRunner) clazz.newInstance());
        } catch (Throwable t) {
            System.err.println("Error setting the remote runner check for " + remoteRunnerClassName);
        }
    }
    boolean toggleTracing = false;
    StartupArguments arguments = new StartupArguments(args);
    Iterator<Entry<String, Object>> iterator = arguments.entrySet().iterator();
    while (iterator.hasNext()) {
        Entry<String, Object> arg = iterator.next();
        if (arg.getKey().startsWith("system.property.")) {
            System.setProperty(arg.getKey().substring(16), (String) arg.getValue());
        }
        if (arg.getKey().equals("tracing") && arg.getValue().equals("true")) {
            toggleTracing = true;
        }
    }
    if (toggleTracing)
        Debug.toggleTracing();
    if (Boolean.getBoolean("servoy.usejaas")) {
        final boolean[] loginShown = new boolean[1];
        System.setProperty("javax.security.auth.useSubjectCredsOnly", "true");
        try {
            Debug.log("creating context");
            LoginContext lc = new LoginContext("ServoyClient", new CallbackHandler() {

                public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
                    Debug.log("handle call back");
                    String loginName = null;
                    String passwordString = null;
                    for (Callback callback : callbacks) {
                        if (callback instanceof TextOutputCallback) {
                            final TextOutputCallback textOutputCallback = (TextOutputCallback) callback;
                            switch(textOutputCallback.getMessageType()) {
                                case TextOutputCallback.INFORMATION:
                                    Debug.log(textOutputCallback.getMessage());
                                    break;
                                case TextOutputCallback.WARNING:
                                    Debug.warn(textOutputCallback.getMessage());
                                    break;
                                case TextOutputCallback.ERROR:
                                    Debug.error(textOutputCallback.getMessage());
                                    break;
                                default:
                                    throw new IOException("Unsupported message type: " + textOutputCallback.getMessageType());
                            }
                        } else if (callback instanceof NameCallback) {
                            final NameCallback nameCallback = (NameCallback) callback;
                            if (loginName == null) {
                                LoginDialog ld = new LoginDialog((Frame) null, null, "Sign on", false, true);
                                Object[] credentials = ld.showDialog(null);
                                if (credentials != null && credentials.length == 2) {
                                    loginName = (String) credentials[0];
                                    passwordString = (String) credentials[1];
                                }
                                if (loginName == null) {
                                    loginName = "";
                                    passwordString = "";
                                }
                                loginShown[0] = true;
                            }
                            nameCallback.setName(loginName);
                        } else if (callback instanceof PasswordCallback) {
                            final PasswordCallback passwordCallback = (PasswordCallback) callback;
                            if (passwordString == null) {
                                LoginDialog ld = new LoginDialog((Frame) null, null, "Sign on", false, true);
                                Object[] credentials = ld.showDialog(null);
                                if (credentials != null && credentials.length == 2) {
                                    loginName = (String) credentials[0];
                                    passwordString = (String) credentials[1];
                                }
                                if (passwordString == null) {
                                    loginName = "";
                                    passwordString = "";
                                }
                                loginShown[0] = true;
                            }
                            passwordCallback.setPassword(passwordString.toCharArray());
                        } else {
                            throw new UnsupportedCallbackException(callback, "Unrecognized Callback");
                        }
                    }
                }
            });
            Debug.log("context created");
            boolean loggedIn = true;
            try {
                lc.login();
            } catch (LoginException e) {
                Debug.log("login failed", e);
                loggedIn = false;
            }
            if (loggedIn) {
                Subject.doAsPrivileged(lc.getSubject(), new PrivilegedExceptionAction<Void>() {

                    public Void run() throws Exception {
                        mainImpl(args);
                        return null;
                    }
                }, null);
            } else {
                mainImpl(args);
            }
        } catch (Exception e) {
            Debug.log("context creation failed", e);
            if (loginShown[0]) {
                JOptionPane.showMessageDialog(null, "Couldnt login", "Login failed", JOptionPane.ERROR_MESSAGE);
            }
            mainImpl(args);
        }
    } else {
        mainImpl(args);
    }
}
Also used : CallbackHandler(javax.security.auth.callback.CallbackHandler) Frame(java.awt.Frame) JFrame(javax.swing.JFrame) Entry(java.util.Map.Entry) LoginContext(javax.security.auth.login.LoginContext) PasswordCallback(javax.security.auth.callback.PasswordCallback) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) StartupArguments(com.servoy.j2db.scripting.StartupArguments) IOException(java.io.IOException) TextOutputCallback(javax.security.auth.callback.TextOutputCallback) IOException(java.io.IOException) ApplicationException(com.servoy.j2db.ApplicationException) RemoteException(java.rmi.RemoteException) RepositoryException(com.servoy.j2db.persistence.RepositoryException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) ServoyException(com.servoy.j2db.util.ServoyException) LoginException(javax.security.auth.login.LoginException) MalformedURLException(java.net.MalformedURLException) TimeZone(java.util.TimeZone) NameCallback(javax.security.auth.callback.NameCallback) IMessagesCallback(com.servoy.j2db.IMessagesCallback) Callback(javax.security.auth.callback.Callback) PasswordCallback(javax.security.auth.callback.PasswordCallback) TextOutputCallback(javax.security.auth.callback.TextOutputCallback) NameCallback(javax.security.auth.callback.NameCallback) LoginException(javax.security.auth.login.LoginException) UnicastRemoteObject(java.rmi.server.UnicastRemoteObject) LoginDialog(com.servoy.j2db.gui.LoginDialog)

Example 28 with TextOutputCallback

use of javax.security.auth.callback.TextOutputCallback in project pgjdbc by pgjdbc.

the class GSSCallbackHandler method handle.

@Override
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
    for (Callback callback : callbacks) {
        if (callback instanceof TextOutputCallback) {
            TextOutputCallback toc = (TextOutputCallback) callback;
            switch(toc.getMessageType()) {
                case TextOutputCallback.INFORMATION:
                    System.out.println("INFO: " + toc.getMessage());
                    break;
                case TextOutputCallback.ERROR:
                    System.out.println("ERROR: " + toc.getMessage());
                    break;
                case TextOutputCallback.WARNING:
                    System.out.println("WARNING: " + toc.getMessage());
                    break;
                default:
                    throw new IOException("Unsupported message type: " + toc.getMessageType());
            }
        } else if (callback instanceof NameCallback) {
            NameCallback nc = (NameCallback) callback;
            nc.setName(user);
        } else if (callback instanceof PasswordCallback) {
            PasswordCallback pc = (PasswordCallback) callback;
            if (password == null) {
                throw new IOException("No cached kerberos ticket found and no password supplied.");
            }
            pc.setPassword(password);
        } else {
            throw new UnsupportedCallbackException(callback, "Unrecognized Callback");
        }
    }
}
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) PasswordCallback(javax.security.auth.callback.PasswordCallback) TextOutputCallback(javax.security.auth.callback.TextOutputCallback) IOException(java.io.IOException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException)

Example 29 with TextOutputCallback

use of javax.security.auth.callback.TextOutputCallback in project structure-project by wudskq.

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 30 with TextOutputCallback

use of javax.security.auth.callback.TextOutputCallback in project jboss-remoting by jboss-remoting.

the class PasswordClientCallbackHandler method handle.

/**
 * Handle the array of given callbacks.
 *
 * @param callbacks the callbacks to handle
 * @throws UnsupportedCallbackException if a callback is unsupported
 */
public void handle(final Callback[] callbacks) throws UnsupportedCallbackException {
    MAIN: for (Callback callback : callbacks) {
        if (callback instanceof NameCallback) {
            final NameCallback nameCallback = (NameCallback) callback;
            final String defaultName = nameCallback.getDefaultName();
            log.tracef("User name requested; prompt '%s', default is '%s', ours is '%s'", nameCallback.getPrompt(), defaultName, actualUserName);
            if (actualUserName == null) {
                if (defaultName != null) {
                    nameCallback.setName(defaultName);
                }
            } else {
                nameCallback.setName(actualUserName);
            }
        } else if (callback instanceof RealmCallback) {
            final RealmCallback realmCallback = (RealmCallback) callback;
            final String defaultRealm = realmCallback.getDefaultText();
            log.tracef("Realm requested; prompt '%s', default is '%s', ours is '%s'", realmCallback.getPrompt(), defaultRealm, actualUserRealm);
            if (actualUserRealm == null) {
                if (defaultRealm != null) {
                    realmCallback.setText(defaultRealm);
                }
            } else {
                realmCallback.setText(actualUserRealm);
            }
        } else if (callback instanceof RealmChoiceCallback && actualUserRealm != null) {
            final RealmChoiceCallback realmChoiceCallback = (RealmChoiceCallback) callback;
            final String[] choices = realmChoiceCallback.getChoices();
            for (int i = 0; i < choices.length; i++) {
                if (choices[i] != null && choices[i].equals(actualUserRealm)) {
                    realmChoiceCallback.setSelectedIndex(i);
                    continue MAIN;
                }
            }
            throw new UnsupportedCallbackException(callback, "No realm choices match realm '" + actualUserRealm + "'");
        } else if (callback instanceof TextOutputCallback) {
            final TextOutputCallback textOutputCallback = (TextOutputCallback) callback;
            final String kind;
            switch(textOutputCallback.getMessageType()) {
                case TextOutputCallback.ERROR:
                    kind = "ERROR";
                    break;
                case TextOutputCallback.INFORMATION:
                    kind = "INFORMATION";
                    break;
                case TextOutputCallback.WARNING:
                    kind = "WARNING";
                    break;
                default:
                    kind = "UNKNOWN";
                    break;
            }
            log.debugf("Authentication layer produced a %s message: %s", kind, textOutputCallback.getMessage());
        } else if (callback instanceof PasswordCallback) {
            final PasswordCallback passwordCallback = (PasswordCallback) callback;
            passwordCallback.setPassword(password);
        } else {
            throw new UnsupportedCallbackException(callback);
        }
    }
}
Also used : RealmCallback(javax.security.sasl.RealmCallback) PasswordCallback(javax.security.auth.callback.PasswordCallback) NameCallback(javax.security.auth.callback.NameCallback) TextOutputCallback(javax.security.auth.callback.TextOutputCallback) RealmChoiceCallback(javax.security.sasl.RealmChoiceCallback) Callback(javax.security.auth.callback.Callback) NameCallback(javax.security.auth.callback.NameCallback) PasswordCallback(javax.security.auth.callback.PasswordCallback) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) TextOutputCallback(javax.security.auth.callback.TextOutputCallback) RealmChoiceCallback(javax.security.sasl.RealmChoiceCallback) RealmCallback(javax.security.sasl.RealmCallback)

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