Java Code Examples for javax.security.sasl.RealmCallback#setText()

The following examples show how to use javax.security.sasl.RealmCallback#setText() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HostControllerConnection.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
    for (Callback current : callbacks) {
        if (current instanceof RealmCallback) {
            RealmCallback rcb = (RealmCallback) current;
            String defaultText = rcb.getDefaultText();
            rcb.setText(defaultText); // For now just use the realm suggested.
        } else if (current instanceof RealmChoiceCallback) {
            throw new UnsupportedCallbackException(current, "Realm choice not currently supported.");
        } else if (current instanceof NameCallback) {
            NameCallback ncb = (NameCallback) current;
            ncb.setName(userName);
        } else if (current instanceof PasswordCallback) {
            PasswordCallback pcb = (PasswordCallback) current;
            pcb.setPassword(authKey.toCharArray());
        } else {
            throw new UnsupportedCallbackException(current);
        }
    }
}
 
Example 2
Source File: Authentication.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
    for (Callback current : callbacks) {
        if (current instanceof NameCallback) {
            NameCallback ncb = (NameCallback) current;
            ncb.setName(username);
        } else if (current instanceof PasswordCallback) {
            PasswordCallback pcb = (PasswordCallback) current;
            pcb.setPassword(password.toCharArray());
        } else if (current instanceof RealmCallback) {
            RealmCallback rcb = (RealmCallback) current;
            rcb.setText(rcb.getDefaultText());
        } else {
            throw new UnsupportedCallbackException(current);
        }
    }
}
 
Example 3
Source File: Authentication.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
    for (Callback current : callbacks) {
        if (current instanceof NameCallback) {
            NameCallback ncb = (NameCallback) current;
            ncb.setName(username);
        } else if (current instanceof PasswordCallback) {
            PasswordCallback pcb = (PasswordCallback) current;
            pcb.setPassword(password.toCharArray());
        } else if (current instanceof RealmCallback) {
            RealmCallback rcb = (RealmCallback) current;
            rcb.setText(rcb.getDefaultText());
        } else {
            throw new UnsupportedCallbackException(current);
        }
    }
}
 
Example 4
Source File: ServerCallbackHandler.java    From tutorials with MIT License 6 votes vote down vote up
@Override
public void handle(Callback[] cbs) throws IOException, UnsupportedCallbackException {
    for (Callback cb : cbs) {
        if (cb instanceof AuthorizeCallback) {
            AuthorizeCallback ac = (AuthorizeCallback) cb;
            ac.setAuthorized(true);
        } else if (cb instanceof NameCallback) {
            NameCallback nc = (NameCallback) cb;
            nc.setName("username");

        } else if (cb instanceof PasswordCallback) {
            PasswordCallback pc = (PasswordCallback) cb;
            pc.setPassword("password".toCharArray());
        } else if (cb instanceof RealmCallback) {
            RealmCallback rc = (RealmCallback) cb;
            rc.setText("myServer");
        }
    }
}
 
Example 5
Source File: Authentication.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private CallbackHandler createCallBackHandler(){
    return new CallbackHandler() {

        @Override
        public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
            for (Callback current : callbacks) {
                if (current instanceof NameCallback) {
                    NameCallback nameCallback = (NameCallback) current;
                    nameCallback.setName(username);
                } else if (current instanceof PasswordCallback) {
                    PasswordCallback pwdCallback = (PasswordCallback) current;
                    pwdCallback.setPassword(password);
                } else if (current instanceof RealmCallback) {
                    RealmCallback realmCallback = (RealmCallback) current;
                    realmCallback.setText(realmCallback.getDefaultText());
                } else {
                    throw new UnsupportedCallbackException(current);
                }
            }
        }
    };
}
 
