valohai / django-allauth-2fa

Two-factor authentication for Django Allauth
Other
212 stars 50 forks source link

Potential bugfix for AttributeError: 'PasswordResetFromKeyView' object has no attribute 'redirect_field_name' #178

Closed pbadeer closed 1 week ago

pbadeer commented 1 year ago

Getting this error when submitting the change password form after opening a key-based password reset link.

AttributeError: 'PasswordResetFromKeyView' object has no attribute 'redirect_field_name'

I'm using an Adapter that combines the OTPAdapter from this library and InvitationsAdapter from django-invitations, so this issue may or may not be present for others, but the change adds a simple safety check.

Full stack trace:

127.0.0.1 - - [17/Jul/2023 11:50:26] "POST /accounts/password/reset/key/1-set-password/ HTTP/1.1" 500 -
Traceback (most recent call last):
  File "\lib\site-packages\django\contrib\staticfiles\handlers.py", line 76, in __call__
    return self.application(environ, start_response)
  File "\lib\site-packages\django\core\handlers\wsgi.py", line 133, in __call__
    response = self.get_response(request)
  File "\lib\site-packages\django\core\handlers\base.py", line 130, in get_response
    response = self._middleware_chain(request)
  File "\lib\site-packages\django\core\handlers\exception.py", line 49, in inner
    response = response_for_exception(request, exc)
  File "\lib\site-packages\django\core\handlers\exception.py", line 114, in response_for_exception
    response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
  File "\lib\site-packages\django\core\handlers\exception.py", line 149, in handle_uncaught_exception
    return debug.technical_500_response(request, *exc_info)
  File "\lib\site-packages\django_extensions\management\technical_response.py", line 40, in null_technical_500_response
    raise exc_value.with_traceback(tb)
  File "\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "\lib\site-packages\django\views\generic\base.py", line 70, in view
    return self.dispatch(request, *args, **kwargs)
  File "\lib\site-packages\django\utils\decorators.py", line 43, in _wrapper
    return bound_method(*args, **kwargs)
  File "\lib\site-packages\allauth\decorators.py", line 20, in wrap
    resp = function(request, *args, **kwargs)
  File "\lib\site-packages\allauth\account\views.py", line 763, in dispatch
    return super(PasswordResetFromKeyView, self).dispatch(
  File "\lib\site-packages\django\views\generic\base.py", line 98, in dispatch
    return handler(request, *args, **kwargs)
  File "\lib\site-packages\allauth\account\views.py", line 105, in post
    response = self.form_valid(form)
  File "\lib\site-packages\allauth\account\views.py", line 822, in form_valid
    return perform_login(
  File "\lib\site-packages\allauth\account\utils.py", line 168, in perform_login
    response = adapter.pre_login(request, user, **hook_kwargs)
  File "\src\django-allauth-2fa\allauth_2fa\adapter.py", line 33, in pre_login
    redirect_url = self.get_2fa_authenticate_url(request)
  File "\src\django-allauth-2fa\allauth_2fa\adapter.py", line 53, in get_2fa_authenticate_url
    query_params[view.redirect_field_name] = success_url
AttributeError: 'PasswordResetFromKeyView' object has no attribute 'redirect_field_name'
codecov-commenter commented 1 year ago

Codecov Report

Merging #178 (13a2177) into main (cdc1602) will increase coverage by 0.10%. The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main     #178      +/-   ##
==========================================
+ Coverage   92.95%   93.06%   +0.10%     
==========================================
  Files          15       15              
  Lines         568      577       +9     
==========================================
+ Hits          528      537       +9     
  Misses         40       40              
Impacted Files Coverage Δ
allauth_2fa/adapter.py 82.14% <100.00%> (ø)
tests/test_allauth_2fa.py 100.00% <100.00%> (ø)

:mega: We’re building smart automated test selection to slash your CI/CD build times. Learn more

valberg commented 1 year ago

Hi @pbadeer!

Thank you!

Can you maybe write a test case which can prove that this fixes your bug?

pbadeer commented 1 year ago

I can certainly try! I'm not well versed on pytest and I've never used Hatch so I'm struggling a bit, but I'll throw some time at it.

pbadeer commented 1 year ago

BTW I got 34 test failures (18 passes) with a new test environment due to:

FAILED tests/test_allauth_2fa.py::test_require_2fa_middleware[tests.adapter.CustomAdapter-True] - django.template.library.InvalidTemplateLibrary: Invalid template library specified. ImportError raised when trying to load 'django_extensions.templatetags...

When I changed the requirements-dev.txt line: django-extensions==2.2.6 and removed the pin to look like this: django-extensions all tests passed.

One of the fuller stack traces, they're all the same error:

\\lib\\site-packages\\django_extensions\\templatetags\\__init__.py'>

    def get_package_libraries(pkg):
        """
        Recursively yield template tag libraries defined in submodules of a
        package.
        """
        for entry in walk_packages(pkg.__path__, pkg.__name__ + "."):
            try:
                module = import_module(entry[1])
            except ImportError as e:
>               raise InvalidTemplateLibrary(
                    "Invalid template library specified. ImportError raised when "
                    "trying to load '%s': %s" % (entry[1], e)
                ) from e
E               django.template.library.InvalidTemplateLibrary: Invalid template library specified. ImportError raised when trying to load 'django_extensions.templatetags.widont': cannot import name 'force_text' from 'django.utils.encoding' (\lib\site-packages\django\utils\encoding.py)

\lib\site-packages\django\template\backends\django.py:130: InvalidTemplateLibrary
pbadeer commented 1 year ago

@valberg Test case added. Let me know if I screwed anything up, apologies in advance!

pbadeer commented 1 year ago

pre-commit.ci autofix

pbadeer commented 1 year ago

pre-commit.ci autofix

uberfresh commented 1 year ago

This PR would be great. I have the same problem and am waiting for a fix :sweat_smile:.

pescheck-bram commented 1 year ago

This PR would be great. I have the same problem and am waiting for a fix 😅.

Same here, we are waiting for this to push our code to production.

violuke commented 1 year ago

Ditto the above, I can confirm this fix has worked for us, so a merge and release would be great 👍 Thank you

pbadeer commented 10 months ago

pre-commit.ci autofix