-
-
Notifications
You must be signed in to change notification settings - Fork 963
/
manager.go
67 lines (55 loc) · 1.71 KB
/
manager.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
package errorx
import (
"context"
"net/http"
"net/url"
"github.com/ory/x/urlx"
"github.com/ory/kratos/x"
)
type (
managerDependencies interface {
PersistenceProvider
x.LoggingProvider
x.WriterProvider
x.CSRFTokenGeneratorProvider
}
Manager struct {
d managerDependencies
c baseManagerConfiguration
}
ManagementProvider interface {
// SelfServiceErrorManager returns the errorx.Manager.
SelfServiceErrorManager() *Manager
}
baseManagerConfiguration interface {
SelfServiceFlowErrorURL() *url.URL
}
)
func NewManager(d managerDependencies, c baseManagerConfiguration) *Manager {
return &Manager{d: d, c: c}
}
// Create is a simple helper that saves all errors in the store and returns the
// error url, appending the error ID.
func (m *Manager) Create(ctx context.Context, w http.ResponseWriter, r *http.Request, errs ...error) (string, error) {
for _, err := range errs {
m.d.Logger().WithError(err).WithRequest(r).Errorf("An error occurred and is being forwarded to the error user interface.")
}
id, emerr := m.d.SelfServiceErrorPersister().Add(ctx, m.d.GenerateCSRFToken(r), errs...)
if emerr != nil {
return "", emerr
}
q := url.Values{}
q.Set("error", id.String())
return urlx.CopyWithQuery(m.c.SelfServiceFlowErrorURL(), q).String(), nil
}
// Forward is a simple helper that saves all errors in the store and forwards the HTTP Request
// to the error url, appending the error ID.
func (m *Manager) Forward(ctx context.Context, w http.ResponseWriter, r *http.Request, errs ...error) {
to, err := m.Create(ctx, w, r, errs...)
if err != nil {
// Everything failed. Resort to standard error output.
m.d.Writer().WriteError(w, r, err)
return
}
http.Redirect(w, r, to, http.StatusFound)
}