changeset 225:1814c26fe6f1

sample code for using an external auth cookie imported from: moin--main--1.5--patch-228
author Thomas Waldmann <tw@waldmann-edv.de>
date Sat, 19 Nov 2005 22:38:05 +0000
parents 88304513aa73
children 52ee38346bc7
files contrib/auth_externalcookie/wikiconfig.py docs/CHANGES
diffstat 2 files changed, 66 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/contrib/auth_externalcookie/wikiconfig.py	Sat Nov 19 22:38:05 2005 +0000
@@ -0,0 +1,62 @@
+# -*- coding: iso-8859-1 -*-
+# This is some sample code you might find useful when you want to use some
+# external cookie (made by some other program, not moin) with moin.
+# See the XXX places for customizing it to your needs. You need to put this
+# code into your farmconfig.py or wikiconfig.py.
+
+# ...
+
+class FarmConfig(DefaultConfig):
+    def external_cookie(request):
+        """ authenticate via external cookie """
+        import Cookie
+        cookiename = "whatever" # XXX external cookie name you want to use
+        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#firstname#lastname#email
+            
+            auth_username = cookievalue[0] # having this cookie means user auth has already been done!
+            aliasname = email = ''
+            try:
+                aliasname = "%s %s" % (cookievalue[1], cookievalue[2]) # aliasname is for cosmetical stuff only
+                email = cookievalue[3]
+            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
+
+            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 not user.valid and not user.disabled or changed: # do we need to save/update?
+                user.save() # yes, create/update user profile
+            if user.valid: # did we succeed making up a valid user?
+                return user # yes, return user object and stop processing auth method list
+        return None # no, return None and continue with next method in auth list
+
+    from MoinMoin.auth import moin_cookie, http
+    # first try the external_cookie, then http basic auth, then the usual moin_cookie
+    auth = [external_cookie, http, moin_cookie]
+
+    # ... (rest of your config follows here) ...
+
--- a/docs/CHANGES	Sat Nov 19 20:26:19 2005 +0000
+++ b/docs/CHANGES	Sat Nov 19 22:38:05 2005 +0000
@@ -9,7 +9,10 @@
     * cfg.stylesheets = [] (default). You can use this on wiki or farm level
       to emit stylesheets after the theme css and before the user prefs css.
       The list entries must be ('screen', '/where/ever/is/my.css') style.
-  
+    * Added sample code for auth using an external cookie made by some other
+      program. See contrib/auth_externalcookie/*. You need to edit it to
+      fit whatever cookie you want to use.
+
 Version 1.5.0beta3:
   Fixes:
     * fixed editor preview throwing away page content for new pages