Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions apps/users/serializers/user_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def generate():
image = ImageCaptcha()
data = image.generate(chars)
captcha = base64.b64encode(data.getbuffer())
captcha_cache.set(f"LOGIN:{chars}", chars, timeout=5 * 60)
captcha_cache.set(f"LOGIN:{chars.lower()}", chars, timeout=5 * 60)
return 'data:image/png;base64,' + captcha.decode()


Expand Down Expand Up @@ -105,7 +105,7 @@ def is_valid(self, *, raise_exception=False):
"""
super().is_valid(raise_exception=True)
captcha = self.data.get('captcha')
captcha_value = captcha_cache.get(f"LOGIN:{captcha}")
captcha_value = captcha_cache.get(f"LOGIN:{captcha.lower()}")
if captcha_value is None:
raise AppApiException(1005, _("Captcha code error or expiration"))
username = self.data.get("username")
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The provided code snippet has a few improvements and clarifications that can be made:

  1. Case Insensitivity Fix: The captcha_cache keys should be consistent to avoid errors related to case sensitivity. Change both instances where keys are formed from chars (line 63) and captcha_value (line 108). Use lowercase for uniformity, as shown:

    captcha_cache.set(f"LOGIN:{captcha.lower()}", chars, timeout=5 * 60)
  2. Code Readability: Consider adding comments at the beginning of your functions to explain what they do briefly, especially if they are large.

  3. Error Handling: Ensure you handle exceptions correctly within the is_valid method to provide meaningful feedback to the user when the captcha code is invalid or expires.

  4. Security Advice: Although not directly relevant to the code itself, it's worth considering that using passwords or captchas in plain text storage could expose sensitive information. In practice, use environment variables or secure vaults to store such credentials rather than hardcoding them in your application.

With these changes, the code will behave more robustly and securely while maintaining its functionality.

Expand Down