diff options
author | Ralf Jung <post@ralfj.de> | 2019-06-10 17:29:24 +0200 |
---|---|---|
committer | Ralf Jung <post@ralfj.de> | 2019-06-10 17:29:24 +0200 |
commit | b7476d1c86053181cb38aa3acd3fc718fde55979 (patch) | |
tree | 9aa2c07ef0d77f857d5cbcfeacd19abeaa064840 /Mailman/Utils.py | |
parent | 56188e427f80ed350b6608ce47124402c90b9d40 (diff) | |
download | mailman2-b7476d1c86053181cb38aa3acd3fc718fde55979.tar.gz mailman2-b7476d1c86053181cb38aa3acd3fc718fde55979.tar.xz mailman2-b7476d1c86053181cb38aa3acd3fc718fde55979.zip |
implement a simple CAPTCHA scheme based on questions and answers configured by the site admin
Diffstat (limited to 'Mailman/Utils.py')
-rw-r--r-- | Mailman/Utils.py | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/Mailman/Utils.py b/Mailman/Utils.py index 10629fc4..9ab35a1c 100644 --- a/Mailman/Utils.py +++ b/Mailman/Utils.py @@ -1576,3 +1576,32 @@ def banned_domain(email): if not re.search(r'127\.0\.1\.255$', text, re.MULTILINE): return True return False + + +def captcha_display(mlist, lang, captchas): + """Returns a CAPTCHA question, the HTML for the answer box, and + the data to be put into the CSRF token""" + if not lang in captchas: + lang = 'en' + captchas = captchas[lang] + idx = random.randrange(len(captchas)) + question = captchas[idx][0] + box_html = mlist.FormatBox('captcha_answer', size=30) + # Remember to encode the language in the index so that we can get it out again! + return (websafe(question), box_html, lang + "-" + str(idx)) + +def captcha_verify(idx, given_answer, captchas): + try: + (lang, idx) = idx.split("-") + idx = int(idx) + except ValueError: + return False + if not lang in captchas: + return False + captchas = captchas[lang] + if not idx in range(len(captchas)): + return False + # Check the given answer. + # We append a `$` to emulate `re.fullmatch`. + correct_answer_pattern = captchas[idx][1] + "$" + return re.match(correct_answer_pattern, given_answer) |