-
Notifications
You must be signed in to change notification settings - Fork 22
/
converters.go
146 lines (126 loc) · 3.18 KB
/
converters.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
package biscuit
import (
"fmt"
"github.com/flynn/biscuit-go/datalog"
"github.com/flynn/biscuit-go/pb"
"github.com/flynn/biscuit-go/sig"
)
func tokenBlockToProtoBlock(input *Block) (*pb.Block, error) {
out := &pb.Block{
Index: input.index,
Symbols: *input.symbols,
Context: input.context,
Version: input.version,
}
out.FactsV1 = make([]*pb.FactV1, len(*input.facts))
var err error
for i, fact := range *input.facts {
out.FactsV1[i], err = tokenFactToProtoFactV1(fact)
if err != nil {
return nil, err
}
}
out.RulesV1 = make([]*pb.RuleV1, len(input.rules))
for i, rule := range input.rules {
r, err := tokenRuleToProtoRuleV1(rule)
if err != nil {
return nil, err
}
out.RulesV1[i] = r
}
out.ChecksV1 = make([]*pb.CheckV1, len(input.checks))
for i, check := range input.checks {
c, err := tokenCaveatToProtoCheckV1(check)
if err != nil {
return nil, err
}
out.ChecksV1[i] = c
}
return out, nil
}
func protoBlockToTokenBlock(input *pb.Block) (*Block, error) {
symbols := datalog.SymbolTable(input.Symbols)
var facts datalog.FactSet
var rules []datalog.Rule
var checks []datalog.Check
if input.Version > MaxSchemaVersion {
return nil, fmt.Errorf(
"biscuit: failed to convert proto block to token block: block version: %d > library version %d",
input.Version,
MaxSchemaVersion,
)
}
switch input.Version {
case 0:
facts = make(datalog.FactSet, len(input.FactsV0))
rules = make([]datalog.Rule, len(input.RulesV0))
checks = make([]datalog.Check, len(input.CaveatsV0))
for i, pbFact := range input.FactsV0 {
f, err := protoFactToTokenFactV0(pbFact)
if err != nil {
return nil, err
}
facts[i] = *f
}
for i, pbRule := range input.RulesV0 {
r, err := protoRuleToTokenRuleV0(pbRule)
if err != nil {
return nil, err
}
rules[i] = *r
}
for i, pbCaveat := range input.CaveatsV0 {
c, err := protoCaveatToTokenCheckV0(pbCaveat)
if err != nil {
return nil, err
}
checks[i] = *c
}
case 1:
facts = make(datalog.FactSet, len(input.FactsV1))
rules = make([]datalog.Rule, len(input.RulesV1))
checks = make([]datalog.Check, len(input.ChecksV1))
for i, pbFact := range input.FactsV1 {
f, err := protoFactToTokenFactV1(pbFact)
if err != nil {
return nil, err
}
facts[i] = *f
}
for i, pbRule := range input.RulesV1 {
r, err := protoRuleToTokenRuleV1(pbRule)
if err != nil {
return nil, err
}
rules[i] = *r
}
for i, pbCheck := range input.ChecksV1 {
c, err := protoCheckToTokenCheckV1(pbCheck)
if err != nil {
return nil, err
}
checks[i] = *c
}
default:
return nil, fmt.Errorf("biscuit: failed to convert proto block to token block: unsupported version: %d", input.Version)
}
return &Block{
index: input.Index,
symbols: &symbols,
facts: &facts,
rules: rules,
checks: checks,
context: input.Context,
version: input.Version,
}, nil
}
func tokenSignatureToProtoSignature(ts *sig.TokenSignature) *pb.Signature {
params, z := ts.Encode()
return &pb.Signature{
Parameters: params,
Z: z,
}
}
func protoSignatureToTokenSignature(ps *pb.Signature) (*sig.TokenSignature, error) {
return sig.Decode(ps.Parameters, ps.Z)
}