diff --git a/tokens/delegation/delegation.go b/tokens/delegation/delegation.go index 83a8555..b950bb9 100644 --- a/tokens/delegation/delegation.go +++ b/tokens/delegation/delegation.go @@ -172,73 +172,6 @@ func (t *Token) validate() error { return errs } -// Option is a type that allows optional fields to be set during the -// creation of a Token. -type Option func(*Token) error - -// WithExpiration set's the Token's optional "expiration" field to the -// value of the provided time.Time. -func WithExpiration(exp time.Time) Option { - return func(t *Token) error { - if exp.Before(time.Now()) { - return fmt.Errorf("a Token's expiration should be set to a time in the future: %s", exp.String()) - } - - t.expiration = &exp - - return nil - } -} - -// WithMeta adds a key/value pair in the "meta" field. -// -// WithMeta can be used multiple times in the same call. -// Accepted types for the value are: bool, string, int, int32, int64, []byte, -// and ipld.Node. -func WithMeta(key string, val any) Option { - return func(t *Token) error { - return t.meta.Add(key, val) - } -} - -// WithNotBefore set's the Token's optional "notBefore" field to the value -// of the provided time.Time. -func WithNotBefore(nbf time.Time) Option { - return func(t *Token) error { - if nbf.Before(time.Now()) { - return fmt.Errorf("a Token's \"not before\" field should be set to a time in the future: %s", nbf.String()) - } - - t.notBefore = &nbf - - return nil - } -} - -// WithSubject sets the Tokens's optional "subject" field to the value of -// provided did.DID. -// -// This Option should only be used with the New constructor - since -// Subject is a required parameter when creating a Token via the Root -// constructor, any value provided via this Option will be silently -// overwritten. -func WithSubject(sub did.DID) Option { - return func(t *Token) error { - t.subject = sub - - return nil - } -} - -// WithNonce sets the Token's nonce with the given value. -// If this option is not used, a random 12-byte nonce is generated for this required field. -func WithNonce(nonce []byte) Option { - return func(t *Token) error { - t.nonce = nonce - return nil - } -} - // tokenFromModel build a decoded view of the raw IPLD data. // This function also serves as validation. func tokenFromModel(m tokenPayloadModel) (*Token, error) { diff --git a/tokens/delegation/options.go b/tokens/delegation/options.go new file mode 100644 index 0000000..83ffa03 --- /dev/null +++ b/tokens/delegation/options.go @@ -0,0 +1,72 @@ +package delegation + +import ( + "fmt" + "time" + + "github.com/ucan-wg/go-ucan/did" +) + +// Option is a type that allows optional fields to be set during the +// creation of a Token. +type Option func(*Token) error + +// WithExpiration set's the Token's optional "expiration" field to the +// value of the provided time.Time. +func WithExpiration(exp time.Time) Option { + return func(t *Token) error { + if exp.Before(time.Now()) { + return fmt.Errorf("a Token's expiration should be set to a time in the future: %s", exp.String()) + } + + t.expiration = &exp + return nil + } +} + +// WithMeta adds a key/value pair in the "meta" field. +// +// WithMeta can be used multiple times in the same call. +// Accepted types for the value are: bool, string, int, int32, int64, []byte, +// and ipld.Node. +func WithMeta(key string, val any) Option { + return func(t *Token) error { + return t.meta.Add(key, val) + } +} + +// WithNotBefore set's the Token's optional "notBefore" field to the value +// of the provided time.Time. +func WithNotBefore(nbf time.Time) Option { + return func(t *Token) error { + if nbf.Before(time.Now()) { + return fmt.Errorf("a Token's \"not before\" field should be set to a time in the future: %s", nbf.String()) + } + + t.notBefore = &nbf + return nil + } +} + +// WithSubject sets the Tokens's optional "subject" field to the value of +// provided did.DID. +// +// This Option should only be used with the New constructor - since +// Subject is a required parameter when creating a Token via the Root +// constructor, any value provided via this Option will be silently +// overwritten. +func WithSubject(sub did.DID) Option { + return func(t *Token) error { + t.subject = sub + return nil + } +} + +// WithNonce sets the Token's nonce with the given value. +// If this option is not used, a random 12-byte nonce is generated for this required field. +func WithNonce(nonce []byte) Option { + return func(t *Token) error { + t.nonce = nonce + return nil + } +}