Merge pull request #32 from ucan-wg/cleanups

Cleanups and Token interface
This commit is contained in:
Michael Muré
2024-10-01 17:20:52 +02:00
committed by GitHub
23 changed files with 94 additions and 52 deletions

View File

@@ -11,7 +11,7 @@ import (
"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/command"
"github.com/ucan-wg/go-ucan/pkg/policy" "github.com/ucan-wg/go-ucan/pkg/policy"
"github.com/ucan-wg/go-ucan/tokens/delegation" "github.com/ucan-wg/go-ucan/token/delegation"
) )
const ( const (

View File

@@ -12,11 +12,12 @@ import (
"github.com/ipld/go-ipld-prime/codec/dagcbor" "github.com/ipld/go-ipld-prime/codec/dagcbor"
"github.com/ipld/go-ipld-prime/codec/dagjson" "github.com/ipld/go-ipld-prime/codec/dagjson"
"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/pkg/command" "github.com/ucan-wg/go-ucan/pkg/command"
"github.com/ucan-wg/go-ucan/pkg/policy" "github.com/ucan-wg/go-ucan/pkg/policy"
"github.com/ucan-wg/go-ucan/tokens/delegation" "github.com/ucan-wg/go-ucan/token/delegation"
"github.com/ucan-wg/go-ucan/tokens/internal/envelope" "github.com/ucan-wg/go-ucan/token/internal/envelope"
) )
// The following example shows how to create a delegation.Token with // The following example shows how to create a delegation.Token with
@@ -50,11 +51,11 @@ func ExampleNew() {
printCIDAndSealed(id, data) printCIDAndSealed(id, data)
//Output: // Output:
//CID (base58BTC): zdpuAw26pFuvZa2Z9YAtpZZnWN6VmnRFr7Z8LVY5c7RVWoxGY // CID (base58BTC): zdpuAw26pFuvZa2Z9YAtpZZnWN6VmnRFr7Z8LVY5c7RVWoxGY
//DAG-CBOR (base64) out: glhAmnAkgfjAx4SA5pzJmtaHRJtTGNpF1y6oqb4yhGoM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KQnFFOWpqTWlMeXhaQmc5YTZ4Z1BQS0pqTXFzczZaYzJkbWV0YaBlbm9uY2VMAAECAwQFBgcICQoL
//Converted to DAG-JSON out: // Converted to DAG-JSON out:
//[ // [
// { // {
// "/": { // "/": {
// "bytes": "mnAkgfjAx4SA5pzJmtaHRJtTGNpF1y6oqb4yhGoM2H2EUGbBYT4rVDjMKBgCjhdGHjipm00L8iR5SsQh3sIEBQ" // "bytes": "mnAkgfjAx4SA5pzJmtaHRJtTGNpF1y6oqb4yhGoM2H2EUGbBYT4rVDjMKBgCjhdGHjipm00L8iR5SsQh3sIEBQ"
@@ -115,7 +116,7 @@ func ExampleNew() {
// "sub": "did:key:z6MktA1uBdCpq4uJBqE9jjMiLyxZBg9a6xgPPKJjMqss6Zc2" // "sub": "did:key:z6MktA1uBdCpq4uJBqE9jjMiLyxZBg9a6xgPPKJjMqss6Zc2"
// } // }
// } // }
//] // ]
} }
// The following example shows how to create a UCAN root delegation.Token // The following example shows how to create a UCAN root delegation.Token
@@ -148,11 +149,11 @@ func ExampleRoot() {
printCIDAndSealed(id, data) printCIDAndSealed(id, data)
//Output: // Output:
//CID (base58BTC): zdpuAnbsR3e6DK8hBk5WA7KwbHYN6CKY4a3Bv1GNehvFYShQ8 // CID (base58BTC): zdpuAnbsR3e6DK8hBk5WA7KwbHYN6CKY4a3Bv1GNehvFYShQ8
//DAG-CBOR (base64) out: glhA67ASBczF/wlIP0ESENn+4ZNQKukjcTNz+fo7K2tYa6OUm0rWICDJJkDWm7lJeQt+KvSA+Y4ctHTQbAr3Lr7mDqJhaEQ07QFxc3VjYW4vZGxnQDEuMC4wLXJjLjGoY2F1ZHg4ZGlkOmtleTp6Nk1rcTVZbWJKY1RyUEV4TkRpMjZpbXJUQ3BLaGVwakJGQlNIcXJCRE4yQXJQa3ZjY21kaC9mb28vYmFyY2V4cPZjaXNzeDhkaWQ6a2V5Ono2TWtwem4ybjNaR1QyVmFxTUdTUUMzdHptelY0VFM5UzcxaUZzRFhF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Ya6OUm0rWICDJJkDWm7lJeQt+KvSA+Y4ctHTQbAr3Lr7mDg"
@@ -213,7 +214,7 @@ func ExampleRoot() {
// "sub": "did:key:z6Mkpzn2n3ZGT2VaqMGSQC3tzmzV4TS9S71iFsDXE1WnoNH2" // "sub": "did:key:z6Mkpzn2n3ZGT2VaqMGSQC3tzmzV4TS9S71iFsDXE1WnoNH2"
// } // }
// } // }
//] // ]
} }
// The following example demonstrates how to get a delegation.Token from // The following example demonstrates how to get a delegation.Token from
@@ -236,18 +237,18 @@ func ExampleToken_FromSealed() {
fmt.Println("NotBefore (nbf):", tkn.NotBefore()) fmt.Println("NotBefore (nbf):", tkn.NotBefore())
fmt.Println("Expiration (exp):", tkn.Expiration()) fmt.Println("Expiration (exp):", tkn.Expiration())
//Output: // Output:
//DAG-CBOR (base64) in: glhAmnAkgfjAx4SA5pzJmtaHRJtTGNpF1y6oqb4yhGoM2H2EUGbBYT4rVDjMKBgCjhdGHjipm00L8iR5SsQh3sIEBaJhaEQ07QFxc3VjYW4vZGxnQDEuMC4wLXJjLjGoY2F1ZHg4ZGlkOmtleTp6Nk1rcTVZbWJKY1RyUEV4TkRpMjZpbXJUQ3BLaGVwakJGQlNIcXJCRE4yQXJQa3ZjY21kaC9mb28vYmFyY2V4cPZjaXNzeDhkaWQ6a2V5Ono2TWtwem4ybjNaR1QyVmFxTUdTUUMzdHptelY0VFM5UzcxaUZzRFhFMVdub05IMmNwb2yDg2I9PWcuc3RhdHVzZWRyYWZ0g2NhbGxpLnJldmlld2Vyg2RsaWtlZi5lbWFpbG0qQGV4YW1wbGUuY29tg2NhbnllLnRhZ3OCYm9ygoNiPT1hLmRuZXdzg2I9PWEuZXByZXNzY3N1Yng4ZGlkOmtleTp6Nk1rdEExdUJkQ3BxNHVKQnFFOWpqTWlMeXhaQmc5YTZ4Z1BQS0pqTXFzczZaYzJkbWV0YaBlbm9uY2VMAAECAwQFBgcICQoL // DAG-CBOR (base64) in: glhAmnAkgfjAx4SA5pzJmtaHRJtTGNpF1y6oqb4yhGoM2H2EUGbBYT4rVDjMKBgCjhdGHjipm00L8iR5SsQh3sIEBaJhaEQ07QFxc3VjYW4vZGxnQDEuMC4wLXJjLjGoY2F1ZHg4ZGlkOmtleTp6Nk1rcTVZbWJKY1RyUEV4TkRpMjZpbXJUQ3BLaGVwakJGQlNIcXJCRE4yQXJQa3ZjY21kaC9mb28vYmFyY2V4cPZjaXNzeDhkaWQ6a2V5Ono2TWtwem4ybjNaR1QyVmFxTUdTUUMzdHptelY0VFM5UzcxaUZzRFhFMVdub05IMmNwb2yDg2I9PWcuc3RhdHVzZWRyYWZ0g2NhbGxpLnJldmlld2Vyg2RsaWtlZi5lbWFpbG0qQGV4YW1wbGUuY29tg2NhbnllLnRhZ3OCYm9ygoNiPT1hLmRuZXdzg2I9PWEuZXByZXNzY3N1Yng4ZGlkOmtleTp6Nk1rdEExdUJkQ3BxNHVKQnFFOWpqTWlMeXhaQmc5YTZ4Z1BQS0pqTXFzczZaYzJkbWV0YaBlbm9uY2VMAAECAwQFBgcICQoL
//CID (base58BTC): zdpuAw26pFuvZa2Z9YAtpZZnWN6VmnRFr7Z8LVY5c7RVWoxGY // CID (base58BTC): zdpuAw26pFuvZa2Z9YAtpZZnWN6VmnRFr7Z8LVY5c7RVWoxGY
//Issuer (iss): did:key:z6Mkpzn2n3ZGT2VaqMGSQC3tzmzV4TS9S71iFsDXE1WnoNH2 // Issuer (iss): did:key:z6Mkpzn2n3ZGT2VaqMGSQC3tzmzV4TS9S71iFsDXE1WnoNH2
//Audience (aud): did:key:z6Mkq5YmbJcTrPExNDi26imrTCpKhepjBFBSHqrBDN2ArPkv // Audience (aud): did:key:z6Mkq5YmbJcTrPExNDi26imrTCpKhepjBFBSHqrBDN2ArPkv
//Subject (sub): did:key:z6MktA1uBdCpq4uJBqE9jjMiLyxZBg9a6xgPPKJjMqss6Zc2 // Subject (sub): did:key:z6MktA1uBdCpq4uJBqE9jjMiLyxZBg9a6xgPPKJjMqss6Zc2
//Command (cmd): /foo/bar // Command (cmd): /foo/bar
//Policy (pol): TODO // Policy (pol): TODO
//Nonce (nonce): 000102030405060708090a0b // Nonce (nonce): 000102030405060708090a0b
//Meta (meta): TODO // Meta (meta): TODO
//NotBefore (nbf): <nil> // NotBefore (nbf): <nil>
//Expiration (exp): <nil> // Expiration (exp): <nil>
} }
func exampleCBORData() []byte { func exampleCBORData() []byte {

View File

@@ -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/tokens/internal/envelope" "github.com/ucan-wg/go-ucan/token/internal/envelope"
) )
// ToSealed wraps the delegation token in an envelope, generates the // ToSealed wraps the delegation token in an envelope, generates the
@@ -32,7 +32,7 @@ func (t *Token) ToSealed(privKey crypto.PrivKey) ([]byte, cid.Cid, error) {
return data, id, nil return data, id, nil
} }
// ToSealedWriter is the same as Seal but accepts an io.Writer. // ToSealedWriter is the same as ToSealed but accepts an io.Writer.
func (t *Token) ToSealedWriter(w io.Writer, privKey crypto.PrivKey) (cid.Cid, error) { func (t *Token) ToSealedWriter(w io.Writer, privKey crypto.PrivKey) (cid.Cid, error) {
cidWriter := envelope.NewCIDWriter(w) cidWriter := envelope.NewCIDWriter(w)
@@ -93,7 +93,7 @@ func (t *Token) Encode(privKey crypto.PrivKey, encFn codec.Encoder) ([]byte, err
return ipld.Encode(node, encFn) return ipld.Encode(node, encFn)
} }
// EncodeWriter is the same as Encode but accepts an io.Writer. // EncodeWriter is the same as Encode, but accepts an io.Writer.
func (t *Token) EncodeWriter(w io.Writer, privKey crypto.PrivKey, encFn codec.Encoder) error { func (t *Token) EncodeWriter(w io.Writer, privKey crypto.PrivKey, encFn codec.Encoder) error {
node, err := t.toIPLD(privKey) node, err := t.toIPLD(privKey)
if err != nil { if err != nil {
@@ -108,7 +108,7 @@ func (t *Token) ToDagCbor(privKey crypto.PrivKey) ([]byte, error) {
return t.Encode(privKey, dagcbor.Encode) return t.Encode(privKey, dagcbor.Encode)
} }
// ToDagCborWriter is the same as ToDagCbor but it accepts an io.Writer. // ToDagCborWriter is the same as ToDagCbor, but it accepts an io.Writer.
func (t *Token) ToDagCborWriter(w io.Writer, privKey crypto.PrivKey) error { func (t *Token) ToDagCborWriter(w io.Writer, privKey crypto.PrivKey) error {
return t.EncodeWriter(w, privKey, dagcbor.Encode) return t.EncodeWriter(w, privKey, dagcbor.Encode)
} }
@@ -118,7 +118,7 @@ func (t *Token) ToDagJson(privKey crypto.PrivKey) ([]byte, error) {
return t.Encode(privKey, dagjson.Encode) return t.Encode(privKey, dagjson.Encode)
} }
// ToDagJsonWriter is the same as ToDagJson but it accepts an io.Writer. // ToDagJsonWriter is the same as ToDagJson, but it accepts an io.Writer.
func (t *Token) ToDagJsonWriter(w io.Writer, privKey crypto.PrivKey) error { func (t *Token) ToDagJsonWriter(w io.Writer, privKey crypto.PrivKey) error {
return t.EncodeWriter(w, privKey, dagjson.Encode) return t.EncodeWriter(w, privKey, dagjson.Encode)
} }

View File

@@ -11,7 +11,7 @@ import (
"github.com/ipld/go-ipld-prime/schema" "github.com/ipld/go-ipld-prime/schema"
"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" "github.com/ucan-wg/go-ucan/token/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

View File

@@ -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/tokens/delegation" "github.com/ucan-wg/go-ucan/token/delegation"
"github.com/ucan-wg/go-ucan/tokens/internal/envelope" "github.com/ucan-wg/go-ucan/token/internal/envelope"
) )
//go:embed delegation.ipldsch //go:embed delegation.ipldsch

View File

@@ -1,9 +1,9 @@
package tokens package token
import ( import (
"github.com/ipld/go-ipld-prime/datamodel" "github.com/ipld/go-ipld-prime/datamodel"
"github.com/ucan-wg/go-ucan/tokens/internal/envelope" "github.com/ucan-wg/go-ucan/token/internal/envelope"
) )
type Info = envelope.Info type Info = envelope.Info

41
token/interface.go Normal file
View File

@@ -0,0 +1,41 @@
package token
import (
"io"
"github.com/ipfs/go-cid"
"github.com/ipld/go-ipld-prime/codec"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/ucan-wg/go-ucan/did"
"github.com/ucan-wg/go-ucan/pkg/meta"
)
type Token interface {
Marshaller
// Issuer returns the did.DID representing the Token's issuer.
Issuer() did.DID
// Meta returns the Token's metadata.
Meta() *meta.Meta
}
type Marshaller interface {
// ToSealed wraps the token in an envelope, generates the signature, encodes
// the result to DAG-CBOR and calculates the CID of the resulting binary data.
ToSealed(privKey crypto.PrivKey) ([]byte, cid.Cid, error)
// ToSealedWriter is the same as ToSealed but accepts an io.Writer.
ToSealedWriter(w io.Writer, privKey crypto.PrivKey) (cid.Cid, error)
// Encode marshals a Token to the format specified by the provided codec.Encoder.
Encode(privKey crypto.PrivKey, encFn codec.Encoder) ([]byte, error)
// EncodeWriter is the same as Encode, but accepts an io.Writer.
EncodeWriter(w io.Writer, privKey crypto.PrivKey, encFn codec.Encoder) error
// ToDagCbor marshals the Token to the DAG-CBOR format.
ToDagCbor(privKey crypto.PrivKey) ([]byte, error)
// ToDagCborWriter is the same as ToDagCbor, but it accepts an io.Writer.
ToDagCborWriter(w io.Writer, privKey crypto.PrivKey) error
// ToDagJson marshals the Token to the DAG-JSON format.
ToDagJson(privKey crypto.PrivKey) ([]byte, error)
// ToDagJsonWriter is the same as ToDagJson, but it accepts an io.Writer.
ToDagJsonWriter(w io.Writer, privKey crypto.PrivKey) error
}

View File

@@ -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/tokens/internal/envelope" "github.com/ucan-wg/go-ucan/token/internal/envelope"
) )
func TestCidFromBytes(t *testing.T) { func TestCidFromBytes(t *testing.T) {

View File

@@ -18,7 +18,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/tokens/internal/envelope" "github.com/ucan-wg/go-ucan/token/internal/envelope"
) )
const ( const (

View File

@@ -43,7 +43,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/tokens/internal/varsig" "github.com/ucan-wg/go-ucan/token/internal/varsig"
) )
const ( const (

View File

@@ -13,7 +13,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/tokens/internal/envelope" "github.com/ucan-wg/go-ucan/token/internal/envelope"
) )
func TestDecode(t *testing.T) { func TestDecode(t *testing.T) {

View File

@@ -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/tokens/internal/varsig" "github.com/ucan-wg/go-ucan/token/internal/varsig"
) )
func TestDecode(t *testing.T) { func TestDecode(t *testing.T) {

View File

@@ -1,4 +1,4 @@
package tokens package token
import ( import (
"fmt" "fmt"
@@ -10,8 +10,8 @@ import (
"github.com/ipld/go-ipld-prime/codec/dagjson" "github.com/ipld/go-ipld-prime/codec/dagjson"
"github.com/ipld/go-ipld-prime/datamodel" "github.com/ipld/go-ipld-prime/datamodel"
"github.com/ucan-wg/go-ucan/tokens/delegation" "github.com/ucan-wg/go-ucan/token/delegation"
"github.com/ucan-wg/go-ucan/tokens/internal/envelope" "github.com/ucan-wg/go-ucan/token/internal/envelope"
) )
// Decode unmarshals the input data using the format specified by the // Decode unmarshals the input data using the format specified by the
@@ -20,7 +20,7 @@ import (
// Token is invalid. // Token is invalid.
// Supported and returned types are: // Supported and returned types are:
// - delegation.Token // - delegation.Token
func Decode(b []byte, decFn codec.Decoder) (any, error) { func Decode(b []byte, decFn codec.Decoder) (Token, error) {
node, err := ipld.Decode(b, decFn) node, err := ipld.Decode(b, decFn)
if err != nil { if err != nil {
return nil, err return nil, err
@@ -29,7 +29,7 @@ func Decode(b []byte, decFn codec.Decoder) (any, error) {
} }
// DecodeReader is the same as Decode, but accept an io.Reader. // DecodeReader is the same as Decode, but accept an io.Reader.
func DecodeReader(r io.Reader, decFn codec.Decoder) (any, error) { func DecodeReader(r io.Reader, decFn codec.Decoder) (Token, error) {
node, err := ipld.DecodeStreaming(r, decFn) node, err := ipld.DecodeStreaming(r, decFn)
if err != nil { if err != nil {
return nil, err return nil, err
@@ -42,12 +42,12 @@ func DecodeReader(r io.Reader, decFn codec.Decoder) (any, error) {
// Token is invalid. // Token is invalid.
// Supported and returned types are: // Supported and returned types are:
// - delegation.Token // - delegation.Token
func FromDagCbor(b []byte) (any, error) { func FromDagCbor(b []byte) (Token, error) {
return Decode(b, dagcbor.Decode) return Decode(b, dagcbor.Decode)
} }
// FromDagCborReader is the same as FromDagCbor, but accept an io.Reader. // FromDagCborReader is the same as FromDagCbor, but accept an io.Reader.
func FromDagCborReader(r io.Reader) (any, error) { func FromDagCborReader(r io.Reader) (Token, error) {
return DecodeReader(r, dagcbor.Decode) return DecodeReader(r, dagcbor.Decode)
} }
@@ -56,16 +56,16 @@ func FromDagCborReader(r io.Reader) (any, error) {
// Token is invalid. // Token is invalid.
// Supported and returned types are: // Supported and returned types are:
// - delegation.Token // - delegation.Token
func FromDagJson(b []byte) (any, error) { func FromDagJson(b []byte) (Token, error) {
return Decode(b, dagjson.Decode) return Decode(b, dagjson.Decode)
} }
// FromDagJsonReader is the same as FromDagJson, but accept an io.Reader. // FromDagJsonReader is the same as FromDagJson, but accept an io.Reader.
func FromDagJsonReader(r io.Reader) (any, error) { func FromDagJsonReader(r io.Reader) (Token, error) {
return DecodeReader(r, dagjson.Decode) return DecodeReader(r, dagjson.Decode)
} }
func fromIPLD(node datamodel.Node) (any, error) { func fromIPLD(node datamodel.Node) (Token, error) {
tag, err := envelope.FindTag(node) tag, err := envelope.FindTag(node)
if err != nil { if err != nil {
return nil, err return nil, err