-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.go
110 lines (90 loc) · 2.92 KB
/
middleware.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
package server
import (
"context"
"errors"
"fmt"
"net/http"
"github.com/davecgh/go-spew/spew"
"github.com/sirupsen/logrus"
"github.com/tomasen/realip"
"github.com/vliubezny/gstore/internal/auth"
)
const (
headerContentType = "Content-Type"
contentTypeJSON = "application/json"
)
type loggerKey struct{}
type claimsKey struct{}
// setContentTypeMiddleware sets default content type.
func setContentTypeMiddleware(contentType string) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set(headerContentType, contentTypeJSON)
next.ServeHTTP(w, r)
})
}
}
// loggerMiddleware populates request context with logger and logs request entry.
func loggerMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
logger := logrus.WithFields(logrus.Fields{
"ip": realip.FromRequest(r),
"agent": r.UserAgent(),
})
ctx := context.WithValue(r.Context(), loggerKey{}, logger)
logger.Debugf("%s %s", r.Method, r.RequestURI)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
// recoveryMiddleware recovers after panic.
func recoveryMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
if e := recover(); e != nil {
writeInternalError(getLogger(r), w, fmt.Sprintf("recover from panic: %s\n", spew.Sdump(e)))
}
}()
next.ServeHTTP(w, r)
})
}
// jwtAuthMiddleware authenticates user with JWT.
func jwtAuthMiddleware(accessTokenValidator auth.AccessTokenValidator) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
l := getLogger(r)
token := extractBearer(r)
if token == "" {
writeError(l, w, http.StatusUnauthorized, "missing token")
return
}
claims, err := accessTokenValidator(token)
if err != nil {
if errors.Is(err, auth.ErrInvalidToken) {
writeError(l.WithError(err), w, http.StatusUnauthorized, "invalid access token")
return
}
writeInternalError(l.WithError(err), w, "failed to validate access token")
return
}
ctx := context.WithValue(r.Context(), claimsKey{}, claims)
ctx = context.WithValue(ctx, loggerKey{}, l.WithField("userID", claims.UserID))
next.ServeHTTP(w, r.WithContext(ctx))
})
}
}
// allowAdminMiddleware authorizes admin to access resource.
func allowAdminMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
l := getLogger(r)
claims, ok := r.Context().Value(claimsKey{}).(auth.AccessTokenClaims)
if !ok {
writeError(l, w, http.StatusUnauthorized, "authentication required")
return
}
if !claims.IsAdmin {
writeError(l, w, http.StatusForbidden, "access not allowed")
return
}
next.ServeHTTP(w, r)
})
}