This repository has been archived by the owner on Mar 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scopes.go
191 lines (176 loc) · 4.64 KB
/
scopes.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
package scopes
import (
"context"
"errors"
"sort"
yall "yall.in"
)
const (
// PolicyDenyAll defines a string to use to deny all access.
PolicyDenyAll = "DENY_ALL"
// PolicyDefaultDeny defines a string to use to deny access by default, with exceptions.
PolicyDefaultDeny = "DEFAULT_DENY"
// PolicyAllowAll defines a string to use to allow all access.
PolicyAllowAll = "ALLOW_ALL"
// PolicyDefaultAllow defines a string to use to allow access by default, with exceptions.
PolicyDefaultAllow = "DEFAULT_ALLOW"
)
var (
// ErrScopeAlreadyExists is returned when attempting to create a Scope that already exists.
ErrScopeAlreadyExists = errors.New("scope already exists")
)
// Scope defines a scope of access to user data that users can grant.
type Scope struct {
ID string
UserPolicy string
UserExceptions []string
ClientPolicy string
ClientExceptions []string
IsDefault bool
}
// IsValidPolicy returns whether a string is a valid policy or not.
func IsValidPolicy(p string) bool {
if p == PolicyDenyAll ||
p == PolicyDefaultDeny ||
p == PolicyDefaultAllow ||
p == PolicyAllowAll {
return true
}
return false
}
// Change represents a change to a Scope.
type Change struct {
UserPolicy *string
UserExceptions *[]string
ClientPolicy *string
ClientExceptions *[]string
IsDefault *bool
}
// IsEmpty returns true if the Change should be considered empty.
func (c Change) IsEmpty() bool {
if c.UserPolicy != nil {
return false
}
if c.ClientPolicy != nil {
return false
}
if c.IsDefault != nil {
return false
}
if c.UserExceptions != nil {
return false
}
if c.ClientExceptions != nil {
return false
}
return true
}
// Apply returns a Scope that is a copy of `scope` with Change applied.
func Apply(change Change, scope Scope) Scope {
if change.IsEmpty() {
return scope
}
res := scope
if change.UserPolicy != nil {
res.UserPolicy = *change.UserPolicy
}
if change.UserExceptions != nil {
res.UserExceptions = append([]string{}, *change.UserExceptions...)
}
if change.ClientPolicy != nil {
res.ClientPolicy = *change.ClientPolicy
}
if change.ClientExceptions != nil {
res.ClientExceptions = append([]string{}, *change.ClientExceptions...)
}
if change.IsDefault != nil {
res.IsDefault = *change.IsDefault
}
return res
}
// Dependencies holds the common dependencies that will be used throughout the
// package.
type Dependencies struct {
Storer Storer
}
// ByID sorts the passed Scopes in place lexicographically by their IDs.
func ByID(scopes []Scope) {
sort.Slice(scopes, func(i, j int) bool {
return scopes[i].ID < scopes[j].ID
})
}
// FilterByClientID returns which of the Scopes of `scopes` the client specified
// by `clientID` can use.
func FilterByClientID(ctx context.Context, scopes []Scope, clientID string) []Scope {
var results []Scope
for _, scope := range scopes {
if ClientCanUseScope(ctx, scope, clientID) {
results = append(results, scope)
}
}
return results
}
// ClientCanUseScope returns true if the client specified by `client` can use `scope`.
func ClientCanUseScope(ctx context.Context, scope Scope, client string) bool {
switch scope.ClientPolicy {
case PolicyDenyAll:
return false
case PolicyAllowAll:
return true
case PolicyDefaultDeny:
for _, id := range scope.ClientExceptions {
if id == client {
return true
}
}
return false
case PolicyDefaultAllow:
for _, id := range scope.ClientExceptions {
if id == client {
return false
}
}
return true
default:
yall.FromContext(ctx).WithField("scope", scope.ID).WithField("client", client).Warn("unknown scope client policy, restricting access")
return false
}
}
// FilterByUserID returns which of the Scopes of `scopes` the user specified by
// `userID` can use.
func FilterByUserID(ctx context.Context, scopes []Scope, userID string) []Scope {
var results []Scope
for _, scope := range scopes {
if UserCanUseScope(ctx, scope, userID) {
results = append(results, scope)
}
}
return results
}
// UserCanUseScope returns true if the user specified by `userID` can use
// `scope`.
func UserCanUseScope(ctx context.Context, scope Scope, userID string) bool {
switch scope.UserPolicy {
case PolicyDenyAll:
return false
case PolicyAllowAll:
return true
case PolicyDefaultDeny:
for _, id := range scope.UserExceptions {
if id == userID {
return true
}
}
return false
case PolicyDefaultAllow:
for _, id := range scope.UserExceptions {
if id == userID {
return false
}
}
return true
default:
yall.FromContext(ctx).WithField("scope", scope.ID).WithField("user", userID).Warn("unknown scope user policy, restricting access")
return false
}
}