2024-08-30 22:06:59 +02:00
|
|
|
package delegation
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
_ "embed"
|
|
|
|
|
"fmt"
|
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
|
|
"github.com/ipld/go-ipld-prime"
|
|
|
|
|
"github.com/ipld/go-ipld-prime/datamodel"
|
2024-09-18 12:20:54 -04:00
|
|
|
"github.com/ipld/go-ipld-prime/node/bindnode"
|
2024-08-30 22:06:59 +02:00
|
|
|
"github.com/ipld/go-ipld-prime/schema"
|
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/tokens/internal/envelope"
|
2024-08-30 22:06:59 +02:00
|
|
|
)
|
|
|
|
|
|
2024-09-24 09:00:59 -04:00
|
|
|
// [Tag] is the string used as a key within the SigPayload that identifies
|
|
|
|
|
// that the TokenPayload is a delegation.
|
|
|
|
|
//
|
|
|
|
|
// [Tag]: https://github.com/ucan-wg/delegation/tree/v1_ipld#type-tag
|
2024-09-18 12:20:54 -04:00
|
|
|
const Tag = "ucan/dlg@1.0.0-rc.1"
|
|
|
|
|
|
2024-09-24 09:00:59 -04:00
|
|
|
// TODO: update the above Tag URL once the delegation specification is merged.
|
|
|
|
|
|
2024-08-30 22:06:59 +02:00
|
|
|
//go:embed delegation.ipldsch
|
|
|
|
|
var schemaBytes []byte
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
once sync.Once
|
|
|
|
|
ts *schema.TypeSystem
|
|
|
|
|
err error
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func mustLoadSchema() *schema.TypeSystem {
|
|
|
|
|
once.Do(func() {
|
|
|
|
|
ts, err = ipld.LoadSchemaBytes(schemaBytes)
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(fmt.Errorf("failed to load IPLD schema: %s", err))
|
|
|
|
|
}
|
|
|
|
|
return ts
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-18 13:57:40 -04:00
|
|
|
func payloadType() schema.Type {
|
2024-08-30 22:06:59 +02:00
|
|
|
return mustLoadSchema().TypeByName("Payload")
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-18 13:57:40 -04:00
|
|
|
var _ envelope.Tokener = (*tokenPayloadModel)(nil)
|
2024-09-18 12:20:54 -04:00
|
|
|
|
2024-09-18 13:57:40 -04:00
|
|
|
type tokenPayloadModel struct {
|
2024-08-30 22:06:59 +02:00
|
|
|
// Issuer DID (sender)
|
|
|
|
|
Iss string
|
|
|
|
|
// Audience DID (receiver)
|
|
|
|
|
Aud string
|
|
|
|
|
// Principal that the chain is about (the Subject)
|
|
|
|
|
// optional: can be nil
|
|
|
|
|
Sub *string
|
|
|
|
|
|
|
|
|
|
// The Command to eventually invoke
|
|
|
|
|
Cmd string
|
|
|
|
|
|
|
|
|
|
// The delegation policy
|
2024-09-02 02:26:48 +02:00
|
|
|
Pol datamodel.Node
|
2024-08-30 22:06:59 +02:00
|
|
|
|
|
|
|
|
// A unique, random nonce
|
|
|
|
|
Nonce []byte
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
// optional: can be nil
|
|
|
|
|
Nbf *int64
|
|
|
|
|
// The timestamp at which the Invocation becomes invalid
|
|
|
|
|
// optional: can be nil
|
|
|
|
|
Exp *int64
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-18 13:57:40 -04:00
|
|
|
func (e *tokenPayloadModel) Prototype() schema.TypedPrototype {
|
|
|
|
|
return bindnode.Prototype((*tokenPayloadModel)(nil), payloadType())
|
2024-09-18 12:20:54 -04:00
|
|
|
}
|
|
|
|
|
|
2024-09-18 13:57:40 -04:00
|
|
|
func (*tokenPayloadModel) Tag() string {
|
2024-09-18 12:20:54 -04:00
|
|
|
return Tag
|
|
|
|
|
}
|