Example 6
Source File: RbacAdminCallbackHandler.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
    for (Callback current : callbacks) {
        if (current instanceof NameCallback) {
            NameCallback ncb = (NameCallback) current;
            ncb.setName(userName);
            System.out.println("set user " + userName);
        } else if (current instanceof PasswordCallback) {
            PasswordCallback pcb = (PasswordCallback) current;
            pcb.setPassword(password.toCharArray());
            System.out.println("set password " + password);
        } else if (current instanceof RealmCallback) {
            RealmCallback rcb = (RealmCallback) current;
            rcb.setText(rcb.getDefaultText());
        } else {
            throw new UnsupportedCallbackException(current);
        }
    }
}
 
Example 7
Source File: Login.java    From Krackle with Apache License 2.0 5 votes vote down vote up
@Override
public void handle(Callback[] callbacks) throws
	 UnsupportedCallbackException {
	for (Callback callback : callbacks) {
		if (callback instanceof NameCallback) {
			NameCallback nc = (NameCallback) callback;
			nc.setName(nc.getDefaultName());
		} else {
			if (callback instanceof PasswordCallback) {
				LOG.warn("Could not login: the client is being asked for a password");
			} else {
				if (callback instanceof RealmCallback) {
					RealmCallback rc = (RealmCallback) callback;
					rc.setText(rc.getDefaultText());
				} else {
					if (callback instanceof AuthorizeCallback) {
						AuthorizeCallback ac = (AuthorizeCallback) callback;
						String authid = ac.getAuthenticationID();
						String authzid = ac.getAuthorizationID();
						if (authid.equals(authzid)) {
							ac.setAuthorized(true);
						} else {
							ac.setAuthorized(false);
						}
						if (ac.isAuthorized()) {
							ac.setAuthorizedID(authzid);
						}
					} else {
						throw new UnsupportedCallbackException(callback, "Unrecognized SASL ClientCallback");
					}
				}
			}
		}
	}
}
 
Example 8
Source File: SaslNettyServer.java    From blazingcache with Apache License 2.0 5 votes vote down vote up
@Override
public void handle(Callback[] callbacks) throws
    UnsupportedCallbackException {
    for (Callback callback : callbacks) {
        if (callback instanceof NameCallback) {
            NameCallback nc = (NameCallback) callback;
            nc.setName(nc.getDefaultName());
        } else {
            if (callback instanceof PasswordCallback) {
                PasswordCallback pc = (PasswordCallback) callback;
                if (password != null) {
                    pc.setPassword(this.password.toCharArray());
                }
            } else {
                if (callback instanceof RealmCallback) {
                    RealmCallback rc = (RealmCallback) callback;
                    rc.setText(rc.getDefaultText());
                } else {
                    if (callback instanceof AuthorizeCallback) {
                        AuthorizeCallback ac = (AuthorizeCallback) callback;
                        String authid = ac.getAuthenticationID();
                        String authzid = ac.getAuthorizationID();
                        if (authid.equals(authzid)) {
                            ac.setAuthorized(true);
                        } else {
                            ac.setAuthorized(false);
                        }
                        if (ac.isAuthorized()) {
                            ac.setAuthorizedID(authzid);
                        }
                    } else {
                        throw new UnsupportedCallbackException(callback, "Unrecognized SASL ClientCallback");
                    }
                }
            }
        }
    }
}
 
Example 9
Source File: SaslNettyClient.java    From herddb with Apache License 2.0 5 votes vote down vote up
@Override
public void handle(Callback[] callbacks) throws
        UnsupportedCallbackException {
    for (Callback callback : callbacks) {
        if (callback instanceof NameCallback) {
            NameCallback nc = (NameCallback) callback;
            nc.setName(nc.getDefaultName());
        } else {
            if (callback instanceof PasswordCallback) {
                PasswordCallback pc = (PasswordCallback) callback;
                if (password != null) {
                    pc.setPassword(this.password.toCharArray());
                }
            } else {
                if (callback instanceof RealmCallback) {
                    RealmCallback rc = (RealmCallback) callback;
                    rc.setText(rc.getDefaultText());
                } else {
                    if (callback instanceof AuthorizeCallback) {
                        AuthorizeCallback ac = (AuthorizeCallback) callback;
                        String authid = ac.getAuthenticationID();
                        String authzid = ac.getAuthorizationID();
                        if (authid.equals(authzid)) {
                            ac.setAuthorized(true);
                        } else {
                            ac.setAuthorized(false);
                        }
                        if (ac.isAuthorized()) {
                            ac.setAuthorizedID(authzid);
                        }
                    } else {
                        throw new UnsupportedCallbackException(callback, "Unrecognized SASL ClientCallback");
                    }
                }
            }
        }
    }
}
 
