Back to index

moin  1.9.0~rc2
wikiconfig.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 # This is some sample code you might find useful when you want to use some
00003 # external cookie (made by some other program, not moin) with moin.
00004 # See the +++ places for customizing it to your needs. You need to put this
00005 # code into your farmconfig.py or wikiconfig.py.
00006 
00007 # HINT: this code is slightly outdated, if you fix it to work with 1.7, please send us a copy.
00008 from MoinMoin.config.multiconfig import DefaultConfig
00009 from MoinMoin.auth import BaseAuth
00010 
00011 class ExternalCookie(BaseAuth):
00012     name = 'external_cookie'
00013 
00014     def request(self, request, user_obj, **kw):
00015         """ authenticate via external cookie """
00016         import Cookie
00017         user = None
00018         try_next = True # if True, moin tries the next auth method
00019         cookiename = "whatever" # +++ external cookie name you want to use
00020 
00021         try:
00022             cookie = Cookie.SimpleCookie(request.saved_cookie)
00023         except Cookie.CookieError:
00024             # ignore invalid cookies
00025             cookie = None
00026         if cookie and cookiename in cookie:
00027             import urllib
00028             cookievalue = cookie[cookiename].value
00029             # +++ now we decode and parse the cookie value - edit this to fit your needs.
00030             # the minimum we need to get is auth_username. aliasname and email is optional.
00031             cookievalue = urllib.unquote(cookievalue) # cookie value is urlencoded, decode it
00032             cookievalue = cookievalue.decode('iso-8859-1') # decode cookie charset to unicode
00033             cookievalue = cookievalue.split('#') # cookie has format loginname#firstname#lastname#email
00034 
00035             auth_username = cookievalue[0] # having this cookie means user auth has already been done!
00036             aliasname = email = ''
00037             try:
00038                 aliasname = "%s %s" % (cookievalue[1], cookievalue[2]) # aliasname is for cosmetical stuff only
00039                 email = cookievalue[3]
00040             except IndexError: # +++ this is for debugging it, in case it does not work
00041                 if 0:
00042                     f = open("cookie.log", "w")
00043                     f.write(repr(cookie))
00044                     f.write(repr(cookievalue))
00045                     f.close()
00046                 pass
00047 
00048             from MoinMoin.user import User
00049             # giving auth_username to User constructor means that authentication has already been done.
00050             user = User(request, name=auth_username, auth_username=auth_username, auth_method=self.name)
00051 
00052             changed = False
00053             if aliasname != user.aliasname: # was the aliasname externally updated?
00054                 user.aliasname = aliasname
00055                 changed = True # yes -> update user profile
00056             if email != user.email: # was the email addr externally updated?
00057                 user.email = email
00058                 changed = True # yes -> update user profile
00059 
00060             if user:
00061                 user.create_or_update(changed)
00062             if user and user.valid: # did we succeed making up a valid user?
00063                 try_next = False # stop processing auth method list
00064         return user, try_next
00065 
00066 class FarmConfig(DefaultConfig):
00067     from MoinMoin.auth import MoinAuth
00068     # use ExternalCookie, also allow the usual moin login
00069     auth = [ExternalCookie(), MoinAuth()]
00070 
00071     # ... (rest of your config follows here) ...
00072