forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tokenauthenticator.go
69 lines (59 loc) · 1.82 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
package registry
import (
"errors"
"fmt"
"time"
"github.com/openshift/origin/pkg/auth/userregistry/identitymapper"
authorizationapi "github.com/openshift/origin/pkg/authorization/api"
"github.com/openshift/origin/pkg/oauth/registry/oauthaccesstoken"
"github.com/openshift/origin/pkg/user/registry/user"
"k8s.io/kubernetes/pkg/api"
kuser "k8s.io/kubernetes/pkg/auth/user"
)
type TokenAuthenticator struct {
tokens oauthaccesstoken.Registry
users user.Registry
groupMapper identitymapper.UserToGroupMapper
}
var ErrExpired = errors.New("Token is expired")
func NewTokenAuthenticator(tokens oauthaccesstoken.Registry, users user.Registry, groupMapper identitymapper.UserToGroupMapper) *TokenAuthenticator {
return &TokenAuthenticator{
tokens: tokens,
users: users,
groupMapper: groupMapper,
}
}
func (a *TokenAuthenticator) AuthenticateToken(value string) (kuser.Info, bool, error) {
ctx := api.NewContext()
token, err := a.tokens.GetAccessToken(ctx, value)
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
}
u, err := a.users.GetUser(ctx, token.UserName)
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
}