Skip to content

Commit

Permalink
Merge branch 'v1' into v1-policy-subset-selection
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiobozzo committed Sep 24, 2024
2 parents d66b8e4 + 4a65550 commit bfd5f00
Show file tree
Hide file tree
Showing 39 changed files with 799 additions and 291 deletions.
82 changes: 0 additions & 82 deletions delegation/schema_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/libp2p/go-libp2p v0.36.3
github.com/multiformats/go-multibase v0.2.0
github.com/multiformats/go-multicodec v0.9.0
github.com/multiformats/go-multihash v0.2.3
github.com/multiformats/go-varint v0.0.7
github.com/stretchr/testify v1.9.0
gotest.tools/v3 v3.5.1
Expand All @@ -24,7 +25,6 @@ require (
github.com/mr-tron/base58 v1.2.0 // indirect
github.com/multiformats/go-base32 v0.1.0 // indirect
github.com/multiformats/go-base36 v0.2.0 // indirect
github.com/multiformats/go-multihash v0.2.3 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/polydawn/refmt v0.89.0 // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"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) {
Expand Down
18 changes: 17 additions & 1 deletion pkg/meta/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ package meta

import (
"errors"
"fmt"
"reflect"

"github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/datamodel"
"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")

// Meta is a container for meta key-value pairs in a UCAN token.
Expand Down Expand Up @@ -113,8 +117,20 @@ func (m *Meta) Add(key string, val any) error {
case datamodel.Node:
m.Values[key] = val
default:
panic("invalid value type")
return fmt.Errorf("%w: %s", ErrUnsupported, fqtn(val))
}
m.Keys = append(m.Keys, key)
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 changes: 23 additions & 0 deletions pkg/meta/meta_test.go
Original file line number Diff line number Diff line change
@@ -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")
})
}
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion capability/policy/ipld.go → pkg/policy/ipld.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/ipld/go-ipld-prime/must"
"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) {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion capability/policy/match.go → pkg/policy/match.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/ipld/go-ipld-prime/datamodel"
"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"
)

func (p Policy) Filter(sel selector.Selector) Policy {
Expand Down
4 changes: 2 additions & 2 deletions capability/policy/match_test.go → pkg/policy/match_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/ucan-wg/go-ucan/capability/policy/literal"
"github.com/ucan-wg/go-ucan/capability/policy/selector"
"github.com/ucan-wg/go-ucan/pkg/policy/literal"
"github.com/ucan-wg/go-ucan/pkg/policy/selector"
)

func TestMatch(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion capability/policy/policy.go → pkg/policy/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package policy
import (
"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 (
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/stretchr/testify/assert"
"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
Expand Down
90 changes: 24 additions & 66 deletions delegation/delegation.go → tokens/delegation/delegation.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
// Package delegation implements the UCAN [delegation] specification with
// an immutable Token type as well as methods to convert the Token to and
// from the [envelope]-enclosed, signed and DAG-CBOR-encoded form that
// should most commonly be used for transport and storage.
//
// [delegation]: https://github.com/ucan-wg/delegation/tree/v1_ipld
// [envelope]: https://github.com/ucan-wg/spec#envelope
package delegation

// TODO: change the "delegation" link above when the specification is merged

import (
"crypto/rand"
"errors"
"fmt"
"time"

"github.com/ipfs/go-cid"
"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/pkg/command"
"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.
type Token struct {
// Issuer DID (sender)
issuer did.DID
Expand All @@ -33,6 +44,8 @@ type Token struct {
notBefore *time.Time
// The timestamp at which the Invocation becomes invalid
expiration *time.Time
// The CID of the Token when enclosed in an Envelope and encoded to DAG-CBOR
cid cid.Cid
}

// New creates a validated Token from the provided parameters and options.
Expand All @@ -50,6 +63,7 @@ func New(privKey crypto.PrivKey, aud did.DID, cmd command.Command, pol policy.Po
policy: pol,
meta: meta.NewMeta(),
nonce: nil,
cid: cid.Undef,
}

for _, opt := range opts {
Expand Down Expand Up @@ -132,6 +146,13 @@ func (t *Token) Expiration() *time.Time {
return t.expiration
}

// CID returns the content identifier of the Token model when enclosed
// in an Envelope and encoded to DAG-CBOR.
// Returns cid.Undef if the token has not been serialized or deserialized yet.
func (t *Token) CID() cid.Cid {
return t.cid
}

func (t *Token) validate() error {
var errs error

Expand All @@ -151,70 +172,6 @@ func (t *Token) validate() error {
return errs
}

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.
// This function also serves as validation.
func tokenFromModel(m tokenPayloadModel) (*Token, error) {
Expand Down Expand Up @@ -277,6 +234,7 @@ func tokenFromModel(m tokenPayloadModel) (*Token, error) {
}

// generateNonce creates a 12-byte random nonce.
// TODO: some crypto scheme require more, is that our case?
func generateNonce() ([]byte, error) {
res := make([]byte, 12)
_, err := rand.Read(res)
Expand Down
File renamed without changes.
Loading

0 comments on commit bfd5f00

Please sign in to comment.