Example 10
Source File: ModelControllerClientFactory.java    From hawkular-agent with Apache License 2.0 5 votes vote down vote up
protected ModelControllerClient createClient(final MonitoredEndpoint endpoint) {
    final ConnectionData cnData = endpoint.getConnectionData();
    final CallbackHandler callbackHandler = new CallbackHandler() {
        public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
            for (Callback current : callbacks) {
                if (current instanceof NameCallback) {
                    NameCallback ncb = (NameCallback) current;
                    ncb.setName(cnData.getUsername());
                } else if (current instanceof PasswordCallback) {
                    PasswordCallback pcb = (PasswordCallback) current;
                    pcb.setPassword(cnData.getPassword().toCharArray());
                } else if (current instanceof RealmCallback) {
                    RealmCallback rcb = (RealmCallback) current;
                    rcb.setText(rcb.getDefaultText());
                } else {
                    throw new UnsupportedCallbackException(current);
                }
            }
        }
    };
    final URI uri = cnData.getUri();
    try {
        ModelControllerClientConfiguration config = new ModelControllerClientConfiguration.Builder()
                .setProtocol(uri.getScheme())
                .setHostName(uri.getHost())
                .setPort(uri.getPort())
                .setSslContext(endpoint.getSSLContext())
                .setHandler(callbackHandler)
                .build();

        return ModelControllerClient.Factory.create(config);
    } catch (Exception e) {
        throw new RuntimeException("Failed to create management client", e);
    }
}
 
Example 11
Source File: SASLMechanism.java    From saros with GNU General Public License v2.0 5 votes vote down vote up
/** */
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
  for (int i = 0; i < callbacks.length; i++) {
    if (callbacks[i] instanceof NameCallback) {
      NameCallback ncb = (NameCallback) callbacks[i];
      ncb.setName(authenticationId);
    } else if (callbacks[i] instanceof PasswordCallback) {
      PasswordCallback pcb = (PasswordCallback) callbacks[i];
      pcb.setPassword(password.toCharArray());
    } else if (callbacks[i] instanceof RealmCallback) {
      RealmCallback rcb = (RealmCallback) callbacks[i];
      // Retrieve the REALM from the challenge response that the
      // server returned when the client initiated the authentication
      // exchange. If this value is not null or empty, *this value*
      // has to be sent back to the server in the client's response
      // to the server's challenge
      String text = rcb.getDefaultText();
      // The SASL client (sc) created in smack uses rcb.getText when
      // creating the negotiatedRealm to send it back to the server
      // Make sure that this value matches the server's realm
      rcb.setText(text);
    } else if (callbacks[i] instanceof RealmChoiceCallback) {
      // unused
      // RealmChoiceCallback rccb = (RealmChoiceCallback)callbacks[i];
    } else {
      throw new UnsupportedCallbackException(callbacks[i]);
    }
  }
}
 
Example 12
Source File: ServerCallbackHandler.java    From jstorm with Apache License 2.0 4 votes vote down vote up
private void handleRealmCallback(RealmCallback rc) {
    LOG.debug("handleRealmCallback: " + rc.getDefaultText());
    rc.setText(rc.getDefaultText());
}
 
