forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tokenauthenticator.go
62 lines (53 loc) · 1.85 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
package internaloauth
import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kauthenticator "k8s.io/apiserver/pkg/authentication/authenticator"
kuser "k8s.io/apiserver/pkg/authentication/user"
userclient "github.com/openshift/client-go/user/clientset/versioned/typed/user/v1"
authorizationapi "github.com/openshift/origin/pkg/authorization/apis/authorization"
oauthclient "github.com/openshift/origin/pkg/oauth/generated/internalclientset/typed/oauth/internalversion"
)
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(name string) (kuser.Info, bool, error) {
token, err := a.tokens.Get(name, metav1.GetOptions{})
if err != nil {
return nil, false, err
}
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)+len(user.Groups))
for _, group := range groups {
groupNames = append(groupNames, group.Name)
}
groupNames = append(groupNames, user.Groups...)
return &kuser.DefaultInfo{
Name: user.Name,
UID: string(user.UID),
Groups: groupNames,
Extra: map[string][]string{
authorizationapi.ScopesKey: token.Scopes,
},
}, true, nil
}