forked from eclipse-biscuit/biscuit-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtypes.go
393 lines (328 loc) · 8.78 KB
/
types.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
package biscuit
import (
"encoding/hex"
"fmt"
"regexp"
"strings"
"time"
"github.com/flynn/biscuit-go/datalog"
)
const SymbolAuthority = Symbol("authority")
// defaultSymbolTable predefines some symbols available in every implementation, to avoid
// transmitting them with every token
var defaultSymbolTable = &datalog.SymbolTable{
"authority",
"ambient",
"resource",
"operation",
"right",
"current_time",
"revocation_id",
}
type Block struct {
index uint32
symbols *datalog.SymbolTable
facts *datalog.FactSet
rules []datalog.Rule
caveats []datalog.Caveat
context string
}
func (b *Block) String(symbols *datalog.SymbolTable) string {
debug := &datalog.SymbolDebugger{
SymbolTable: symbols,
}
rules := make([]string, len(b.rules))
for i, r := range b.rules {
rules[i] = debug.Rule(r)
}
caveats := make([]string, len(b.caveats))
for i, c := range b.caveats {
caveats[i] = debug.Caveat(c)
}
return fmt.Sprintf(`Block[%d] {
symbols: %+q
context: %q
facts: %v
rules: %v
caveats: %v
}`,
b.index,
*b.symbols,
b.context,
debug.FactSet(b.facts),
rules,
caveats,
)
}
type FactSet []Fact
func (fs FactSet) String() string {
out := make([]string, 0, len(fs))
for _, f := range fs {
out = append(out, f.String())
}
var outStr string
if len(out) > 0 {
outStr = fmt.Sprintf("\n\t%s\n", strings.Join(out, ",\n\t"))
}
return fmt.Sprintf("[%s]", outStr)
}
type Fact struct {
Predicate
}
func (f Fact) convert(symbols *datalog.SymbolTable) datalog.Fact {
return datalog.Fact{
Predicate: f.Predicate.convert(symbols),
}
}
func (f Fact) String() string {
return f.Predicate.String()
}
func fromDatalogFact(symbols *datalog.SymbolTable, f datalog.Fact) (*Fact, error) {
pred, err := fromDatalogPredicate(symbols, f.Predicate)
if err != nil {
return nil, err
}
return &Fact{
Predicate: *pred,
}, nil
}
func fromDatalogPredicate(symbols *datalog.SymbolTable, p datalog.Predicate) (*Predicate, error) {
atoms := make([]Atom, 0, len(p.IDs))
for _, a := range p.IDs {
switch a.Type() {
case datalog.IDTypeSymbol:
atoms = append(atoms, Symbol(symbols.Str(a.(datalog.Symbol))))
case datalog.IDTypeVariable:
atoms = append(atoms, Variable(a.(datalog.Variable)))
case datalog.IDTypeInteger:
atoms = append(atoms, Integer(a.(datalog.Integer)))
case datalog.IDTypeString:
atoms = append(atoms, String(a.(datalog.String)))
case datalog.IDTypeDate:
atoms = append(atoms, Date(time.Unix(int64(a.(datalog.Date)), 0)))
case datalog.IDTypeBytes:
atoms = append(atoms, Bytes(a.(datalog.Bytes)))
default:
return nil, fmt.Errorf("unsupported atom type: %v", a.Type())
}
}
return &Predicate{
Name: symbols.Str(p.Name),
IDs: atoms,
}, nil
}
type Rule struct {
Head Predicate
Body []Predicate
Constraints []Constraint
}
func (r Rule) convert(symbols *datalog.SymbolTable) datalog.Rule {
dlBody := make([]datalog.Predicate, len(r.Body))
for i, p := range r.Body {
dlBody[i] = p.convert(symbols)
}
dlConstraints := make([]datalog.Constraint, len(r.Constraints))
for i, c := range r.Constraints {
dlConstraints[i] = c.convert(symbols)
}
return datalog.Rule{
Head: r.Head.convert(symbols),
Body: dlBody,
Constraints: dlConstraints,
}
}
type Caveat struct {
Queries []Rule
}
func (c Caveat) convert(symbols *datalog.SymbolTable) datalog.Caveat {
queries := make([]datalog.Rule, len(c.Queries))
for i, q := range c.Queries {
queries[i] = q.convert(symbols)
}
return datalog.Caveat{
Queries: queries,
}
}
type Constraint struct {
Name Variable
Checker
}
func (c Constraint) convert(symbols *datalog.SymbolTable) datalog.Constraint {
return datalog.Constraint{
Name: c.Name.convert(symbols).(datalog.Variable),
Checker: c.Checker.convert(symbols),
}
}
type Checker interface {
convert(symbols *datalog.SymbolTable) datalog.Checker
}
type IntegerComparisonChecker struct {
Comparison datalog.IntegerComparison
Integer Integer
}
func (c IntegerComparisonChecker) convert(symbols *datalog.SymbolTable) datalog.Checker {
return datalog.IntegerComparisonChecker{
Comparison: c.Comparison,
Integer: c.Integer.convert(symbols).(datalog.Integer),
}
}
type IntegerInChecker struct {
Set map[Integer]struct{}
Not bool
}
func (c IntegerInChecker) convert(symbols *datalog.SymbolTable) datalog.Checker {
dlSet := make(map[datalog.Integer]struct{}, len(c.Set))
for i := range c.Set {
dlSet[i.convert(symbols).(datalog.Integer)] = struct{}{}
}
return datalog.IntegerInChecker{
Set: dlSet,
Not: c.Not,
}
}
type StringComparison byte
type StringComparisonChecker struct {
Comparison datalog.StringComparison
Str String
}
func (c StringComparisonChecker) convert(symbols *datalog.SymbolTable) datalog.Checker {
return datalog.StringComparisonChecker{
Comparison: c.Comparison,
Str: c.Str.convert(symbols).(datalog.String),
}
}
type StringInChecker struct {
Set map[String]struct{}
Not bool
}
func (c StringInChecker) convert(symbols *datalog.SymbolTable) datalog.Checker {
dlSet := make(map[datalog.String]struct{}, len(c.Set))
for i := range c.Set {
dlSet[i.convert(symbols).(datalog.String)] = struct{}{}
}
return datalog.StringInChecker{
Set: dlSet,
Not: c.Not,
}
}
type StringRegexpChecker regexp.Regexp
func (c StringRegexpChecker) convert(symbols *datalog.SymbolTable) datalog.Checker {
re := datalog.StringRegexpChecker(c)
return &re
}
type DateComparison byte
type DateComparisonChecker struct {
Comparison datalog.DateComparison
Date Date
}
func (c DateComparisonChecker) convert(symbols *datalog.SymbolTable) datalog.Checker {
return datalog.DateComparisonChecker{
Comparison: c.Comparison,
Date: c.Date.convert(symbols).(datalog.Date),
}
}
type SymbolInChecker struct {
Set map[Symbol]struct{}
Not bool
}
func (c SymbolInChecker) convert(symbols *datalog.SymbolTable) datalog.Checker {
dlSet := make(map[datalog.Symbol]struct{}, len(c.Set))
for i := range c.Set {
dlSet[i.convert(symbols).(datalog.Symbol)] = struct{}{}
}
return datalog.SymbolInChecker{
Set: dlSet,
Not: c.Not,
}
}
type BytesComparisonChecker struct {
Comparison datalog.BytesComparison
Bytes Bytes
}
func (c BytesComparisonChecker) convert(symbols *datalog.SymbolTable) datalog.Checker {
return datalog.BytesComparisonChecker{
Comparison: c.Comparison,
Bytes: c.Bytes.convert(symbols).(datalog.Bytes),
}
}
type BytesInChecker struct {
Set map[string]struct{}
Not bool
}
func (c BytesInChecker) convert(symbols *datalog.SymbolTable) datalog.Checker {
return datalog.BytesInChecker{
Set: c.Set,
Not: c.Not,
}
}
type Predicate struct {
Name string
IDs []Atom
}
func (p Predicate) convert(symbols *datalog.SymbolTable) datalog.Predicate {
var ids []datalog.ID
for _, a := range p.IDs {
ids = append(ids, a.convert(symbols))
}
return datalog.Predicate{
Name: symbols.Insert(p.Name),
IDs: ids,
}
}
func (p Predicate) String() string {
atoms := make([]string, 0, len(p.IDs))
for _, a := range p.IDs {
atoms = append(atoms, a.String())
}
return fmt.Sprintf("%s(%s)", p.Name, strings.Join(atoms, ", "))
}
type AtomType byte
const (
AtomTypeSymbol AtomType = iota
AtomTypeVariable
AtomTypeInteger
AtomTypeString
AtomTypeDate
AtomTypeBytes
)
type Atom interface {
Type() AtomType
String() string
convert(symbols *datalog.SymbolTable) datalog.ID
}
type Symbol string
func (a Symbol) Type() AtomType { return AtomTypeSymbol }
func (a Symbol) convert(symbols *datalog.SymbolTable) datalog.ID {
return datalog.Symbol(symbols.Insert(string(a)))
}
func (a Symbol) String() string { return fmt.Sprintf("#%s", string(a)) }
type Variable uint32
func (a Variable) Type() AtomType { return AtomTypeVariable }
func (a Variable) convert(symbols *datalog.SymbolTable) datalog.ID {
return datalog.Variable(a)
}
func (a Variable) String() string { return fmt.Sprintf("$%d", a) }
type Integer int64
func (a Integer) Type() AtomType { return AtomTypeInteger }
func (a Integer) convert(symbols *datalog.SymbolTable) datalog.ID {
return datalog.Integer(a)
}
func (a Integer) String() string { return fmt.Sprintf("%d", a) }
type String string
func (a String) Type() AtomType { return AtomTypeString }
func (a String) convert(symbols *datalog.SymbolTable) datalog.ID {
return datalog.String(a)
}
func (a String) String() string { return fmt.Sprintf("%q", string(a)) }
type Date time.Time
func (a Date) Type() AtomType { return AtomTypeDate }
func (a Date) convert(symbols *datalog.SymbolTable) datalog.ID {
return datalog.Date(time.Time(a).Unix())
}
func (a Date) String() string { return time.Time(a).Format(time.RFC3339) }
type Bytes []byte
func (a Bytes) Type() AtomType { return AtomTypeBytes }
func (a Bytes) convert(symbols *datalog.SymbolTable) datalog.ID {
return datalog.Bytes(a)
}
func (a Bytes) String() string { return fmt.Sprintf("hex:%s", hex.EncodeToString(a)) }