stages/password: Improve support for password managers

This commit is contained in:
Jens Langhammer 2020-06-15 18:37:59 +02:00
parent e3d6ca6ab4
commit 34c1b3b68b
3 changed files with 19 additions and 1 deletions

View file

@ -10,6 +10,9 @@
</div>
{% endif %}
{% for field in form %}
{% if field.field.widget|fieldtype == 'HiddenInput' %}
{{ field }}
{% else %}
<div class="pf-c-form__group {% if field.errors %} has-error {% endif %}">
{% if field.field.widget|fieldtype == 'RadioSelect' %}
<label class="pf-c-form__label" {% if field.field.required %}class="required" {% endif %}
@ -66,4 +69,5 @@
</p>
{% endfor %}
</div>
{% endif %}
{% endfor %}

View file

@ -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={

View file

@ -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()