commit ba88dbc286c065ff49bd1d585789eaaa6af7690a Author: Philipp Winter phw@nymity.ch Date: Wed Feb 5 15:57:23 2020 -0800
Make getCaptchaImage return (bytes, str). --- bridgedb/captcha.py | 2 +- bridgedb/distributors/https/server.py | 8 ++++---- bridgedb/test/test_captcha.py | 8 ++++---- 3 files changed, 9 insertions(+), 9 deletions(-)
diff --git a/bridgedb/captcha.py b/bridgedb/captcha.py index 23bb333..2bdf6b9 100644 --- a/bridgedb/captcha.py +++ b/bridgedb/captcha.py @@ -373,7 +373,7 @@ class GimpCaptcha(Captcha): encBlob = self.publicKey.encrypt(blob.encode('utf-8')) hmac = crypto.getHMAC(self.hmacKey, encBlob) challenge = urlsafe_b64encode(hmac + encBlob) - return challenge + return challenge.decode("utf-8")
def get(self): """Get a random CAPTCHA from the cache directory. diff --git a/bridgedb/distributors/https/server.py b/bridgedb/distributors/https/server.py index 52a083f..98b31d0 100644 --- a/bridgedb/distributors/https/server.py +++ b/bridgedb/distributors/https/server.py @@ -473,11 +473,11 @@ class CaptchaProtectedResource(CustomErrorHandlingResource, CSPResource):
:rtype: tuple :returns: A 2-tuple of ``(image, challenge)``, where ``image`` is a - binary, JPEG-encoded image, and ``challenge`` is a unique + JPEG-encoded image of type bytes, and ``challenge`` is a unique string. If unable to retrieve a CAPTCHA, returns a tuple - containing two empty strings. + containing (b'', ''). """ - return ('', '') + return (b'', '')
def extractClientSolution(self, request): """Extract the client's CAPTCHA solution from a POST request. @@ -539,7 +539,7 @@ class CaptchaProtectedResource(CustomErrorHandlingResource, CSPResource): lang=langs[0], langOverride=translations.isLangOverridden(request), imgstr=imgstr.decode("utf-8"), - challenge_field=challenge.decode("utf-8")) + challenge_field=challenge) except Exception as err: rendered = replaceErrorPage(request, err, 'captcha.html')
diff --git a/bridgedb/test/test_captcha.py b/bridgedb/test/test_captcha.py index d52440d..b87251e 100644 --- a/bridgedb/test/test_captcha.py +++ b/bridgedb/test/test_captcha.py @@ -78,7 +78,7 @@ class ReCaptchaTests(unittest.TestCase): raise unittest.SkipTest(reason) else: self.assertIsInstance(self.c.image, bytes) - self.assertIsInstance(self.c.challenge, bytes) + self.assertIsInstance(self.c.challenge, str) finally: # Replace the original environment variable if there was one: if oldkey: @@ -146,7 +146,7 @@ class GimpCaptchaTests(unittest.TestCase): c = captcha.GimpCaptcha(self.publik, self.sekrit, self.hmacKey, self.cacheDir) challenge = c.createChallenge('w00t') - self.assertIsInstance(challenge, bytes) + self.assertIsInstance(challenge, str)
def test_createChallenge_base64(self): """createChallenge() return value should be urlsafe base64-encoded.""" @@ -190,7 +190,7 @@ class GimpCaptchaTests(unittest.TestCase): self.cacheDir) image, challenge = c.get() self.assertIsInstance(image, bytes) - self.assertIsInstance(challenge, bytes) + self.assertIsInstance(challenge, str)
def test_get_emptyCacheDir(self): """An empty cacheDir should raise GimpCaptchaError.""" @@ -238,7 +238,7 @@ class GimpCaptchaTests(unittest.TestCase): c = captcha.GimpCaptcha(self.publik, self.sekrit, self.hmacKey, self.cacheDir) image, challenge = c.get() - challengeBadB64 = challenge.decode('utf-8').rstrip('==') + "\x42\x42\x42" + challengeBadB64 = challenge.rstrip('==') + "\x42\x42\x42" self.assertEquals( c.check(challenge, c.answer, c.secretKey, c.hmacKey), True)