forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
remotemaster.go
48 lines (38 loc) · 1.1 KB
/
remotemaster.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
package remotemaster
import (
"k8s.io/kubernetes/pkg/auth/user"
"k8s.io/kubernetes/pkg/client/restclient"
"github.com/openshift/origin/pkg/client"
"github.com/openshift/origin/pkg/cmd/util/clientcmd"
)
type Authenticator struct {
anonymousConfig restclient.Config
}
// NewAuthenticator authenticates by fetching users/~ using the provided token as a bearer token
func NewAuthenticator(anonymousConfig restclient.Config) (*Authenticator, error) {
// Ensure credentials are removed from the anonymous config
anonymousConfig = clientcmd.AnonymousClientConfig(&anonymousConfig)
return &Authenticator{
anonymousConfig: anonymousConfig,
}, nil
}
func (a *Authenticator) AuthenticateToken(value string) (user.Info, bool, error) {
if len(value) == 0 {
return nil, false, nil
}
tokenConfig := a.anonymousConfig
tokenConfig.BearerToken = value
c, err := client.New(&tokenConfig)
if err != nil {
return nil, false, err
}
u, err := c.Users().Get("~")
if err != nil {
return nil, false, err
}
return &user.DefaultInfo{
Name: u.Name,
UID: string(u.UID),
Groups: u.Groups,
}, true, nil
}