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);
}
}
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);
}
}
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");
}
}
}
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);
}
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);
}
}
}
Aggregations