django-recaptcha3
django-recaptcha3 copied to clipboard
Don't require RECAPTCHA_PRIVATE_KEY to be set if RECAPTCHA_DISABLE is set
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'
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?
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.
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.
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.
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?
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!