-
-
Notifications
You must be signed in to change notification settings - Fork 931
/
hook.go
205 lines (176 loc) · 6.48 KB
/
hook.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
package settings
import (
"fmt"
"net/http"
"time"
"github.com/pkg/errors"
"github.com/ory/kratos/driver/configuration"
"github.com/ory/kratos/identity"
"github.com/ory/kratos/selfservice/flow"
"github.com/ory/kratos/x"
)
type (
PostHookPrePersistExecutor interface {
ExecuteSettingsPrePersistHook(w http.ResponseWriter, r *http.Request, a *Flow, s *identity.Identity) error
}
PostHookPrePersistExecutorFunc func(w http.ResponseWriter, r *http.Request, a *Flow, s *identity.Identity) error
PostHookPostPersistExecutor interface {
ExecuteSettingsPostPersistHook(w http.ResponseWriter, r *http.Request, a *Flow, s *identity.Identity) error
}
PostHookPostPersistExecutorFunc func(w http.ResponseWriter, r *http.Request, a *Flow, s *identity.Identity) error
HooksProvider interface {
PostSettingsPrePersistHooks(settingsType string) []PostHookPrePersistExecutor
PostSettingsPostPersistHooks(settingsType string) []PostHookPostPersistExecutor
}
)
func (f PostHookPrePersistExecutorFunc) ExecuteSettingsPrePersistHook(w http.ResponseWriter, r *http.Request, a *Flow, s *identity.Identity) error {
return f(w, r, a, s)
}
func (f PostHookPostPersistExecutorFunc) ExecuteSettingsPostPersistHook(w http.ResponseWriter, r *http.Request, a *Flow, s *identity.Identity) error {
return f(w, r, a, s)
}
type (
executorDependencies interface {
identity.ManagementProvider
identity.ValidationProvider
HooksProvider
x.LoggingProvider
FlowPersistenceProvider
x.WriterProvider
}
HookExecutor struct {
d executorDependencies
c configuration.Provider
}
HookExecutorProvider interface {
SettingsHookExecutor() *HookExecutor
}
)
func PostHookPostPersistExecutorNames(e []PostHookPostPersistExecutor) []string {
names := make([]string, len(e))
for k, ee := range e {
names[k] = fmt.Sprintf("%T", ee)
}
return names
}
func NewHookExecutor(
d executorDependencies,
c configuration.Provider,
) *HookExecutor {
return &HookExecutor{
d: d,
c: c,
}
}
type PostSettingsHookOption func(o *postSettingsHookOptions)
type postSettingsHookOptions struct {
cb func(ctxUpdate *UpdateContext) error
}
func WithCallback(cb func(ctxUpdate *UpdateContext) error) func(o *postSettingsHookOptions) {
return func(o *postSettingsHookOptions) {
o.cb = cb
}
}
func (e *HookExecutor) PostSettingsHook(w http.ResponseWriter, r *http.Request, settingsType string, ctxUpdate *UpdateContext, i *identity.Identity, opts ...PostSettingsHookOption) error {
e.d.Logger().
WithRequest(r).
WithField("identity_id", i.ID).
WithField("flow_method", settingsType).
Debug("Running PostSettingsPrePersistHooks.")
config := new(postSettingsHookOptions)
for _, f := range opts {
f(config)
}
for k, executor := range e.d.PostSettingsPrePersistHooks(settingsType) {
if err := executor.ExecuteSettingsPrePersistHook(w, r, ctxUpdate.Flow, i); err != nil {
if errors.Is(err, ErrHookAbortRequest) {
e.d.Logger().
WithRequest(r).
WithField("executor", fmt.Sprintf("%T", executor)).
WithField("executor_position", k).
WithField("executors", PostHookPostPersistExecutorNames(e.d.PostSettingsPostPersistHooks(settingsType))).
WithField("identity_id", i.ID).
WithField("flow_method", settingsType).
Debug("A ExecuteSettingsPrePersistHook hook aborted early.")
return nil
}
return err
}
e.d.Logger().WithRequest(r).
WithField("executor", fmt.Sprintf("%T", executor)).
WithField("executor_position", k).
WithField("executors", PostHookPostPersistExecutorNames(e.d.PostSettingsPostPersistHooks(settingsType))).
WithField("identity_id", i.ID).
WithField("flow_method", settingsType).
Debug("ExecuteSettingsPrePersistHook completed successfully.")
}
options := []identity.ManagerOption{identity.ManagerExposeValidationErrorsForInternalTypeAssertion}
ttl := e.c.SelfServiceFlowSettingsPrivilegedSessionMaxAge()
if ctxUpdate.Session.AuthenticatedAt.Add(ttl).After(time.Now()) {
options = append(options, identity.ManagerAllowWriteProtectedTraits)
}
if err := e.d.IdentityManager().Update(r.Context(), i, options...); err != nil {
if errors.Is(err, identity.ErrProtectedFieldModified) {
e.d.Logger().WithError(err).Debug("Modifying protected field requires re-authentication.")
return errors.WithStack(NewFlowNeedsReAuth())
}
return err
}
e.d.Audit().
WithRequest(r).
WithField("identity_id", i.ID).
Debug("An identity's settings have been updated.")
ctxUpdate.Session.Identity = i
ctxUpdate.Flow.State = StateSuccess
if config.cb != nil {
if err := config.cb(ctxUpdate); err != nil {
return err
}
}
if method, ok := ctxUpdate.Flow.Methods[settingsType]; ok {
method.Config.ResetMessages()
}
if err := e.d.SettingsFlowPersister().UpdateSettingsFlow(r.Context(), ctxUpdate.Flow); err != nil {
return err
}
for k, executor := range e.d.PostSettingsPostPersistHooks(settingsType) {
if err := executor.ExecuteSettingsPostPersistHook(w, r, ctxUpdate.Flow, i); err != nil {
if errors.Is(err, ErrHookAbortRequest) {
e.d.Logger().
WithRequest(r).
WithField("executor", fmt.Sprintf("%T", executor)).
WithField("executor_position", k).
WithField("executors", PostHookPostPersistExecutorNames(e.d.PostSettingsPostPersistHooks(settingsType))).
WithField("identity_id", i.ID).
WithField("flow_method", settingsType).
Debug("A ExecuteSettingsPostPersistHook hook aborted early.")
return nil
}
return err
}
e.d.Logger().WithRequest(r).
WithField("executor", fmt.Sprintf("%T", executor)).
WithField("executor_position", k).
WithField("executors", PostHookPostPersistExecutorNames(e.d.PostSettingsPostPersistHooks(settingsType))).
WithField("identity_id", i.ID).
WithField("flow_method", settingsType).
Debug("ExecuteSettingsPostPersistHook completed successfully.")
}
e.d.Logger().
WithRequest(r).
WithField("identity_id", i.ID).
WithField("flow_method", settingsType).
Debug("Completed all PostSettingsPrePersistHooks and PostSettingsPostPersistHooks.")
if ctxUpdate.Flow.Type == flow.TypeAPI {
updatedFlow, err := e.d.SettingsFlowPersister().GetSettingsFlow(r.Context(), ctxUpdate.Flow.ID)
if err != nil {
return err
}
e.d.Writer().Write(w, r, &APIFlowResponse{Flow: updatedFlow, Identity: i})
return nil
}
return x.SecureContentNegotiationRedirection(w, r, ctxUpdate.Session.Declassify(), ctxUpdate.Flow.RequestURL, e.d.Writer(), e.c,
x.SecureRedirectOverrideDefaultReturnTo(
e.c.SelfServiceFlowSettingsReturnTo(settingsType,
ctxUpdate.Flow.AppendTo(e.c.SelfServiceFlowSettingsUI()))))
}