Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions ucan/capability.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
package ucan

import (
"encoding/json"
)

type jsonModel struct {
With Resource `json:"with"`
Can Ability `json:"can"`
Nb interface{} `json:"nb,omitempty"`
}

type capability[T any] struct {
can Ability
nb T
Expand All @@ -20,6 +30,14 @@ func (c *capability[T]) With() Resource {
return c.with
}

func (c *capability[T]) MarshalJSON() ([]byte, error) {
return json.Marshal(jsonModel{
With: c.with,
Can: c.can,
Nb: c.nb,
})
}

func NewCapability[Caveats any](can Ability, with Resource, nb Caveats) Capability[Caveats] {
return &capability[Caveats]{
can: can,
Expand Down
14 changes: 11 additions & 3 deletions ucan/ucan.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package ucan

import (
"encoding/json"

"github.com/ipld/go-ipld-prime"
"github.com/web3-storage/go-ucanto/did"
"github.com/web3-storage/go-ucanto/ucan/crypto"
Expand All @@ -15,11 +17,17 @@ type Resource = string
// It MUST have format `${string}/${string}` | "*"
type Ability = string

// UnknownCapability is a capability whose Nb type is unknown
type UnknownCapability interface {
json.Marshaler
Can() Ability
With() Resource
}

// Capability represents an ability that a UCAN holder can perform with some
// resource.
type Capability[Caveats any] interface {
Can() Ability
With() Resource
UnknownCapability
Nb() Caveats
}

Expand All @@ -35,7 +43,7 @@ type Link = ipld.Link
// It MUST have format `${number}.${number}.${number}`
type Version = string

// UTCUnixTimestamp is a timestamp in seconds since the Unix epoch.
// UTCUnixTimestamp is a timestamp in milliseconds since the Unix epoch.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

type UTCUnixTimestamp = uint64

// https://github.com/ucan-wg/spec/#324-nonce
Expand Down
63 changes: 63 additions & 0 deletions validator/datamodel/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package datamodel

import (
// for go:embed
_ "embed"
"fmt"

"github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/schema"
)

//go:embed errors.ipldsch
var errorsch []byte

var (
errorTypeSystem *schema.TypeSystem
)

func init() {
ts, err := ipld.LoadSchemaBytes(errorsch)
if err != nil {
panic(fmt.Errorf("failed to load IPLD schema: %s", err))
}
errorTypeSystem = ts
}

func InvalidAudienceType() schema.Type {
return errorTypeSystem.TypeByName("InvalidAudience")
}

type Delegation struct {
Audience string
}

type InvalidAudienceModel struct {
Name *string
Audience string
Delegation Delegation
Message string
Stack *string
}

type ExpiredModel struct {
Name *string
Message string
ExpiredAt int64
Stack *string
}

func ExpiredType() schema.Type {
return errorTypeSystem.TypeByName("Expired")
}

type NotValidBeforeModel struct {
Name *string
Message string
ValidAt int64
Stack *string
}

func NotValidBeforeType() schema.Type {
return errorTypeSystem.TypeByName("NotValidBefore")
}
25 changes: 25 additions & 0 deletions validator/datamodel/errors.ipldsch
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
type Delegation struct {
Audience String
}

type InvalidAudience struct {
Name optional String
Audience String
Delegation Delegation
Message String
Stack optional Sstring
}

type Expired struct {
Name optional String
Message String
ExpiredAt Integer
Stack optional String
}

type NotValidBefore struct {
Name optional String
Message String
ValidAt Integer
Stack optional String
}
Loading