Skip to content

Commit

Permalink
feat: propagate password policy to UI
Browse files Browse the repository at this point in the history
  • Loading branch information
mmeller-wikia committed Feb 26, 2024
1 parent 6a52949 commit 4a95d8e
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 4 deletions.
2 changes: 1 addition & 1 deletion selfservice/strategy/password/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion selfservice/strategy/password/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion selfservice/strategy/password/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion selfservice/strategy/password/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions ui/node/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`

Expand Down

0 comments on commit 4a95d8e

Please sign in to comment.