SASL XMPP authentication on Ejabberd with PHP

I am trying to authenticate using an XMPP server using SASL.

/**
     * Send Authentication, SASL
     * @return Bool
     * @param $username String
     * @param $password String
     */
    function authenticate($username, $password) {
        $this->username = $username;
        $this->password = $password;

        var_dump($username, $password, $this->domain);

        $auth = base64_encode($username.'@'.$this->domain."\u0000".$username."\u0000".$password);
        $xml = '<auth mechanism="PLAIN" xmlns="urn:ietf:params:xml:ns:xmpp-sasl">'.$auth.'</auth>';
        if ($this->write($xml)) {
            if ($xml = $this->listen(1, true)) {
                if (preg_match("/<success/i", $xml)) {
                    $this->authenticated = $this->_sendStream();
                }
            }
        }
        $this->events->trigger('authenticate', $this->authenticated);
        return $this->authenticated;
    }

However, the XMPP server responds:

<failure xmlns='urn:ietf:params:xml:ns:xmpp-sasl'><bad-protocol/></failure>

This is against the Ejabberd server. When I open the XMPP stream, it advertises:

<stream:features><starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/><mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'><mechanism>DIGEST-MD5</mechanism><mechanism>PLAIN</mechanism></mechanisms><register xmlns='http://jabber.org/features/iq-register'/></stream:features>

So, it seems to me that SASL-PLAIN should work. I have a version of JavaScript that works fine on an OpenFire server. (I can't check this on Ejabberd at the moment)

sendAuthentication: function() {
        clearTimeout(XMPP.sendAuthentication_timer);
        var auth = Base64.encode(XMPP.username+'@'+XMPP.domain+'\u0000'+XMPP.username+'\u0000'+XMPP.password);
        mySocket.events.receive.observe(XMPP.receivedAuthSuccess, function() {
            mySocket.send('<auth mechanism="PLAIN" xmlns="urn:ietf:params:xml:ns:xmpp-sasl">' + auth + '</auth>');
        });
    }

Therefore, I cannot understand why the version of PHP is not working.

+3
source share
2 answers

Found out what the problem is. EJabberd will advertise SASL PLAIN and DIGEST-MD5, but will actually only accept DIGEST-MD5.

+1
source

Try this in Python:

Username+"@xmpp.poppen.lab"+ chr(0) + Username + chr(0) + Password

PHP chr(0) "\u0000"

+2

Source: https://habr.com/ru/post/1714118/


All Articles