11 Mar

ejabberd authentication using a db table

ejabberd is a distributed scalable instant messenger server using the XMPP (Jabber) protocol.

I’m using it in a project upgrade at the moment which has a large established set of users. As such, when the upgrade goes live, I don’t want the users to have to recreate their user accounts. So, when the users load up their shiny new clients some time soon and see they have a new instant messenger to use, they should be able to use it flawlessly with their already-authenticated sessions, and not have to log in again.

So, I went looking for already-built code for this. The ejabberd site has a list of contributed scripts for authenticating against various scenarios. The scripts are usually in PHP or Perl.

The closest to what I wanted was the Joomla authentication one. The script needs to connect to a db and then validate the user against a table contained in that DB.

So I took the script I needed and I adapted it:

download (rename to .php when using)

I made some changes to the script. The original was strictly for the mysqli driver. I’ve changed the DB usage to use the Pear MDB2 library, so you can use whatever database you have a library for – in my case, I needed to use MSSQL. Also, the authentication SQL has been made configurable, so it should be easier to configure the script to your own usage.

The only fiddly thing is that in my database, passwords are encoded in a particular way, so you may need to change the following line to something that fits your own DB:

$convertedpass=$this->oDB->quote(base64_encode(sha1($aCommand[3],true)));