2024-10-01 17:08:57 +02:00
|
|
|
package token
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"io"
|
2024-10-18 10:48:47 +02:00
|
|
|
"time"
|
2024-10-01 17:08:57 +02:00
|
|
|
|
2025-07-31 14:43:42 +02:00
|
|
|
"github.com/MetaMask/go-did-it/crypto"
|
2024-10-01 17:08:57 +02:00
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
|
"github.com/ipld/go-ipld-prime/codec"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Token interface {
|
|
|
|
|
Marshaller
|
|
|
|
|
|
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.
|
|
|
|
|
IsValidNow() bool
|
2025-08-05 16:54:18 +02:00
|
|
|
// IsValidAt verifies that the token can be used at the given time, based on expiration or "not before" fields.
|
2024-10-18 10:48:47 +02:00
|
|
|
// This does NOT do any other kind of verifications.
|
|
|
|
|
IsValidAt(t time.Time) bool
|
2024-10-01 17:08:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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.
|
2025-07-31 14:43:42 +02:00
|
|
|
ToSealed(privKey crypto.PrivateKeySigningBytes) ([]byte, cid.Cid, error)
|
2024-10-01 17:08:57 +02:00
|
|
|
// ToSealedWriter is the same as ToSealed but accepts an io.Writer.
|
2025-07-31 14:43:42 +02:00
|
|
|
ToSealedWriter(w io.Writer, privKey crypto.PrivateKeySigningBytes) (cid.Cid, error)
|
2024-10-01 17:08:57 +02:00
|
|
|
// Encode marshals a Token to the format specified by the provided codec.Encoder.
|
2025-07-31 14:43:42 +02:00
|
|
|
Encode(privKey crypto.PrivateKeySigningBytes, encFn codec.Encoder) ([]byte, error)
|
2024-10-01 17:08:57 +02:00
|
|
|
// EncodeWriter is the same as Encode, but accepts an io.Writer.
|
2025-07-31 14:43:42 +02:00
|
|
|
EncodeWriter(w io.Writer, privKey crypto.PrivateKeySigningBytes, encFn codec.Encoder) error
|
2024-10-01 17:08:57 +02:00
|
|
|
// ToDagCbor marshals the Token to the DAG-CBOR format.
|
2025-07-31 14:43:42 +02:00
|
|
|
ToDagCbor(privKey crypto.PrivateKeySigningBytes) ([]byte, error)
|
2024-10-01 17:08:57 +02:00
|
|
|
// ToDagCborWriter is the same as ToDagCbor, but it accepts an io.Writer.
|
2025-07-31 14:43:42 +02:00
|
|
|
ToDagCborWriter(w io.Writer, privKey crypto.PrivateKeySigningBytes) error
|
2024-10-01 17:08:57 +02:00
|
|
|
// ToDagJson marshals the Token to the DAG-JSON format.
|
2025-07-31 14:43:42 +02:00
|
|
|
ToDagJson(privKey crypto.PrivateKeySigningBytes) ([]byte, error)
|
2024-10-01 17:08:57 +02:00
|
|
|
// ToDagJsonWriter is the same as ToDagJson, but it accepts an io.Writer.
|
2025-07-31 14:43:42 +02:00
|
|
|
ToDagJsonWriter(w io.Writer, privKey crypto.PrivateKeySigningBytes) error
|
2024-10-01 17:08:57 +02:00
|
|
|
}
|
2025-01-23 17:12:26 +01:00
|
|
|
|
|
|
|
|
// Bundle carries together a decoded token with its Cid and raw signed data.
|
2025-01-29 14:28:13 +01:00
|
|
|
type Bundle struct {
|
2025-01-23 17:12:26 +01:00
|
|
|
Cid cid.Cid
|
2025-01-29 14:28:13 +01:00
|
|
|
Decoded Token
|
2025-01-23 17:12:26 +01:00
|
|
|
Sealed []byte
|
|
|
|
|
}
|