Example 13
Source File: DefaultCallbackHandler.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public void handle(Callback[] callbacks)
    throws IOException, UnsupportedCallbackException {
        for (int i = 0; i < callbacks.length; i++) {
            if (callbacks[i] instanceof NameCallback) {
                ((NameCallback)callbacks[i]).setName(authenticationID);

            } else if (callbacks[i] instanceof PasswordCallback) {
                ((PasswordCallback)callbacks[i]).setPassword(passwd);

            } else if (callbacks[i] instanceof RealmChoiceCallback) {
                /* Deals with a choice of realms */
                String[] choices =
                    ((RealmChoiceCallback)callbacks[i]).getChoices();
                int selected = 0;

                if (authRealm != null && authRealm.length() > 0) {
                    selected = -1; // no realm chosen
                    for (int j = 0; j < choices.length; j++) {
                        if (choices[j].equals(authRealm)) {
                            selected = j;
                        }
                    }
                    if (selected == -1) {
                        StringBuffer allChoices = new StringBuffer();
                        for (int j = 0; j <  choices.length; j++) {
                            allChoices.append(choices[j] + ",");
                        }
                        throw new IOException("Cannot match " +
                            "'java.naming.security.sasl.realm' property value, '" +
                            authRealm + "' with choices " + allChoices +
                            "in RealmChoiceCallback");
                    }
                }

                ((RealmChoiceCallback)callbacks[i]).setSelectedIndex(selected);

            } else if (callbacks[i] instanceof RealmCallback) {
                /* 1 or 0 realms specified in challenge */
                RealmCallback rcb = (RealmCallback) callbacks[i];
                if (authRealm != null) {
                    rcb.setText(authRealm);  // Use what user supplied
                } else {
                    String defaultRealm = rcb.getDefaultText();
                    if (defaultRealm != null) {
                        rcb.setText(defaultRealm); // Use what server supplied
                    } else {
                        rcb.setText("");  // Specify no realm
                    }
                }
            } else {
                throw new UnsupportedCallbackException(callbacks[i]);
            }
        }
}
 
Example 14
Source File: DefaultCallbackHandler.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
public void handle(Callback[] callbacks)
    throws IOException, UnsupportedCallbackException {
        for (int i = 0; i < callbacks.length; i++) {
            if (callbacks[i] instanceof NameCallback) {
                ((NameCallback)callbacks[i]).setName(authenticationID);

            } else if (callbacks[i] instanceof PasswordCallback) {
                ((PasswordCallback)callbacks[i]).setPassword(passwd);

            } else if (callbacks[i] instanceof RealmChoiceCallback) {
                /* Deals with a choice of realms */
                String[] choices =
                    ((RealmChoiceCallback)callbacks[i]).getChoices();
                int selected = 0;

                if (authRealm != null && authRealm.length() > 0) {
                    selected = -1; // no realm chosen
                    for (int j = 0; j < choices.length; j++) {
                        if (choices[j].equals(authRealm)) {
                            selected = j;
                        }
                    }
                    if (selected == -1) {
                        StringBuffer allChoices = new StringBuffer();
                        for (int j = 0; j <  choices.length; j++) {
                            allChoices.append(choices[j] + ",");
                        }
                        throw new IOException("Cannot match " +
                            "'java.naming.security.sasl.realm' property value, '" +
                            authRealm + "' with choices " + allChoices +
                            "in RealmChoiceCallback");
                    }
                }

                ((RealmChoiceCallback)callbacks[i]).setSelectedIndex(selected);

            } else if (callbacks[i] instanceof RealmCallback) {
                /* 1 or 0 realms specified in challenge */
                RealmCallback rcb = (RealmCallback) callbacks[i];
                if (authRealm != null) {
                    rcb.setText(authRealm);  // Use what user supplied
                } else {
                    String defaultRealm = rcb.getDefaultText();
                    if (defaultRealm != null) {
                        rcb.setText(defaultRealm); // Use what server supplied
                    } else {
                        rcb.setText("");  // Specify no realm
                    }
                }
            } else {
                throw new UnsupportedCallbackException(callbacks[i]);
            }
        }
}
 
