2024-09-24 08:20:42 -04:00
|
|
|
// 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
|
2024-08-30 22:06:59 +02:00
|
|
|
package delegation
|
|
|
|
|
|
2024-09-24 08:20:42 -04:00
|
|
|
// TODO: change the "delegation" link above when the specification is merged
|
|
|
|
|
|
2024-08-30 22:06:59 +02:00
|
|
|
import (
|
2024-09-18 15:48:07 -04:00
|
|
|
"errors"
|
2024-08-30 22:06:59 +02:00
|
|
|
"fmt"
|
|
|
|
|
"time"
|
|
|
|
|
|
2024-09-05 18:49:01 +02:00
|
|
|
"github.com/ucan-wg/go-ucan/did"
|
2024-09-24 11:40:28 -04:00
|
|
|
"github.com/ucan-wg/go-ucan/pkg/command"
|
2024-09-19 10:48:25 +02:00
|
|
|
"github.com/ucan-wg/go-ucan/pkg/meta"
|
2024-09-24 11:40:28 -04:00
|
|
|
"github.com/ucan-wg/go-ucan/pkg/policy"
|
2024-12-02 11:37:03 +01:00
|
|
|
"github.com/ucan-wg/go-ucan/pkg/policy/limits"
|
2024-11-12 10:38:25 +01:00
|
|
|
"github.com/ucan-wg/go-ucan/token/internal/nonce"
|
2024-11-05 17:39:39 +01:00
|
|
|
"github.com/ucan-wg/go-ucan/token/internal/parse"
|
2024-08-30 22:06:59 +02:00
|
|
|
)
|
|
|
|
|
|
2024-09-24 08:20:42 -04:00
|
|
|
// Token is an immutable type that holds the fields of a UCAN delegation.
|
2024-09-18 14:21:53 -04:00
|
|
|
type Token struct {
|
2024-08-30 22:06:59 +02:00
|
|
|
// Issuer DID (sender)
|
2024-09-18 14:21:53 -04:00
|
|
|
issuer did.DID
|
2024-08-30 22:06:59 +02:00
|
|
|
// Audience DID (receiver)
|
2024-09-18 14:21:53 -04:00
|
|
|
audience did.DID
|
2024-08-30 22:06:59 +02:00
|
|
|
// Principal that the chain is about (the Subject)
|
2024-09-18 14:21:53 -04:00
|
|
|
subject did.DID
|
2024-08-30 22:06:59 +02:00
|
|
|
// The Command to eventually invoke
|
2024-09-19 11:16:33 +02:00
|
|
|
command command.Command
|
2024-08-30 22:06:59 +02:00
|
|
|
// The delegation policy
|
2024-09-18 14:21:53 -04:00
|
|
|
policy policy.Policy
|
2024-08-30 22:06:59 +02:00
|
|
|
// A unique, random nonce
|
2024-09-18 14:21:53 -04:00
|
|
|
nonce []byte
|
2024-08-30 22:06:59 +02:00
|
|
|
// Arbitrary Metadata
|
2024-09-19 10:48:25 +02:00
|
|
|
meta *meta.Meta
|
2024-08-30 22:06:59 +02:00
|
|
|
// "Not before" UTC Unix Timestamp in seconds (valid from), 53-bits integer
|
2024-09-18 14:21:53 -04:00
|
|
|
notBefore *time.Time
|
2024-08-30 22:06:59 +02:00
|
|
|
// The timestamp at which the Invocation becomes invalid
|
2024-09-18 14:21:53 -04:00
|
|
|
expiration *time.Time
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-18 15:48:07 -04:00
|
|
|
// New creates a validated Token from the provided parameters and options.
|
2024-09-24 15:31:34 -04:00
|
|
|
//
|
2024-11-20 12:34:24 +01:00
|
|
|
// When creating a delegated token, the Issuer's (iss) DID is assembled
|
2024-09-24 15:31:34 -04:00
|
|
|
// using the public key associated with the private key sent as the first
|
|
|
|
|
// parameter.
|
2024-11-20 15:59:13 +01:00
|
|
|
func New(iss, aud did.DID, cmd command.Command, pol policy.Policy, opts ...Option) (*Token, error) {
|
2024-09-18 15:48:07 -04:00
|
|
|
tkn := &Token{
|
|
|
|
|
issuer: iss,
|
|
|
|
|
audience: aud,
|
|
|
|
|
subject: did.Undef,
|
|
|
|
|
command: cmd,
|
|
|
|
|
policy: pol,
|
2024-09-19 10:48:25 +02:00
|
|
|
meta: meta.NewMeta(),
|
2024-09-19 11:16:33 +02:00
|
|
|
nonce: nil,
|
2024-09-18 15:48:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, opt := range opts {
|
|
|
|
|
if err := opt(tkn); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-20 15:59:13 +01:00
|
|
|
var err error
|
2024-09-19 11:16:33 +02:00
|
|
|
if len(tkn.nonce) == 0 {
|
2024-11-12 10:38:25 +01:00
|
|
|
tkn.nonce, err = nonce.Generate()
|
2024-09-19 11:16:33 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-18 15:48:07 -04:00
|
|
|
if err := tkn.validate(); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return tkn, nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-24 15:31:34 -04:00
|
|
|
// Root creates a validated UCAN delegation Token from the provided
|
|
|
|
|
// parameters and options.
|
|
|
|
|
//
|
|
|
|
|
// When creating a root token, both the Issuer's (iss) and Subject's
|
|
|
|
|
// (sub) DIDs are assembled from the public key associated with the
|
|
|
|
|
// private key passed as the first argument.
|
2024-11-20 15:59:13 +01:00
|
|
|
func Root(iss, aud did.DID, cmd command.Command, pol policy.Policy, opts ...Option) (*Token, error) {
|
|
|
|
|
opts = append(opts, WithSubject(iss))
|
2024-09-18 15:48:07 -04:00
|
|
|
|
2024-11-20 15:59:13 +01:00
|
|
|
return New(iss, aud, cmd, pol, opts...)
|
2024-09-18 15:48:07 -04:00
|
|
|
}
|
|
|
|
|
|
2024-09-18 14:21:53 -04:00
|
|
|
// Issuer returns the did.DID representing the Token's issuer.
|
|
|
|
|
func (t *Token) Issuer() did.DID {
|
|
|
|
|
return t.issuer
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Audience returns the did.DID representing the Token's audience.
|
|
|
|
|
func (t *Token) Audience() did.DID {
|
|
|
|
|
return t.audience
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Subject returns the did.DID representing the Token's subject.
|
|
|
|
|
//
|
|
|
|
|
// This field may be did.Undef for delegations that are [Powerlined] but
|
|
|
|
|
// must be equal to the value returned by the Issuer method for root
|
|
|
|
|
// tokens.
|
|
|
|
|
func (t *Token) Subject() did.DID {
|
|
|
|
|
return t.subject
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Command returns the capability's command.Command.
|
2024-09-19 11:16:33 +02:00
|
|
|
func (t *Token) Command() command.Command {
|
2024-09-18 14:21:53 -04:00
|
|
|
return t.command
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Policy returns the capability's policy.Policy.
|
|
|
|
|
func (t *Token) Policy() policy.Policy {
|
|
|
|
|
return t.policy
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Nonce returns the random Nonce encapsulated in this Token.
|
|
|
|
|
func (t *Token) Nonce() []byte {
|
|
|
|
|
return t.nonce
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Meta returns the Token's metadata.
|
2024-11-06 15:17:35 +01:00
|
|
|
func (t *Token) Meta() meta.ReadOnly {
|
|
|
|
|
return t.meta.ReadOnly()
|
2024-09-18 14:21:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NotBefore returns the time at which the Token becomes "active".
|
|
|
|
|
func (t *Token) NotBefore() *time.Time {
|
|
|
|
|
return t.notBefore
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Expiration returns the time at which the Token expires.
|
|
|
|
|
func (t *Token) Expiration() *time.Time {
|
|
|
|
|
return t.expiration
|
2024-08-30 22:06:59 +02:00
|
|
|
}
|
|
|
|
|
|
2024-10-18 10:48:47 +02:00
|
|
|
// IsValidNow verifies that the token can be used at the current time, based on expiration or "not before" fields.
|
|
|
|
|
// This does NOT do any other kind of verifications.
|
|
|
|
|
func (t *Token) IsValidNow() bool {
|
|
|
|
|
return t.IsValidAt(time.Now())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IsValidNow verifies that the token can be used at the given time, based on expiration or "not before" fields.
|
|
|
|
|
// This does NOT do any other kind of verifications.
|
|
|
|
|
func (t *Token) IsValidAt(ti time.Time) bool {
|
2024-11-13 12:40:25 -05:00
|
|
|
if t.expiration != nil && ti.After(*t.expiration) {
|
2024-10-18 10:48:47 +02:00
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
if t.notBefore != nil && ti.Before(*t.notBefore) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-18 15:48:07 -04:00
|
|
|
func (t *Token) validate() error {
|
|
|
|
|
var errs error
|
|
|
|
|
|
|
|
|
|
requiredDID := func(id did.DID, fieldname string) {
|
2024-09-19 11:16:33 +02:00
|
|
|
if !id.Defined() {
|
|
|
|
|
errs = errors.Join(errs, fmt.Errorf(`a valid did is required for %s: %s`, fieldname, id.String()))
|
2024-09-18 15:48:07 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
requiredDID(t.issuer, "Issuer")
|
|
|
|
|
requiredDID(t.audience, "Audience")
|
|
|
|
|
|
2024-09-19 11:16:33 +02:00
|
|
|
if len(t.nonce) < 12 {
|
|
|
|
|
errs = errors.Join(errs, fmt.Errorf("token nonce too small"))
|
2024-09-18 15:48:07 -04:00
|
|
|
}
|
|
|
|
|
|
2024-12-02 11:37:03 +01:00
|
|
|
if t.notBefore != nil {
|
|
|
|
|
if err := validateTimestamp(t.notBefore.Unix(), "nbf"); err != nil {
|
|
|
|
|
errs = errors.Join(errs, err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if t.expiration != nil {
|
|
|
|
|
if err := validateTimestamp(t.expiration.Unix(), "exp"); err != nil {
|
|
|
|
|
errs = errors.Join(errs, err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-18 15:48:07 -04:00
|
|
|
return errs
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-18 15:53:29 -04:00
|
|
|
// tokenFromModel build a decoded view of the raw IPLD data.
|
2024-08-30 22:06:59 +02:00
|
|
|
// This function also serves as validation.
|
2024-09-18 15:53:29 -04:00
|
|
|
func tokenFromModel(m tokenPayloadModel) (*Token, error) {
|
|
|
|
|
var (
|
|
|
|
|
tkn Token
|
|
|
|
|
err error
|
|
|
|
|
)
|
2024-08-30 22:06:59 +02:00
|
|
|
|
2024-09-18 15:53:29 -04:00
|
|
|
tkn.issuer, err = did.Parse(m.Iss)
|
2024-08-30 22:06:59 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("parse iss: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-05 17:39:39 +01:00
|
|
|
if tkn.audience, err = did.Parse(m.Aud); err != nil {
|
2024-08-30 22:06:59 +02:00
|
|
|
return nil, fmt.Errorf("parse audience: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-05 17:39:39 +01:00
|
|
|
if tkn.subject, err = parse.OptionalDID(m.Sub); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("parse subject: %w", err)
|
2024-08-30 22:06:59 +02:00
|
|
|
}
|
|
|
|
|
|
2024-11-05 17:39:39 +01:00
|
|
|
if tkn.command, err = command.Parse(m.Cmd); err != nil {
|
2024-09-02 02:34:00 +02:00
|
|
|
return nil, fmt.Errorf("parse command: %w", err)
|
|
|
|
|
}
|
2024-08-30 22:06:59 +02:00
|
|
|
|
2024-11-05 17:39:39 +01:00
|
|
|
if tkn.policy, err = policy.FromIPLD(m.Pol); err != nil {
|
2024-09-02 02:34:00 +02:00
|
|
|
return nil, fmt.Errorf("parse policy: %w", err)
|
|
|
|
|
}
|
2024-08-30 22:06:59 +02:00
|
|
|
|
|
|
|
|
if len(m.Nonce) == 0 {
|
|
|
|
|
return nil, fmt.Errorf("nonce is required")
|
|
|
|
|
}
|
2024-09-18 15:53:29 -04:00
|
|
|
tkn.nonce = m.Nonce
|
2024-08-30 22:06:59 +02:00
|
|
|
|
2024-10-24 13:43:52 -04:00
|
|
|
tkn.meta = m.Meta
|
2024-08-30 22:06:59 +02:00
|
|
|
|
2024-11-05 17:39:39 +01:00
|
|
|
tkn.notBefore = parse.OptionalTimestamp(m.Nbf)
|
|
|
|
|
tkn.expiration = parse.OptionalTimestamp(m.Exp)
|
2024-08-30 22:06:59 +02:00
|
|
|
|
2024-09-18 15:53:29 -04:00
|
|
|
if err := tkn.validate(); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &tkn, nil
|
2024-08-30 22:06:59 +02:00
|
|
|
}
|
2024-12-02 11:37:03 +01:00
|
|
|
|
|
|
|
|
func validateTimestamp(ts int64, field string) error {
|
|
|
|
|
if ts > limits.MaxInt53 || ts < limits.MinInt53 {
|
|
|
|
|
return fmt.Errorf("token %s timestamp %d exceeds safe integer bounds", field, ts)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|