Search in sources :

Example 1 with XmppSaslException

use of com.googlecode.asmack.XmppSaslException 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;
}
Also used : Node(org.w3c.dom.Node) XmppSaslException(com.googlecode.asmack.XmppSaslException) PlainSaslClient(org.apache.qpid.management.common.sasl.PlainSaslClient) TreeMap(java.util.TreeMap) SaslException(org.apache.harmony.javax.security.sasl.SaslException) XmppSaslException(com.googlecode.asmack.XmppSaslException) DigestMD5SaslClient(com.novell.sasl.client.DigestMD5SaslClient) PlainSaslClient(org.apache.qpid.management.common.sasl.PlainSaslClient) SaslClient(org.apache.harmony.javax.security.sasl.SaslClient)

Aggregations

XmppSaslException (com.googlecode.asmack.XmppSaslException)1 DigestMD5SaslClient (com.novell.sasl.client.DigestMD5SaslClient)1 TreeMap (java.util.TreeMap)1 SaslClient (org.apache.harmony.javax.security.sasl.SaslClient)1 SaslException (org.apache.harmony.javax.security.sasl.SaslException)1 PlainSaslClient (org.apache.qpid.management.common.sasl.PlainSaslClient)1 Node (org.w3c.dom.Node)1