/
tokenauthenticator.go
68 lines (57 loc) · 1.96 KB
/
tokenauthenticator.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
68
package oauth
import (
"context"
"errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kauthenticator "k8s.io/apiserver/pkg/authentication/authenticator"
kuser "k8s.io/apiserver/pkg/authentication/user"
oauthclient "github.com/openshift/client-go/oauth/clientset/versioned/typed/oauth/v1"
userclient "github.com/openshift/client-go/user/clientset/versioned/typed/user/v1"
authorizationapi "github.com/openshift/origin/pkg/authorization/apis/authorization"
)
var errLookup = errors.New("token lookup failed")
type tokenAuthenticator struct {
tokens oauthclient.OAuthAccessTokenInterface
users userclient.UserInterface
groupMapper UserToGroupMapper
validators OAuthTokenValidator
}
func NewTokenAuthenticator(tokens oauthclient.OAuthAccessTokenInterface, users userclient.UserInterface, groupMapper UserToGroupMapper, validators ...OAuthTokenValidator) kauthenticator.Token {
return &tokenAuthenticator{
tokens: tokens,
users: users,
groupMapper: groupMapper,
validators: OAuthTokenValidators(validators),
}
}
func (a *tokenAuthenticator) AuthenticateToken(ctx context.Context, name string) (*kauthenticator.Response, bool, error) {
token, err := a.tokens.Get(name, metav1.GetOptions{})
if err != nil {
return nil, false, errLookup // mask the error so we do not leak token data in logs
}
user, err := a.users.Get(token.UserName, metav1.GetOptions{})
if err != nil {
return nil, false, err
}
if err := a.validators.Validate(token, user); err != nil {
return nil, false, err
}
groups, err := a.groupMapper.GroupsFor(user.Name)
if err != nil {
return nil, false, err
}
groupNames := make([]string, 0, len(groups))
for _, group := range groups {
groupNames = append(groupNames, group.Name)
}
return &kauthenticator.Response{
User: &kuser.DefaultInfo{
Name: user.Name,
UID: string(user.UID),
Groups: groupNames,
Extra: map[string][]string{
authorizationapi.ScopesKey: token.Scopes,
},
},
}, true, nil
}