forked from biscuit-auth/biscuit-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconverters.go
125 lines (108 loc) · 2.71 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
package biscuit
import (
"fmt"
"github.com/biscuit-auth/biscuit-go/datalog"
"github.com/biscuit-auth/biscuit-go/pb"
"google.golang.org/protobuf/proto"
)
func tokenBlockToProtoBlock(input *Block) (*pb.Block, error) {
out := &pb.Block{
Symbols: *input.symbols,
Context: proto.String(input.context),
Version: proto.Uint32(input.version),
}
facts := input.facts
if facts != nil {
out.FactsV2 = make([]*pb.FactV2, len(*facts))
var err error
for i, fact := range *facts {
out.FactsV2[i], err = tokenFactToProtoFactV2(fact)
if err != nil {
return nil, err
}
}
}
rules := input.rules
if rules != nil {
out.RulesV2 = make([]*pb.RuleV2, len(rules))
for i, rule := range rules {
r, err := tokenRuleToProtoRuleV2(rule)
if err != nil {
return nil, err
}
out.RulesV2[i] = r
}
}
checks := input.checks
if checks != nil {
out.ChecksV2 = make([]*pb.CheckV2, len(checks))
for i, check := range checks {
c, err := tokenCheckToProtoCheckV2(check)
if err != nil {
return nil, err
}
out.ChecksV2[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.GetVersion() > MaxSchemaVersion {
return nil, fmt.Errorf(
"biscuit: failed to convert proto block to token block: block version: %d > library version %d",
input.GetVersion(),
MaxSchemaVersion,
)
}
switch input.GetVersion() {
case 2:
facts = make(datalog.FactSet, len(input.FactsV2))
rules = make([]datalog.Rule, len(input.RulesV2))
checks = make([]datalog.Check, len(input.ChecksV2))
for i, pbFact := range input.FactsV2 {
f, err := protoFactToTokenFactV2(pbFact)
if err != nil {
return nil, err
}
facts[i] = *f
}
for i, pbRule := range input.RulesV2 {
r, err := protoRuleToTokenRuleV2(pbRule)
if err != nil {
return nil, err
}
rules[i] = *r
}
for i, pbCheck := range input.ChecksV2 {
c, err := protoCheckToTokenCheckV2(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.GetVersion())
}
return &Block{
symbols: &symbols,
facts: &facts,
rules: rules,
checks: checks,
context: input.GetContext(),
version: input.GetVersion(),
}, 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)
}*/