From 34c1b3b68b43e02118d467b154872ad94c0e081a Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Mon, 15 Jun 2020 18:37:59 +0200 Subject: [PATCH] stages/password: Improve support for password managers --- passbook/core/templates/partials/form.html | 4 ++++ passbook/stages/password/forms.py | 3 +++ passbook/stages/password/stage.py | 13 ++++++++++++- 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/passbook/core/templates/partials/form.html b/passbook/core/templates/partials/form.html index efd249d08..c954ecb18 100644 --- a/passbook/core/templates/partials/form.html +++ b/passbook/core/templates/partials/form.html @@ -10,6 +10,9 @@ {% endif %} {% for field in form %} +{% if field.field.widget|fieldtype == 'HiddenInput' %} + {{ field }} +{% else %}
{% if field.field.widget|fieldtype == 'RadioSelect' %}
+{% endif %} {% endfor %} diff --git a/passbook/stages/password/forms.py b/passbook/stages/password/forms.py index 73cc76b68..41d2bbe7b 100644 --- a/passbook/stages/password/forms.py +++ b/passbook/stages/password/forms.py @@ -23,6 +23,9 @@ def get_authentication_backends(): class PasswordForm(forms.Form): """Password authentication form""" + username = forms.CharField( + widget=forms.HiddenInput(attrs={"autocomplete": "username"}), required=False + ) password = forms.CharField( widget=forms.PasswordInput( attrs={ diff --git a/passbook/stages/password/stage.py b/passbook/stages/password/stage.py index 41abac5f8..8dd708f25 100644 --- a/passbook/stages/password/stage.py +++ b/passbook/stages/password/stage.py @@ -52,9 +52,20 @@ class PasswordStage(FormView, StageView): form_class = PasswordForm template_name = "stages/password/backend.html" + def get_form(self, form_class=None) -> PasswordForm: + form = super().get_form(form_class=form_class) + + # If there's a pending user, update the `username` field + # this field is only used by password managers. + # If there's no user set, an error is raised later. + if PLAN_CONTEXT_PENDING_USER in self.executor.plan.context: + pending_user: User = self.executor.plan.context[PLAN_CONTEXT_PENDING_USER] + form.fields["username"].initial = pending_user.username + + return form + def get_context_data(self, **kwargs): kwargs = super().get_context_data(**kwargs) - kwargs["primary_action"] = _("Log in") recovery_flow = Flow.objects.filter(designation=FlowDesignation.RECOVERY) if recovery_flow.exists(): kwargs["recovery_flow"] = recovery_flow.first()