Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't require RECAPTCHA_PRIVATE_KEY to be set if RECAPTCHA_DISABLE is set #8

Open
kylegibson opened this issue Jan 31, 2019 · 6 comments
Labels
question Further information is requested

Comments

@kylegibson
Copy link

Currently, RECAPTCHA_PRIVATE_KEY has to be defined, even when RECAPTCHA_DISABLE is set, otherwise this stack trace happens:

  File "/home/vagrant/env/lib/python2.7/site-packages/snowpenguin/django/recaptcha3/fields.py", line 18, in __init__
    self._private_key = kwargs.pop('private_key', settings.RECAPTCHA_PRIVATE_KEY)
  File "/home/vagrant/env/lib/python2.7/site-packages/django/conf/__init__.py", line 57, in __getattr__
    val = getattr(self._wrapped, name)
  File "/home/vagrant/env/lib/python2.7/site-packages/django/conf/__init__.py", line 172, in __getattr__
    return getattr(self.default_settings, name)
AttributeError: 'Settings' object has no attribute 'RECAPTCHA_PRIVATE_KEY'
--------------------- >> end captured logging << ---------------------

This applies to RECAPTCHA_PUBLIC_KEY, but a different stack trace:

  File "/home/vagrant/env/lib/python2.7/site-packages/django/shortcuts.py", line 30, in render
    content = loader.render_to_string(template_name, context, request, using=using)
  File "/home/vagrant/env/lib/python2.7/site-packages/django/template/loader.py", line 68, in render_to_string
    return template.render(context, request)
  File "/home/vagrant/env/lib/python2.7/site-packages/django/template/backends/django.py", line 66, in render
    return self.template.render(context)
  File "/home/vagrant/env/lib/python2.7/site-packages/django/template/base.py", line 207, in render
    return self._render(context)
  File "/home/vagrant/env/lib/python2.7/site-packages/django/test/utils.py", line 107, in instrumented_test_render
    return self.nodelist.render(context)
  File "/home/vagrant/env/lib/python2.7/site-packages/django/template/base.py", line 990, in render
    bit = node.render_annotated(context)
  File "/home/vagrant/env/lib/python2.7/site-packages/django/template/base.py", line 957, in render_annotated
    return self.render(context)
  File "/home/vagrant/env/lib/python2.7/site-packages/django/template/loader_tags.py", line 177, in render
    return compiled_parent._render(context)
  File "/home/vagrant/env/lib/python2.7/site-packages/django/test/utils.py", line 107, in instrumented_test_render
    return self.nodelist.render(context)
  File "/home/vagrant/env/lib/python2.7/site-packages/django/template/base.py", line 990, in render
    bit = node.render_annotated(context)
  File "/home/vagrant/env/lib/python2.7/site-packages/django/template/base.py", line 957, in render_annotated
    return self.render(context)
  File "/home/vagrant/env/lib/python2.7/site-packages/django/template/loader_tags.py", line 72, in render
    result = block.nodelist.render(context)
  File "/home/vagrant/env/lib/python2.7/site-packages/django/template/base.py", line 990, in render
    bit = node.render_annotated(context)
  File "/home/vagrant/env/lib/python2.7/site-packages/django/template/base.py", line 957, in render_annotated
    return self.render(context)
  File "/home/vagrant/env/lib/python2.7/site-packages/django/template/library.py", line 225, in render
    _dict = self.func(*resolved_args, **resolved_kwargs)
  File "/home/vagrant/env/lib/python2.7/site-packages/snowpenguin/django/recaptcha3/templatetags/recaptcha3.py", line 14, in recaptcha_init
    return {'public_key': public_key or settings.RECAPTCHA_PUBLIC_KEY}
  File "/home/vagrant/env/lib/python2.7/site-packages/django/conf/__init__.py", line 57, in __getattr__
    val = getattr(self._wrapped, name)
  File "/home/vagrant/env/lib/python2.7/site-packages/django/conf/__init__.py", line 172, in __getattr__
    return getattr(self.default_settings, name)
AttributeError: 'Settings' object has no attribute 'RECAPTCHA_PUBLIC_KEY'
@kbytesys
Copy link
Owner

The scope of RECAPTCHA_DISABLE is only for testing purpose and RECAPTCHA_PUBLIC_KEY and RECAPTCHA_PRIVATE_KEY are mandatory if you want to use django-recaptcha3.

Why you need to use RECAPTCHA_DISABLE outside automated tests?

@kbytesys kbytesys added the question Further information is requested label Jan 31, 2019
@kylegibson
Copy link
Author

Hello, thank you for the quick response.

I do have RECAPTCHA_DISABLE configured for the test environments. However, the current implementation requires the test environment to have RECAPTCHA_PUBLIC_KEY and RECAPTCHA_PRIVATE_KEY defined, even when RECAPTCHA_DISABLE is set. That does not seem ideal to me, since they aren't being used.

@kbytesys
Copy link
Owner

If you check how the widget is made, also if you can set an empty RECAPTCHA_PUBLIC_KEY you will create more problems than you want to solve, because you will get javascript errors caused by the google recaptcha javascript inclusion.

Change the code to disable the recaptcha at all outside automated tests is too much invasive also because you can always create a set of public/secret keys for your test env from the recaptcha website.

Another solution (but it is invasive I think) is to rework your project in order to add the support for the recaptcha in your forms dynamically based on your env.

Let me know if one of those two alternatives suits your needs.

@kylegibson
Copy link
Author

Hello, thanks again for the quick response.

I am not trying to disable recaptcha outside of the automated test environment.

RECAPTCHA_DISABLE is only defined for my test environments.

Outside of automated tests, recaptcha is working correctly.

@kbytesys
Copy link
Owner

kbytesys commented Jan 31, 2019

If you use RECAPTCHA_DISABLE you can also set dummy values for RECAPTCHA_PUBLIC_KEY and RECAPTCHA_PRIVATE_KEY.

RECAPTCHA_PRIVATE_KEY = 'dummy'
RECAPTCHA_PUBLIC_KEY = 'dummy'
RECAPTCHA_DISABLE = 'True'

It is not enough?

@kylegibson
Copy link
Author

Yes, that does work, and that is what I did before I created this issue. (Although I used None instead of 'dummy', but the effect is the same).

My purpose in creating this issue was to propose changing this behavior: to stop requiring that the keys be defined only when RECAPTCHA_DISABLE is set.

Thank you!

Repository owner deleted a comment from Bishwas-py Mar 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants