...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package middleware
16
17 import "net/http"
18
19 func newSecureAPI(ctx *Context, next http.Handler) http.Handler {
20 return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
21 route, rCtx, _ := ctx.RouteInfo(r)
22 if rCtx != nil {
23 r = rCtx
24 }
25 if route != nil && !route.NeedsAuth() {
26 next.ServeHTTP(rw, r)
27 return
28 }
29
30 _, rCtx, err := ctx.Authorize(r, route)
31 if err != nil {
32 ctx.Respond(rw, r, route.Produces, route, err)
33 return
34 }
35 r = rCtx
36
37 next.ServeHTTP(rw, r)
38 })
39 }
40
View as plain text