Example 15
Source File: DefaultCallbackHandler.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
public void handle(Callback[] callbacks)
    throws IOException, UnsupportedCallbackException {
        for (int i = 0; i < callbacks.length; i++) {
            if (callbacks[i] instanceof NameCallback) {
                ((NameCallback)callbacks[i]).setName(authenticationID);

            } else if (callbacks[i] instanceof PasswordCallback) {
                ((PasswordCallback)callbacks[i]).setPassword(passwd);

            } else if (callbacks[i] instanceof RealmChoiceCallback) {
                /* Deals with a choice of realms */
                String[] choices =
                    ((RealmChoiceCallback)callbacks[i]).getChoices();
                int selected = 0;

                if (authRealm != null && authRealm.length() > 0) {
                    selected = -1; // no realm chosen
                    for (int j = 0; j < choices.length; j++) {
                        if (choices[j].equals(authRealm)) {
                            selected = j;
                        }
                    }
                    if (selected == -1) {
                        StringBuffer allChoices = new StringBuffer();
                        for (int j = 0; j <  choices.length; j++) {
                            allChoices.append(choices[j] + ",");
                        }
                        throw new IOException("Cannot match " +
                            "'java.naming.security.sasl.realm' property value, '" +
                            authRealm + "' with choices " + allChoices +
                            "in RealmChoiceCallback");
                    }
                }

                ((RealmChoiceCallback)callbacks[i]).setSelectedIndex(selected);

            } else if (callbacks[i] instanceof RealmCallback) {
                /* 1 or 0 realms specified in challenge */
                RealmCallback rcb = (RealmCallback) callbacks[i];
                if (authRealm != null) {
                    rcb.setText(authRealm);  // Use what user supplied
                } else {
                    String defaultRealm = rcb.getDefaultText();
                    if (defaultRealm != null) {
                        rcb.setText(defaultRealm); // Use what server supplied
                    } else {
                        rcb.setText("");  // Specify no realm
                    }
                }
            } else {
                throw new UnsupportedCallbackException(callbacks[i]);
            }
        }
}
 
Example 16
Source File: DefaultCallbackHandler.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
public void handle(Callback[] callbacks)
    throws IOException, UnsupportedCallbackException {
        for (int i = 0; i < callbacks.length; i++) {
            if (callbacks[i] instanceof NameCallback) {
                ((NameCallback)callbacks[i]).setName(authenticationID);

            } else if (callbacks[i] instanceof PasswordCallback) {
                ((PasswordCallback)callbacks[i]).setPassword(passwd);

            } else if (callbacks[i] instanceof RealmChoiceCallback) {
                /* Deals with a choice of realms */
                String[] choices =
                    ((RealmChoiceCallback)callbacks[i]).getChoices();
                int selected = 0;

                if (authRealm != null && authRealm.length() > 0) {
                    selected = -1; // no realm chosen
                    for (int j = 0; j < choices.length; j++) {
                        if (choices[j].equals(authRealm)) {
                            selected = j;
                        }
                    }
                    if (selected == -1) {
                        StringBuffer allChoices = new StringBuffer();
                        for (int j = 0; j <  choices.length; j++) {
                            allChoices.append(choices[j] + ",");
                        }
                        throw new IOException("Cannot match " +
                            "'java.naming.security.sasl.realm' property value, '" +
                            authRealm + "' with choices " + allChoices +
                            "in RealmChoiceCallback");
                    }
                }

                ((RealmChoiceCallback)callbacks[i]).setSelectedIndex(selected);

            } else if (callbacks[i] instanceof RealmCallback) {
                /* 1 or 0 realms specified in challenge */
                RealmCallback rcb = (RealmCallback) callbacks[i];
                if (authRealm != null) {
                    rcb.setText(authRealm);  // Use what user supplied
                } else {
                    String defaultRealm = rcb.getDefaultText();
                    if (defaultRealm != null) {
                        rcb.setText(defaultRealm); // Use what server supplied
                    } else {
                        rcb.setText("");  // Specify no realm
                    }
                }
            } else {
                throw new UnsupportedCallbackException(callbacks[i]);
            }
        }
}
 
