forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrequestheader.go
67 lines (56 loc) · 2.22 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
60
61
62
63
64
65
66
67
package headerrequest
import (
"net/http"
"strings"
"k8s.io/apiserver/pkg/authentication/user"
authapi "github.com/openshift/origin/pkg/oauthserver/api"
"github.com/openshift/origin/pkg/oauthserver/authenticator/identitymapper"
)
type Config struct {
// IDHeaders lists the headers to check (in order, case-insensitively) for an identity. The first header with a value wins.
IDHeaders []string
// NameHeaders lists the headers to check (in order, case-insensitively) for a display name. The first header with a value wins.
NameHeaders []string
// PreferredUsernameHeaders lists the headers to check (in order, case-insensitively) for a preferred username. The first header with a value wins. If empty, the ID is used
PreferredUsernameHeaders []string
// EmailHeaders lists the headers to check (in order, case-insensitively) for an email address. The first header with a value wins.
EmailHeaders []string
}
type Authenticator struct {
providerName string
config *Config
mapper authapi.UserIdentityMapper
}
func NewAuthenticator(providerName string, config *Config, mapper authapi.UserIdentityMapper) *Authenticator {
return &Authenticator{providerName, config, mapper}
}
func (a *Authenticator) AuthenticateRequest(req *http.Request) (user.Info, bool, error) {
id := headerValue(req.Header, a.config.IDHeaders)
if len(id) == 0 {
return nil, false, nil
}
identity := authapi.NewDefaultUserIdentityInfo(a.providerName, id)
if email := headerValue(req.Header, a.config.EmailHeaders); len(email) > 0 {
identity.Extra[authapi.IdentityEmailKey] = email
}
if name := headerValue(req.Header, a.config.NameHeaders); len(name) > 0 {
identity.Extra[authapi.IdentityDisplayNameKey] = name
}
if preferredUsername := headerValue(req.Header, a.config.PreferredUsernameHeaders); len(preferredUsername) > 0 {
identity.Extra[authapi.IdentityPreferredUsernameKey] = preferredUsername
}
return identitymapper.UserFor(a.mapper, identity)
}
func headerValue(h http.Header, headerNames []string) string {
for _, headerName := range headerNames {
headerName = strings.TrimSpace(headerName)
if len(headerName) == 0 {
continue
}
headerValue := h.Get(headerName)
if len(headerValue) > 0 {
return headerValue
}
}
return ""
}