use of javax.security.sasl.RealmChoiceCallback in project Smack by igniterealtime.
the class SASLJavaXMechanism method authenticateInternal.
@Override
protected void authenticateInternal() throws SmackException {
String[] mechanisms = { getName() };
Map<String, String> props = getSaslProps();
String authzid = null;
if (authorizationId != null) {
authzid = authorizationId.toString();
}
try {
sc = Sasl.createSaslClient(mechanisms, authzid, "xmpp", getServerName().toString(), props, new CallbackHandler() {
@Override
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, prevents UnsupportedCallbackException
// RealmChoiceCallback rccb =
// (RealmChoiceCallback)callbacks[i];
} else {
throw new UnsupportedCallbackException(callbacks[i]);
}
}
}
});
} catch (SaslException e) {
throw new SmackException(e);
}
}
use of javax.security.sasl.RealmChoiceCallback in project jdk8u_jdk by JetBrains.
the class ClientCallbackHandler method handle.
public void handle(Callback[] callbacks) throws UnsupportedCallbackException, IOException {
NameCallback ncb = null;
PasswordCallback pcb = null;
RealmChoiceCallback rccb = null;
List namePw = new ArrayList(3);
for (int i = 0; i < callbacks.length; i++) {
if (callbacks[i] instanceof NameCallback) {
if (auto) {
((NameCallback) callbacks[i]).setName(username);
} else {
// To be processed by TextCallbackHandler
namePw.add(callbacks[i]);
}
} else if (callbacks[i] instanceof PasswordCallback) {
if (auto) {
((PasswordCallback) callbacks[i]).setPassword(password.toCharArray());
} else {
// To be processed by TextCallbackHandler
namePw.add(callbacks[i]);
}
} else if (callbacks[i] instanceof RealmChoiceCallback) {
RealmChoiceCallback rcb = (RealmChoiceCallback) callbacks[i];
if (!auto) {
System.err.println(rcb.getPrompt());
}
String[] choices = rcb.getChoices();
if (!auto) {
for (int j = 0; j < choices.length; j++) {
System.err.println(j + ":" + choices[j]);
}
}
int selection;
if (auto) {
selection = 0;
} else {
System.err.print("Enter choice number: ");
String result = readLine();
if (result.equals("")) {
selection = rcb.getDefaultChoice();
} else {
selection = Integer.parseInt(result);
}
}
rcb.setSelectedIndex(selection);
} else if (callbacks[i] instanceof RealmCallback) {
RealmCallback rcb = (RealmCallback) callbacks[i];
String realm = rcb.getDefaultText();
if (auto) {
if (realm != null) {
rcb.setText(realm);
}
} else {
if (realm == null) {
System.err.print(rcb.getPrompt());
} else {
System.err.print(rcb.getPrompt() + " [" + realm + "] ");
}
System.err.flush();
String result = readLine();
if (result.equals("")) {
result = realm;
}
rcb.setText(result);
}
} else {
throw new UnsupportedCallbackException(callbacks[i]);
}
}
// Process name/password callbacks using superclass
if (namePw.size() > 0) {
Callback[] np = new Callback[namePw.size()];
namePw.toArray(np);
super.handle(np);
}
}
use of javax.security.sasl.RealmChoiceCallback in project hbase by apache.
the class TestHBaseSaslRpcClient method testSaslClientCallbackHandler.
@Test
public void testSaslClientCallbackHandler() throws UnsupportedCallbackException {
final Token<? extends TokenIdentifier> token = createTokenMock();
when(token.getIdentifier()).thenReturn(DEFAULT_USER_NAME.getBytes());
when(token.getPassword()).thenReturn(DEFAULT_USER_PASSWORD.getBytes());
final NameCallback nameCallback = mock(NameCallback.class);
final PasswordCallback passwordCallback = mock(PasswordCallback.class);
final RealmCallback realmCallback = mock(RealmCallback.class);
final RealmChoiceCallback realmChoiceCallback = mock(RealmChoiceCallback.class);
Callback[] callbackArray = { nameCallback, passwordCallback, realmCallback, realmChoiceCallback };
final SaslClientCallbackHandler saslClCallbackHandler = new SaslClientCallbackHandler(token);
saslClCallbackHandler.handle(callbackArray);
verify(nameCallback).setName(anyString());
verify(realmCallback).setText(anyString());
verify(passwordCallback).setPassword(any(char[].class));
}
Aggregations