forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
lookup.go
55 lines (44 loc) · 1.51 KB
/
lookup.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
package identitymapper
import (
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kuser "k8s.io/apiserver/pkg/authentication/user"
userclient "github.com/openshift/client-go/user/clientset/versioned/typed/user/v1"
authapi "github.com/openshift/origin/pkg/oauthserver/api"
)
var _ = authapi.UserIdentityMapper(&lookupIdentityMapper{})
// lookupIdentityMapper does not provision a new identity or user, it only allows identities already associated with users
type lookupIdentityMapper struct {
mappings userclient.UserIdentityMappingInterface
users userclient.UserInterface
}
// UserFor returns info about the user for whom identity info has been provided
func (p *lookupIdentityMapper) UserFor(info authapi.UserIdentityInfo) (kuser.Info, error) {
mapping, err := p.mappings.Get(info.GetIdentityName(), metav1.GetOptions{})
if err != nil {
return nil, NewLookupError(info, err)
}
u, err := p.users.Get(mapping.User.Name, metav1.GetOptions{})
if err != nil {
return nil, NewLookupError(info, err)
}
return &kuser.DefaultInfo{
Name: u.Name,
UID: string(u.UID),
Groups: u.Groups,
}, nil
}
type lookupError struct {
Identity authapi.UserIdentityInfo
CausedBy error
}
func IsLookupError(err error) bool {
_, ok := err.(lookupError)
return ok
}
func NewLookupError(info authapi.UserIdentityInfo, err error) error {
return lookupError{Identity: info, CausedBy: err}
}
func (c lookupError) Error() string {
return fmt.Sprintf("lookup of user for %q failed: %v", c.Identity.GetIdentityName(), c.CausedBy)
}