Files
ucan/capability/policy/policy.go

126 lines
2.9 KiB
Go
Raw Normal View History

2024-08-19 23:16:36 +02:00
package policy
// https://github.com/ucan-wg/delegation/blob/4094d5878b58f5d35055a3b93fccda0b8329ebae/README.md#policy
import (
2024-08-21 08:13:44 +02:00
"github.com/gobwas/glob"
2024-08-20 15:55:04 +02:00
"github.com/ipld/go-ipld-prime"
2024-08-30 23:11:51 +02:00
"github.com/ucan-wg/go-ucan/v1/capability/policy/selector"
2024-08-19 23:16:36 +02:00
)
const (
KindEqual = "==" // implemented by equality
KindGreaterThan = ">" // implemented by equality
KindGreaterThanOrEqual = ">=" // implemented by equality
KindLessThan = "<" // implemented by equality
KindLessThanOrEqual = "<=" // implemented by equality
KindNot = "not" // implemented by negation
KindAnd = "and" // implemented by connective
KindOr = "or" // implemented by connective
KindLike = "like" // implemented by wildcard
KindAll = "all" // implemented by quantifier
KindAny = "any" // implemented by quantifier
2024-08-19 23:16:36 +02:00
)
2024-09-01 20:27:54 +02:00
type Policy []Statement
2024-08-19 23:16:36 +02:00
type Statement interface {
Kind() string
}
type equality struct {
kind string
selector selector.Selector
2024-08-20 15:55:04 +02:00
value ipld.Node
2024-08-19 23:16:36 +02:00
}
func (e equality) Kind() string {
return e.kind
}
func Equal(selector selector.Selector, value ipld.Node) Statement {
return equality{KindEqual, selector, value}
2024-08-19 23:16:36 +02:00
}
func GreaterThan(selector selector.Selector, value ipld.Node) Statement {
return equality{KindGreaterThan, selector, value}
2024-08-19 23:16:36 +02:00
}
func GreaterThanOrEqual(selector selector.Selector, value ipld.Node) Statement {
return equality{KindGreaterThanOrEqual, selector, value}
2024-08-19 23:16:36 +02:00
}
func LessThan(selector selector.Selector, value ipld.Node) Statement {
return equality{KindLessThan, selector, value}
2024-08-19 23:16:36 +02:00
}
func LessThanOrEqual(selector selector.Selector, value ipld.Node) Statement {
return equality{KindLessThanOrEqual, selector, value}
2024-08-19 23:16:36 +02:00
}
type negation struct {
statement Statement
}
func (n negation) Kind() string {
return KindNot
2024-08-19 23:16:36 +02:00
}
func Not(stmt Statement) Statement {
2024-08-19 23:16:36 +02:00
return negation{stmt}
}
2024-09-01 20:27:54 +02:00
type connective struct {
kind string
2024-08-19 23:16:36 +02:00
statements []Statement
}
2024-09-01 20:27:54 +02:00
func (c connective) Kind() string {
return c.kind
2024-08-19 23:16:36 +02:00
}
func And(stmts ...Statement) Statement {
2024-09-01 20:27:54 +02:00
return connective{KindAnd, stmts}
2024-08-19 23:16:36 +02:00
}
func Or(stmts ...Statement) Statement {
2024-09-01 20:27:54 +02:00
return connective{KindOr, stmts}
2024-08-19 23:16:36 +02:00
}
type wildcard struct {
selector selector.Selector
2024-09-01 20:27:54 +02:00
pattern string
glob glob.Glob // not serialized
2024-08-19 23:16:36 +02:00
}
func (n wildcard) Kind() string {
return KindLike
2024-08-19 23:16:36 +02:00
}
func Like(selector selector.Selector, pattern string) (Statement, error) {
2024-09-01 20:27:54 +02:00
g, err := glob.Compile(pattern)
if err != nil {
return nil, err
}
return wildcard{selector, pattern, g}, nil
2024-08-19 23:16:36 +02:00
}
type quantifier struct {
2024-09-01 20:27:54 +02:00
kind string
selector selector.Selector
statements []Statement
2024-08-19 23:16:36 +02:00
}
func (n quantifier) Kind() string {
return n.kind
}
func All(selector selector.Selector, policy ...Statement) Statement {
return quantifier{KindAll, selector, policy}
2024-08-19 23:16:36 +02:00
}
func Any(selector selector.Selector, policy ...Statement) Statement {
return quantifier{KindAny, selector, policy}
2024-08-19 23:16:36 +02:00
}