-
-
Notifications
You must be signed in to change notification settings - Fork 964
/
registration.go
201 lines (161 loc) · 5.96 KB
/
registration.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package password
import (
"context"
"encoding/json"
"net/http"
"github.com/ory/x/otelx"
"github.com/ory/kratos/text"
"github.com/pkg/errors"
"github.com/ory/herodot"
"github.com/ory/kratos/identity"
"github.com/ory/kratos/schema"
"github.com/ory/kratos/selfservice/flow"
"github.com/ory/kratos/selfservice/flow/registration"
"github.com/ory/kratos/ui/container"
"github.com/ory/kratos/ui/node"
"github.com/ory/kratos/x"
"github.com/ory/x/errorsx"
)
// Update Registration Flow with Password Method
//
// swagger:model updateRegistrationFlowWithPasswordMethod
type UpdateRegistrationFlowWithPasswordMethod struct {
// Password to sign the user up with
//
// required: true
Password string `json:"password"`
// The identity's traits
//
// required: true
Traits json.RawMessage `json:"traits"`
// The CSRF Token
CSRFToken string `json:"csrf_token"`
// Method to use
//
// This field must be set to `password` when using the password method.
//
// required: true
Method string `json:"method"`
// Transient data to pass along to any webhooks
//
// required: false
TransientPayload json.RawMessage `json:"transient_payload,omitempty" form:"transient_payload"`
}
func (s *Strategy) RegisterRegistrationRoutes(*x.RouterPublic) {
}
func (s *Strategy) handleRegistrationError(r *http.Request, f *registration.Flow, p UpdateRegistrationFlowWithPasswordMethod, err error) error {
if f != nil {
for _, n := range container.NewFromJSON("", node.ProfileGroup, p.Traits, "traits").Nodes {
// we only set the value and not the whole field because we want to keep types from the initial form generation
f.UI.Nodes.SetValueAttribute(n.ID(), n.Attributes.GetValue())
}
if f.Type == flow.TypeBrowser {
f.UI.SetCSRF(s.d.GenerateCSRFToken(r))
}
}
return err
}
func (s *Strategy) decode(p *UpdateRegistrationFlowWithPasswordMethod, r *http.Request) (err error) {
return registration.DecodeBody(p, r, s.hd, s.d.Config(), registrationSchema)
}
func (s *Strategy) Register(_ http.ResponseWriter, r *http.Request, f *registration.Flow, i *identity.Identity) (err error) {
ctx, span := s.d.Tracer(r.Context()).Tracer().Start(r.Context(), "selfservice.strategy.password.Strategy.Register")
defer otelx.End(span, &err)
if err := flow.MethodEnabledAndAllowedFromRequest(r, f.GetFlowName(), s.ID().String(), s.d); err != nil {
return err
}
var p UpdateRegistrationFlowWithPasswordMethod
if err := s.decode(&p, r); err != nil {
return s.handleRegistrationError(r, f, p, err)
}
f.TransientPayload = p.TransientPayload
if err := flow.EnsureCSRF(s.d, r, f.Type, s.d.Config().DisableAPIFlowEnforcement(ctx), s.d.GenerateCSRFToken, p.CSRFToken); err != nil {
return s.handleRegistrationError(r, f, p, err)
}
if len(p.Password) == 0 {
return s.handleRegistrationError(r, f, p, schema.NewRequiredError("#/password", "password"))
}
if len(p.Traits) == 0 {
p.Traits = json.RawMessage("{}")
}
hpw := make(chan []byte)
errC := make(chan error)
go func() {
defer close(hpw)
defer close(errC)
h, err := s.d.Hasher(ctx).Generate(ctx, []byte(p.Password))
if err != nil {
errC <- err
return
}
hpw <- h
}()
if err != nil {
return s.handleRegistrationError(r, f, p, err)
}
i.Traits = identity.Traits(p.Traits)
// We have to set the credential here, so the identity validator can populate the identifiers.
// The password hash is computed in parallel and set later.
if err := i.SetCredentialsWithConfig(s.ID(), identity.Credentials{Type: s.ID(), Identifiers: []string{}}, json.RawMessage("{}")); err != nil {
return s.handleRegistrationError(r, f, p, err)
}
if err := s.validateCredentials(ctx, i, p.Password); err != nil {
return s.handleRegistrationError(r, f, p, err)
}
select {
case err := <-errC:
return s.handleRegistrationError(r, f, p, err)
case h := <-hpw:
co, err := json.Marshal(&identity.CredentialsPassword{HashedPassword: string(h)})
if err != nil {
return s.handleRegistrationError(r, f, p, errors.WithStack(herodot.ErrInternalServerError.WithReasonf("Unable to encode password options to JSON: %s", err)))
}
i.UpsertCredentialsConfig(s.ID(), co, 0)
}
return nil
}
func (s *Strategy) validateCredentials(ctx context.Context, i *identity.Identity, pw string) (err error) {
ctx, span := s.d.Tracer(ctx).Tracer().Start(ctx, "selfservice.strategy.password.Strategy.validateCredentials")
defer otelx.End(span, &err)
if err := s.d.IdentityValidator().Validate(ctx, i); err != nil {
return err
}
c, ok := i.GetCredentials(identity.CredentialsTypePassword)
if !ok {
// This should never happen
return errors.WithStack(x.PseudoPanic.WithReasonf("identity object did not provide the %s CredentialType unexpectedly", identity.CredentialsTypePassword))
} else if len(c.Identifiers) == 0 {
return schema.NewMissingIdentifierError()
}
for _, id := range c.Identifiers {
if err := s.d.PasswordValidator().Validate(ctx, id, pw); err != nil {
if _, ok := errorsx.Cause(err).(*herodot.DefaultError); ok {
return err
}
if message := new(text.Message); errors.As(err, &message) {
return schema.NewPasswordPolicyViolationError("#/password", message)
}
return schema.NewPasswordPolicyViolationError("#/password", text.NewErrorValidationPasswordPolicyViolationGeneric(err.Error()))
}
}
return nil
}
func (s *Strategy) PopulateRegistrationMethod(r *http.Request, f *registration.Flow) error {
ds, err := s.d.Config().DefaultIdentityTraitsSchemaURL(r.Context())
if err != nil {
return err
}
nodes, err := container.NodesFromJSONSchema(r.Context(), node.PasswordGroup, ds.String(), "", nil)
if err != nil {
return err
}
for _, n := range nodes {
f.UI.SetNode(n)
}
f.UI.SetCSRF(s.d.GenerateCSRFToken(r))
f.UI.Nodes.Upsert(NewPasswordNode("password", node.InputAttributeAutocompleteNewPassword))
f.UI.Nodes.Append(node.NewInputField("method", "password", node.PasswordGroup, node.InputAttributeTypeSubmit).WithMetaLabel(text.NewInfoRegistration()))
return nil
}