-
Notifications
You must be signed in to change notification settings - Fork 22
/
types.go
437 lines (373 loc) · 9.71 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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
package biscuit
import (
"encoding/hex"
"fmt"
"sort"
"strings"
"time"
"github.com/biscuit-auth/biscuit-go/datalog"
)
const SymbolAuthority = Symbol("authority")
const MaxSchemaVersion uint32 = 1
// 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
checks []datalog.Check
context string
version uint32
}
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)
}
checks := make([]string, len(b.checks))
for i, c := range b.checks {
checks[i] = debug.Check(c)
}
return fmt.Sprintf(`Block[%d] {
symbols: %+q
context: %q
facts: %v
rules: %v
checks: %v
version: %d
}`,
b.index,
*b.symbols,
b.context,
debug.FactSet(b.facts),
rules,
checks,
b.version,
)
}
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) {
terms := make([]Term, 0, len(p.IDs))
for _, id := range p.IDs {
a, err := fromDatalogID(symbols, id)
if err != nil {
return nil, err
}
terms = append(terms, a)
}
return &Predicate{
Name: symbols.Str(p.Name),
IDs: terms,
}, nil
}
func fromDatalogID(symbols *datalog.SymbolTable, id datalog.ID) (Term, error) {
var a Term
switch id.Type() {
case datalog.IDTypeSymbol:
a = Symbol(symbols.Str(id.(datalog.Symbol)))
case datalog.IDTypeVariable:
a = Variable(symbols.Str(datalog.Symbol(id.(datalog.Variable))))
case datalog.IDTypeInteger:
a = Integer(id.(datalog.Integer))
case datalog.IDTypeString:
a = String(id.(datalog.String))
case datalog.IDTypeDate:
a = Date(time.Unix(int64(id.(datalog.Date)), 0))
case datalog.IDTypeBytes:
a = Bytes(id.(datalog.Bytes))
case datalog.IDTypeBool:
a = Bool(id.(datalog.Bool))
case datalog.IDTypeSet:
setIDs := id.(datalog.Set)
set := make(Set, 0, len(setIDs))
for _, i := range setIDs {
setTerm, err := fromDatalogID(symbols, i)
if err != nil {
return nil, err
}
set = append(set, setTerm)
}
a = set
default:
return nil, fmt.Errorf("unsupported term type: %v", id.Type())
}
return a, nil
}
type Rule struct {
Head Predicate
Body []Predicate
Expressions []Expression
}
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)
}
dlExpressions := make([]datalog.Expression, len(r.Expressions))
for i, e := range r.Expressions {
dlExpressions[i] = e.convert(symbols)
}
return datalog.Rule{
Head: r.Head.convert(symbols),
Body: dlBody,
Expressions: dlExpressions,
}
}
type Expression []Op
func (e Expression) convert(symbols *datalog.SymbolTable) datalog.Expression {
expr := make(datalog.Expression, len(e))
for i, elt := range e {
expr[i] = elt.convert(symbols)
}
return expr
}
type Op interface {
Type() OpType
convert(symbols *datalog.SymbolTable) datalog.Op
}
type OpType byte
const (
OpTypeValue OpType = iota
OpTypeUnary
OpTypeBinary
)
type Value struct {
Term Term
}
func (v Value) Type() OpType {
return OpTypeValue
}
func (v Value) convert(symbols *datalog.SymbolTable) datalog.Op {
return datalog.Value{ID: v.Term.convert(symbols)}
}
type unaryOpType byte
type UnaryOp unaryOpType
const (
UnaryUndefined UnaryOp = iota
UnaryNegate
UnaryParens
)
func (UnaryOp) Type() OpType {
return OpTypeUnary
}
func (op UnaryOp) convert(symbols *datalog.SymbolTable) datalog.Op {
switch op {
case UnaryNegate:
return datalog.UnaryOp{UnaryOpFunc: datalog.Negate{}}
case UnaryParens:
return datalog.UnaryOp{UnaryOpFunc: datalog.Parens{}}
default:
panic(fmt.Sprintf("biscuit: cannot convert invalid unary op type: %v", op))
}
}
type binaryOpType byte
type BinaryOp binaryOpType
const (
BinaryUndefined BinaryOp = iota
BinaryLessThan
BinaryLessOrEqual
BinaryGreaterThan
BinaryGreaterOrEqual
BinaryEqual
BinaryContains
BinaryPrefix
BinarySuffix
BinaryRegex
BinaryAdd
BinarySub
BinaryMul
BinaryDiv
BinaryAnd
BinaryOr
)
func (BinaryOp) Type() OpType {
return OpTypeBinary
}
func (op BinaryOp) convert(symbols *datalog.SymbolTable) datalog.Op {
switch op {
case BinaryLessThan:
return datalog.BinaryOp{BinaryOpFunc: datalog.LessThan{}}
case BinaryLessOrEqual:
return datalog.BinaryOp{BinaryOpFunc: datalog.LessOrEqual{}}
case BinaryGreaterThan:
return datalog.BinaryOp{BinaryOpFunc: datalog.GreaterThan{}}
case BinaryGreaterOrEqual:
return datalog.BinaryOp{BinaryOpFunc: datalog.GreaterOrEqual{}}
case BinaryEqual:
return datalog.BinaryOp{BinaryOpFunc: datalog.Equal{}}
case BinaryContains:
return datalog.BinaryOp{BinaryOpFunc: datalog.Contains{}}
case BinaryPrefix:
return datalog.BinaryOp{BinaryOpFunc: datalog.Prefix{}}
case BinarySuffix:
return datalog.BinaryOp{BinaryOpFunc: datalog.Suffix{}}
case BinaryRegex:
return datalog.BinaryOp{BinaryOpFunc: datalog.Regex{}}
case BinaryAdd:
return datalog.BinaryOp{BinaryOpFunc: datalog.Add{}}
case BinarySub:
return datalog.BinaryOp{BinaryOpFunc: datalog.Sub{}}
case BinaryMul:
return datalog.BinaryOp{BinaryOpFunc: datalog.Mul{}}
case BinaryDiv:
return datalog.BinaryOp{BinaryOpFunc: datalog.Div{}}
case BinaryAnd:
return datalog.BinaryOp{BinaryOpFunc: datalog.And{}}
case BinaryOr:
return datalog.BinaryOp{BinaryOpFunc: datalog.Or{}}
default:
panic(fmt.Sprintf("biscuit: cannot convert invalid binary op type: %v", op))
}
}
type Check struct {
Queries []Rule
}
func (c Check) convert(symbols *datalog.SymbolTable) datalog.Check {
queries := make([]datalog.Rule, len(c.Queries))
for i, q := range c.Queries {
queries[i] = q.convert(symbols)
}
return datalog.Check{
Queries: queries,
}
}
type Predicate struct {
Name string
IDs []Term
}
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 {
terms := make([]string, 0, len(p.IDs))
for _, a := range p.IDs {
terms = append(terms, a.String())
}
return fmt.Sprintf("%s(%s)", p.Name, strings.Join(terms, ", "))
}
type TermType byte
const (
TermTypeSymbol TermType = iota
TermTypeVariable
TermTypeInteger
TermTypeString
TermTypeDate
TermTypeBytes
TermTypeBool
TermTypeSet
)
type Term interface {
Type() TermType
String() string
convert(symbols *datalog.SymbolTable) datalog.ID
}
type Symbol string
func (a Symbol) Type() TermType { return TermTypeSymbol }
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 string
func (a Variable) Type() TermType { return TermTypeVariable }
func (a Variable) convert(symbols *datalog.SymbolTable) datalog.ID {
return datalog.Variable(symbols.Insert(string(a)))
}
func (a Variable) String() string { return fmt.Sprintf("$%s", string(a)) }
type Integer int64
func (a Integer) Type() TermType { return TermTypeInteger }
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() TermType { return TermTypeString }
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() TermType { return TermTypeDate }
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() TermType { return TermTypeBytes }
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)) }
type Bool bool
func (b Bool) Type() TermType { return TermTypeBool }
func (b Bool) convert(symbols *datalog.SymbolTable) datalog.ID {
return datalog.Bool(b)
}
func (b Bool) String() string { return fmt.Sprintf("%t", b) }
type Set []Term
func (a Set) Type() TermType { return TermTypeSet }
func (a Set) convert(symbols *datalog.SymbolTable) datalog.ID {
datalogSet := make(datalog.Set, 0, len(a))
for _, e := range a {
datalogSet = append(datalogSet, e.convert(symbols))
}
return datalogSet
}
func (a Set) String() string {
elts := make([]string, 0, len(a))
for _, e := range a {
elts = append(elts, e.String())
}
sort.Strings(elts)
return fmt.Sprintf("[%s]", strings.Join(elts, ", "))
}