forked from biscuit-auth/biscuit-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
620 lines (541 loc) · 14.9 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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
package biscuit
import (
"encoding/hex"
"fmt"
"sort"
"strings"
"time"
"github.com/biscuit-auth/biscuit-go/v2/datalog"
)
const MinSchemaVersion uint32 = 3
const MaxSchemaVersion uint32 = 3
// defaultSymbolTable predefines some symbols available in every implementation, to avoid
// transmitting them with every token
var defaultSymbolTable = &datalog.SymbolTable{}
type Block struct {
symbols *datalog.SymbolTable
facts *datalog.FactSet
rules []datalog.Rule
checks []datalog.Check
context string
version uint32
}
func (b *Block) Code(symbols *datalog.SymbolTable) string {
debug := &datalog.SymbolDebugger{
SymbolTable: symbols,
}
facts := make([]string, len(*b.facts))
for i, f := range *b.facts {
facts[i] = debug.Predicate(f.Predicate)
}
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 {
%v
%s
%s
}`,
strings.Join(facts, ";\n"),
strings.Join(rules, ";\n"),
strings.Join(checks, ";\n"),
)
}
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 {
symbols: %+q
context: %q
facts: %v
rules: %v
checks: [%s]
version: %d
}`,
*b.symbols,
b.context,
debug.FactSet(b.facts),
rules,
strings.Join(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 ParsedBlock struct {
Facts FactSet
Rules []Rule
Checks []Check
}
type ParsedAuthorizer struct {
Policies []Policy
Block ParsedBlock
}
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.Terms))
for _, id := range p.Terms {
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.Term) (Term, error) {
var a Term
switch id.Type() {
case datalog.TermTypeVariable:
a = Variable(symbols.Str(datalog.String(id.(datalog.Variable))))
case datalog.TermTypeInteger:
a = Integer(id.(datalog.Integer))
case datalog.TermTypeString:
a = String(symbols.Str(id.(datalog.String)))
case datalog.TermTypeDate:
a = Date(time.Unix(int64(id.(datalog.Date)), 0))
case datalog.TermTypeBytes:
a = Bytes(id.(datalog.Bytes))
case datalog.TermTypeBool:
a = Bool(id.(datalog.Bool))
case datalog.TermTypeSet:
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,
}
}
func fromDatalogRule(symbols *datalog.SymbolTable, dlRule datalog.Rule) (*Rule, error) {
head, err := fromDatalogPredicate(symbols, dlRule.Head)
if err != nil {
return nil, fmt.Errorf("failed to convert datalog rule head: %v", err)
}
body := make([]Predicate, len(dlRule.Body))
for i, dlPred := range dlRule.Body {
pred, err := fromDatalogPredicate(symbols, dlPred)
if err != nil {
return nil, fmt.Errorf("failed to convert datalog rule body: %v", err)
}
body[i] = *pred
}
expressions := make([]Expression, len(dlRule.Expressions))
for i, dlExpr := range dlRule.Expressions {
expr, err := fromDatalogExpression(symbols, dlExpr)
if err != nil {
return nil, fmt.Errorf("failed to convert datalog rule expression: %v", err)
}
expressions[i] = expr
}
return &Rule{
Head: *head,
Body: body,
Expressions: expressions,
}, nil
}
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
}
func fromDatalogExpression(symbols *datalog.SymbolTable, dlExpr datalog.Expression) (Expression, error) {
expr := make(Expression, len(dlExpr))
for i, dlOP := range dlExpr {
switch dlOP.Type() {
case datalog.OpTypeValue:
v, err := fromDatalogValueOp(symbols, dlOP.(datalog.Value))
if err != nil {
return nil, fmt.Errorf("failed to convert datalog expression value: %w", err)
}
expr[i] = v
case datalog.OpTypeUnary:
u, err := fromDatalogUnaryOp(symbols, dlOP.(datalog.UnaryOp))
if err != nil {
return nil, fmt.Errorf("failed to convert datalog unary expression: %w", err)
}
expr[i] = u
case datalog.OpTypeBinary:
b, err := fromDatalogBinaryOp(symbols, dlOP.(datalog.BinaryOp))
if err != nil {
return nil, fmt.Errorf("failed to convert datalog binary expression: %w", err)
}
expr[i] = b
default:
return nil, fmt.Errorf("unsupported datalog expression type: %v", dlOP.Type())
}
}
return expr, nil
}
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)}
}
func fromDatalogValueOp(symbols *datalog.SymbolTable, dlValue datalog.Value) (Op, error) {
term, err := fromDatalogID(symbols, dlValue.ID)
if err != nil {
return nil, fmt.Errorf("failed to convert datalog expression value: %v", err)
}
return Value{Term: term}, nil
}
type unaryOpType byte
type UnaryOp unaryOpType
const (
UnaryUndefined UnaryOp = iota
UnaryNegate
UnaryParens
UnaryLength
)
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{}}
case UnaryLength:
return datalog.UnaryOp{UnaryOpFunc: datalog.Length{}}
default:
panic(fmt.Sprintf("biscuit: cannot convert invalid unary op type: %v", op))
}
}
func fromDatalogUnaryOp(symbols *datalog.SymbolTable, dlUnary datalog.UnaryOp) (Op, error) {
switch dlUnary.UnaryOpFunc.Type() {
case datalog.UnaryNegate:
return UnaryNegate, nil
case datalog.UnaryParens:
return UnaryParens, nil
case datalog.UnaryLength:
return UnaryLength, nil
default:
return UnaryUndefined, fmt.Errorf("unsupported datalog unary op: %v", dlUnary.UnaryOpFunc.Type())
}
}
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
BinaryIntersection
BinaryUnion
)
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{}}
case BinaryIntersection:
return datalog.BinaryOp{BinaryOpFunc: datalog.Intersection{}}
case BinaryUnion:
return datalog.BinaryOp{BinaryOpFunc: datalog.Union{}}
default:
panic(fmt.Sprintf("biscuit: cannot convert invalid binary op type: %v", op))
}
}
func fromDatalogBinaryOp(symbols *datalog.SymbolTable, dbBinary datalog.BinaryOp) (Op, error) {
switch dbBinary.BinaryOpFunc.Type() {
case datalog.BinaryLessThan:
return BinaryLessThan, nil
case datalog.BinaryLessOrEqual:
return BinaryLessOrEqual, nil
case datalog.BinaryGreaterThan:
return BinaryGreaterThan, nil
case datalog.BinaryGreaterOrEqual:
return BinaryGreaterOrEqual, nil
case datalog.BinaryEqual:
return BinaryEqual, nil
case datalog.BinaryContains:
return BinaryContains, nil
case datalog.BinaryPrefix:
return BinaryPrefix, nil
case datalog.BinarySuffix:
return BinarySuffix, nil
case datalog.BinaryRegex:
return BinaryRegex, nil
case datalog.BinaryAdd:
return BinaryAdd, nil
case datalog.BinarySub:
return BinarySub, nil
case datalog.BinaryMul:
return BinaryMul, nil
case datalog.BinaryDiv:
return BinaryDiv, nil
case datalog.BinaryAnd:
return BinaryAnd, nil
case datalog.BinaryOr:
return BinaryOr, nil
case datalog.BinaryIntersection:
return BinaryIntersection, nil
case datalog.BinaryUnion:
return BinaryUnion, nil
default:
return BinaryUndefined, fmt.Errorf("unsupported datalog binary op: %v", dbBinary.BinaryOpFunc.Type())
}
}
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,
}
}
func fromDatalogCheck(symbols *datalog.SymbolTable, dlCheck datalog.Check) (*Check, error) {
queries := make([]Rule, len(dlCheck.Queries))
for i, q := range dlCheck.Queries {
query, err := fromDatalogRule(symbols, q)
if err != nil {
return nil, fmt.Errorf("failed to convert datalog check query: %w", err)
}
queries[i] = *query
}
return &Check{
Queries: queries,
}, nil
}
type Predicate struct {
Name string
IDs []Term
}
func (p Predicate) convert(symbols *datalog.SymbolTable) datalog.Predicate {
var ids []datalog.Term
for _, a := range p.IDs {
ids = append(ids, a.convert(symbols))
}
return datalog.Predicate{
Name: symbols.Insert(p.Name),
Terms: 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.Term
}
type Variable string
func (a Variable) Type() TermType { return TermTypeVariable }
func (a Variable) convert(symbols *datalog.SymbolTable) datalog.Term {
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.Term {
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.Term {
return datalog.String(symbols.Insert(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.Term {
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.Term {
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.Term {
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.Term {
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, ", "))
}
type PolicyKind byte
const (
PolicyKindAllow = iota
PolicyKindDeny
)
var (
// DefaultAllowPolicy allows the biscuit to verify sucessfully as long as all its checks generate some facts.
DefaultAllowPolicy = Policy{Kind: PolicyKindAllow, Queries: []Rule{{Head: Predicate{Name: "allow"}}}}
// DefaultDenyPolicy makes the biscuit verification fail in all cases.
DefaultDenyPolicy = Policy{Kind: PolicyKindDeny, Queries: []Rule{{Head: Predicate{Name: "deny"}}}}
)
type Policy struct {
Queries []Rule
Kind PolicyKind
}