forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.go
58 lines (51 loc) · 998 Bytes
/
token.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
package filetoken
import (
"encoding/csv"
"errors"
"io"
"os"
"k8s.io/kubernetes/pkg/auth/user"
)
type TokenAuthenticator struct {
path string
tokens map[string]*user.DefaultInfo
}
func NewTokenAuthenticator(path string) (*TokenAuthenticator, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
tokens := make(map[string]*user.DefaultInfo)
reader := csv.NewReader(file)
for {
record, err := reader.Read()
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
if len(record) < 2 {
continue
}
obj := &user.DefaultInfo{
Name: record[1],
}
if len(record) > 2 {
obj.UID = record[2]
}
tokens[record[0]] = obj
}
return &TokenAuthenticator{
path: file.Name(),
tokens: tokens,
}, nil
}
func (a *TokenAuthenticator) AuthenticateToken(value string) (user.Info, bool, error) {
user, ok := a.tokens[value]
if !ok {
return nil, false, errors.New("Invalid token")
}
return user, true, nil
}