-
-
Notifications
You must be signed in to change notification settings - Fork 931
/
strategy.go
347 lines (293 loc) · 11 KB
/
strategy.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package profile
import (
"context"
"encoding/json"
"net/http"
"net/url"
"github.com/gofrs/uuid"
"github.com/julienschmidt/httprouter"
"github.com/markbates/pkger"
"github.com/pkg/errors"
"github.com/tidwall/sjson"
"github.com/ory/herodot"
"github.com/ory/jsonschema/v3"
"github.com/ory/x/decoderx"
"github.com/ory/x/urlx"
"github.com/ory/kratos/continuity"
"github.com/ory/kratos/driver/configuration"
"github.com/ory/kratos/identity"
"github.com/ory/kratos/schema"
"github.com/ory/kratos/selfservice/errorx"
"github.com/ory/kratos/selfservice/flow"
"github.com/ory/kratos/selfservice/flow/settings"
"github.com/ory/kratos/selfservice/form"
"github.com/ory/kratos/session"
"github.com/ory/kratos/x"
)
const (
RouteSettings = "/self-service/settings/methods/profile"
)
var _ settings.Strategy = new(Strategy)
type (
strategyDependencies interface {
x.CSRFProvider
x.CSRFTokenGeneratorProvider
x.WriterProvider
x.LoggingProvider
continuity.ManagementProvider
session.HandlerProvider
session.ManagementProvider
identity.ValidationProvider
identity.ManagementProvider
identity.PrivilegedPoolProvider
errorx.ManagementProvider
settings.HookExecutorProvider
settings.ErrorHandlerProvider
settings.FlowPersistenceProvider
settings.StrategyProvider
settings.HooksProvider
IdentityTraitsSchemas() schema.Schemas
}
Strategy struct {
c configuration.Provider
d strategyDependencies
dc *decoderx.HTTP
}
)
// swagger:model settingsProfileFormConfig
type SettingsProfileRequestMethod struct {
*form.HTMLForm
}
func NewStrategy(d strategyDependencies, c configuration.Provider) *Strategy {
return &Strategy{c: c, d: d, dc: decoderx.NewHTTP()}
}
func (s *Strategy) SettingsStrategyID() string {
return settings.StrategyProfile
}
func (s *Strategy) RegisterSettingsRoutes(public *x.RouterPublic) {
s.d.CSRFHandler().ExemptPath(RouteSettings)
public.POST(RouteSettings, s.d.SessionHandler().IsAuthenticated(s.handleSubmit, settings.OnUnauthenticated(s.c, s.d)))
public.GET(RouteSettings, s.d.SessionHandler().IsAuthenticated(s.handleSubmit, settings.OnUnauthenticated(s.c, s.d)))
}
func (s *Strategy) PopulateSettingsMethod(r *http.Request, id *identity.Identity, pr *settings.Flow) error {
traitsSchema, err := s.c.IdentityTraitsSchemas().FindSchemaByID(id.SchemaID)
if err != nil {
return err
}
// use a schema compiler that disables identifiers
schemaCompiler := jsonschema.NewCompiler()
f, err := form.NewHTMLFormFromJSONSchema(urlx.CopyWithQuery(
urlx.AppendPaths(s.c.SelfPublicURL(), RouteSettings),
url.Values{"flow": {pr.ID.String()}},
).String(), traitsSchema.URL, "", schemaCompiler)
if err != nil {
return err
}
f.SetValuesFromJSON(json.RawMessage(id.Traits), "traits")
f.SetCSRF(s.d.GenerateCSRFToken(r))
if err := f.SortFields(traitsSchema.URL); err != nil {
return err
}
pr.Methods[s.SettingsStrategyID()] = &settings.FlowMethod{
Method: s.SettingsStrategyID(),
Config: &settings.FlowMethodConfig{FlowMethodConfigurator: &SettingsProfileRequestMethod{HTMLForm: f}},
}
return nil
}
// nolint:deadcode,unused
// swagger:parameters completeSelfServiceSettingsFlowWithProfileMethod
type completeSelfServiceSettingsFlowWithProfileMethodParameters struct {
// Flow is flow ID.
//
// in: query
Flow string `json:"flow"`
// in: body
Payload interface{}
}
// swagger:route POST /self-service/settings/methods/profile public completeSelfServiceSettingsFlowWithProfileMethod
//
// Complete Settings Flow with Profile Method
//
// Use this endpoint to complete a settings flow by sending an identity's updated traits. This endpoint
// behaves differently for API and browser flows.
//
// API-initiated flows expect `application/json` to be sent in the body and respond with
// - HTTP 200 and an application/json body with the session token on success;
// - HTTP 302 redirect to a fresh settings flow if the original flow expired with the appropriate error messages set;
// - HTTP 400 on form validation errors.
// - HTTP 401 when the endpoint is called without a valid session token.
// - HTTP 403 when `selfservice.flows.settings.privileged_session_max_age` was reached and a sensitive field was
// updated (e.g. recovery email). Implies that the user needs to re-authenticate.
//
// Browser flows expect `application/x-www-form-urlencoded` to be sent in the body and responds with
// - a HTTP 302 redirect to the post/after settings URL or the `return_to` value if it was set and if the flow succeeded;
// - a HTTP 302 redirect to the settings UI URL with the flow ID containing the validation errors otherwise.
// - a HTTP 302 redirect to the login endpoint when `selfservice.flows.settings.privileged_session_max_age` was reached.
//
// More information can be found at [ORY Kratos User Settings & Profile Management Documentation](../self-service/flows/user-settings).
//
// Consumes:
// - application/json
// - application/x-www-form-urlencoded
//
// Produces:
// - application/json
//
// Security:
// sessionToken:
//
// Schemes: http, https
//
// Responses:
// 200: settingsViaApiResponse
// 200: settingsFlow
// 302: emptyResponse
// 400: settingsFlow
// 401: genericError
// 403: genericError
// 500: genericError
func (s *Strategy) handleSubmit(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
var p CompleteSelfServiceBrowserSettingsProfileStrategyFlow
ctxUpdate, err := settings.PrepareUpdate(s.d, w, r, settings.ContinuityKey(s.SettingsStrategyID()), &p)
if errors.Is(err, settings.ErrContinuePreviousAction) {
s.continueFlow(w, r, ctxUpdate, &p)
return
} else if err != nil {
s.handleSettingsError(w, r, ctxUpdate, nil, &p, err)
return
}
option, err := s.newSettingsProfileDecoder(ctxUpdate.Session.Identity)
if err != nil {
s.handleSettingsError(w, r, ctxUpdate, nil, &p, err)
return
}
if err := s.dc.Decode(r, &p, option,
decoderx.HTTPDecoderSetValidatePayloads(false),
decoderx.HTTPDecoderJSONFollowsFormFormat(),
); err != nil {
s.handleSettingsError(w, r, ctxUpdate, nil, &p, err)
return
}
// Reset after decoding form
p.SetFlowID(ctxUpdate.Flow.ID)
s.continueFlow(w, r, ctxUpdate, &p)
}
func (s *Strategy) continueFlow(w http.ResponseWriter, r *http.Request, ctxUpdate *settings.UpdateContext, p *CompleteSelfServiceBrowserSettingsProfileStrategyFlow) {
if err := flow.VerifyRequest(r, ctxUpdate.Flow.Type, s.d.GenerateCSRFToken, p.CSRFToken); err != nil {
s.handleSettingsError(w, r, ctxUpdate, nil, p, err)
return
}
if len(p.Traits) == 0 {
s.handleSettingsError(w, r, ctxUpdate, nil, p, errors.WithStack(herodot.ErrBadRequest.WithReasonf("Did not receive any value changes.")))
return
}
if err := s.hydrateForm(r, ctxUpdate.Flow, ctxUpdate.Session, p.Traits); err != nil {
s.d.SettingsFlowErrorHandler().WriteFlowError(w, r, settings.StrategyProfile, ctxUpdate.Flow, ctxUpdate.Session.Identity, err)
return
}
update, err := s.d.PrivilegedIdentityPool().GetIdentityConfidential(context.Background(), ctxUpdate.Session.Identity.ID)
if err != nil {
s.d.SettingsFlowErrorHandler().WriteFlowError(w, r, settings.StrategyProfile, ctxUpdate.Flow, ctxUpdate.Session.Identity, err)
return
}
update.Traits = identity.Traits(p.Traits)
if err := s.d.SettingsHookExecutor().PostSettingsHook(w, r,
settings.StrategyProfile, ctxUpdate, update); err != nil {
s.handleSettingsError(w, r, ctxUpdate, p.Traits, p, err)
return
}
}
// Complete profile update payload
//
// swagger:parameters completeSelfServiceBrowserSettingsProfileStrategyFlow
// nolint:deadcode,unused
type completeSelfServiceBrowserSettingsProfileStrategyFlowParameters struct {
// Request is the request ID.
//
// required: true
// in: query
// format: uuid
Flow string `json:"flow"`
// in: body
// required: true
Body CompleteSelfServiceBrowserSettingsProfileStrategyFlow
}
// nolint:deadcode,unused
type CompleteSelfServiceBrowserSettingsProfileStrategyFlow struct {
// Traits contains all of the identity's traits.
Traits json.RawMessage `json:"traits"`
// FlowIDRequestID is the flow ID.
//
// swagger:ignore
FlowID string `json:"flow"`
// The Anti-CSRF Token
//
// This token is only required when performing browser flows.
CSRFToken string `json:"csrf_token"`
}
func (p *CompleteSelfServiceBrowserSettingsProfileStrategyFlow) GetFlowID() uuid.UUID {
return x.ParseUUID(p.FlowID)
}
func (p *CompleteSelfServiceBrowserSettingsProfileStrategyFlow) SetFlowID(rid uuid.UUID) {
p.FlowID = rid.String()
}
func (s *Strategy) hydrateForm(r *http.Request, ar *settings.Flow, ss *session.Session, traits json.RawMessage) error {
action := urlx.CopyWithQuery(urlx.AppendPaths(s.c.SelfPublicURL(), RouteSettings),
url.Values{"flow": {ar.ID.String()}})
ar.Methods[settings.StrategyProfile].Config.Reset()
if traits != nil {
for _, field := range form.NewHTMLFormFromJSON(action.String(), traits, "traits").Fields {
ar.Methods[settings.StrategyProfile].Config.SetField(field)
}
}
ar.Methods[settings.StrategyProfile].Config.SetCSRF(s.d.GenerateCSRFToken(r))
traitsSchema, err := s.c.IdentityTraitsSchemas().FindSchemaByID(ss.Identity.SchemaID)
if err != nil {
return err
}
if err = ar.Methods[settings.StrategyProfile].Config.SortFields(traitsSchema.URL); err != nil {
return err
}
return nil
}
// handleSettingsError is a convenience function for handling all types of errors that may occur (e.g. validation error)
// during a settings request.
func (s *Strategy) handleSettingsError(w http.ResponseWriter, r *http.Request, puc *settings.UpdateContext, traits json.RawMessage, p *CompleteSelfServiceBrowserSettingsProfileStrategyFlow, err error) {
if e := new(settings.FlowNeedsReAuth); errors.As(err, &e) {
if err := s.d.ContinuityManager().Pause(r.Context(), w, r,
settings.ContinuityKey(s.SettingsStrategyID()),
settings.ContinuityOptions(p, puc.Session.Identity)...); err != nil {
s.d.SettingsFlowErrorHandler().WriteFlowError(w, r, s.SettingsStrategyID(), puc.Flow, puc.Session.Identity, err)
return
}
}
if puc.Flow != nil {
if traits == nil {
traits = json.RawMessage(puc.GetSessionIdentity().Traits)
}
if err := s.hydrateForm(r, puc.Flow, puc.Session, traits); err != nil {
s.d.SettingsFlowErrorHandler().WriteFlowError(w, r, s.SettingsStrategyID(), puc.Flow, puc.GetSessionIdentity(), err)
return
}
}
s.d.SettingsFlowErrorHandler().WriteFlowError(w, r, s.SettingsStrategyID(), puc.Flow, puc.GetSessionIdentity(), err)
}
// newSettingsProfileDecoder returns a decoderx.HTTPDecoderOption with a JSON Schema for type assertion and
// validation.
func (s *Strategy) newSettingsProfileDecoder(i *identity.Identity) (decoderx.HTTPDecoderOption, error) {
ss, err := s.d.IdentityTraitsSchemas().GetByID(i.SchemaID)
if err != nil {
return nil, err
}
raw, err := sjson.SetBytes(x.MustPkgerRead(pkger.Open(
"/selfservice/strategy/password/.schema/settings.schema.json")),
"properties.traits.$ref", ss.URL.String()+"#/properties/traits")
if err != nil {
return nil, errors.WithStack(err)
}
o, err := decoderx.HTTPRawJSONSchemaCompiler(raw)
if err != nil {
return nil, errors.WithStack(err)
}
return o, nil
}