All pastes #801506 Raw Edit

Vermischtes

public text v1 · immutable
#801506 ·published 2007-11-29 10:20 UTC
rendered paste body
def external_cookie(request, **kw):
        """ authenticate via external cookie """
        import md5
        import Cookie
        user = None
        try_next = True # if True, moin tries the next auth method
        cookiename = "moinmoin" # XXX external cookie name you want to use
        my_salt = settings.WIKI_USER_SALT # define a salt for your cookie

        try:
            cookie = Cookie.SimpleCookie(request.saved_cookie)
        except Cookie.CookieError:
            # ignore invalid cookies
            cookie = None
        if cookie and cookie.has_key(cookiename):
            import urllib
            cookievalue = cookie[cookiename].value
            # XXX now we decode and parse the cookie value - edit this to fit your needs.
            # the minimum we need to get is auth_username. aliasname and email is optional.
            cookievalue = urllib.unquote(cookievalue) # cookie value is urlencoded, decode it
            #cookievalue = cookievalue.decode('iso-8859-1') # decode cookie charset to unicode
            cookievalue = cookievalue.split('#') # cookie has format loginname#hash#firstname#lastname#email

            auth_username = cookievalue[0] # having this cookie means user auth has already been done!
            auth_hash = cookievalue[1] # this is our digest!
            #aliasname = email = ''
            try:
                aliasname = "%s %s" % (cookievalue[2], cookievalue[3]) # aliasname is for cosmetical stuff only
                #email = cookievalue[4]
            except IndexError: # XXX this is for debugging it, in case it does not work
                if 0:
                    f = open("cookie.log", "w")
                    f.write(repr(cookie))
                    f.write(repr(cookievalue))
                    f.close()
                pass
            # we just authenticate the user, if the hash is valid
            if auth_hash == md5.new(auth_username + my_salt).hexdigest():
                from MoinMoin.user import User
                # giving auth_username to User constructor means that authentication has already been done.
                user = User(request, name=auth_username, auth_username=auth_username)
    
                changed = False
                #if aliasname != user.aliasname: # was the aliasname externally updated?
                    #user.aliasname = aliasname ; changed = True # yes -> update user profile
                #if email != user.email: # was the email addr externally updated?
                    #user.email = email ; changed = True # yes -> update user profile
    
                if user:
                    user.create_or_update(changed)
                if user and user.valid: # did we succeed making up a valid user?
                    try_next = False # stop processing auth method list
        return user, try_next

    from MoinMoin.auth import moin_cookie, http
    # first try the external_cookie, then http basic auth, then the usual moin_cookie
    auth = [external_cookie,]