use of org.apache.harmony.javax.security.sasl.SaslClient in project AsmackService by rtreffer.
the class SASLEngine method login.
/**
* Perform the sasl roundtrip on a given connection.
* @param xmppInputStream XmppInputStream The underlying xmpp input stream.
* @param xmppOutputStream XmppOutputStream The underlying xmpp output
* stream.
* @param methods Set<String> The set of allowed authentification methods.
* @param account XmppAccount The internal xmpp account.
* @return boolean True on success.
* @throws XmppException In case of a hard xml/xmpp error.
*/
public static boolean login(XmppInputStream xmppInputStream, XmppOutputStream xmppOutputStream, Set<String> methods, XmppAccount account) throws XmppException {
SaslClient saslClient = null;
if (methods.contains("DIGEST-MD5")) {
saslClient = DigestMD5SaslClient.getClient(XMPPUtils.getUser(account.getJid()), "xmpp", XMPPUtils.getDomain(account.getJid()), new TreeMap<Object, Object>(), new AccountCallbackHander(account));
} else if (methods.contains("PLAIN")) {
try {
saslClient = new PlainSaslClient(null, new AccountCallbackHander(account));
} catch (SaslException e) {
throw new XmppSaslException("Could not instanciate plain auth", e);
}
}
if (saslClient.hasInitialResponse()) {
try {
xmppOutputStream.sendUnchecked("<auth " + "xmlns='" + NAMESPACE + "' " + "mechanism='" + saslClient.getMechanismName() + "'>" + encodeBase64(saslClient.evaluateChallenge(null)) + "</auth>");
} catch (SaslException e) {
throw new XmppSaslException("Could not instanciate plain auth", e);
}
} else {
xmppOutputStream.sendUnchecked("<auth " + "xmlns='" + NAMESPACE + "' " + "mechanism='" + saslClient.getMechanismName() + "'/>");
}
Node stanza = xmppInputStream.nextStanza().getDocumentNode();
while (!XMLUtils.isInstance(stanza, NAMESPACE, "success")) {
if (!XMLUtils.isInstance(stanza, NAMESPACE, "challenge")) {
throw new XmppSaslException("Authentification failed: " + stanza.getNodeValue());
}
String content = stanza.getFirstChild().getNodeValue().trim();
byte[] response;
try {
response = saslClient.evaluateChallenge(decodeBase64(content));
} catch (SaslException e) {
throw new XmppSaslException("Could not evaluate challenge", e);
}
if (saslClient.isComplete()) {
xmppOutputStream.sendUnchecked("<response xmlns='" + NAMESPACE + "'/>");
} else {
xmppOutputStream.sendUnchecked("<response xmlns='" + NAMESPACE + "'>" + encodeBase64(response) + "</response>");
}
stanza = xmppInputStream.nextStanza().getDocumentNode();
}
return true;
}
Aggregations