Back to index

moin  1.9.0~rc2
botbouncer.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 """
00003     MoinMoin - botbouncer.com verifier for OpenID login
00004 
00005     @copyright: 2007 MoinMoin:JohannesBerg
00006     @license: GNU GPL, see COPYING for details.
00007 """
00008 from MoinMoin import user
00009 from MoinMoin.auth import BaseAuth, CancelLogin, ContinueLogin, MultistageRedirectLogin
00010 from urllib import urlopen, quote_plus
00011 
00012 class BotBouncer(BaseAuth):
00013     name = 'botbouncer'
00014 
00015     def __init__(self, apikey):
00016         BaseAuth.__init__(self)
00017         self.apikey = apikey
00018 
00019     def login(self, request, user_obj, **kw):
00020         if kw.get('multistage'):
00021             uid = request.session.get('botbouncer.uid', None)
00022             if not uid:
00023                 return CancelLogin(None)
00024             openid = request.session['botbouncer.id']
00025             del request.session['botbouncer.id']
00026             del request.session['botbouncer.uid']
00027             user_obj = user.User(request, uid, auth_method='openid',
00028                                  auth_username=openid)
00029 
00030         if not user_obj or not user_obj.valid:
00031             return ContinueLogin(user_obj)
00032 
00033         if user_obj.auth_method != 'openid':
00034             return ContinueLogin(user_obj)
00035 
00036         openid_id = user_obj.auth_username
00037 
00038         _ = request.getText
00039 
00040         try:
00041             url = "http://botbouncer.com/api/info?openid=%s&api_key=%s" % (
00042                            quote_plus(openid_id), self.apikey)
00043             data = urlopen(url).read().strip()
00044         except IOError:
00045             return CancelLogin(_('Could not contact botbouncer.com.'))
00046 
00047         data = data.split(':')
00048         if len(data) != 2 or data[0] != 'verified':
00049             return CancelLogin('botbouncer.com verification failed, probably invalid API key.')
00050 
00051         if data[1].lower() == 'true':
00052             # they proved they are human already
00053             return ContinueLogin(user_obj)
00054 
00055         # tell them to verify at bot bouncer first
00056         request.session['botbouncer.id'] = openid_id
00057         request.session['botbouncer.uid'] = user_obj.id
00058 
00059         goto = "http://botbouncer.com/captcha/queryuser?return_to=%%return_form&openid=%s" % (
00060             quote_plus(request.session['botbouncer.id']))
00061         return MultistageRedirectLogin(goto)