fix(delegation): make Expiration an Option

This commit is contained in:
Steve Moyer
2024-09-17 08:08:13 -04:00
parent f85ece49fa
commit 658794041e
3 changed files with 17 additions and 9 deletions

View File

@@ -24,6 +24,13 @@ func applyConfigOptions(c *config, options ...Option) error {
return nil
}
func WithExpiration(o *time.Time) Option {
return func(c *config) error {
c.Expiration = o
return nil
}
}
func WithMeta(o map[string]datamodel.Node) Option {
return func(c *config) error {
c.Meta = o

View File

@@ -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
@@ -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
}
@@ -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 {

View File

@@ -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()
@@ -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()