Skip to content

Commit fb556da

Browse files
committed
refactor: subject as delegation argument
1 parent dce9150 commit fb556da

12 files changed

Lines changed: 478 additions & 86 deletions

File tree

examples/capability_definition_test.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"github.com/alanshaw/ucantone/examples/types"
88
"github.com/alanshaw/ucantone/ipld/datamodel"
99
"github.com/alanshaw/ucantone/principal/ed25519"
10-
"github.com/alanshaw/ucantone/ucan/delegation"
1110
"github.com/alanshaw/ucantone/ucan/delegation/policy"
1211
"github.com/alanshaw/ucantone/ucan/invocation"
1312
"github.com/alanshaw/ucantone/validator/capability"
@@ -39,11 +38,7 @@ func TestCapabilityDefinition(t *testing.T) {
3938
}
4039

4140
// delegate alice capability to use the email service
42-
dlg, err := messageSendCapability.Delegate(
43-
mailer,
44-
alice,
45-
delegation.WithSubject(mailer),
46-
)
41+
dlg, err := messageSendCapability.Delegate(mailer, alice, mailer)
4742
if err != nil {
4843
panic(err)
4944
}
@@ -96,11 +91,7 @@ func TestCapabilityDefinitionGenericMap(t *testing.T) {
9691
}
9792

9893
// delegate alice capability to use the email service
99-
dlg, err := messageSendCapability.Delegate(
100-
mailer,
101-
alice,
102-
delegation.WithSubject(mailer),
103-
)
94+
dlg, err := messageSendCapability.Delegate(mailer, alice, mailer)
10495
if err != nil {
10596
panic(err)
10697
}

