From 4a95d8ea90547d571e10bf434ab3154da1fd8d4f Mon Sep 17 00:00:00 2001 From: mikolajmeller Date: Mon, 26 Feb 2024 09:42:07 +0100 Subject: [PATCH] feat: propagate password policy to UI --- selfservice/strategy/password/login.go | 2 +- selfservice/strategy/password/nodes.go | 5 ++++- selfservice/strategy/password/registration.go | 2 +- selfservice/strategy/password/settings.go | 2 +- ui/node/attributes.go | 6 ++++++ 5 files changed, 13 insertions(+), 4 deletions(-) diff --git a/selfservice/strategy/password/login.go b/selfservice/strategy/password/login.go index a60f202a9cf6..a2eba067a445 100644 --- a/selfservice/strategy/password/login.go +++ b/selfservice/strategy/password/login.go @@ -167,7 +167,7 @@ func (s *Strategy) PopulateLoginMethod(r *http.Request, requestedAAL identity.Au } sr.UI.SetCSRF(s.d.GenerateCSRFToken(r)) - sr.UI.SetNode(NewPasswordNode("password", node.InputAttributeAutocompleteCurrentPassword)) + sr.UI.SetNode(NewPasswordNode("password", node.InputAttributeAutocompleteCurrentPassword, s.d.Config().PasswordPolicyConfig(r.Context()))) sr.UI.GetNodes().Append(node.NewInputField("method", "password", node.PasswordGroup, node.InputAttributeTypeSubmit).WithMetaLabel(text.NewInfoLogin())) return nil diff --git a/selfservice/strategy/password/nodes.go b/selfservice/strategy/password/nodes.go index f45a9a5f3ba4..54c0d2be216f 100644 --- a/selfservice/strategy/password/nodes.go +++ b/selfservice/strategy/password/nodes.go @@ -4,15 +4,18 @@ package password import ( + "github.com/ory/kratos/driver/config" "github.com/ory/kratos/text" "github.com/ory/kratos/ui/node" ) -func NewPasswordNode(name string, autocomplete node.UiNodeInputAttributeAutocomplete) *node.Node { +func NewPasswordNode(name string, autocomplete node.UiNodeInputAttributeAutocomplete, passwordPolicy *config.PasswordPolicy) *node.Node { return node.NewInputField(name, nil, node.PasswordGroup, node.InputAttributeTypePassword, node.WithRequiredInputAttribute, node.WithInputAttributes(func(a *node.InputAttributes) { + a.MinLength = passwordPolicy.MinPasswordLength + a.MaxLength = passwordPolicy.MaxPasswordLength a.Autocomplete = autocomplete })). WithMetaLabel(text.NewInfoNodeInputPassword()) diff --git a/selfservice/strategy/password/registration.go b/selfservice/strategy/password/registration.go index 32b090cf1183..2336db9ba8b8 100644 --- a/selfservice/strategy/password/registration.go +++ b/selfservice/strategy/password/registration.go @@ -188,7 +188,7 @@ func (s *Strategy) PopulateRegistrationMethod(r *http.Request, f *registration.F } f.UI.SetCSRF(s.d.GenerateCSRFToken(r)) - f.UI.Nodes.Upsert(NewPasswordNode("password", node.InputAttributeAutocompleteNewPassword)) + f.UI.Nodes.Upsert(NewPasswordNode("password", node.InputAttributeAutocompleteNewPassword, s.d.Config().PasswordPolicyConfig(r.Context()))) f.UI.Nodes.Append(node.NewInputField("method", "password", node.PasswordGroup, node.InputAttributeTypeSubmit).WithMetaLabel(text.NewInfoRegistration())) return nil diff --git a/selfservice/strategy/password/settings.go b/selfservice/strategy/password/settings.go index f763163d3180..8b84d151ef7b 100644 --- a/selfservice/strategy/password/settings.go +++ b/selfservice/strategy/password/settings.go @@ -169,7 +169,7 @@ func (s *Strategy) continueSettingsFlow( func (s *Strategy) PopulateSettingsMethod(r *http.Request, _ *identity.Identity, f *settings.Flow) error { f.UI.SetCSRF(s.d.GenerateCSRFToken(r)) - f.UI.Nodes.Upsert(NewPasswordNode("password", node.InputAttributeAutocompleteNewPassword).WithMetaLabel(text.NewInfoNodeInputPassword())) + f.UI.Nodes.Upsert(NewPasswordNode("password", node.InputAttributeAutocompleteNewPassword, s.d.Config().PasswordPolicyConfig(r.Context())).WithMetaLabel(text.NewInfoNodeInputPassword())) f.UI.Nodes.Append(node.NewInputField("method", "password", node.PasswordGroup, node.InputAttributeTypeSubmit).WithMetaLabel(text.NewInfoNodeLabelSave())) return nil diff --git a/ui/node/attributes.go b/ui/node/attributes.go index 346e523801c9..dd36e6e675e1 100644 --- a/ui/node/attributes.go +++ b/ui/node/attributes.go @@ -78,6 +78,12 @@ type InputAttributes struct { // The autocomplete attribute for the input. Autocomplete UiNodeInputAttributeAutocomplete `json:"autocomplete,omitempty"` + // The minlength attribute for the input. + MinLength uint `json:"minlength,omitempty"` + + // The maxlength attribute for the input. + MaxLength uint `json:"maxlength,omitempty"` + // The input's label text. Label *text.Message `json:"label,omitempty"`