use of javax.security.sasl.RealmCallback 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.RealmCallback in project storm by nathanmarz.
the class ClientCallbackHandler method handle.
/**
* This method is invoked by SASL for authentication challenges
* @param callbacks a collection of challenge callbacks
*/
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
for (Callback c : callbacks) {
if (c instanceof NameCallback) {
LOG.debug("name callback");
NameCallback nc = (NameCallback) c;
nc.setName(_username);
} else if (c instanceof PasswordCallback) {
LOG.debug("password callback");
PasswordCallback pc = (PasswordCallback) c;
if (_password != null) {
pc.setPassword(_password.toCharArray());
}
} else if (c instanceof AuthorizeCallback) {
LOG.debug("authorization callback");
AuthorizeCallback ac = (AuthorizeCallback) c;
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 if (c instanceof RealmCallback) {
RealmCallback rc = (RealmCallback) c;
((RealmCallback) c).setText(rc.getDefaultText());
} else {
throw new UnsupportedCallbackException(c);
}
}
}
use of javax.security.sasl.RealmCallback 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.RealmCallback in project jdk8u_jdk by JetBrains.
the class SampleCallbackHandler method handle.
public void handle(Callback[] callbacks) throws java.io.IOException, UnsupportedCallbackException {
for (int i = 0; i < callbacks.length; i++) {
if (callbacks[i] instanceof NameCallback) {
NameCallback cb = (NameCallback) callbacks[i];
cb.setName(getInput(cb.getPrompt()));
} else if (callbacks[i] instanceof PasswordCallback) {
PasswordCallback cb = (PasswordCallback) callbacks[i];
String pw = getInput(cb.getPrompt());
char[] passwd = new char[pw.length()];
pw.getChars(0, passwd.length, passwd, 0);
cb.setPassword(passwd);
} else if (callbacks[i] instanceof RealmCallback) {
RealmCallback cb = (RealmCallback) callbacks[i];
cb.setText(getInput(cb.getPrompt()));
} else {
throw new UnsupportedCallbackException(callbacks[i]);
}
}
}
use of javax.security.sasl.RealmCallback in project jdk8u_jdk by JetBrains.
the class PropertiesFileCallbackHandler method handle.
public void handle(Callback[] callbacks) throws UnsupportedCallbackException {
NameCallback ncb = null;
PasswordCallback pcb = null;
AuthorizeCallback acb = null;
RealmCallback rcb = null;
for (int i = 0; i < callbacks.length; i++) {
if (callbacks[i] instanceof NameCallback) {
ncb = (NameCallback) callbacks[i];
} else if (callbacks[i] instanceof PasswordCallback) {
pcb = (PasswordCallback) callbacks[i];
} else if (callbacks[i] instanceof AuthorizeCallback) {
acb = (AuthorizeCallback) callbacks[i];
} else if (callbacks[i] instanceof RealmCallback) {
rcb = (RealmCallback) callbacks[i];
} else {
throw new UnsupportedCallbackException(callbacks[i]);
}
}
if (pcb != null && ncb != null) {
String username = ncb.getDefaultName();
String pw = pwDb.getProperty(username);
if (pw != null) {
char[] pwchars = pw.toCharArray();
pcb.setPassword(pwchars);
// Clear pw
for (int i = 0; i < pwchars.length; i++) {
pwchars[i] = 0;
}
// Set canonicalized username if any
String canonAuthid = (namesDb != null ? namesDb.getProperty(username) : null);
if (canonAuthid != null) {
ncb.setName(canonAuthid);
}
}
}
if (acb != null) {
String authid = acb.getAuthenticationID();
String authzid = acb.getAuthorizationID();
if (authid.equals(authzid)) {
// Self is always authorized
acb.setAuthorized(true);
} else {
// Check db for allowed authzids
String authzes = (proxyDb != null ? proxyDb.getProperty(authid) : null);
if (authzes != null && authzes.indexOf(authzid) >= 0) {
// XXX need to search for subtrings or use StringTokenizer
// to avoid incorrectly matching subnames
acb.setAuthorized(true);
}
}
if (acb.isAuthorized()) {
// Set canonicalized name
String canonAuthzid = (namesDb != null ? namesDb.getProperty(authzid) : null);
if (canonAuthzid != null) {
acb.setAuthorizedID(canonAuthzid);
}
}
}
}
Aggregations