Authenticating TRAC against SMF forum (Yes, I am a masochist)

OK. So imagine that you have a mega popular forum for dev nerds, and want them to be able to authenticate against that forum when they go to edit their wiki. Imagine further that the forum is SMF, which is a really nice forum package, other than the fact that it is written in PHP (they know not what they do).

Other than the fact that this is kinda like getting your peanut butter in my chocolate, or something, it is also a problem since the languages of your wiki (TRAC in Python) and your forum (SMF in PHP) are completely different, and the libraries don't match. Good thing I am here to do this horrible cludge for y'all.

The first thing we need is a script...

The Script! (smf_auth.py)

try:
    from mod_python import apache
except:
    pass


def check_smf_auth(user,passwd):
    import _mysql
    import sha
    db=_mysql.connect("server_address_goes_here","mysql_user","mysql_pass","mysql_db")
    db.query("select passwd from smf_members where memberName='%s'"%_mysql.escape_string(user))
    r=db.store_result()
    hash=r.fetch_row()[0][0]
    #print dir(r)
    #print hash
    myhash=sha.sha(user.lower()+passwd).hexdigest()
    #print myhash    

    if myhash==hash:
        return True
    else:
        return False

def authenhandler(req):
    pw = req.get_basic_auth_pw()
    user = req.user
    #if user == "spam" and pw == "eggs":
    try:
        if check_smf_auth(user,pw):
            return apache.OK
        else:
            return apache.HTTP_UNAUTHORIZED
    except:
        return apache.HTTP_UNAUTHORIZED


if __name__=="__main__":
    import sys
    print "Harness"
    print check_smf_auth(sys.argv[1],sys.argv[2])

But wait, theres more!

Now you need to get apache (yes, icky icky apache) to figure out that we are going to auth against a python script instead of some stupid .htpasswd or digest file or something (ew). Just add the following to your vhost config:

vhost.trac.conf

   ...Your other mod_python crap goes up here...
    <Location /location/of/trac/login> #Probably /trac/login
      AuthType Basic
      AuthName Trac
      Require valid-user
      PythonAuthenHandler smf_auth
    </Location>
    PythonPath "sys.path+['/where/you/put/that/script/folder']"

Now you need to test your script, and make sure it works. You can probably handle that yourself.

Home Home