-
Notifications
You must be signed in to change notification settings - Fork 0
/
checks.go
224 lines (177 loc) · 5.64 KB
/
checks.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package fgax
import (
"context"
ofgaclient "github.com/openfga/go-sdk/client"
)
const (
// subject types
defaultSubject = userSubject
userSubject = "user"
serviceSubject = "service"
// object types
organizationObject = "organization"
groupObject = "group"
roleObject = "role"
)
// AccessCheck is a struct to hold the information needed to check access
type AccessCheck struct {
// ObjectType is the type of object being checked
ObjectType Kind
// ObjectID is the ID of the object being checked
ObjectID string
// SubjectID is the ID of the user making the request
SubjectID string
// SubjectType is the type of subject being checked
SubjectType string
// Relation is the relationship being checked (e.g. "view", "edit", "delete")
Relation string
}
// ListAccess is a struct to hold the information needed to list all relations
type ListAccess struct {
// ObjectType is the type of object being checked
ObjectType Kind
// ObjectID is the ID of the object being checked
ObjectID string
// SubjectID is the ID of the user making the request
SubjectID string
// SubjectType is the type of subject being checked
SubjectType string
// Relations is the relationship being checked (e.g. "can_view", "can_edit", "can_delete")
Relations []string
}
// CheckAccess checks if the user has access to the object type with the given relation
func (c *Client) CheckAccess(ctx context.Context, ac AccessCheck) (bool, error) {
if err := validateAccessCheck(ac); err != nil {
return false, err
}
if ac.SubjectType == "" {
ac.SubjectType = defaultSubject
}
sub := Entity{
Kind: Kind(ac.SubjectType),
Identifier: ac.SubjectID,
}
obj := Entity{
Kind: ac.ObjectType,
Identifier: ac.ObjectID,
}
c.Logger.Infow("checking relationship tuples", "relation", ac.Relation, "object", obj.String())
checkReq := ofgaclient.ClientCheckRequest{
User: sub.String(),
Relation: ac.Relation,
Object: obj.String(),
}
return c.checkTuple(ctx, checkReq)
}
// ListRelations returns the list of relations the user has with the object
func (c *Client) ListRelations(ctx context.Context, ac ListAccess) ([]string, error) {
if err := validateListAccess(ac); err != nil {
return nil, err
}
if ac.SubjectType == "" {
ac.SubjectType = defaultSubject
}
sub := Entity{
Kind: Kind(ac.SubjectType),
Identifier: ac.SubjectID,
}
obj := Entity{
Kind: ac.ObjectType,
Identifier: ac.ObjectID,
}
checks := []ofgaclient.ClientCheckRequest{}
for _, rel := range ac.Relations {
check := ofgaclient.ClientCheckRequest{
User: sub.String(),
Relation: rel,
Object: obj.String(),
}
checks = append(checks, check)
}
return c.batchCheckTuples(ctx, checks)
}
// CheckOrgReadAccess checks if the user has read access to the organization
func (c *Client) CheckOrgReadAccess(ctx context.Context, ac AccessCheck) (bool, error) {
ac.ObjectType = organizationObject
ac.Relation = CanView // read access
return c.CheckAccess(ctx, ac)
}
// CheckOrgWriteAccess checks if the user has write access to the organization
func (c *Client) CheckOrgWriteAccess(ctx context.Context, ac AccessCheck) (bool, error) {
ac.ObjectType = organizationObject
ac.Relation = CanEdit // write access
return c.CheckAccess(ctx, ac)
}
// CheckOrgAccess checks if the user has access to the organization with the given relation
func (c *Client) CheckOrgAccess(ctx context.Context, ac AccessCheck) (bool, error) {
ac.ObjectType = organizationObject
return c.CheckAccess(ctx, ac)
}
// CheckGroupAccess checks if the user has access to the group with the given relation
func (c *Client) CheckGroupAccess(ctx context.Context, ac AccessCheck) (bool, error) {
ac.ObjectType = groupObject
return c.CheckAccess(ctx, ac)
}
// checkTuple checks the openFGA store for provided relationship tuple
func (c *Client) checkTuple(ctx context.Context, check ofgaclient.ClientCheckRequest) (bool, error) {
data, err := c.Ofga.Check(ctx).Body(check).Execute()
if err != nil {
c.Logger.Errorw("error checking tuple", "tuple", check, "error", err.Error())
return false, err
}
return *data.Allowed, nil
}
// batchCheckTuples checks the openFGA store for provided relationship tuples and returns the allowed relations
func (c *Client) batchCheckTuples(ctx context.Context, checks []ofgaclient.ClientCheckRequest) ([]string, error) {
res, err := c.Ofga.BatchCheck(ctx).Body(checks).Execute()
if err != nil || res == nil {
return nil, err
}
relations := []string{}
for _, r := range *res {
if r.Allowed != nil && *r.Allowed {
relations = append(relations, r.Request.Relation)
}
}
return relations, nil
}
// CheckSystemAdminRole checks if the user has system admin access
func (c *Client) CheckSystemAdminRole(ctx context.Context, userID string) (bool, error) {
ac := AccessCheck{
ObjectType: roleObject,
ObjectID: SystemAdminRole,
Relation: RoleRelation,
SubjectID: userID,
SubjectType: userSubject, // admin roles are always user roles, never an API token
}
return c.CheckAccess(ctx, ac)
}
// validateAccessCheck checks if the AccessCheck struct is valid
func validateAccessCheck(ac AccessCheck) error {
if ac.SubjectID == "" {
return ErrInvalidAccessCheck
}
if ac.ObjectType == "" {
return ErrInvalidAccessCheck
}
if ac.ObjectID == "" {
return ErrInvalidAccessCheck
}
if ac.Relation == "" {
return ErrInvalidAccessCheck
}
return nil
}
// validateListAccess checks if the ListAccess struct is valid
func validateListAccess(ac ListAccess) error {
if ac.SubjectID == "" {
return ErrInvalidAccessCheck
}
if ac.ObjectType == "" {
return ErrInvalidAccessCheck
}
if ac.ObjectID == "" {
return ErrInvalidAccessCheck
}
return nil
}