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