-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
132 lines (110 loc) · 2.79 KB
/
main.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
import (
"encoding/json"
"errors"
"io/ioutil"
"strings"
userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
"github.com/cs3org/reva/pkg/errtypes"
"github.com/hashicorp/go-plugin"
"github.com/jimil749/reva-plugin-benchmark/pkg/shared"
)
// Here is a real implementation of Manager
type Manager struct {
users []*userpb.User
}
// func (m *Manager) OnLoad(userFile string) error {
// f, err := ioutil.ReadFile(userFile)
// if err != nil {
// return err
// }
// users := []*userpb.User{}
// err = json.Unmarshal(f, &users)
// if err != nil {
// return err
// }
// m.users = users
// return nil
// }
func (m *Manager) GetUser(uid *userpb.UserId) (*userpb.User, error) {
for _, u := range m.users {
if (u.Id.GetOpaqueId() == uid.OpaqueId || u.Username == uid.OpaqueId) && (uid.Idp == "" || uid.Idp == u.Id.GetIdp()) {
return u, nil
}
}
return nil, nil
}
func (m *Manager) GetUserByClaim(claim, value string) (*userpb.User, error) {
for _, u := range m.users {
if userClaim, err := extractClaim(u, claim); err == nil && value == userClaim {
return u, nil
}
}
return nil, errtypes.NotFound(value)
}
func extractClaim(u *userpb.User, claim string) (string, error) {
switch claim {
case "mail":
return u.Mail, nil
case "username":
return u.Username, nil
case "uid":
if u.Opaque != nil && u.Opaque.Map != nil {
if uidObj, ok := u.Opaque.Map["uid"]; ok {
if uidObj.Decoder == "plain" {
return string(uidObj.Value), nil
}
}
}
}
return "", errors.New("json: invalid field")
}
// TODO(jfd) search Opaque? compare sub?
func userContains(u *userpb.User, query string) bool {
query = strings.ToLower(query)
return strings.Contains(strings.ToLower(u.Username), query) || strings.Contains(strings.ToLower(u.DisplayName), query) ||
strings.Contains(strings.ToLower(u.Mail), query) || strings.Contains(strings.ToLower(u.Id.OpaqueId), query)
}
func (m *Manager) FindUsers(query string) ([]*userpb.User, error) {
users := []*userpb.User{}
for _, u := range m.users {
if userContains(u, query) {
users = append(users, u)
}
}
return users, nil
}
func (m *Manager) GetUserGroups(uid *userpb.UserId) ([]string, error) {
user, err := m.GetUser(uid)
if err != nil {
return nil, err
}
return user.Groups, nil
}
func New(userFile string) (shared.UserManager, error) {
f, err := ioutil.ReadFile(userFile)
if err != nil {
return nil, err
}
users := []*userpb.User{}
err = json.Unmarshal(f, &users)
if err != nil {
return nil, err
}
return &Manager{
users: users,
}, nil
}
var UserFile string
func main() {
mgr, err := New(UserFile)
if err != nil {
panic(err)
}
plugin.Serve(&plugin.ServeConfig{
HandshakeConfig: shared.Handshake,
Plugins: map[string]plugin.Plugin{
"json": &shared.JSONPlugin{Impl: mgr},
},
})
}