-
Notifications
You must be signed in to change notification settings - Fork 22
/
example_test.go
131 lines (113 loc) · 3.29 KB
/
example_test.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
package biscuit_test
import (
"crypto/rand"
"fmt"
"github.com/biscuit-auth/biscuit-go"
"github.com/biscuit-auth/biscuit-go/sig"
)
func ExampleBiscuit() {
rng := rand.Reader
root := sig.GenerateKeypair(rng)
builder := biscuit.NewBuilder(root)
err := builder.AddAuthorityFact(biscuit.Fact{biscuit.Predicate{
Name: "right",
IDs: []biscuit.Term{
biscuit.Symbol("authority"),
biscuit.String("/a/file1.txt"),
biscuit.Symbol("read"),
},
}})
if err != nil {
panic(fmt.Errorf("failed to add authority facts: %v", err))
}
err = builder.AddAuthorityFact(biscuit.Fact{biscuit.Predicate{
Name: "right",
IDs: []biscuit.Term{
biscuit.Symbol("authority"),
biscuit.String("/a/file1.txt"),
biscuit.Symbol("write"),
},
}})
if err != nil {
panic(fmt.Errorf("failed to add authority facts: %v", err))
}
err = builder.AddAuthorityFact(biscuit.Fact{biscuit.Predicate{
Name: "right",
IDs: []biscuit.Term{
biscuit.Symbol("authority"),
biscuit.String("/a/file2.txt"),
biscuit.Symbol("read"),
},
}})
if err != nil {
panic(fmt.Errorf("failed to add authority facts: %v", err))
}
err = builder.AddAuthorityFact(biscuit.Fact{biscuit.Predicate{
Name: "right",
IDs: []biscuit.Term{
biscuit.Symbol("authority"),
biscuit.String("/a/file3.txt"),
biscuit.Symbol("write"),
},
}})
if err != nil {
panic(fmt.Errorf("failed to add authority facts: %v", err))
}
b, err := builder.Build()
if err != nil {
panic(fmt.Errorf("failed to build biscuit: %v", err))
}
token, err := b.Serialize()
if err != nil {
panic(fmt.Errorf("failed to serialize biscuit: %v", err))
}
fmt.Printf("Token1 length: %d\n", len(token))
deser, err := biscuit.Unmarshal(token)
if err != nil {
panic(fmt.Errorf("failed to deserialize biscuit: %v", err))
}
blockBuilder := deser.CreateBlock()
blockBuilder.AddCheck(biscuit.Check{
Queries: []biscuit.Rule{
{
Head: biscuit.Predicate{
Name: "check",
IDs: []biscuit.Term{biscuit.String("/a/file1.txt")},
},
Body: []biscuit.Predicate{
{Name: "resource", IDs: []biscuit.Term{biscuit.Symbol("ambient"), biscuit.String("/a/file1.txt")}},
{Name: "operation", IDs: []biscuit.Term{biscuit.Symbol("ambient"), biscuit.Symbol("read")}},
},
},
},
})
newKeyPair := sig.GenerateKeypair(rng)
b2, err := deser.Append(rng, newKeyPair, blockBuilder.Build())
if err != nil {
panic(fmt.Errorf("failed to append: %v", err))
}
token2, err := b2.Serialize()
if err != nil {
panic(fmt.Errorf("failed to serialize biscuit: %v", err))
}
fmt.Printf("Token2 length: %d\n", len(token2))
// Verify
b2, err = biscuit.Unmarshal(token2)
if err != nil {
panic(fmt.Errorf("failed to deserialize token: %v", err))
}
v1, err := b2.Verify(root.Public())
if err != nil {
panic(fmt.Errorf("failed to create verifier: %v", err))
}
v1.AddFact(biscuit.Fact{Predicate: biscuit.Predicate{Name: "resource", IDs: []biscuit.Term{biscuit.Symbol("ambient"), biscuit.String("/a/file1.txt")}}})
v1.AddFact(biscuit.Fact{Predicate: biscuit.Predicate{Name: "operation", IDs: []biscuit.Term{biscuit.Symbol("ambient"), biscuit.Symbol("read")}}})
if err := v1.Verify(); err != nil {
fmt.Printf("failed to verify token: %v\n", err)
} else {
fmt.Println("verified token")
}
// Output: Token1 length: 242
// Token2 length: 383
// verified token
}