forked from projectatomic/atomic-enterprise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
userconversion.go
63 lines (54 loc) · 1.61 KB
/
userconversion.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
package registry
import (
"errors"
kuser "github.com/GoogleCloudPlatform/kubernetes/pkg/auth/user"
oapi "github.com/openshift/origin/pkg/oauth/api"
)
type UserConversion struct{}
// NewUserConversion creates an object that can convert the user.Info object to and from
// an oauth access/authorize token object.
func NewUserConversion() *UserConversion {
return &UserConversion{}
}
func (s *UserConversion) ConvertToAuthorizeToken(user interface{}, token *oapi.OAuthAuthorizeToken) error {
info, ok := user.(kuser.Info)
if !ok {
return errors.New("did not receive user.Info")
}
token.UserName = info.GetName()
if token.UserName == "" {
return errors.New("user name is empty")
}
token.UserUID = info.GetUID()
return nil
}
func (s *UserConversion) ConvertToAccessToken(user interface{}, token *oapi.OAuthAccessToken) error {
info, ok := user.(kuser.Info)
if !ok {
return errors.New("did not receive user.Info")
}
token.UserName = info.GetName()
if token.UserName == "" {
return errors.New("user name is empty")
}
token.UserUID = info.GetUID()
return nil
}
func (s *UserConversion) ConvertFromAuthorizeToken(token *oapi.OAuthAuthorizeToken) (interface{}, error) {
if token.UserName == "" {
return nil, errors.New("token has no user name stored")
}
return &kuser.DefaultInfo{
Name: token.UserName,
UID: token.UserUID,
}, nil
}
func (s *UserConversion) ConvertFromAccessToken(token *oapi.OAuthAccessToken) (interface{}, error) {
if token.UserName == "" {
return nil, errors.New("token has no user name stored")
}
return &kuser.DefaultInfo{
Name: token.UserName,
UID: token.UserUID,
}, nil
}