-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.go
76 lines (72 loc) · 2.27 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
package jwt
import (
"context"
"encoding/json"
"net/http"
"github.com/cristalhq/jwt/v3"
_claims "github.com/factorysh/microdensity/claims"
"github.com/factorysh/microdensity/httpcontext"
"github.com/getsentry/sentry-go"
"go.uber.org/zap"
)
// Auth will ensure JWT token is valid
func (j *JWTAuthenticator) Handler(isBlocking bool) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
l := j.logger.With(zap.String("url", r.URL.String()))
token, err := getJWTToken(r)
if err != nil {
l.Warn("cant't read JWT token", zap.Error(err))
w.WriteHeader(http.StatusBadRequest)
return
}
if token == nil && isBlocking {
l.Warn("jwt token not found, middleware blocking")
w.WriteHeader(http.StatusUnauthorized)
return
}
if token != nil {
l = l.With(zap.String("token header", string(token.RawHeader())),
zap.String("token claims", string(token.RawClaims())))
if hub := sentry.GetHubFromContext(r.Context()); hub != nil {
hub.WithScope(func(scope *sentry.Scope) {
scope.SetExtra("jwt", token.RawClaims())
})
}
err = j.VerifySignature(token)
if err != nil {
l.Warn("rotten token", zap.Error(err))
w.WriteHeader(http.StatusBadRequest)
return
}
l = l.With(zap.ByteString("claims", token.RawClaims()))
r = r.WithContext(context.WithValue(r.Context(), httpcontext.JWT, token))
var claims _claims.Claims
err = json.Unmarshal(token.RawClaims(), &claims)
if err != nil {
l.Warn("Can't read claims JSON",
zap.Error(err))
w.WriteHeader(http.StatusBadRequest)
return
}
r = r.WithContext(claims.ToCtx(r.Context()))
r = r.WithContext(context.WithValue(r.Context(), httpcontext.User, claims.UserLogin))
r = r.WithContext(context.WithValue(r.Context(), httpcontext.RequestedProject, claims.ProjectPath))
r = r.WithContext(claims.ToCtx(r.Context()))
} else {
l.Info("No JWT token, but it's not blocking")
}
next.ServeHTTP(w, r)
})
}
}
func getJWTToken(r *http.Request) (*jwt.Token, error) {
p := r.Header.Get("PRIVATE-TOKEN")
if p == "" {
p = r.URL.Query().Get("private_token")
}
if p == "" {
return nil, nil
}
return jwt.ParseString(p)
}