-
-
Notifications
You must be signed in to change notification settings - Fork 930
/
settings.go
241 lines (208 loc) · 8.11 KB
/
settings.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
package password
import (
"encoding/json"
"net/http"
"net/url"
"time"
"github.com/gofrs/uuid"
"github.com/julienschmidt/httprouter"
"github.com/markbates/pkger"
"github.com/pkg/errors"
"github.com/ory/herodot"
"github.com/ory/x/decoderx"
"github.com/ory/x/urlx"
"github.com/ory/kratos/identity"
"github.com/ory/kratos/schema"
"github.com/ory/kratos/selfservice/flow"
"github.com/ory/kratos/selfservice/flow/settings"
"github.com/ory/kratos/selfservice/form"
"github.com/ory/kratos/x"
)
const (
RouteSettings = "/self-service/settings/methods/password"
)
func (s *Strategy) RegisterSettingsRoutes(router *x.RouterPublic) {
s.d.CSRFHandler().ExemptPath(RouteSettings)
router.POST(RouteSettings, s.submitSettingsFlow)
router.GET(RouteSettings, s.submitSettingsFlow)
}
func (s *Strategy) SettingsStrategyID() string {
return identity.CredentialsTypePassword.String()
}
// nolint:deadcode,unused
// swagger:parameters completeSelfServiceSettingsFlowWithPasswordMethod
type completeSelfServiceSettingsFlowWithPasswordMethod struct {
// in: body
Body CompleteSelfServiceSettingsFlowWithPasswordMethod
// Flow is flow ID.
//
// in: query
Flow string `json:"flow"`
}
type CompleteSelfServiceSettingsFlowWithPasswordMethod struct {
// Password is the updated password
//
// type: string
// required: true
Password string `json:"password"`
// CSRFToken is the anti-CSRF token
//
// type: string
CSRFToken string `json:"csrf_token"`
// Flow is flow ID.
//
// swagger:ignore
Flow string `json:"flow"`
}
func (p *CompleteSelfServiceSettingsFlowWithPasswordMethod) GetFlowID() uuid.UUID {
return x.ParseUUID(p.Flow)
}
func (p *CompleteSelfServiceSettingsFlowWithPasswordMethod) SetFlowID(rid uuid.UUID) {
p.Flow = rid.String()
}
// swagger:route POST /self-service/settings/methods/password public completeSelfServiceSettingsFlowWithPasswordMethod
//
// Complete Settings Flow with Username/Email Password Method
//
// Use this endpoint to complete a settings flow by sending an identity's updated password. 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.
// 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
// 302: emptyResponse
// 400: settingsFlow
// 401: genericError
// 403: genericError
// 500: genericError
func (s *Strategy) submitSettingsFlow(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
var p CompleteSelfServiceSettingsFlowWithPasswordMethod
ctxUpdate, err := settings.PrepareUpdate(s.d, w, r, settings.ContinuityKey(s.SettingsStrategyID()), &p)
if errors.Is(err, settings.ErrContinuePreviousAction) {
s.continueSettingsFlow(w, r, ctxUpdate, &p)
return
} else if err != nil {
s.handleSettingsError(w, r, ctxUpdate, &p, err)
return
}
if err := s.decodeSettingsFlow(r, &p); err != nil {
s.handleSettingsError(w, r, ctxUpdate, &p, err)
return
}
// This does not come from the payload!
p.Flow = ctxUpdate.Flow.ID.String()
s.continueSettingsFlow(w, r, ctxUpdate, &p)
}
func (s *Strategy) decodeSettingsFlow(r *http.Request, dest interface{}) error {
compiler, err := decoderx.HTTPRawJSONSchemaCompiler(x.MustPkgerRead(pkger.Open("/selfservice/strategy/password/.schema/settings.schema.json")))
if err != nil {
return errors.WithStack(err)
}
return decoderx.NewHTTP().Decode(r, dest, compiler,
decoderx.HTTPDecoderSetValidatePayloads(false),
decoderx.HTTPDecoderJSONFollowsFormFormat(),
)
}
func (s *Strategy) continueSettingsFlow(
w http.ResponseWriter, r *http.Request,
ctxUpdate *settings.UpdateContext, p *CompleteSelfServiceSettingsFlowWithPasswordMethod,
) {
if err := flow.VerifyRequest(r, ctxUpdate.Flow.Type, s.d.GenerateCSRFToken, p.CSRFToken); err != nil {
s.handleSettingsError(w, r, ctxUpdate, p, err)
return
}
if ctxUpdate.Session.AuthenticatedAt.Add(s.c.SelfServiceFlowSettingsPrivilegedSessionMaxAge()).Before(time.Now()) {
s.handleSettingsError(w, r, ctxUpdate, p, errors.WithStack(settings.NewFlowNeedsReAuth()))
return
}
if len(p.Password) == 0 {
s.handleSettingsError(w, r, ctxUpdate, p, schema.NewRequiredError("#/password", "password"))
return
}
hpw, err := s.d.Hasher().Generate([]byte(p.Password))
if err != nil {
s.handleSettingsError(w, r, ctxUpdate, p, err)
return
}
co, err := json.Marshal(&CredentialsConfig{HashedPassword: string(hpw)})
if err != nil {
s.handleSettingsError(w, r, ctxUpdate, p, errors.WithStack(herodot.ErrInternalServerError.WithReasonf("Unable to encode password options to JSON: %s", err)))
return
}
i, err := s.d.PrivilegedIdentityPool().GetIdentityConfidential(r.Context(), ctxUpdate.Session.Identity.ID)
if err != nil {
s.handleSettingsError(w, r, ctxUpdate, p, err)
return
}
c, ok := i.GetCredentials(s.ID())
if !ok {
c = &identity.Credentials{Type: s.ID(),
// We need to insert a random identifier now...
Identifiers: []string{x.NewUUID().String()}}
}
c.Config = co
i.SetCredentials(s.ID(), *c)
if err := s.validateCredentials(i, p.Password); err != nil {
s.handleSettingsError(w, r, ctxUpdate, p, err)
return
}
if err := s.d.SettingsHookExecutor().PostSettingsHook(w, r,
s.SettingsStrategyID(), ctxUpdate, i); err != nil {
s.handleSettingsError(w, r, ctxUpdate, p, err)
return
}
}
func (s *Strategy) PopulateSettingsMethod(r *http.Request, _ *identity.Identity, f *settings.Flow) error {
hf := &form.HTMLForm{Action: urlx.CopyWithQuery(urlx.AppendPaths(s.c.SelfPublicURL(), RouteSettings),
url.Values{"flow": {f.ID.String()}}).String(), Fields: form.Fields{{Name: "password",
Type: "password", Required: true}}, Method: "POST"}
hf.SetCSRF(s.d.GenerateCSRFToken(r))
f.Methods[string(s.ID())] = &settings.FlowMethod{
Method: string(s.ID()),
Config: &settings.FlowMethodConfig{FlowMethodConfigurator: &FlowMethod{HTMLForm: hf}},
}
return nil
}
func (s *Strategy) handleSettingsError(w http.ResponseWriter, r *http.Request, ctxUpdate *settings.UpdateContext, p *CompleteSelfServiceSettingsFlowWithPasswordMethod, err error) {
// Do not pause flow if the flow type is an API flow as we can't save cookies in those flows.
if e := new(settings.FlowNeedsReAuth); errors.As(err, &e) && ctxUpdate.Flow != nil && ctxUpdate.Flow.Type == flow.TypeBrowser {
if err := s.d.ContinuityManager().Pause(r.Context(), w, r,
settings.ContinuityKey(s.SettingsStrategyID()), settings.ContinuityOptions(p, ctxUpdate.Session.Identity)...); err != nil {
s.d.SettingsFlowErrorHandler().WriteFlowError(w, r, s.SettingsStrategyID(), ctxUpdate.Flow, ctxUpdate.Session.Identity, err)
return
}
}
var id *identity.Identity
if ctxUpdate.Flow != nil {
ctxUpdate.Flow.Methods[s.SettingsStrategyID()].Config.Reset()
ctxUpdate.Flow.Methods[s.SettingsStrategyID()].Config.SetCSRF(s.d.GenerateCSRFToken(r))
id = ctxUpdate.Session.Identity
}
s.d.SettingsFlowErrorHandler().WriteFlowError(w, r, s.SettingsStrategyID(), ctxUpdate.Flow, id, err)
}