-
Notifications
You must be signed in to change notification settings - Fork 22
/
website_test.go
115 lines (93 loc) · 3.1 KB
/
website_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
package biscuit_test
import (
"crypto/ed25519"
"crypto/rand"
"fmt"
"io"
"github.com/biscuit-auth/biscuit-go/v2"
"github.com/biscuit-auth/biscuit-go/v2/parser"
)
// code examples for the documentation at https://www.biscuitsec.org
// if these functions change, please send a PR at https://github.com/biscuit-auth/website
func CreateKey() (ed25519.PublicKey, ed25519.PrivateKey) {
rng := rand.Reader
publicRoot, privateRoot, _ := ed25519.GenerateKey(rng)
return publicRoot, privateRoot
}
func CreateToken(root *ed25519.PrivateKey) (*biscuit.Biscuit, error) {
builder := biscuit.NewBuilder(*root)
fact, err := parser.FromStringFact(`user("1234")`)
if err != nil {
return nil, fmt.Errorf("failed to parse authority facts: %v", err)
}
err = builder.AddAuthorityFact(fact)
if err != nil {
return nil, fmt.Errorf("failed to add authority facts: %v", err)
}
check, err := parser.FromStringCheck(`check if operation("read")`)
if err != nil {
return nil, fmt.Errorf("failed to parse authority checks: %v", err)
}
err = builder.AddAuthorityCheck(check)
if err != nil {
return nil, fmt.Errorf("failed to add authority checks: %v", err)
}
token, err := builder.Build()
if err != nil {
return nil, fmt.Errorf("failed to build biscuit: %v", err)
}
return token, nil
}
func Authorize(token *biscuit.Biscuit, root *ed25519.PublicKey) error {
authorizer, err := token.Authorizer(*root)
if err != nil {
return fmt.Errorf("failed to create verifier: %v", err)
}
fact1, err := parser.FromStringFact(`resource("/a/file1.txt")`)
if err != nil {
return fmt.Errorf("failed to parse verifier fact: %v", err)
}
authorizer.AddFact(fact1)
fact2, err := parser.FromStringFact(`operation("read")`)
if err != nil {
return fmt.Errorf("failed to parse verifier fact: %v", err)
}
authorizer.AddFact(fact2)
policy, err := parser.FromStringPolicy(`allow if resource("/a/file1.txt")`)
if err != nil {
return fmt.Errorf("failed to parse verifier policy: %v", err)
}
authorizer.AddPolicy(policy)
return authorizer.Authorize()
}
func Attenuate(serializedToken []byte, root *ed25519.PublicKey) ([]byte, error) {
token, err := biscuit.Unmarshal(serializedToken)
if err != nil {
return nil, fmt.Errorf("failed to deserialize biscuit: %v", err)
}
blockBuilder := token.CreateBlock()
check, err := parser.FromStringCheck(`check if resource($file), operation($permission), ["read"].contains($permission)`)
if err != nil {
return nil, fmt.Errorf("failed to parse check: %v", err)
}
err = blockBuilder.AddCheck(check)
if err != nil {
return nil, fmt.Errorf("failed to add block check: %v", err)
}
rng := rand.Reader
token2, err := token.Append(rng, blockBuilder.Build())
if err != nil {
return nil, fmt.Errorf("failed to append: %v", err)
}
return token2.Serialize()
}
func Seal(b *biscuit.Biscuit, rng io.Reader) (*biscuit.Biscuit, error) {
return b.Seal(rng)
}
func Query(authorizer biscuit.Authorizer) (biscuit.FactSet, error) {
rule, err := parser.FromStringRule(`data($name, $id) <- user($name, $id`)
if err != nil {
return nil, fmt.Errorf("failed to parse check: %v", err)
}
return authorizer.Query(rule)
}