delegation: use the fancy Meta
This commit is contained in:
@@ -5,12 +5,12 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/ipld/go-ipld-prime/datamodel"
|
|
||||||
"github.com/libp2p/go-libp2p/core/crypto"
|
"github.com/libp2p/go-libp2p/core/crypto"
|
||||||
|
|
||||||
"github.com/ucan-wg/go-ucan/capability/command"
|
"github.com/ucan-wg/go-ucan/capability/command"
|
||||||
"github.com/ucan-wg/go-ucan/capability/policy"
|
"github.com/ucan-wg/go-ucan/capability/policy"
|
||||||
"github.com/ucan-wg/go-ucan/did"
|
"github.com/ucan-wg/go-ucan/did"
|
||||||
|
"github.com/ucan-wg/go-ucan/pkg/meta"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Token struct {
|
type Token struct {
|
||||||
@@ -27,7 +27,7 @@ type Token struct {
|
|||||||
// A unique, random nonce
|
// A unique, random nonce
|
||||||
nonce []byte
|
nonce []byte
|
||||||
// Arbitrary Metadata
|
// Arbitrary Metadata
|
||||||
meta map[string]datamodel.Node
|
meta *meta.Meta
|
||||||
// "Not before" UTC Unix Timestamp in seconds (valid from), 53-bits integer
|
// "Not before" UTC Unix Timestamp in seconds (valid from), 53-bits integer
|
||||||
notBefore *time.Time
|
notBefore *time.Time
|
||||||
// The timestamp at which the Invocation becomes invalid
|
// The timestamp at which the Invocation becomes invalid
|
||||||
@@ -47,6 +47,7 @@ func New(privKey crypto.PrivKey, aud did.DID, cmd *command.Command, pol policy.P
|
|||||||
subject: did.Undef,
|
subject: did.Undef,
|
||||||
command: cmd,
|
command: cmd,
|
||||||
policy: pol,
|
policy: pol,
|
||||||
|
meta: meta.NewMeta(),
|
||||||
nonce: nonce,
|
nonce: nonce,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -109,7 +110,7 @@ func (t *Token) Nonce() []byte {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Meta returns the Token's metadata.
|
// Meta returns the Token's metadata.
|
||||||
func (t *Token) Meta() map[string]datamodel.Node {
|
func (t *Token) Meta() *meta.Meta {
|
||||||
return t.meta
|
return t.meta
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -165,13 +166,13 @@ func WithExpiration(exp time.Time) Option {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithMetadata sets the Token's optional "meta" field to the provided
|
// WithMeta adds a key/value pair in the "meta" field.
|
||||||
// value.
|
// WithMeta can be used multiple times in the same call.
|
||||||
func WithMetadata(meta map[string]datamodel.Node) Option {
|
// Accepted types for the value are: bool, string, int, int32, int64, []byte,
|
||||||
|
// and ipld.Node.
|
||||||
|
func WithMeta(key string, val any) Option {
|
||||||
return func(t *Token) error {
|
return func(t *Token) error {
|
||||||
t.meta = meta
|
return t.meta.Add(key, val)
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -246,8 +247,7 @@ func tokenFromModel(m tokenPayloadModel) (*Token, error) {
|
|||||||
}
|
}
|
||||||
tkn.nonce = m.Nonce
|
tkn.nonce = m.Nonce
|
||||||
|
|
||||||
// TODO: copy?
|
tkn.meta = &m.Meta
|
||||||
tkn.meta = m.Meta.Values
|
|
||||||
|
|
||||||
if m.Nbf != nil {
|
if m.Nbf != nil {
|
||||||
t := time.Unix(*m.Nbf, 0)
|
t := time.Unix(*m.Nbf, 0)
|
||||||
|
|||||||
@@ -5,15 +5,14 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/ipld/go-ipld-prime/datamodel"
|
|
||||||
"github.com/ipld/go-ipld-prime/node/basicnode"
|
|
||||||
"github.com/libp2p/go-libp2p/core/crypto"
|
"github.com/libp2p/go-libp2p/core/crypto"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
"gotest.tools/v3/golden"
|
||||||
|
|
||||||
"github.com/ucan-wg/go-ucan/capability/command"
|
"github.com/ucan-wg/go-ucan/capability/command"
|
||||||
"github.com/ucan-wg/go-ucan/capability/policy"
|
"github.com/ucan-wg/go-ucan/capability/policy"
|
||||||
"github.com/ucan-wg/go-ucan/delegation"
|
"github.com/ucan-wg/go-ucan/delegation"
|
||||||
"github.com/ucan-wg/go-ucan/did"
|
"github.com/ucan-wg/go-ucan/did"
|
||||||
"gotest.tools/v3/golden"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -86,13 +85,13 @@ func TestConstructors(t *testing.T) {
|
|||||||
exp, err := time.Parse(time.RFC3339, "2200-01-01T00:00:00Z")
|
exp, err := time.Parse(time.RFC3339, "2200-01-01T00:00:00Z")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
meta := map[string]datamodel.Node{
|
|
||||||
"foo": basicnode.NewString("fooo"),
|
|
||||||
"bar": basicnode.NewString("barr"),
|
|
||||||
}
|
|
||||||
|
|
||||||
t.Run("New", func(t *testing.T) {
|
t.Run("New", func(t *testing.T) {
|
||||||
dlg, err := delegation.New(privKey, aud, cmd, pol, []byte(nonce), delegation.WithSubject(sub), delegation.WithExpiration(exp), delegation.WithMetadata(meta))
|
dlg, err := delegation.New(privKey, aud, cmd, pol, []byte(nonce),
|
||||||
|
delegation.WithSubject(sub),
|
||||||
|
delegation.WithExpiration(exp),
|
||||||
|
delegation.WithMeta("foo", "fooo"),
|
||||||
|
delegation.WithMeta("bar", "barr"),
|
||||||
|
)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
data, err := dlg.ToDagJson(privKey)
|
data, err := dlg.ToDagJson(privKey)
|
||||||
@@ -106,7 +105,11 @@ func TestConstructors(t *testing.T) {
|
|||||||
t.Run("Root", func(t *testing.T) {
|
t.Run("Root", func(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
dlg, err := delegation.Root(privKey, aud, cmd, pol, []byte(nonce), delegation.WithExpiration(exp), delegation.WithMetadata(meta))
|
dlg, err := delegation.Root(privKey, aud, cmd, pol, []byte(nonce),
|
||||||
|
delegation.WithExpiration(exp),
|
||||||
|
delegation.WithMeta("foo", "fooo"),
|
||||||
|
delegation.WithMeta("bar", "barr"),
|
||||||
|
)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
data, err := dlg.ToDagJson(privKey)
|
data, err := dlg.ToDagJson(privKey)
|
||||||
|
|||||||
@@ -68,14 +68,6 @@ func (t *Token) ToIPLD(privKey crypto.PrivKey) (datamodel.Node, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
metaKeys := make([]string, len(t.meta))
|
|
||||||
i := 0
|
|
||||||
|
|
||||||
for k := range t.meta {
|
|
||||||
metaKeys[i] = k
|
|
||||||
i++
|
|
||||||
}
|
|
||||||
|
|
||||||
var nbf *int64
|
var nbf *int64
|
||||||
if t.notBefore != nil {
|
if t.notBefore != nil {
|
||||||
u := t.notBefore.Unix()
|
u := t.notBefore.Unix()
|
||||||
@@ -95,12 +87,9 @@ func (t *Token) ToIPLD(privKey crypto.PrivKey) (datamodel.Node, error) {
|
|||||||
Cmd: t.command.String(),
|
Cmd: t.command.String(),
|
||||||
Pol: pol,
|
Pol: pol,
|
||||||
Nonce: t.nonce,
|
Nonce: t.nonce,
|
||||||
Meta: metaModel{
|
Meta: *t.meta,
|
||||||
Keys: metaKeys,
|
Nbf: nbf,
|
||||||
Values: t.meta,
|
Exp: exp,
|
||||||
},
|
|
||||||
Nbf: nbf,
|
|
||||||
Exp: exp,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return envelope.ToIPLD(privKey, model)
|
return envelope.ToIPLD(privKey, model)
|
||||||
|
|||||||
@@ -9,7 +9,9 @@ import (
|
|||||||
"github.com/ipld/go-ipld-prime/datamodel"
|
"github.com/ipld/go-ipld-prime/datamodel"
|
||||||
"github.com/ipld/go-ipld-prime/node/bindnode"
|
"github.com/ipld/go-ipld-prime/node/bindnode"
|
||||||
"github.com/ipld/go-ipld-prime/schema"
|
"github.com/ipld/go-ipld-prime/schema"
|
||||||
|
|
||||||
"github.com/ucan-wg/go-ucan/internal/envelope"
|
"github.com/ucan-wg/go-ucan/internal/envelope"
|
||||||
|
"github.com/ucan-wg/go-ucan/pkg/meta"
|
||||||
)
|
)
|
||||||
|
|
||||||
const Tag = "ucan/dlg@1.0.0-rc.1"
|
const Tag = "ucan/dlg@1.0.0-rc.1"
|
||||||
@@ -58,8 +60,7 @@ type tokenPayloadModel struct {
|
|||||||
Nonce []byte
|
Nonce []byte
|
||||||
|
|
||||||
// Arbitrary Metadata
|
// Arbitrary Metadata
|
||||||
// optional: can be nil
|
Meta meta.Meta
|
||||||
Meta metaModel
|
|
||||||
|
|
||||||
// "Not before" UTC Unix Timestamp in seconds (valid from), 53-bits integer
|
// "Not before" UTC Unix Timestamp in seconds (valid from), 53-bits integer
|
||||||
// optional: can be nil
|
// optional: can be nil
|
||||||
@@ -76,8 +77,3 @@ func (e *tokenPayloadModel) Prototype() schema.TypedPrototype {
|
|||||||
func (*tokenPayloadModel) Tag() string {
|
func (*tokenPayloadModel) Tag() string {
|
||||||
return Tag
|
return Tag
|
||||||
}
|
}
|
||||||
|
|
||||||
type metaModel struct {
|
|
||||||
Keys []string
|
|
||||||
Values map[string]datamodel.Node
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -7,8 +7,9 @@ import (
|
|||||||
|
|
||||||
"github.com/ipld/go-ipld-prime"
|
"github.com/ipld/go-ipld-prime"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"github.com/ucan-wg/go-ucan/delegation"
|
|
||||||
"gotest.tools/v3/golden"
|
"gotest.tools/v3/golden"
|
||||||
|
|
||||||
|
"github.com/ucan-wg/go-ucan/delegation"
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:embed delegation.ipldsch
|
//go:embed delegation.ipldsch
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ func NewMeta() *Meta {
|
|||||||
// GetBool retrieves a value as a bool.
|
// GetBool retrieves a value as a bool.
|
||||||
// Returns ErrNotFound if the given key is missing.
|
// Returns ErrNotFound if the given key is missing.
|
||||||
// Returns datamodel.ErrWrongKind if the value has the wrong type.
|
// Returns datamodel.ErrWrongKind if the value has the wrong type.
|
||||||
func (m Meta) GetBool(key string) (bool, error) {
|
func (m *Meta) GetBool(key string) (bool, error) {
|
||||||
v, ok := m.Values[key]
|
v, ok := m.Values[key]
|
||||||
if !ok {
|
if !ok {
|
||||||
return false, ErrNotFound
|
return false, ErrNotFound
|
||||||
@@ -37,7 +37,7 @@ func (m Meta) GetBool(key string) (bool, error) {
|
|||||||
// GetString retrieves a value as a string.
|
// GetString retrieves a value as a string.
|
||||||
// Returns ErrNotFound if the given key is missing.
|
// Returns ErrNotFound if the given key is missing.
|
||||||
// Returns datamodel.ErrWrongKind if the value has the wrong type.
|
// Returns datamodel.ErrWrongKind if the value has the wrong type.
|
||||||
func (m Meta) GetString(key string) (string, error) {
|
func (m *Meta) GetString(key string) (string, error) {
|
||||||
v, ok := m.Values[key]
|
v, ok := m.Values[key]
|
||||||
if !ok {
|
if !ok {
|
||||||
return "", ErrNotFound
|
return "", ErrNotFound
|
||||||
@@ -48,7 +48,7 @@ func (m Meta) GetString(key string) (string, error) {
|
|||||||
// GetInt64 retrieves a value as an int64.
|
// GetInt64 retrieves a value as an int64.
|
||||||
// Returns ErrNotFound if the given key is missing.
|
// Returns ErrNotFound if the given key is missing.
|
||||||
// Returns datamodel.ErrWrongKind if the value has the wrong type.
|
// Returns datamodel.ErrWrongKind if the value has the wrong type.
|
||||||
func (m Meta) GetInt64(key string) (int64, error) {
|
func (m *Meta) GetInt64(key string) (int64, error) {
|
||||||
v, ok := m.Values[key]
|
v, ok := m.Values[key]
|
||||||
if !ok {
|
if !ok {
|
||||||
return 0, ErrNotFound
|
return 0, ErrNotFound
|
||||||
@@ -59,7 +59,7 @@ func (m Meta) GetInt64(key string) (int64, error) {
|
|||||||
// GetFloat64 retrieves a value as a float64.
|
// GetFloat64 retrieves a value as a float64.
|
||||||
// Returns ErrNotFound if the given key is missing.
|
// Returns ErrNotFound if the given key is missing.
|
||||||
// Returns datamodel.ErrWrongKind if the value has the wrong type.
|
// Returns datamodel.ErrWrongKind if the value has the wrong type.
|
||||||
func (m Meta) GetFloat64(key string) (float64, error) {
|
func (m *Meta) GetFloat64(key string) (float64, error) {
|
||||||
v, ok := m.Values[key]
|
v, ok := m.Values[key]
|
||||||
if !ok {
|
if !ok {
|
||||||
return 0, ErrNotFound
|
return 0, ErrNotFound
|
||||||
@@ -70,7 +70,7 @@ func (m Meta) GetFloat64(key string) (float64, error) {
|
|||||||
// GetBytes retrieves a value as a []byte.
|
// GetBytes retrieves a value as a []byte.
|
||||||
// Returns ErrNotFound if the given key is missing.
|
// Returns ErrNotFound if the given key is missing.
|
||||||
// Returns datamodel.ErrWrongKind if the value has the wrong type.
|
// Returns datamodel.ErrWrongKind if the value has the wrong type.
|
||||||
func (m Meta) GetBytes(key string) ([]byte, error) {
|
func (m *Meta) GetBytes(key string) ([]byte, error) {
|
||||||
v, ok := m.Values[key]
|
v, ok := m.Values[key]
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, ErrNotFound
|
return nil, ErrNotFound
|
||||||
@@ -81,7 +81,7 @@ func (m Meta) GetBytes(key string) ([]byte, error) {
|
|||||||
// GetNode retrieves a value as a raw IPLD node.
|
// GetNode retrieves a value as a raw IPLD node.
|
||||||
// Returns ErrNotFound if the given key is missing.
|
// Returns ErrNotFound if the given key is missing.
|
||||||
// Returns datamodel.ErrWrongKind if the value has the wrong type.
|
// Returns datamodel.ErrWrongKind if the value has the wrong type.
|
||||||
func (m Meta) GetNode(key string) (ipld.Node, error) {
|
func (m *Meta) GetNode(key string) (ipld.Node, error) {
|
||||||
v, ok := m.Values[key]
|
v, ok := m.Values[key]
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, ErrNotFound
|
return nil, ErrNotFound
|
||||||
@@ -92,7 +92,7 @@ func (m Meta) GetNode(key string) (ipld.Node, error) {
|
|||||||
// Add adds a key/value pair in the meta set.
|
// Add adds a key/value pair in the meta set.
|
||||||
// Accepted types for the value are: bool, string, int, int32, int64, []byte,
|
// Accepted types for the value are: bool, string, int, int32, int64, []byte,
|
||||||
// and ipld.Node.
|
// and ipld.Node.
|
||||||
func (m Meta) Add(key string, val any) error {
|
func (m *Meta) Add(key string, val any) error {
|
||||||
switch val := val.(type) {
|
switch val := val.(type) {
|
||||||
case bool:
|
case bool:
|
||||||
m.Values[key] = basicnode.NewBool(val)
|
m.Values[key] = basicnode.NewBool(val)
|
||||||
|
|||||||
Reference in New Issue
Block a user