just found out that I'll miss both 4th of July fireworks (I am in France), and July 14th fireworks (I'll be back in the US by then). Bummer!

Authenticating Ejabberd against a Ruby On Rails database

Notifixious just switched to the EJabberd server for its XMPP capabilities. One of the key point in this migration was the ability to authenticate users with an external script. I won’t go into details here, but this a wonderful thing for Notifixious! Anyway, the ejabberd website gives a certain number of scripts to authenticate users against external databases/architecture. Notifixious is using Ruby On Rails. There was no script. We made it (or at least, one… that probably needs improvment).

Someone actually made a Ruby script that checks directly into the database. If you’re using Rails, you can actually DRY this up a little bit!

Make sure you have the ActiveRecord gem (and ruby of course…) installed on the same server where you have your ejabberd. 

Then, you just have to copy your user class (in my case, I used the Restful Authentication) and change ejabberd.cfg to use an external authentication script.

The script looks like this :

#!/usr/bin/ruby
# This script is used by ejabberd to authenticate jabber users against a Rails Database
require 'rubygems'
require 'active_record' # Because, we are authenticating ActiveRecord users
require '/etc/ejabberd/user.rb' # User class from Rails application (Make sure to use an absolute path)
 
ActiveRecord::Base.establish_connection( # Database Connection settings for ActiveRecord
   :adapter  => "mysql",
   :host     => "host",
   :username => "user",
   :password => "password",
   :database => "database"
 )
 
buffer = String.new 
 
while STDIN.sysread(2, buffer) && buffer.length == 2 # See the Ejabber docs for the syntax to use
  length = buffer.unpack('n')[0]
  operation, username, domain, password = STDIN.sysread(length).split(':')
  begin
    response = case operation
    when 'auth'
      user = User.authenticate(username, password.strip) ? 1 : 0 # Authenticating method from Rails Model 'User'
    when 'isuser'
      User.find_by_login(username) ? 1 : 0 # Finding a User from Rails Model 'User'
    else
      0
    end
  rescue
    response = 0
  end
  STDOUT.syswrite([2, response].pack('nn')) # Sending back the response see ejabberd doc for syntax
end

Viewing 7 Comments

 

Trackbacks

(Trackback URL)

close Reblog this comment
blog comments powered by Disqus