Compare commits
4 Commits
feat/calcu
...
feat/reorg
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
93dd3ef719 | ||
|
|
6075c19957 | ||
|
|
6161f2e440 | ||
|
|
5202056cc7 |
@@ -5,7 +5,7 @@ import (
|
|||||||
|
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"github.com/ucan-wg/go-ucan/capability/command"
|
"github.com/ucan-wg/go-ucan/pkg/command"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestTop(t *testing.T) {
|
func TestTop(t *testing.T) {
|
||||||
@@ -2,12 +2,16 @@ package meta
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
|
||||||
"github.com/ipld/go-ipld-prime"
|
"github.com/ipld/go-ipld-prime"
|
||||||
"github.com/ipld/go-ipld-prime/datamodel"
|
"github.com/ipld/go-ipld-prime/datamodel"
|
||||||
"github.com/ipld/go-ipld-prime/node/basicnode"
|
"github.com/ipld/go-ipld-prime/node/basicnode"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var ErrUnsupported = errors.New("failure adding unsupported type to meta")
|
||||||
|
|
||||||
var ErrNotFound = errors.New("key-value not found in meta")
|
var ErrNotFound = errors.New("key-value not found in meta")
|
||||||
|
|
||||||
// Meta is a container for meta key-value pairs in a UCAN token.
|
// Meta is a container for meta key-value pairs in a UCAN token.
|
||||||
@@ -113,8 +117,20 @@ func (m *Meta) Add(key string, val any) error {
|
|||||||
case datamodel.Node:
|
case datamodel.Node:
|
||||||
m.Values[key] = val
|
m.Values[key] = val
|
||||||
default:
|
default:
|
||||||
panic("invalid value type")
|
return fmt.Errorf("%w: %s", ErrUnsupported, fqtn(val))
|
||||||
}
|
}
|
||||||
m.Keys = append(m.Keys, key)
|
m.Keys = append(m.Keys, key)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func fqtn(val any) string {
|
||||||
|
var name string
|
||||||
|
|
||||||
|
t := reflect.TypeOf(val)
|
||||||
|
for t.Kind() == reflect.Pointer {
|
||||||
|
name += "*"
|
||||||
|
t = t.Elem()
|
||||||
|
}
|
||||||
|
|
||||||
|
return name + t.PkgPath() + "." + t.Name()
|
||||||
|
}
|
||||||
|
|||||||
23
pkg/meta/meta_test.go
Normal file
23
pkg/meta/meta_test.go
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
package meta_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
"github.com/ucan-wg/go-ucan/pkg/meta"
|
||||||
|
"gotest.tools/v3/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestMeta_Add(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
type Unsupported struct{}
|
||||||
|
|
||||||
|
t.Run("error if not primative or Node", func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
err := (&meta.Meta{}).Add("invalid", &Unsupported{})
|
||||||
|
require.ErrorIs(t, err, meta.ErrUnsupported)
|
||||||
|
assert.ErrorContains(t, err, "*github.com/ucan-wg/go-ucan/pkg/meta_test.Unsupported")
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -9,7 +9,7 @@ import (
|
|||||||
"github.com/ipld/go-ipld-prime/must"
|
"github.com/ipld/go-ipld-prime/must"
|
||||||
"github.com/ipld/go-ipld-prime/node/basicnode"
|
"github.com/ipld/go-ipld-prime/node/basicnode"
|
||||||
|
|
||||||
"github.com/ucan-wg/go-ucan/capability/policy/selector"
|
"github.com/ucan-wg/go-ucan/pkg/policy/selector"
|
||||||
)
|
)
|
||||||
|
|
||||||
func FromIPLD(node datamodel.Node) (Policy, error) {
|
func FromIPLD(node datamodel.Node) (Policy, error) {
|
||||||
@@ -8,7 +8,7 @@ import (
|
|||||||
"github.com/ipld/go-ipld-prime/datamodel"
|
"github.com/ipld/go-ipld-prime/datamodel"
|
||||||
"github.com/ipld/go-ipld-prime/must"
|
"github.com/ipld/go-ipld-prime/must"
|
||||||
|
|
||||||
"github.com/ucan-wg/go-ucan/capability/policy/selector"
|
"github.com/ucan-wg/go-ucan/pkg/policy/selector"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Match determines if the IPLD node matches the policy document.
|
// Match determines if the IPLD node matches the policy document.
|
||||||
@@ -11,8 +11,8 @@ import (
|
|||||||
"github.com/ipld/go-ipld-prime/node/basicnode"
|
"github.com/ipld/go-ipld-prime/node/basicnode"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"github.com/ucan-wg/go-ucan/capability/policy/literal"
|
"github.com/ucan-wg/go-ucan/pkg/policy/literal"
|
||||||
"github.com/ucan-wg/go-ucan/capability/policy/selector"
|
"github.com/ucan-wg/go-ucan/pkg/policy/selector"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestMatch(t *testing.T) {
|
func TestMatch(t *testing.T) {
|
||||||
@@ -5,7 +5,7 @@ package policy
|
|||||||
import (
|
import (
|
||||||
"github.com/ipld/go-ipld-prime"
|
"github.com/ipld/go-ipld-prime"
|
||||||
|
|
||||||
"github.com/ucan-wg/go-ucan/capability/policy/selector"
|
"github.com/ucan-wg/go-ucan/pkg/policy/selector"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -12,7 +12,7 @@ import (
|
|||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"github.com/ucan-wg/go-ucan/capability/policy/selector"
|
"github.com/ucan-wg/go-ucan/pkg/policy/selector"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TestSupported Forms runs tests against the Selector according to the
|
// TestSupported Forms runs tests against the Selector according to the
|
||||||
@@ -18,10 +18,10 @@ import (
|
|||||||
"github.com/ipfs/go-cid"
|
"github.com/ipfs/go-cid"
|
||||||
"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/policy"
|
|
||||||
"github.com/ucan-wg/go-ucan/did"
|
"github.com/ucan-wg/go-ucan/did"
|
||||||
|
"github.com/ucan-wg/go-ucan/pkg/command"
|
||||||
"github.com/ucan-wg/go-ucan/pkg/meta"
|
"github.com/ucan-wg/go-ucan/pkg/meta"
|
||||||
|
"github.com/ucan-wg/go-ucan/pkg/policy"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Token is an immutable type that holds the fields of a UCAN delegation.
|
// Token is an immutable type that holds the fields of a UCAN delegation.
|
||||||
@@ -172,73 +172,6 @@ func (t *Token) validate() error {
|
|||||||
return errs
|
return errs
|
||||||
}
|
}
|
||||||
|
|
||||||
// Option is a type that allows optional fields to be set during the
|
|
||||||
// creation of a Token.
|
|
||||||
type Option func(*Token) error
|
|
||||||
|
|
||||||
// WithExpiration set's the Token's optional "expiration" field to the
|
|
||||||
// value of the provided time.Time.
|
|
||||||
func WithExpiration(exp time.Time) Option {
|
|
||||||
return func(t *Token) error {
|
|
||||||
if exp.Before(time.Now()) {
|
|
||||||
return fmt.Errorf("a Token's expiration should be set to a time in the future: %s", exp.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
t.expiration = &exp
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// WithMeta adds a key/value pair in the "meta" field.
|
|
||||||
//
|
|
||||||
// WithMeta can be used multiple times in the same call.
|
|
||||||
// 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 t.meta.Add(key, val)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// WithNotBefore set's the Token's optional "notBefore" field to the value
|
|
||||||
// of the provided time.Time.
|
|
||||||
func WithNotBefore(nbf time.Time) Option {
|
|
||||||
return func(t *Token) error {
|
|
||||||
if nbf.Before(time.Now()) {
|
|
||||||
return fmt.Errorf("a Token's \"not before\" field should be set to a time in the future: %s", nbf.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
t.notBefore = &nbf
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// WithSubject sets the Tokens's optional "subject" field to the value of
|
|
||||||
// provided did.DID.
|
|
||||||
//
|
|
||||||
// This Option should only be used with the New constructor - since
|
|
||||||
// Subject is a required parameter when creating a Token via the Root
|
|
||||||
// constructor, any value provided via this Option will be silently
|
|
||||||
// overwritten.
|
|
||||||
func WithSubject(sub did.DID) Option {
|
|
||||||
return func(t *Token) error {
|
|
||||||
t.subject = sub
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// WithNonce sets the Token's nonce with the given value.
|
|
||||||
// If this option is not used, a random 12-byte nonce is generated for this required field.
|
|
||||||
func WithNonce(nonce []byte) Option {
|
|
||||||
return func(t *Token) error {
|
|
||||||
t.nonce = nonce
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// tokenFromModel build a decoded view of the raw IPLD data.
|
// tokenFromModel build a decoded view of the raw IPLD data.
|
||||||
// This function also serves as validation.
|
// This function also serves as validation.
|
||||||
func tokenFromModel(m tokenPayloadModel) (*Token, error) {
|
func tokenFromModel(m tokenPayloadModel) (*Token, error) {
|
||||||
@@ -8,10 +8,10 @@ import (
|
|||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"gotest.tools/v3/golden"
|
"gotest.tools/v3/golden"
|
||||||
|
|
||||||
"github.com/ucan-wg/go-ucan/capability/command"
|
|
||||||
"github.com/ucan-wg/go-ucan/capability/policy"
|
|
||||||
"github.com/ucan-wg/go-ucan/delegation"
|
|
||||||
"github.com/ucan-wg/go-ucan/did"
|
"github.com/ucan-wg/go-ucan/did"
|
||||||
|
"github.com/ucan-wg/go-ucan/pkg/command"
|
||||||
|
"github.com/ucan-wg/go-ucan/pkg/policy"
|
||||||
|
"github.com/ucan-wg/go-ucan/tokens/delegation"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -12,7 +12,7 @@ import (
|
|||||||
"github.com/libp2p/go-libp2p/core/crypto"
|
"github.com/libp2p/go-libp2p/core/crypto"
|
||||||
|
|
||||||
"github.com/ucan-wg/go-ucan/did"
|
"github.com/ucan-wg/go-ucan/did"
|
||||||
"github.com/ucan-wg/go-ucan/internal/envelope"
|
"github.com/ucan-wg/go-ucan/tokens/internal/envelope"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ToSealed wraps the delegation token in an envelope, generates the
|
// ToSealed wraps the delegation token in an envelope, generates the
|
||||||
72
tokens/delegation/options.go
Normal file
72
tokens/delegation/options.go
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
package delegation
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/ucan-wg/go-ucan/did"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Option is a type that allows optional fields to be set during the
|
||||||
|
// creation of a Token.
|
||||||
|
type Option func(*Token) error
|
||||||
|
|
||||||
|
// WithExpiration set's the Token's optional "expiration" field to the
|
||||||
|
// value of the provided time.Time.
|
||||||
|
func WithExpiration(exp time.Time) Option {
|
||||||
|
return func(t *Token) error {
|
||||||
|
if exp.Before(time.Now()) {
|
||||||
|
return fmt.Errorf("a Token's expiration should be set to a time in the future: %s", exp.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
t.expiration = &exp
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithMeta adds a key/value pair in the "meta" field.
|
||||||
|
//
|
||||||
|
// WithMeta can be used multiple times in the same call.
|
||||||
|
// 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 t.meta.Add(key, val)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithNotBefore set's the Token's optional "notBefore" field to the value
|
||||||
|
// of the provided time.Time.
|
||||||
|
func WithNotBefore(nbf time.Time) Option {
|
||||||
|
return func(t *Token) error {
|
||||||
|
if nbf.Before(time.Now()) {
|
||||||
|
return fmt.Errorf("a Token's \"not before\" field should be set to a time in the future: %s", nbf.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
t.notBefore = &nbf
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithSubject sets the Tokens's optional "subject" field to the value of
|
||||||
|
// provided did.DID.
|
||||||
|
//
|
||||||
|
// This Option should only be used with the New constructor - since
|
||||||
|
// Subject is a required parameter when creating a Token via the Root
|
||||||
|
// constructor, any value provided via this Option will be silently
|
||||||
|
// overwritten.
|
||||||
|
func WithSubject(sub did.DID) Option {
|
||||||
|
return func(t *Token) error {
|
||||||
|
t.subject = sub
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithNonce sets the Token's nonce with the given value.
|
||||||
|
// If this option is not used, a random 12-byte nonce is generated for this required field.
|
||||||
|
func WithNonce(nonce []byte) Option {
|
||||||
|
return func(t *Token) error {
|
||||||
|
t.nonce = nonce
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -10,8 +10,8 @@ import (
|
|||||||
"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/pkg/meta"
|
"github.com/ucan-wg/go-ucan/pkg/meta"
|
||||||
|
"github.com/ucan-wg/go-ucan/tokens/internal/envelope"
|
||||||
)
|
)
|
||||||
|
|
||||||
// [Tag] is the string used as a key within the SigPayload that identifies
|
// [Tag] is the string used as a key within the SigPayload that identifies
|
||||||
@@ -11,8 +11,8 @@ import (
|
|||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"gotest.tools/v3/golden"
|
"gotest.tools/v3/golden"
|
||||||
|
|
||||||
"github.com/ucan-wg/go-ucan/delegation"
|
"github.com/ucan-wg/go-ucan/tokens/delegation"
|
||||||
"github.com/ucan-wg/go-ucan/internal/envelope"
|
"github.com/ucan-wg/go-ucan/tokens/internal/envelope"
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:embed delegation.ipldsch
|
//go:embed delegation.ipldsch
|
||||||
@@ -11,7 +11,7 @@ import (
|
|||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"gotest.tools/v3/golden"
|
"gotest.tools/v3/golden"
|
||||||
|
|
||||||
"github.com/ucan-wg/go-ucan/internal/envelope"
|
"github.com/ucan-wg/go-ucan/tokens/internal/envelope"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestCidFromBytes(t *testing.T) {
|
func TestCidFromBytes(t *testing.T) {
|
||||||
@@ -16,7 +16,7 @@ import (
|
|||||||
"github.com/ipld/go-ipld-prime/schema"
|
"github.com/ipld/go-ipld-prime/schema"
|
||||||
"github.com/libp2p/go-libp2p/core/crypto"
|
"github.com/libp2p/go-libp2p/core/crypto"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"github.com/ucan-wg/go-ucan/internal/envelope"
|
"github.com/ucan-wg/go-ucan/tokens/internal/envelope"
|
||||||
"gotest.tools/v3/golden"
|
"gotest.tools/v3/golden"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -41,7 +41,7 @@ import (
|
|||||||
"github.com/libp2p/go-libp2p/core/crypto"
|
"github.com/libp2p/go-libp2p/core/crypto"
|
||||||
|
|
||||||
"github.com/ucan-wg/go-ucan/did"
|
"github.com/ucan-wg/go-ucan/did"
|
||||||
"github.com/ucan-wg/go-ucan/internal/varsig"
|
"github.com/ucan-wg/go-ucan/tokens/internal/varsig"
|
||||||
)
|
)
|
||||||
|
|
||||||
const varsigHeaderKey = "h"
|
const varsigHeaderKey = "h"
|
||||||
@@ -7,7 +7,7 @@ import (
|
|||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"github.com/ucan-wg/go-ucan/internal/envelope"
|
"github.com/ucan-wg/go-ucan/tokens/internal/envelope"
|
||||||
"gotest.tools/v3/golden"
|
"gotest.tools/v3/golden"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -8,7 +8,7 @@ import (
|
|||||||
"github.com/libp2p/go-libp2p/core/crypto/pb"
|
"github.com/libp2p/go-libp2p/core/crypto/pb"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
"github.com/ucan-wg/go-ucan/internal/varsig"
|
"github.com/ucan-wg/go-ucan/tokens/internal/varsig"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestDecode(t *testing.T) {
|
func TestDecode(t *testing.T) {
|
||||||
Reference in New Issue
Block a user