/
handlers_auth_service.go
64 lines (50 loc) · 1.47 KB
/
handlers_auth_service.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
package handlers
import (
"net/http"
"github.com/thecodeteam/rexray/libstorage/api/context"
"github.com/thecodeteam/rexray/libstorage/api/server/auth"
"github.com/thecodeteam/rexray/libstorage/api/types"
)
// authSvcHandler is an HTTP filter for validating the JWT.
type authSvcHandler struct {
handler types.APIFunc
}
// NewAuthSvcHandler returns a new authSvcHandler.
func NewAuthSvcHandler() types.Middleware {
return &authSvcHandler{}
}
func (h *authSvcHandler) Name() string {
return "auth-svc-handler"
}
func (h *authSvcHandler) Handler(m types.APIFunc) types.APIFunc {
return (&authSvcHandler{m}).Handle
}
// Handle is the type's Handler function.
func (h *authSvcHandler) Handle(
ctx types.Context,
w http.ResponseWriter,
req *http.Request,
store types.Store) error {
svc, ok := context.Service(ctx)
if !ok {
return types.ErrMissingStorageService
}
if svc.AuthConfig() == nil {
ctx.Debug("skipping service auth handler; empty auth config")
return h.handler(ctx, w, req, store)
}
if len(svc.AuthConfig().Allow) == 0 && len(svc.AuthConfig().Deny) == 0 {
ctx.Debug("skipping svc auth handler; empty allow & deny lists")
return h.handler(ctx, w, req, store)
}
tok, err := auth.ValidateAuthTokenWithCtxOrReq(ctx, svc.AuthConfig(), req)
if err != nil {
return err
}
if tok == nil {
panic("token should never be nil here")
}
ctx.Debug("validated service security token")
return h.handler(
ctx.WithValue(context.AuthTokenKey, tok), w, req, store)
}