Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
captcha: Store solutions in g.gencache
Browse files Browse the repository at this point in the history
  • Loading branch information
bsimpson63 committed Jun 20, 2016
1 parent 6f78328 commit 260268d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
1 change: 1 addition & 0 deletions install/setup_mcrouter.sh
Expand Up @@ -72,6 +72,7 @@ if [ ! -f /etc/mcrouter/global.conf ]; then
"reportmessage:": "local-pool",
"modinbox:": "local-pool",
"otp:": "local-pool",
"captcha:": "local-pool",
},
"wildcard": {
"type": "PoolRoute",
Expand Down
26 changes: 14 additions & 12 deletions r2/r2/lib/captcha.py
Expand Up @@ -29,7 +29,6 @@
from Captcha.Base import randomIdentifier
from Captcha.Visual import Text, Backgrounds, Distortions, ImageCaptcha

from r2.lib.cache import make_key

IDEN_LENGTH = 32
SOL_LENGTH = 6
Expand All @@ -54,24 +53,27 @@ def make_solution():
return randomIdentifier(alphabet=string.ascii_letters, length = SOL_LENGTH).upper()

def get_image(iden):
key = make_key(iden)
solution = g.cache.get(key)
key = "captcha:%s" % iden
solution = g.gencache.get(key)
if not solution:
solution = make_solution()
g.cache.set(key, solution, time = 300)
g.gencache.set(key, solution, time=300)
return RandCaptcha(solution=solution).render()


def valid_solution(iden, solution):
key = make_key(iden)
key = "captcha:%s" % iden

if (not iden
or not solution
or len(iden) != IDEN_LENGTH
or len(solution) != SOL_LENGTH
or solution.upper() != g.cache.get(key)):
if (not iden or
not solution or
len(iden) != IDEN_LENGTH or
len(solution) != SOL_LENGTH or
solution.upper() != g.gencache.get(key)):
# the guess was wrong so make a new solution for the next attempt--the
# client will need to refresh the image before guessing again
solution = make_solution()
g.cache.set(key, solution, time = 300)
g.gencache.set(key, solution, time=300)
return False
else:
g.cache.delete(key)
g.gencache.delete(key)
return True

0 comments on commit 260268d

Please sign in to comment.