Mercurial > p > roundup > code
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 5974:98a8509ce45c | 5975:59842a3e8108 |
|---|---|
| 1 '''Set of functions of adding/checking timestamp to be used to limit | |
| 2 form submission for cgi actions. | |
| 3 ''' | |
| 4 | |
| 5 import time, struct, binascii, base64 | |
| 6 from roundup.cgi.exceptions import FormError | |
| 7 from roundup.i18n import _ | |
| 8 from roundup.anypy.strings import b2s, s2b | |
| 9 | |
| 10 def pack_timestamp(): | |
| 11 return b2s(base64.b64encode(struct.pack("i", int(time.time()))).strip()) | |
| 12 | |
| 13 def unpack_timestamp(s): | |
| 14 try: | |
| 15 timestamp = struct.unpack("i",base64.b64decode(s2b(s)))[0] | |
| 16 except (struct.error, binascii.Error, TypeError) as e: | |
| 17 raise FormError(_("Form is corrupted.")) | |
| 18 return timestamp | |
| 19 | |
| 20 class Timestamped: | |
| 21 def timecheck(self,field,delay): | |
| 22 try: | |
| 23 created = unpack_timestamp(self.form[field].value) | |
| 24 except KeyError: | |
| 25 raise FormError(_("Form is corrupted, missing: %s."%field)) | |
| 26 if time.time() - created < delay: | |
| 27 raise FormError(_("Responding to form too quickly.")) | |
| 28 return True |
