-
-
Notifications
You must be signed in to change notification settings - Fork 931
/
handler.go
126 lines (107 loc) · 3.2 KB
/
handler.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
package errorx
import (
"encoding/json"
"net/http"
"github.com/julienschmidt/httprouter"
"github.com/justinas/nosurf"
"github.com/pkg/errors"
"github.com/ory/herodot"
"github.com/ory/kratos/x"
)
const ErrorsPath = "/self-service/errors"
var stub500, _ = json.Marshal([]interface{}{herodot.ErrInternalServerError.WithReasonf("This is a stub error.")})
type (
handlerDependencies interface {
x.WriterProvider
PersistenceProvider
}
HandlerProvider interface {
SelfServiceErrorHandler() *Handler
}
Handler struct {
r handlerDependencies
csrf x.CSRFToken
}
)
func NewHandler(
r handlerDependencies,
) *Handler {
return &Handler{r: r, csrf: nosurf.Token}
}
func (h *Handler) WithTokenGenerator(f func(r *http.Request) string) {
h.csrf = f
}
func (h *Handler) RegisterPublicRoutes(public *x.RouterPublic) {
public.GET(ErrorsPath, h.publicFetchError)
}
func (h *Handler) RegisterAdminRoutes(public *x.RouterAdmin) {
public.GET(ErrorsPath, h.adminFetchError)
}
// User-facing error response
//
// swagger:response errorContainer
// nolint:deadcode,unused
type errorContainerResponse struct {
// in: body
Body ErrorContainer `json:"error"`
}
// nolint:deadcode,unused
// swagger:parameters getSelfServiceError
type errorContainerParameters struct {
// in: query
Error string `json:"error"`
}
// swagger:route GET /self-service/errors common public admin getSelfServiceError
//
// Get User-Facing Self-Service Errors
//
// This endpoint returns the error associated with a user-facing self service errors.
//
// When accessing this endpoint through ORY Kratos' Public API, ensure that cookies are set as they are required for CSRF to work. To prevent
// token scanning attacks, the public endpoint does not return 404 status codes.
//
// This endpoint supports stub values to help you implement the error UI:
//
// - `?error=stub:500` - returns a stub 500 (Internal Server Error) error.
//
// More information can be found at [ORY Kratos User User Facing Error Documentation](https://www.ory.sh/docs/kratos/self-service/flows/user-facing-errors).
//
// Produces:
// - application/json
//
// Schemes: http, https
//
// Responses:
// 200: errorContainer
// 403: genericError
// 404: genericError
// 500: genericError
func (h *Handler) publicFetchError(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
if err := h.fetchError(w, r, true); err != nil {
h.r.Writer().WriteError(w, r, x.ErrInvalidCSRFToken.WithTrace(err).WithDebugf("%s", err))
return
}
}
func (h *Handler) adminFetchError(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
if err := h.fetchError(w, r, false); err != nil {
h.r.Writer().WriteError(w, r, err)
return
}
}
func (h *Handler) fetchError(w http.ResponseWriter, r *http.Request, mustVerify bool) error {
id := r.URL.Query().Get("error")
switch id {
case "stub:500":
h.r.Writer().Write(w, r, &ErrorContainer{ID: x.NewUUID(), Errors: stub500})
return nil
}
es, err := h.r.SelfServiceErrorPersister().Read(r.Context(), x.ParseUUID(id))
if err != nil {
return err
}
if mustVerify && !nosurf.VerifyToken(h.csrf(r), es.CSRFToken) {
return errors.WithStack(x.ErrInvalidCSRFToken)
}
h.r.Writer().Write(w, r, es)
return nil
}