Mercurial > hg > CAPTCHAmiddleware
annotate captchamiddleware/middleware.py @ 0:21ec6325ae0e
initial import of CAPTCHA middleware; unfinished
author | k0s <k0scist@gmail.com> |
---|---|
date | Sun, 24 Jan 2010 23:15:57 -0500 |
parents | |
children | 478d13061336 |
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) |
21ec6325ae0e
initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
42 self.words = [ i.strip() for i in f.readlines() |
21ec6325ae0e
initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
43 if len(i.strip()) > self.minimum_length ] |
21ec6325ae0e
initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
44 random.shuffle(self.words) |
21ec6325ae0e
initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
45 |
21ec6325ae0e
initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
46 |
21ec6325ae0e
initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
47 def __call__(self, environ, start_response): |
21ec6325ae0e
initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
48 request = Request(environ) |
21ec6325ae0e
initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
49 if request.method == 'post' and not request.remote_user: |
21ec6325ae0e
initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
50 pass # TODO: check CAPTCHA |
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 return LXMLMiddleware.__call__(self, environ, start_response) |
21ec6325ae0e
initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
53 |
21ec6325ae0e
initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
54 def manipulate(self, environ, tree): |
21ec6325ae0e
initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
55 """manipulate the DOM; should return an etree._Element""" |
21ec6325ae0e
initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
56 |
21ec6325ae0e
initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
57 request = Request(environ) |
21ec6325ae0e
initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
58 |
21ec6325ae0e
initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
59 # don't use CAPTCHAs for authorized users |
21ec6325ae0e
initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
60 if request.remote_user: |
21ec6325ae0e
initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
61 return tree |
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 for element in tree.findall(".//form[@method='post']"): |
21ec6325ae0e
initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
64 key = random.Random().randint(0, len(self.words)) |
21ec6325ae0e
initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
65 word = self.words[key] |
21ec6325ae0e
initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
66 captcha = skimpyAPI.Pre(word).data() |
21ec6325ae0e
initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
67 string = '<div class="captcha">%s<input type="hidden" value="%s"/></div>' % (captcha, key) |
21ec6325ae0e
initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
68 addition = etree.fromstring(string) |
21ec6325ae0e
initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
69 insertion_point = element.find('.' + self.path) |
21ec6325ae0e
initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
70 insertion_point.addprevious(addition) |
21ec6325ae0e
initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
71 |
21ec6325ae0e
initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
72 return tree |
21ec6325ae0e
initial import of CAPTCHA middleware; unfinished
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
73 |