Skip to content

Commit

Permalink
fix(delegation): make Expiration an Option
Browse files Browse the repository at this point in the history
  • Loading branch information
smoyer64 committed Sep 17, 2024
1 parent f85ece4 commit 6587940
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
7 changes: 7 additions & 0 deletions delegation/delegatiom_options.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 8 additions & 7 deletions delegation/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ type Delegation struct {
//go:generate options -type=config -prefix=With -output=delegatiom_options.go -cmp=false -stringer=false -imports=time,github.com/ipld/go-ipld-prime/datamodel

type config struct {
Meta map[string]datamodel.Node
NotBefore *time.Time
Expiration *time.Time
Meta map[string]datamodel.Node
NotBefore *time.Time
}

func New(privKey crypto.PrivKey, aud did.DID, sub *did.DID, cmd *command.Command, pol policy.Policy, nonce []byte, exp *time.Time, opts ...Option) (*Delegation, error) {
func New(privKey crypto.PrivKey, aud did.DID, sub *did.DID, cmd *command.Command, pol policy.Policy, nonce []byte, opts ...Option) (*Delegation, error) {
cfg, err := newConfig(opts...)
if err != nil {
return nil, err
Expand Down Expand Up @@ -71,8 +72,8 @@ func New(privKey crypto.PrivKey, aud did.DID, sub *did.DID, cmd *command.Command
}

var expiration *int
if exp != nil {
e := int(exp.Unix())
if cfg.Expiration != nil {
e := int(cfg.Expiration.Unix())
expiration = &e
}

Expand Down Expand Up @@ -102,13 +103,13 @@ func New(privKey crypto.PrivKey, aud did.DID, sub *did.DID, cmd *command.Command
return dlg, nil
}

func Root(privKey crypto.PrivKey, aud did.DID, cmd *command.Command, pol policy.Policy, nonce []byte, exp *time.Time, opts ...Option) (*Delegation, error) {
func Root(privKey crypto.PrivKey, aud did.DID, cmd *command.Command, pol policy.Policy, nonce []byte, opts ...Option) (*Delegation, error) {
sub, err := did.FromPrivKey(privKey)
if err != nil {
return nil, err
}

return New(privKey, aud, &sub, cmd, pol, nonce, exp, opts...)
return New(privKey, aud, &sub, cmd, pol, nonce, opts...)
}

func (d *Delegation) Audience() did.DID {
Expand Down
4 changes: 2 additions & 2 deletions delegation/delegation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func TestConstructors(t *testing.T) {
}

t.Run("New", func(t *testing.T) {
dlg, err := delegation.New(privKey, aud, &sub, cmd, pol, []byte(nonce), &exp, delegation.WithMeta(meta))
dlg, err := delegation.New(privKey, aud, &sub, cmd, pol, []byte(nonce), delegation.WithExpiration(&exp), delegation.WithMeta(meta))
require.NoError(t, err)

data, err := dlg.ToDagJson()
Expand All @@ -105,7 +105,7 @@ func TestConstructors(t *testing.T) {
t.Run("Root", func(t *testing.T) {
t.Parallel()

dlg, err := delegation.Root(privKey, aud, cmd, pol, []byte(nonce), &exp, delegation.WithMeta(meta))
dlg, err := delegation.Root(privKey, aud, cmd, pol, []byte(nonce), delegation.WithExpiration(&exp), delegation.WithMeta(meta))
require.NoError(t, err)

data, err := dlg.ToDagJson()
Expand Down

0 comments on commit 6587940

Please sign in to comment.