-
Notifications
You must be signed in to change notification settings - Fork 113
/
demo.go
175 lines (160 loc) · 4.75 KB
/
demo.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Copyright 2018-2020 CERN
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// In applying this license, CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
package demo
import (
"context"
"errors"
"strings"
userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
types "github.com/cs3org/go-cs3apis/cs3/types/v1beta1"
"github.com/cs3org/reva/pkg/errtypes"
"github.com/cs3org/reva/pkg/user"
"github.com/cs3org/reva/pkg/user/manager/registry"
)
func init() {
registry.Register("demo", New)
}
type manager struct {
catalog map[string]*userpb.User
}
// New returns a new user manager.
func New(m map[string]interface{}) (user.Manager, error) {
cat := getUsers()
return &manager{catalog: cat}, nil
}
func (m *manager) GetUser(ctx context.Context, uid *userpb.UserId) (*userpb.User, error) {
if user, ok := m.catalog[uid.OpaqueId]; ok {
return user, nil
}
return nil, errtypes.NotFound(uid.OpaqueId)
}
func (m *manager) GetUserByClaim(ctx context.Context, claim, value string) (*userpb.User, error) {
for _, u := range m.catalog {
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("demo: invalid field")
}
// TODO(jfd) search Opaque? compare sub?
func userContains(u *userpb.User, query string) bool {
return strings.Contains(u.Username, query) || strings.Contains(u.DisplayName, query) || strings.Contains(u.Mail, query)
}
func (m *manager) FindUsers(ctx context.Context, query string) ([]*userpb.User, error) {
users := []*userpb.User{}
for _, u := range m.catalog {
if userContains(u, query) {
users = append(users, u)
}
}
return users, nil
}
func (m *manager) GetUserGroups(ctx context.Context, uid *userpb.UserId) ([]string, error) {
user, err := m.GetUser(ctx, uid)
if err != nil {
return nil, err
}
return user.Groups, nil
}
func (m *manager) IsInGroup(ctx context.Context, uid *userpb.UserId, group string) (bool, error) {
user, err := m.GetUser(ctx, uid)
if err != nil {
return false, err
}
for _, g := range user.Groups {
if group == g {
return true, nil
}
}
return false, nil
}
func getUsers() map[string]*userpb.User {
return map[string]*userpb.User{
"4c510ada-c86b-4815-8820-42cdf82c3d51": &userpb.User{
Id: &userpb.UserId{
Idp: "http://localhost:9998",
OpaqueId: "4c510ada-c86b-4815-8820-42cdf82c3d51",
},
Username: "einstein",
Groups: []string{"sailing-lovers", "violin-haters", "physics-lovers"},
Mail: "einstein@example.org",
DisplayName: "Albert Einstein",
Opaque: &types.Opaque{
Map: map[string]*types.OpaqueEntry{
"uid": &types.OpaqueEntry{
Decoder: "plain",
Value: []byte("123"),
},
"gid": &types.OpaqueEntry{
Decoder: "plain",
Value: []byte("987"),
},
},
},
},
"f7fbf8c8-139b-4376-b307-cf0a8c2d0d9c": &userpb.User{
Id: &userpb.UserId{
Idp: "http://localhost:9998",
OpaqueId: "f7fbf8c8-139b-4376-b307-cf0a8c2d0d9c",
},
Username: "marie",
Groups: []string{"radium-lovers", "polonium-lovers", "physics-lovers"},
Mail: "marie@example.org",
DisplayName: "Marie Curie",
Opaque: &types.Opaque{
Map: map[string]*types.OpaqueEntry{
"uid": &types.OpaqueEntry{
Decoder: "plain",
Value: []byte("456"),
},
"gid": &types.OpaqueEntry{
Decoder: "plain",
Value: []byte("987"),
},
},
},
},
"932b4540-8d16-481e-8ef4-588e4b6b151c": &userpb.User{
Id: &userpb.UserId{
Idp: "http://localhost:9998",
OpaqueId: "932b4540-8d16-481e-8ef4-588e4b6b151c",
},
Username: "richard",
Groups: []string{"quantum-lovers", "philosophy-haters", "physics-lovers"},
Mail: "richard@example.org",
DisplayName: "Richard Feynman",
},
}
}