examples/container_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ func TestContainer(t *testing.T) {
2929
dlg, err := delegation.Delegate(
3030
mailer,
3131
alice,
32+
mailer,
3233
"/message/send",
33-
delegation.WithSubject(mailer),
3434
delegation.WithPolicyBuilder(
3535
policy.All(".to", policy.Like(".", "*.example.com")),
3636
),

examples/delegations_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ func TestDelegations(t *testing.T) {
2929
_, err = delegation.Delegate(
3030
mailer,
3131
alice,
32+
mailer,
3233
"/message/send",
33-
delegation.WithSubject(mailer),
3434
)
3535
if err != nil {
3636
panic(err)
@@ -39,8 +39,8 @@ func TestDelegations(t *testing.T) {
3939
_, err = delegation.Delegate(
4040
alice,
4141
bob,
42+
mailer,
4243
"/message/send",
43-
delegation.WithSubject(mailer),
4444
// alice delegates bob capability to use the email service, but only allows
4545
// bob to send to example.com email addresses
4646
delegation.WithPolicyBuilder(

examples/promises_test.go

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
package examples
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/alanshaw/ucantone/examples/types"
8+
"github.com/alanshaw/ucantone/ipld"
9+
"github.com/alanshaw/ucantone/principal/ed25519"
10+
"github.com/alanshaw/ucantone/ucan/delegation"
11+
"github.com/alanshaw/ucantone/ucan/invocation"
12+
"github.com/alanshaw/ucantone/ucan/promise"
13+
"github.com/alanshaw/ucantone/validator/capability"
14+
)
15+
16+
func TestPromises(t *testing.T) {
17+
// Mailer is an email service that can send emails
18+
mailer, err := ed25519.Generate()
19+
if err != nil {
20+
panic(err)
21+
}
22+
23+
alice, err := ed25519.Generate()
24+
if err != nil {
25+
panic(err)
26+
}
27+
28+
// A list of email addresses
29+
mailingList, err := ed25519.Generate()
30+
if err != nil {
31+
panic(err)
32+
}
33+
34+
// A delegation from the mailer to alice allowing her to send emails
35+
msgSendDlg, err := delegation.Delegate(mailer, alice, mailer, "/msg/send")
36+
if err != nil {
37+
panic(err)
38+
}
39+
40+
// A delegation from the mailing list to alice allowing her to read the emails
41+
listEmailsDlg, err := delegation.Delegate(mailingList, alice, mailingList, "/emails/list")
42+
if err != nil {
43+
panic(err)
44+
}
45+
46+
// Read the emails on the mailing list. The mailer stores the email listings
47+
// so the invocation audience is the mailer.
48+
readListInv, err := invocation.Invoke(
49+
alice,
50+
mailingList,
51+
"/emails/list",
52+
ipld.Map{"limit": 100},
53+
invocation.WithAudience(mailer),
54+
invocation.WithProofs(listEmailsDlg.Link()),
55+
)
56+
if err != nil {
57+
panic(err)
58+
}
59+
60+
// Send a test email to the list.
61+
// This invocation is blocked on the successful result of the `/emails/list`
62+
// task above, due to the `await/ok` promise.
63+
msgSendInv, err := invocation.Invoke(
64+
alice,
65+
mailer,
66+
"/msg/send",
67+
ipld.Map{
68+
"from": "alice@example.com",
69+
"to": ipld.Map{"await/ok": readListInv.Task().Link()},
70+
"message": "test",
71+
},
72+
invocation.WithAudience(mailer),
73+
invocation.WithProofs(msgSendDlg.Link()),
74+
)
75+
if err != nil {
76+
panic(err)
77+
}
78+
fmt.Println(msgSendInv.Link())
79+
80+
// Now send these invocations to the mailer for execution. You'll need to also
81+
// send the two delegations as proof. You may want to use a _container_ for
82+
// this. See `container_test.go` in this directory.
83+
}
84+
85+
func TestTypedPromises(t *testing.T) {
86+
// Define a capability for sending emails
87+
msgSendCap, err := capability.New[*types.PromisedMsgSendArguments]("/msg/send")
88+
if err != nil {
89+
panic(err)
90+
}
91+
92+
// Define a capability listing emails on a mailing list
93+
emailListCap, err := capability.New[*types.EmailsListArguments]("/emails/list")
94+
if err != nil {
95+
panic(err)
96+
}
97+
98+
// Mailer is an email service that can send emails
99+
mailer, err := ed25519.Generate()
100+
if err != nil {
101+
panic(err)
102+
}
103+
104+
alice, err := ed25519.Generate()
105+
if err != nil {
106+
panic(err)
107+
}
108+
109+
// A list of email addresses
110+
mailingList, err := ed25519.Generate()
111+
if err != nil {
112+
panic(err)
113+
}
114+
115+
// A delegation from the mailer to alice allowing her to send emails
116+
msgSendDlg, err := msgSendCap.Delegate(mailer, alice, mailer)
117+
if err != nil {
118+
panic(err)
119+
}
120+
121+
// A delegation from the mailing list to alice allowing her to read the emails
122+
listEmailsDlg, err := emailListCap.Delegate(mailingList, alice, mailingList)
123+
if err != nil {
124+
panic(err)
125+
}
126+
127+
// Read the emails on the mailing list. The mailer stores the email listings
128+
// so the invocation audience is the mailer.
129+
readListInv, err := emailListCap.Invoke(
130+
alice,
131+
mailingList,
132+
&types.EmailsListArguments{
133+
Limit: uint64(100),
134+
},
135+
invocation.WithAudience(mailer),
136+
invocation.WithProofs(listEmailsDlg.Link()),
137+
)
138+
if err != nil {
139+
panic(err)
140+
}
141+
142+
// Send a test email to the list.
143+
// This invocation is blocked on the successful result of the `/emails/list`
144+
// task above, due to the `await/ok` promise.
145+
msgSendInv, err := msgSendCap.Invoke(
146+
alice,
147+
mailer,
148+
&types.PromisedMsgSendArguments{
149+
From: "alice@example.com",
150+
To: promise.AwaitOK{Task: readListInv.Task().Link()},
151+
Message: "test",
152+
},
153+
invocation.WithAudience(mailer),
154+
invocation.WithProofs(msgSendDlg.Link()),
155+
)
156+
if err != nil {
157+
panic(err)
158+
}
159+
fmt.Println(msgSendInv.Link())
160+
161+
// Now send these invocations to the mailer for execution. You'll need to also
162+
// send the two delegations as proof. You may want to use a _container_ for
163+
// this. See `container_test.go` in this directory.
164+
}

0 commit comments

Comments
 (0)