forked from thecodeteam/libstorage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers_auth_global.go
61 lines (48 loc) · 1.44 KB
/
handlers_auth_global.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
package handlers
import (
"net/http"
"github.com/codedellemc/libstorage/api/context"
"github.com/codedellemc/libstorage/api/server/auth"
"github.com/codedellemc/libstorage/api/types"
)
// authGlobalHandler is an HTTP filter for validating the JWT.
type authGlobalHandler struct {
handler types.APIFunc
config *types.AuthConfig
}
// NewAuthGlobalHandler returns a new authGlobalHandler.
func NewAuthGlobalHandler(
config *types.AuthConfig) types.Middleware {
return &authGlobalHandler{config: config}
}
func (h *authGlobalHandler) Name() string {
return "auth-global-handler"
}
func (h *authGlobalHandler) Handler(m types.APIFunc) types.APIFunc {
return (&authGlobalHandler{m, h.config}).Handle
}
// Handle is the type's Handler function.
func (h *authGlobalHandler) Handle(
ctx types.Context,
w http.ResponseWriter,
req *http.Request,
store types.Store) error {
if h.config == nil {
ctx.Debug("skipping global auth handler; empty auth config")
return h.handler(ctx, w, req, store)
}
if len(h.config.Allow) == 0 && len(h.config.Deny) == 0 {
ctx.Debug("skipping global auth handler; empty allow & deny lists")
return h.handler(ctx, w, req, store)
}
tok, err := auth.ValidateAuthTokenWithReq(ctx, h.config, req)
if err != nil {
return err
}
if tok == nil {
panic("token should never be nil here")
}
ctx.Debug("validated global security token")
return h.handler(
ctx.WithValue(context.AuthTokenKey, tok), w, req, store)
}