Example 17
Source File: DefaultCallbackHandler.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
public void handle(Callback[] callbacks)
    throws IOException, UnsupportedCallbackException {
        for (int i = 0; i < callbacks.length; i++) {
            if (callbacks[i] instanceof NameCallback) {
                ((NameCallback)callbacks[i]).setName(authenticationID);

            } else if (callbacks[i] instanceof PasswordCallback) {
                ((PasswordCallback)callbacks[i]).setPassword(passwd);

            } else if (callbacks[i] instanceof RealmChoiceCallback) {
                /* Deals with a choice of realms */
                String[] choices =
                    ((RealmChoiceCallback)callbacks[i]).getChoices();
                int selected = 0;

                if (authRealm != null && authRealm.length() > 0) {
                    selected = -1; // no realm chosen
                    for (int j = 0; j < choices.length; j++) {
                        if (choices[j].equals(authRealm)) {
                            selected = j;
                        }
                    }
                    if (selected == -1) {
                        StringBuffer allChoices = new StringBuffer();
                        for (int j = 0; j <  choices.length; j++) {
                            allChoices.append(choices[j] + ",");
                        }
                        throw new IOException("Cannot match " +
                            "'java.naming.security.sasl.realm' property value, '" +
                            authRealm + "' with choices " + allChoices +
                            "in RealmChoiceCallback");
                    }
                }

                ((RealmChoiceCallback)callbacks[i]).setSelectedIndex(selected);

            } else if (callbacks[i] instanceof RealmCallback) {
                /* 1 or 0 realms specified in challenge */
                RealmCallback rcb = (RealmCallback) callbacks[i];
                if (authRealm != null) {
                    rcb.setText(authRealm);  // Use what user supplied
                } else {
                    String defaultRealm = rcb.getDefaultText();
                    if (defaultRealm != null) {
                        rcb.setText(defaultRealm); // Use what server supplied
                    } else {
                        rcb.setText("");  // Specify no realm
                    }
                }
            } else {
                throw new UnsupportedCallbackException(callbacks[i]);
            }
        }
}
 
Example 18
Source File: DefaultCallbackHandler.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
public void handle(Callback[] callbacks)
    throws IOException, UnsupportedCallbackException {
        for (int i = 0; i < callbacks.length; i++) {
            if (callbacks[i] instanceof NameCallback) {
                ((NameCallback)callbacks[i]).setName(authenticationID);

            } else if (callbacks[i] instanceof PasswordCallback) {
                ((PasswordCallback)callbacks[i]).setPassword(passwd);

            } else if (callbacks[i] instanceof RealmChoiceCallback) {
                /* Deals with a choice of realms */
                String[] choices =
                    ((RealmChoiceCallback)callbacks[i]).getChoices();
                int selected = 0;

                if (authRealm != null && authRealm.length() > 0) {
                    selected = -1; // no realm chosen
                    for (int j = 0; j < choices.length; j++) {
                        if (choices[j].equals(authRealm)) {
                            selected = j;
                        }
                    }
                    if (selected == -1) {
                        StringBuffer allChoices = new StringBuffer();
                        for (int j = 0; j <  choices.length; j++) {
                            allChoices.append(choices[j] + ",");
                        }
                        throw new IOException("Cannot match " +
                            "'java.naming.security.sasl.realm' property value, '" +
                            authRealm + "' with choices " + allChoices +
                            "in RealmChoiceCallback");
                    }
                }

                ((RealmChoiceCallback)callbacks[i]).setSelectedIndex(selected);

            } else if (callbacks[i] instanceof RealmCallback) {
                /* 1 or 0 realms specified in challenge */
                RealmCallback rcb = (RealmCallback) callbacks[i];
                if (authRealm != null) {
                    rcb.setText(authRealm);  // Use what user supplied
                } else {
                    String defaultRealm = rcb.getDefaultText();
                    if (defaultRealm != null) {
                        rcb.setText(defaultRealm); // Use what server supplied
                    } else {
                        rcb.setText("");  // Specify no realm
                    }
                }
            } else {
                throw new UnsupportedCallbackException(callbacks[i]);
            }
        }
}
 
