forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 1
/
filter.go
46 lines (37 loc) · 1.08 KB
/
filter.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
package requests
import (
"context"
"fmt"
"net/http"
"github.com/rancher/rancher/pkg/auth/util"
"github.com/rancher/types/config"
"github.com/sirupsen/logrus"
)
func NewAuthenticationFilter(ctx context.Context, managementContext *config.ScaledContext, next http.Handler) (http.Handler, error) {
if managementContext == nil {
return nil, fmt.Errorf("Failed to build NewAuthenticationFilter, nil ManagementContext")
}
auth := NewAuthenticator(ctx, managementContext)
return &authHeaderHandler{
auth: auth,
next: next,
}, nil
}
type authHeaderHandler struct {
auth Authenticator
next http.Handler
}
func (h authHeaderHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
authed, user, groups, err := h.auth.Authenticate(req)
if err != nil || !authed {
util.ReturnHTTPError(rw, req, 401, err.Error())
return
}
logrus.Debugf("Impersonating user %v, groups %v", user, groups)
req.Header.Set("Impersonate-User", user)
req.Header.Del("Impersonate-Group")
for _, group := range groups {
req.Header.Add("Impersonate-Group", group)
}
h.next.ServeHTTP(rw, req)
}