Mercurial > p > roundup > code
annotate roundup/cgi/timestamp.py @ 5975:59842a3e8108
issue2550919 - Anti-bot signup using 4 second delay
Oops another missing file 8-/.
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Sat, 09 Nov 2019 00:41:53 -0500 |
| parents | |
| children | 5ec3171580a6 |
| rev | line source |
|---|---|
|
5975
59842a3e8108
issue2550919 - Anti-bot signup using 4 second delay
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
1 '''Set of functions of adding/checking timestamp to be used to limit |
|
59842a3e8108
issue2550919 - Anti-bot signup using 4 second delay
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
2 form submission for cgi actions. |
|
59842a3e8108
issue2550919 - Anti-bot signup using 4 second delay
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
3 ''' |
|
59842a3e8108
issue2550919 - Anti-bot signup using 4 second delay
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
4 |
|
59842a3e8108
issue2550919 - Anti-bot signup using 4 second delay
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
5 import time, struct, binascii, base64 |
|
59842a3e8108
issue2550919 - Anti-bot signup using 4 second delay
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
6 from roundup.cgi.exceptions import FormError |
|
59842a3e8108
issue2550919 - Anti-bot signup using 4 second delay
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
7 from roundup.i18n import _ |
|
59842a3e8108
issue2550919 - Anti-bot signup using 4 second delay
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
8 from roundup.anypy.strings import b2s, s2b |
|
59842a3e8108
issue2550919 - Anti-bot signup using 4 second delay
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
9 |
|
59842a3e8108
issue2550919 - Anti-bot signup using 4 second delay
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
10 def pack_timestamp(): |
|
59842a3e8108
issue2550919 - Anti-bot signup using 4 second delay
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
11 return b2s(base64.b64encode(struct.pack("i", int(time.time()))).strip()) |
|
59842a3e8108
issue2550919 - Anti-bot signup using 4 second delay
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
12 |
|
59842a3e8108
issue2550919 - Anti-bot signup using 4 second delay
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
13 def unpack_timestamp(s): |
|
59842a3e8108
issue2550919 - Anti-bot signup using 4 second delay
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
14 try: |
|
59842a3e8108
issue2550919 - Anti-bot signup using 4 second delay
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
15 timestamp = struct.unpack("i",base64.b64decode(s2b(s)))[0] |
|
59842a3e8108
issue2550919 - Anti-bot signup using 4 second delay
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
16 except (struct.error, binascii.Error, TypeError) as e: |
|
59842a3e8108
issue2550919 - Anti-bot signup using 4 second delay
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
17 raise FormError(_("Form is corrupted.")) |
|
59842a3e8108
issue2550919 - Anti-bot signup using 4 second delay
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
18 return timestamp |
|
59842a3e8108
issue2550919 - Anti-bot signup using 4 second delay
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
19 |
|
59842a3e8108
issue2550919 - Anti-bot signup using 4 second delay
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
20 class Timestamped: |
|
59842a3e8108
issue2550919 - Anti-bot signup using 4 second delay
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
21 def timecheck(self,field,delay): |
|
59842a3e8108
issue2550919 - Anti-bot signup using 4 second delay
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
22 try: |
|
59842a3e8108
issue2550919 - Anti-bot signup using 4 second delay
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
23 created = unpack_timestamp(self.form[field].value) |
|
59842a3e8108
issue2550919 - Anti-bot signup using 4 second delay
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
24 except KeyError: |
|
59842a3e8108
issue2550919 - Anti-bot signup using 4 second delay
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
25 raise FormError(_("Form is corrupted, missing: %s."%field)) |
|
59842a3e8108
issue2550919 - Anti-bot signup using 4 second delay
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
26 if time.time() - created < delay: |
|
59842a3e8108
issue2550919 - Anti-bot signup using 4 second delay
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
27 raise FormError(_("Responding to form too quickly.")) |
|
59842a3e8108
issue2550919 - Anti-bot signup using 4 second delay
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
28 return True |
