annotate captchamiddleware/middleware.py @ 2:c861518b2a44

fix bug whereby forbidden characters cause issues
author k0s <k0scist@gmail.com>
date Tue, 23 Feb 2010 19:50:23 -0500
parents 478d13061336
children b0ef5452a740
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
1 """
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
2 CAPTCHA middleware
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
3 """
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
4
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
5 import os
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
6 import random
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
7 import sys
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
8 from lxml import etree
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
9 from lxmlmiddleware import LXMLMiddleware
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
10 from skimpyGimpy import skimpyAPI
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
11 from urllib2 import urlopen
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
12 from webob import Request, exc
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
13
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
14
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
15 class CAPTCHAmiddleware(LXMLMiddleware):
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
16 """
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
17 put CAPTCHAs on forms for unauthorized users
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
18 """
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
19
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
20 ### class level variables
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
21 defaults = {
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
22 'dictionary': '/usr/share/dict/words',
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
23 'error': '<span class="error">Please type the CAPTCHA</span>',
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
24 'minimum_length': 5,
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
25 'path': "/input[@type='submit']",
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
26 }
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
27
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
28 def __init__(self, app, **kw):
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
29 self.app = app
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
30
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
31 # set instance parameters from kw and defaults
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
32 for key in self.defaults:
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
33 setattr(self, key, kw.get(key, self.defaults[key]))
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
34 self.minimum_length = int(self.minimum_length)
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
35 assert os.path.exists(self.dictionary)
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
36
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
37 # get dictionary
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
38 if self.dictionary.startswith('http://') or self.dictionary.startswith('https://'):
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
39 f = urlopen(self.dictionary)
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
40 else:
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
41 f = file(self.dictionary)
2
c861518b2a44 fix bug whereby forbidden characters cause issues
k0s <k0scist@gmail.com>
parents: 1
diff changeset
42
c861518b2a44 fix bug whereby forbidden characters cause issues
k0s <k0scist@gmail.com>
parents: 1
diff changeset
43 # characters skimpygimpy doesnt know about
c861518b2a44 fix bug whereby forbidden characters cause issues
k0s <k0scist@gmail.com>
parents: 1
diff changeset
44 forbidden_characters = set(["'"])
c861518b2a44 fix bug whereby forbidden characters cause issues
k0s <k0scist@gmail.com>
parents: 1
diff changeset
45
0
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
46 self.words = [ i.strip() for i in f.readlines()
2
c861518b2a44 fix bug whereby forbidden characters cause issues
k0s <k0scist@gmail.com>
parents: 1
diff changeset
47 if (len(i.strip()) > self.minimum_length)
c861518b2a44 fix bug whereby forbidden characters cause issues
k0s <k0scist@gmail.com>
parents: 1
diff changeset
48 and not forbidden_characters.intersection(i) ]
0
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
49 random.shuffle(self.words)
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
50
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
51
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
52 def __call__(self, environ, start_response):
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
53 request = Request(environ)
2
c861518b2a44 fix bug whereby forbidden characters cause issues
k0s <k0scist@gmail.com>
parents: 1
diff changeset
54 if request.method == 'POST' and not request.remote_user:
c861518b2a44 fix bug whereby forbidden characters cause issues
k0s <k0scist@gmail.com>
parents: 1
diff changeset
55 import pdb; pdb.set_trace()
0
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
56 pass # TODO: check CAPTCHA
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
57
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
58 return LXMLMiddleware.__call__(self, environ, start_response)
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
59
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
60 def manipulate(self, environ, tree):
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
61 """manipulate the DOM; should return an etree._Element"""
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
62
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
63 request = Request(environ)
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
64
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
65 # don't use CAPTCHAs for authorized users
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
66 if request.remote_user:
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
67 return tree
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
68
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
69 for element in tree.findall(".//form[@method='post']"):
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
70 key = random.Random().randint(0, len(self.words))
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
71 word = self.words[key]
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
72 captcha = skimpyAPI.Pre(word).data()
1
478d13061336 include a text input
k0s <k0scist@gmail.com>
parents: 0
diff changeset
73 string = '<div class="captcha">%s<input type="hidden" value="%s"/><input type="text" name="captcha"/></div>' % (captcha, key)
0
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
74 addition = etree.fromstring(string)
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
75 insertion_point = element.find('.' + self.path)
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
76 insertion_point.addprevious(addition)
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
77
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
78 return tree
21ec6325ae0e initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff changeset
79