Skip to content

Commit ed53555

Browse files
committed
refactor: policies
1 parent 071eb6a commit ed53555

15 files changed

Lines changed: 348 additions & 555 deletions

File tree

examples/capability_definition_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"github.com/alanshaw/ucantone/principal/ed25519"
1111
"github.com/alanshaw/ucantone/ucan/command"
1212
"github.com/alanshaw/ucantone/ucan/delegation"
13-
"github.com/alanshaw/ucantone/ucan/delegation/policy/builder"
13+
"github.com/alanshaw/ucantone/ucan/delegation/policy"
1414
"github.com/alanshaw/ucantone/ucan/invocation"
1515
"github.com/alanshaw/ucantone/validator/capability"
1616
)
@@ -22,7 +22,7 @@ func TestCapabilityDefinition(t *testing.T) {
2222
messageSendCapability, err := capability.New[*types.MessageSendArguments](
2323
must(command.Parse("/message/send")),
2424
capability.WithPolicy(
25-
must(builder.Build(builder.NotEqual(".to", []string{}))),
25+
must(policy.Build(policy.NotEqual(".to", []string{}))),
2626
),
2727
)
2828
if err != nil {
@@ -79,7 +79,7 @@ func TestCapabilityDefinitionGenericMap(t *testing.T) {
7979
messageSendCapability, err := capability.New[*datamodel.Map](
8080
must(command.Parse("/message/send")),
8181
capability.WithPolicy(
82-
must(builder.Build(builder.NotEqual(".to", []string{}))),
82+
must(policy.Build(policy.NotEqual(".to", []string{}))),
8383
),
8484
)
8585
if err != nil {

examples/container_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"github.com/alanshaw/ucantone/ucan/command"
1010
"github.com/alanshaw/ucantone/ucan/container"
1111
"github.com/alanshaw/ucantone/ucan/delegation"
12-
"github.com/alanshaw/ucantone/ucan/delegation/policy/builder"
12+
"github.com/alanshaw/ucantone/ucan/delegation/policy"
1313
"github.com/alanshaw/ucantone/ucan/invocation"
1414
)
1515

@@ -25,7 +25,7 @@ func TestContainer(t *testing.T) {
2525
panic(err)
2626
}
2727

28-
policy, err := builder.Build(builder.All(".to", builder.Like(".", "*.example.com")))
28+
pol, err := policy.Build(policy.All(".to", policy.Like(".", "*.example.com")))
2929
if err != nil {
3030
panic(err)
3131
}
@@ -37,7 +37,7 @@ func TestContainer(t *testing.T) {
3737
alice,
3838
must(command.Parse("/message/send")),
3939
delegation.WithSubject(mailer),
40-
delegation.WithPolicy(policy),
40+
delegation.WithPolicy(pol),
4141
)
4242
if err != nil {
4343
panic(err)

examples/delegations_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"github.com/alanshaw/ucantone/principal/ed25519"
77
"github.com/alanshaw/ucantone/ucan/command"
88
"github.com/alanshaw/ucantone/ucan/delegation"
9-
"github.com/alanshaw/ucantone/ucan/delegation/policy/builder"
9+
"github.com/alanshaw/ucantone/ucan/delegation/policy"
1010
)
1111

1212
func TestDelegations(t *testing.T) {
@@ -39,7 +39,7 @@ func TestDelegations(t *testing.T) {
3939

4040
// alice delegates bob capability to use the email service, but only allows
4141
// bob to send to example.com email addresses
42-
policy, err := builder.Build(builder.All(".to", builder.Like(".", "*.example.com")))
42+
policy, err := policy.Build(policy.All(".to", policy.Like(".", "*.example.com")))
4343
if err != nil {
4444
panic(err)
4545
}

examples/policies_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package examples
2+
3+
import (
4+
"testing"
5+
6+
"github.com/alanshaw/ucantone/ipld/datamodel"
7+
"github.com/alanshaw/ucantone/ucan/delegation/policy"
8+
)
9+
10+
func TestParsePolicy(t *testing.T) {
11+
// Create some data to match against the policy:
12+
msg := datamodel.NewMap(
13+
datamodel.WithEntry("to", []string{"bob@example.com"}),
14+
datamodel.WithEntry("from", "alice@example.com"),
15+
datamodel.WithEntry("message", "Hello bob!"),
16+
)
17+
18+
// A policy is a list of statements.
19+
// See https://github.com/ucan-wg/delegation/blob/main/README.md#policy
20+
pol, err := policy.Build(
21+
policy.All(".to", policy.Like(".", "*.example.com")),
22+
policy.Equal(".from", "alice@example.com"),
23+
)
24+
if err != nil {
25+
panic(err)
26+
}
27+
28+
ok, err := policy.Match(pol, msg)
29+
if err != nil {
30+
panic(err)
31+
}
32+
// expect this policy to match the data
33+
if ok != true {
34+
panic("policy did not match")
35+
}
36+
37+
// Alternatively you can parse a DAG-JSON encoded policy:
38+
pol, err = policy.Parse(`[
39+
["all", ".to", ["like", ".", "*@example.com"]],
40+
["==", ".from", "alice@example.com"]
41+
]`)
42+
if err != nil {
43+
panic(err)
44+
}
45+
46+
ok, err = policy.Match(pol, msg)
47+
if err != nil {
48+
panic(err)
49+
}
50+
// expect this policy to match the data
51+
if ok != true {
52+
panic("policy did not match")
53+
}
54+
}

notes.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
* No IPLD prime - [CBOR gen](https://github.com/whyrusleeping/cbor-gen) is adequete and significantly less complicated. It's a shame to not have IPLD schemas, but the inflexibility and boilerplate it introduces is prohibitive.
77
* Consequently, no `ipld.Link` usage. The `cid.Cid` type is actually useful, despite it being a bit heavy. We _have_ to use it anyways, since it's the only thing that implements `ipld.Link`. Also `Link` is just such a nothing interface.
88
* Fewer generics. Generic types in Go are not super powerful and can easily get in the way. We use generics more sparingly in this version.
9+
* DAG-JSON all the things to make debugging easier!
910

1011
## Specifics
1112

12-
* `DID` is now in string representation (not their binary representation as a string). You must call `Encode` and `Decode` to move to/from binary. Note, it does not have a `Bytes()` method since encoding to bytes may raise an error - you must use `Encode` instead.
13+
* `DID` is now in string representation (not their binary representation as a string). You can call `Encode` and `Decode` to move to/from binary. Note, it does not have a `Bytes()` method since encoding to bytes may raise an error - you must use `Encode` instead.
1314
* Receipt is not defined properly in the specs...
1415
* Signatures
1516
* Varsig does not implement anything other than ed25519 signature and dag-cbor payload right now.
@@ -23,4 +24,4 @@
2324
## TODOs
2425

2526
* Policy code needs finishing off and testing against the fixture
26-
* IPLD layer needs to support floats
27+
* IPLD layer needs to support floats(?)

ucan/delegation/policy/builder/builder.go

Lines changed: 0 additions & 202 deletions
This file was deleted.

0 commit comments

Comments
 (0)