fix(delegation): make Expiration an Option
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user