Skip to content

Commit ac3b7f8

Browse files
committed
refactor: bind in task constructor to ensure args are correct when validating
1 parent b2bc61b commit ac3b7f8

2 files changed

Lines changed: 16 additions & 13 deletions

File tree

validator/capability/capability.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type Arguments interface {
2323

2424
type Task[A Arguments] struct {
2525
*invocation.Task
26+
args A
2627
}
2728

2829
func NewTask[A Arguments](
@@ -31,26 +32,26 @@ func NewTask[A Arguments](
3132
arguments ipld.Map,
3233
nonce ucan.Nonce,
3334
) (*Task[A], error) {
34-
task, err := invocation.NewTask(subject, command, arguments, nonce)
35-
if err != nil {
36-
return nil, err
37-
}
38-
return &Task[A]{Task: task}, nil
39-
}
40-
41-
// BindArguments binds the arguments to the arguments type for this task.
42-
func (t *Task[A]) BindArguments() (A, error) {
4335
var args A
4436
// if args is a pointer type, then we need to create an instance of it because
4537
// rebind requires a non-nil pointer.
4638
typ := reflect.TypeOf(args)
4739
if typ.Kind() == reflect.Ptr {
4840
args = reflect.New(typ.Elem()).Interface().(A)
4941
}
50-
if err := datamodel.Rebind(datamodel.Map(t.Arguments()), args); err != nil {
51-
return args, verrs.NewMalformedArgumentsError(t.Command(), err)
42+
if err := datamodel.Rebind(datamodel.Map(arguments), args); err != nil {
43+
return nil, verrs.NewMalformedArgumentsError(command, err)
5244
}
53-
return args, nil
45+
task, err := invocation.NewTask(subject, command, arguments, nonce)
46+
if err != nil {
47+
return nil, err
48+
}
49+
return &Task[A]{Task: task, args: args}, nil
50+
}
51+
52+
// BindArguments returns the arguments bound to the type for this task.
53+
func (t *Task[A]) BindArguments() A {
54+
return t.args
5455
}
5556

5657
var _ ucan.Task = (*Task[Arguments])(nil)
@@ -61,6 +62,8 @@ type Match[A Arguments] struct {
6162
Proofs map[cid.Cid]ucan.Delegation
6263
}
6364

65+
// Capability is a capability that can be used to validate an invocation and its
66+
// proofs.
6467
type Capability[A Arguments] struct {
6568
cmd ucan.Command
6669
pol ucan.Policy

validator/validator_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func TestFixtures(t *testing.T) {
5555
authorization, err := validator.Access(t.Context(), vrf, cap, inv, opts...)
5656
require.NoError(t, err, "validation should have passed for invocation with %s", vector.Description)
5757

58-
_, err = authorization.Task.BindArguments()
58+
_ = authorization.Task.BindArguments()
5959
require.NoError(t, err)
6060
})
6161
}

0 commit comments

Comments
 (0)