forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokenauthenticator.go
36 lines (30 loc) · 919 Bytes
/
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
package registry
import (
"errors"
"time"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/auth/user"
"github.com/openshift/origin/pkg/oauth/registry/oauthaccesstoken"
)
type TokenAuthenticator struct {
registry oauthaccesstoken.Registry
}
var ErrExpired = errors.New("Token is expired")
func NewTokenAuthenticator(registry oauthaccesstoken.Registry) *TokenAuthenticator {
return &TokenAuthenticator{
registry: registry,
}
}
func (a *TokenAuthenticator) AuthenticateToken(value string) (user.Info, bool, error) {
token, err := a.registry.GetAccessToken(api.NewContext(), 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
}
return &user.DefaultInfo{
Name: token.UserName,
UID: token.UserUID,
}, true, nil
}