~ubuntu-branches/ubuntu/natty/moin/natty-updates

« back to all changes in this revision

Viewing changes to MoinMoin/auth/botbouncer.py

  • Committer: Bazaar Package Importer
  • Author(s): Jonas Smedegaard
  • Date: 2008-06-22 21:17:13 UTC
  • mfrom: (0.9.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080622211713-fpo2zrq3s5dfecxg
Tags: 1.7.0-3
Simplify /etc/moin/wikilist format: "USER URL" (drop unneeded middle
CONFIG_DIR that was wrongly advertised as DATA_DIR).  Make
moin-mass-migrate handle both formats and warn about deprecation of
the old one.

Show diffs side-by-side

added added

removed removed

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