annotate MoinMoin/auth/botbouncer.py @ 3175:2a3a6cb34e45

pylint findings: fix some harmless stuff
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Mon, 03 Mar 2008 02:02:55 +0100
parents 95e57cb59bb0
children e523dc1c4758
rev   line source
2300
95e57cb59bb0 botbouncer.com OpenID verification plugin
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
1 # -*- coding: iso-8859-1 -*-
95e57cb59bb0 botbouncer.com OpenID verification plugin
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
2 """
95e57cb59bb0 botbouncer.com OpenID verification plugin
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
3 MoinMoin - botbouncer.com verifier for OpenID login
95e57cb59bb0 botbouncer.com OpenID verification plugin
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
4
95e57cb59bb0 botbouncer.com OpenID verification plugin
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
5 @copyright: 2007 MoinMoin:JohannesBerg
95e57cb59bb0 botbouncer.com OpenID verification plugin
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
6 @license: GNU GPL, see COPYING for details.
95e57cb59bb0 botbouncer.com OpenID verification plugin
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
7 """
95e57cb59bb0 botbouncer.com OpenID verification plugin
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
8 from MoinMoin import user
95e57cb59bb0 botbouncer.com OpenID verification plugin
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
9 from MoinMoin.auth import BaseAuth, CancelLogin, ContinueLogin, MultistageRedirectLogin
95e57cb59bb0 botbouncer.com OpenID verification plugin
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
10 from urllib import urlopen, quote_plus
95e57cb59bb0 botbouncer.com OpenID verification plugin
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
11
95e57cb59bb0 botbouncer.com OpenID verification plugin
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
12 class BotBouncer(BaseAuth):
95e57cb59bb0 botbouncer.com OpenID verification plugin
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
13 name = 'botbouncer'
95e57cb59bb0 botbouncer.com OpenID verification plugin
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
14
95e57cb59bb0 botbouncer.com OpenID verification plugin
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
15 def __init__(self, apikey):
95e57cb59bb0 botbouncer.com OpenID verification plugin
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
16 BaseAuth.__init__(self)
95e57cb59bb0 botbouncer.com OpenID verification plugin
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
17 self.apikey = apikey
95e57cb59bb0 botbouncer.com OpenID verification plugin
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
18
95e57cb59bb0 botbouncer.com OpenID verification plugin
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
19 def login(self, request, user_obj, **kw):
95e57cb59bb0 botbouncer.com OpenID verification plugin
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
20 if kw.get('multistage'):
95e57cb59bb0 botbouncer.com OpenID verification plugin
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
21 uid = request.session.get('botbouncer.uid', None)
95e57cb59bb0 botbouncer.com OpenID verification plugin
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
22 if not uid:
95e57cb59bb0 botbouncer.com OpenID verification plugin
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
23 return CancelLogin()
95e57cb59bb0 botbouncer.com OpenID verification plugin
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
24 openid = request.session['botbouncer.id']
95e57cb59bb0 botbouncer.com OpenID verification plugin
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
25 del request.session['botbouncer.id']
95e57cb59bb0 botbouncer.com OpenID verification plugin
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
26 del request.session['botbouncer.uid']
95e57cb59bb0 botbouncer.com OpenID verification plugin
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
27 user_obj = user.User(request, uid, auth_method='openid',
95e57cb59bb0 botbouncer.com OpenID verification plugin
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
28 auth_username=openid)
95e57cb59bb0 botbouncer.com OpenID verification plugin
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
29
95e57cb59bb0 botbouncer.com OpenID verification plugin
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
30 if not user_obj or not user_obj.valid:
95e57cb59bb0 botbouncer.com OpenID verification plugin
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
31 return ContinueLogin(user_obj)
95e57cb59bb0 botbouncer.com OpenID verification plugin
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
32
95e57cb59bb0 botbouncer.com OpenID verification plugin
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
33 if user_obj.auth_method != 'openid':
95e57cb59bb0 botbouncer.com OpenID verification plugin
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
34 return ContinueLogin(user_obj)
95e57cb59bb0 botbouncer.com OpenID verification plugin
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
35
95e57cb59bb0 botbouncer.com OpenID verification plugin
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
36 openid_id = user_obj.auth_username
95e57cb59bb0 botbouncer.com OpenID verification plugin
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
37
95e57cb59bb0 botbouncer.com OpenID verification plugin
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
38 _ = request.getText
95e57cb59bb0 botbouncer.com OpenID verification plugin
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
39
95e57cb59bb0 botbouncer.com OpenID verification plugin
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
40 try:
95e57cb59bb0 botbouncer.com OpenID verification plugin
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
41 url = "http://botbouncer.com/api/info?openid=%s&api_key=%s" % (
95e57cb59bb0 botbouncer.com OpenID verification plugin
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
42 quote_plus(openid_id), self.apikey)
95e57cb59bb0 botbouncer.com OpenID verification plugin
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
43 data = urlopen(url).read().strip()
95e57cb59bb0 botbouncer.com OpenID verification plugin
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
44 except IOError:
95e57cb59bb0 botbouncer.com OpenID verification plugin
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
45 return CancelLogin(_('Could not contact botbouncer.com.'))
95e57cb59bb0 botbouncer.com OpenID verification plugin
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
46
95e57cb59bb0 botbouncer.com OpenID verification plugin
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
47 data = data.split(':')
95e57cb59bb0 botbouncer.com OpenID verification plugin
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
48 if len(data) != 2 or data[0] != 'verified':
95e57cb59bb0 botbouncer.com OpenID verification plugin
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
49 return CancelLogin('botbouncer.com verification failed, probably invalid API key.')
95e57cb59bb0 botbouncer.com OpenID verification plugin
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
50
95e57cb59bb0 botbouncer.com OpenID verification plugin
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
51 if data[1].lower() == 'true':
95e57cb59bb0 botbouncer.com OpenID verification plugin
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
52 # they proved they are human already
95e57cb59bb0 botbouncer.com OpenID verification plugin
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
53 return ContinueLogin(user_obj)
95e57cb59bb0 botbouncer.com OpenID verification plugin
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
54
95e57cb59bb0 botbouncer.com OpenID verification plugin
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
55 # tell them to verify at bot bouncer first
95e57cb59bb0 botbouncer.com OpenID verification plugin
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
56 request.session['botbouncer.id'] = openid_id
95e57cb59bb0 botbouncer.com OpenID verification plugin
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
57 request.session['botbouncer.uid'] = user_obj.id
95e57cb59bb0 botbouncer.com OpenID verification plugin
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
58
95e57cb59bb0 botbouncer.com OpenID verification plugin
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
59 goto = "http://botbouncer.com/captcha/queryuser?return_to=%%return_form&openid=%s" % (
95e57cb59bb0 botbouncer.com OpenID verification plugin
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
60 quote_plus(request.session['botbouncer.id']))
95e57cb59bb0 botbouncer.com OpenID verification plugin
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
61 return MultistageRedirectLogin(goto)