forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
requestheader.go
59 lines (49 loc) · 1.27 KB
/
requestheader.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
package headerrequest
import (
"net/http"
"strings"
"github.com/golang/glog"
"github.com/GoogleCloudPlatform/kubernetes/pkg/auth/user"
authapi "github.com/openshift/origin/pkg/auth/api"
)
type Config struct {
// UserNameHeaders lists the headers to check (in order, case-insensitively) for a username. The first header with a value wins.
UserNameHeaders []string
}
func NewDefaultConfig() *Config {
return &Config{
UserNameHeaders: []string{"X-Remote-User"},
}
}
type Authenticator struct {
config *Config
mapper authapi.UserIdentityMapper
}
func NewAuthenticator(config *Config, mapper authapi.UserIdentityMapper) *Authenticator {
return &Authenticator{config, mapper}
}
func (a *Authenticator) AuthenticateRequest(req *http.Request) (user.Info, bool, error) {
username := ""
for _, header := range a.config.UserNameHeaders {
header = strings.TrimSpace(header)
if len(header) == 0 {
continue
}
username = req.Header.Get(header)
if len(username) != 0 {
break
}
}
if len(username) == 0 {
return nil, false, nil
}
identity := &authapi.DefaultUserIdentityInfo{
UserName: username,
}
user, err := a.mapper.UserFor(identity)
if err != nil {
return nil, false, err
}
glog.V(4).Infof("Got userIdentityMapping: %#v", user)
return user, true, nil
}