forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tokenauthenticator.go
71 lines (61 loc) · 2.08 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
69
70
71
package registry
import (
"errors"
"fmt"
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kuser "k8s.io/apiserver/pkg/authentication/user"
"github.com/openshift/origin/pkg/auth/userregistry/identitymapper"
authorizationapi "github.com/openshift/origin/pkg/authorization/apis/authorization"
oauthclient "github.com/openshift/origin/pkg/oauth/generated/internalclientset/typed/oauth/internalversion"
userclient "github.com/openshift/origin/pkg/user/generated/internalclientset/typed/user/internalversion"
)
type TokenAuthenticator struct {
tokens oauthclient.OAuthAccessTokenInterface
users userclient.UserResourceInterface
groupMapper identitymapper.UserToGroupMapper
}
var ErrExpired = errors.New("Token is expired")
func NewTokenAuthenticator(tokens oauthclient.OAuthAccessTokenInterface, users userclient.UserResourceInterface, groupMapper identitymapper.UserToGroupMapper) *TokenAuthenticator {
return &TokenAuthenticator{
tokens: tokens,
users: users,
groupMapper: groupMapper,
}
}
func (a *TokenAuthenticator) AuthenticateToken(value string) (kuser.Info, bool, error) {
token, err := a.tokens.Get(value, metav1.GetOptions{})
if err != nil {
return nil, false, err
}
if token.CreationTimestamp.Time.Add(time.Duration(token.ExpiresIn) * time.Second).Before(time.Now()) {
return nil, false, ErrExpired
}
if token.DeletionTimestamp != nil {
return nil, false, ErrExpired
}
u, err := a.users.Get(token.UserName, metav1.GetOptions{})
if err != nil {
return nil, false, err
}
if string(u.UID) != token.UserUID {
return nil, false, fmt.Errorf("user.UID (%s) does not match token.userUID (%s)", u.UID, token.UserUID)
}
groups, err := a.groupMapper.GroupsFor(u.Name)
if err != nil {
return nil, false, err
}
groupNames := []string{}
for _, group := range groups {
groupNames = append(groupNames, group.Name)
}
groupNames = append(groupNames, u.Groups...)
return &kuser.DefaultInfo{
Name: u.Name,
UID: string(u.UID),
Groups: groupNames,
Extra: map[string][]string{
authorizationapi.ScopesKey: token.Scopes,
},
}, true, nil
}