Andy McKay

May 02, 2008

Login decorator for OpenId


The login decorator for Django looks for a user, as if you'd logged in through the admin interface. That's not what OpenId gives you. For that you'd want something like this:

def login_required(fn):
    def new(*args, **kw):
        request = args[0]
        if request.openid is None:
            return HttpResponseRedirect("/login")
        else:
            return fn(*args, **kw)
    return new  

Then for any views that require OpenId login on them, slap the following before them just like normal:

@login_required