Example 19
Source File: DefaultCallbackHandler.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
public void handle(Callback[] callbacks)
    throws IOException, UnsupportedCallbackException {
        for (int i = 0; i < callbacks.length; i++) {
            if (callbacks[i] instanceof NameCallback) {
                ((NameCallback)callbacks[i]).setName(authenticationID);

            } else if (callbacks[i] instanceof PasswordCallback) {
                ((PasswordCallback)callbacks[i]).setPassword(passwd);

            } else if (callbacks[i] instanceof RealmChoiceCallback) {
                /* Deals with a choice of realms */
                String[] choices =
                    ((RealmChoiceCallback)callbacks[i]).getChoices();
                int selected = 0;

                if (authRealm != null && authRealm.length() > 0) {
                    selected = -1; // no realm chosen
                    for (int j = 0; j < choices.length; j++) {
                        if (choices[j].equals(authRealm)) {
                            selected = j;
                        }
                    }
                    if (selected == -1) {
                        StringBuffer allChoices = new StringBuffer();
                        for (int j = 0; j <  choices.length; j++) {
                            allChoices.append(choices[j] + ",");
                        }
                        throw new IOException("Cannot match " +
                            "'java.naming.security.sasl.realm' property value, '" +
                            authRealm + "' with choices " + allChoices +
                            "in RealmChoiceCallback");
                    }
                }

                ((RealmChoiceCallback)callbacks[i]).setSelectedIndex(selected);

            } else if (callbacks[i] instanceof RealmCallback) {
                /* 1 or 0 realms specified in challenge */
                RealmCallback rcb = (RealmCallback) callbacks[i];
                if (authRealm != null) {
                    rcb.setText(authRealm);  // Use what user supplied
                } else {
                    String defaultRealm = rcb.getDefaultText();
                    if (defaultRealm != null) {
                        rcb.setText(defaultRealm); // Use what server supplied
                    } else {
                        rcb.setText("");  // Specify no realm
                    }
                }
            } else {
                throw new UnsupportedCallbackException(callbacks[i]);
            }
        }
}
 
Example 20
Source File: DefaultCallbackHandler.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
public void handle(Callback[] callbacks)
    throws IOException, UnsupportedCallbackException {
        for (int i = 0; i < callbacks.length; i++) {
            if (callbacks[i] instanceof NameCallback) {
                ((NameCallback)callbacks[i]).setName(authenticationID);

            } else if (callbacks[i] instanceof PasswordCallback) {
                ((PasswordCallback)callbacks[i]).setPassword(passwd);

            } else if (callbacks[i] instanceof RealmChoiceCallback) {
                /* Deals with a choice of realms */
                String[] choices =
                    ((RealmChoiceCallback)callbacks[i]).getChoices();
                int selected = 0;

                if (authRealm != null && authRealm.length() > 0) {
                    selected = -1; // no realm chosen
                    for (int j = 0; j < choices.length; j++) {
                        if (choices[j].equals(authRealm)) {
                            selected = j;
                        }
                    }
                    if (selected == -1) {
                        StringBuffer allChoices = new StringBuffer();
                        for (int j = 0; j <  choices.length; j++) {
                            allChoices.append(choices[j] + ",");
                        }
                        throw new IOException("Cannot match " +
                            "'java.naming.security.sasl.realm' property value, '" +
                            authRealm + "' with choices " + allChoices +
                            "in RealmChoiceCallback");
                    }
                }

                ((RealmChoiceCallback)callbacks[i]).setSelectedIndex(selected);

            } else if (callbacks[i] instanceof RealmCallback) {
                /* 1 or 0 realms specified in challenge */
                RealmCallback rcb = (RealmCallback) callbacks[i];
                if (authRealm != null) {
                    rcb.setText(authRealm);  // Use what user supplied
                } else {
                    String defaultRealm = rcb.getDefaultText();
                    if (defaultRealm != null) {
                        rcb.setText(defaultRealm); // Use what server supplied
                    } else {
                        rcb.setText("");  // Specify no realm
                    }
                }
            } else {
                throw new UnsupportedCallbackException(callbacks[i]);
            }
        }
}