annotate contrib/auth_externalcookie/wikiconfig.py @ 300:174cba552bd9

Fixed a few copy & paste bugs. imported from: moin--main--1.5--patch-304
author Alexander Schremmer <alex@alexanderweb.de.tla>
date Thu, 08 Dec 2005 20:04:35 +0000
parents 91d47ebee530
children ad642b8d5fec
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
225
1814c26fe6f1 sample code for using an external auth cookie
Thomas Waldmann <tw@waldmann-edv.de>
parents:
diff changeset
1 # -*- coding: iso-8859-1 -*-
1814c26fe6f1 sample code for using an external auth cookie
Thomas Waldmann <tw@waldmann-edv.de>
parents:
diff changeset
2 # This is some sample code you might find useful when you want to use some
1814c26fe6f1 sample code for using an external auth cookie
Thomas Waldmann <tw@waldmann-edv.de>
parents:
diff changeset
3 # external cookie (made by some other program, not moin) with moin.
1814c26fe6f1 sample code for using an external auth cookie
Thomas Waldmann <tw@waldmann-edv.de>
parents:
diff changeset
4 # See the XXX places for customizing it to your needs. You need to put this
1814c26fe6f1 sample code for using an external auth cookie
Thomas Waldmann <tw@waldmann-edv.de>
parents:
diff changeset
5 # code into your farmconfig.py or wikiconfig.py.
1814c26fe6f1 sample code for using an external auth cookie
Thomas Waldmann <tw@waldmann-edv.de>
parents:
diff changeset
6
1814c26fe6f1 sample code for using an external auth cookie
Thomas Waldmann <tw@waldmann-edv.de>
parents:
diff changeset
7 # ...
1814c26fe6f1 sample code for using an external auth cookie
Thomas Waldmann <tw@waldmann-edv.de>
parents:
diff changeset
8
1814c26fe6f1 sample code for using an external auth cookie
Thomas Waldmann <tw@waldmann-edv.de>
parents:
diff changeset
9 class FarmConfig(DefaultConfig):
295
91d47ebee530 make posted UserPreferences form values easily available for auth methods
Thomas Waldmann <tw@waldmann-edv.de>
parents: 268
diff changeset
10 def external_cookie(request, **kw):
225
1814c26fe6f1 sample code for using an external auth cookie
Thomas Waldmann <tw@waldmann-edv.de>
parents:
diff changeset
11 """ authenticate via external cookie """
1814c26fe6f1 sample code for using an external auth cookie
Thomas Waldmann <tw@waldmann-edv.de>
parents:
diff changeset
12 import Cookie
239
9ae17b303304 added cfg.user_autocreate support for contrib/auth_externalcookie
Thomas Waldmann <tw@waldmann-edv.de>
parents: 225
diff changeset
13 user = None
300
174cba552bd9 Fixed a few copy & paste bugs.
Alexander Schremmer <alex@alexanderweb.de.tla>
parents: 295
diff changeset
14 try_next = True # if True, moin tries the next auth method
225
1814c26fe6f1 sample code for using an external auth cookie
Thomas Waldmann <tw@waldmann-edv.de>
parents:
diff changeset
15 cookiename = "whatever" # XXX external cookie name you want to use
300
174cba552bd9 Fixed a few copy & paste bugs.
Alexander Schremmer <alex@alexanderweb.de.tla>
parents: 295
diff changeset
16
225
1814c26fe6f1 sample code for using an external auth cookie
Thomas Waldmann <tw@waldmann-edv.de>
parents:
diff changeset
17 try:
1814c26fe6f1 sample code for using an external auth cookie
Thomas Waldmann <tw@waldmann-edv.de>
parents:
diff changeset
18 cookie = Cookie.SimpleCookie(request.saved_cookie)
1814c26fe6f1 sample code for using an external auth cookie
Thomas Waldmann <tw@waldmann-edv.de>
parents:
diff changeset
19 except Cookie.CookieError:
1814c26fe6f1 sample code for using an external auth cookie
Thomas Waldmann <tw@waldmann-edv.de>
parents:
diff changeset
20 # ignore invalid cookies
1814c26fe6f1 sample code for using an external auth cookie
Thomas Waldmann <tw@waldmann-edv.de>
parents:
diff changeset
21 cookie = None
1814c26fe6f1 sample code for using an external auth cookie
Thomas Waldmann <tw@waldmann-edv.de>
parents:
diff changeset
22 if cookie and cookie.has_key(cookiename):
1814c26fe6f1 sample code for using an external auth cookie
Thomas Waldmann <tw@waldmann-edv.de>
parents:
diff changeset
23 import urllib
1814c26fe6f1 sample code for using an external auth cookie
Thomas Waldmann <tw@waldmann-edv.de>
parents:
diff changeset
24 cookievalue = cookie[cookiename].value
1814c26fe6f1 sample code for using an external auth cookie
Thomas Waldmann <tw@waldmann-edv.de>
parents:
diff changeset
25 # XXX now we decode and parse the cookie value - edit this to fit your needs.
1814c26fe6f1 sample code for using an external auth cookie
Thomas Waldmann <tw@waldmann-edv.de>
parents:
diff changeset
26 # the minimum we need to get is auth_username. aliasname and email is optional.
1814c26fe6f1 sample code for using an external auth cookie
Thomas Waldmann <tw@waldmann-edv.de>
parents:
diff changeset
27 cookievalue = urllib.unquote(cookievalue) # cookie value is urlencoded, decode it
1814c26fe6f1 sample code for using an external auth cookie
Thomas Waldmann <tw@waldmann-edv.de>
parents:
diff changeset
28 cookievalue = cookievalue.decode('iso-8859-1') # decode cookie charset to unicode
1814c26fe6f1 sample code for using an external auth cookie
Thomas Waldmann <tw@waldmann-edv.de>
parents:
diff changeset
29 cookievalue = cookievalue.split('#') # cookie has format loginname#firstname#lastname#email
1814c26fe6f1 sample code for using an external auth cookie
Thomas Waldmann <tw@waldmann-edv.de>
parents:
diff changeset
30
1814c26fe6f1 sample code for using an external auth cookie
Thomas Waldmann <tw@waldmann-edv.de>
parents:
diff changeset
31 auth_username = cookievalue[0] # having this cookie means user auth has already been done!
1814c26fe6f1 sample code for using an external auth cookie
Thomas Waldmann <tw@waldmann-edv.de>
parents:
diff changeset
32 aliasname = email = ''
1814c26fe6f1 sample code for using an external auth cookie
Thomas Waldmann <tw@waldmann-edv.de>
parents:
diff changeset
33 try:
1814c26fe6f1 sample code for using an external auth cookie
Thomas Waldmann <tw@waldmann-edv.de>
parents:
diff changeset
34 aliasname = "%s %s" % (cookievalue[1], cookievalue[2]) # aliasname is for cosmetical stuff only
1814c26fe6f1 sample code for using an external auth cookie
Thomas Waldmann <tw@waldmann-edv.de>
parents:
diff changeset
35 email = cookievalue[3]
1814c26fe6f1 sample code for using an external auth cookie
Thomas Waldmann <tw@waldmann-edv.de>
parents:
diff changeset
36 except IndexError: # XXX this is for debugging it, in case it does not work
1814c26fe6f1 sample code for using an external auth cookie
Thomas Waldmann <tw@waldmann-edv.de>
parents:
diff changeset
37 if 0:
1814c26fe6f1 sample code for using an external auth cookie
Thomas Waldmann <tw@waldmann-edv.de>
parents:
diff changeset
38 f = open("cookie.log", "w")
1814c26fe6f1 sample code for using an external auth cookie
Thomas Waldmann <tw@waldmann-edv.de>
parents:
diff changeset
39 f.write(repr(cookie))
1814c26fe6f1 sample code for using an external auth cookie
Thomas Waldmann <tw@waldmann-edv.de>
parents:
diff changeset
40 f.write(repr(cookievalue))
1814c26fe6f1 sample code for using an external auth cookie
Thomas Waldmann <tw@waldmann-edv.de>
parents:
diff changeset
41 f.close()
1814c26fe6f1 sample code for using an external auth cookie
Thomas Waldmann <tw@waldmann-edv.de>
parents:
diff changeset
42 pass
1814c26fe6f1 sample code for using an external auth cookie
Thomas Waldmann <tw@waldmann-edv.de>
parents:
diff changeset
43
1814c26fe6f1 sample code for using an external auth cookie
Thomas Waldmann <tw@waldmann-edv.de>
parents:
diff changeset
44 from MoinMoin.user import User
1814c26fe6f1 sample code for using an external auth cookie
Thomas Waldmann <tw@waldmann-edv.de>
parents:
diff changeset
45 # giving auth_username to User constructor means that authentication has already been done.
1814c26fe6f1 sample code for using an external auth cookie
Thomas Waldmann <tw@waldmann-edv.de>
parents:
diff changeset
46 user = User(request, name=auth_username, auth_username=auth_username)
1814c26fe6f1 sample code for using an external auth cookie
Thomas Waldmann <tw@waldmann-edv.de>
parents:
diff changeset
47
1814c26fe6f1 sample code for using an external auth cookie
Thomas Waldmann <tw@waldmann-edv.de>
parents:
diff changeset
48 changed = False
1814c26fe6f1 sample code for using an external auth cookie
Thomas Waldmann <tw@waldmann-edv.de>
parents:
diff changeset
49 if aliasname != user.aliasname: # was the aliasname externally updated?
1814c26fe6f1 sample code for using an external auth cookie
Thomas Waldmann <tw@waldmann-edv.de>
parents:
diff changeset
50 user.aliasname = aliasname ; changed = True # yes -> update user profile
1814c26fe6f1 sample code for using an external auth cookie
Thomas Waldmann <tw@waldmann-edv.de>
parents:
diff changeset
51 if email != user.email: # was the email addr externally updated?
1814c26fe6f1 sample code for using an external auth cookie
Thomas Waldmann <tw@waldmann-edv.de>
parents:
diff changeset
52 user.email = email ; changed = True # yes -> update user profile
239
9ae17b303304 added cfg.user_autocreate support for contrib/auth_externalcookie
Thomas Waldmann <tw@waldmann-edv.de>
parents: 225
diff changeset
53
300
174cba552bd9 Fixed a few copy & paste bugs.
Alexander Schremmer <alex@alexanderweb.de.tla>
parents: 295
diff changeset
54 if user:
174cba552bd9 Fixed a few copy & paste bugs.
Alexander Schremmer <alex@alexanderweb.de.tla>
parents: 295
diff changeset
55 user.create_or_update(changed)
174cba552bd9 Fixed a few copy & paste bugs.
Alexander Schremmer <alex@alexanderweb.de.tla>
parents: 295
diff changeset
56 if user and user.valid: # did we succeed making up a valid user?
174cba552bd9 Fixed a few copy & paste bugs.
Alexander Schremmer <alex@alexanderweb.de.tla>
parents: 295
diff changeset
57 try_next = False # stop processing auth method list
174cba552bd9 Fixed a few copy & paste bugs.
Alexander Schremmer <alex@alexanderweb.de.tla>
parents: 295
diff changeset
58 return user, try_next
225
1814c26fe6f1 sample code for using an external auth cookie
Thomas Waldmann <tw@waldmann-edv.de>
parents:
diff changeset
59
1814c26fe6f1 sample code for using an external auth cookie
Thomas Waldmann <tw@waldmann-edv.de>
parents:
diff changeset
60 from MoinMoin.auth import moin_cookie, http
1814c26fe6f1 sample code for using an external auth cookie
Thomas Waldmann <tw@waldmann-edv.de>
parents:
diff changeset
61 # first try the external_cookie, then http basic auth, then the usual moin_cookie
1814c26fe6f1 sample code for using an external auth cookie
Thomas Waldmann <tw@waldmann-edv.de>
parents:
diff changeset
62 auth = [external_cookie, http, moin_cookie]
1814c26fe6f1 sample code for using an external auth cookie
Thomas Waldmann <tw@waldmann-edv.de>
parents:
diff changeset
63
1814c26fe6f1 sample code for using an external auth cookie
Thomas Waldmann <tw@waldmann-edv.de>
parents:
diff changeset
64 # ... (rest of your config follows here) ...
1814c26fe6f1 sample code for using an external auth cookie
Thomas Waldmann <tw@waldmann-edv.de>
parents:
diff changeset
65