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 22:06:59 +02:00
|
|
|
|
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 (
|
2024-09-01 17:06:21 +02:00
|
|
|
KindEqual = "=="
|
|
|
|
|
KindGreaterThan = ">"
|
|
|
|
|
KindGreaterThanOrEqual = ">="
|
|
|
|
|
KindLessThan = "<"
|
|
|
|
|
KindLessThanOrEqual = "<="
|
|
|
|
|
KindNot = "not"
|
|
|
|
|
KindAnd = "and"
|
|
|
|
|
KindOr = "or"
|
|
|
|
|
KindLike = "like"
|
|
|
|
|
KindAll = "all"
|
|
|
|
|
KindAny = "any"
|
2024-08-19 23:16:36 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Policy = []Statement
|
|
|
|
|
|
|
|
|
|
type Statement interface {
|
|
|
|
|
Kind() string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type EqualityStatement interface {
|
|
|
|
|
Statement
|
|
|
|
|
Selector() selector.Selector
|
2024-08-20 15:55:04 +02:00
|
|
|
Value() ipld.Node
|
2024-08-19 23:16:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type InequalityStatement interface {
|
|
|
|
|
Statement
|
|
|
|
|
Selector() selector.Selector
|
2024-08-20 15:55:04 +02:00
|
|
|
Value() ipld.Node
|
2024-08-19 23:16:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type WildcardStatement interface {
|
|
|
|
|
Statement
|
|
|
|
|
Selector() selector.Selector
|
2024-08-21 08:13:44 +02:00
|
|
|
Value() glob.Glob
|
2024-08-19 23:16:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type ConnectiveStatement interface {
|
|
|
|
|
Statement
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type NegationStatement interface {
|
|
|
|
|
ConnectiveStatement
|
|
|
|
|
Value() Statement
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type ConjunctionStatement interface {
|
|
|
|
|
ConnectiveStatement
|
|
|
|
|
Value() []Statement
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type DisjunctionStatement interface {
|
|
|
|
|
ConnectiveStatement
|
|
|
|
|
Value() []Statement
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type QuantifierStatement interface {
|
|
|
|
|
Statement
|
|
|
|
|
Selector() selector.Selector
|
|
|
|
|
Value() Policy
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-20 15:55:04 +02:00
|
|
|
func (e equality) Value() ipld.Node {
|
2024-08-19 23:16:36 +02:00
|
|
|
return e.value
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e equality) Selector() selector.Selector {
|
|
|
|
|
return e.selector
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-20 15:55:04 +02:00
|
|
|
func Equal(selector selector.Selector, value ipld.Node) EqualityStatement {
|
2024-09-01 17:06:21 +02:00
|
|
|
return equality{KindEqual, selector, value}
|
2024-08-19 23:16:36 +02:00
|
|
|
}
|
|
|
|
|
|
2024-08-20 15:55:04 +02:00
|
|
|
func GreaterThan(selector selector.Selector, value ipld.Node) InequalityStatement {
|
2024-09-01 17:06:21 +02:00
|
|
|
return equality{KindGreaterThan, selector, value}
|
2024-08-19 23:16:36 +02:00
|
|
|
}
|
|
|
|
|
|
2024-08-20 15:55:04 +02:00
|
|
|
func GreaterThanOrEqual(selector selector.Selector, value ipld.Node) InequalityStatement {
|
2024-09-01 17:06:21 +02:00
|
|
|
return equality{KindGreaterThanOrEqual, selector, value}
|
2024-08-19 23:16:36 +02:00
|
|
|
}
|
|
|
|
|
|
2024-08-20 15:55:04 +02:00
|
|
|
func LessThan(selector selector.Selector, value ipld.Node) InequalityStatement {
|
2024-09-01 17:06:21 +02:00
|
|
|
return equality{KindLessThan, selector, value}
|
2024-08-19 23:16:36 +02:00
|
|
|
}
|
|
|
|
|
|
2024-08-20 15:55:04 +02:00
|
|
|
func LessThanOrEqual(selector selector.Selector, value ipld.Node) InequalityStatement {
|
2024-09-01 17:06:21 +02:00
|
|
|
return equality{KindLessThanOrEqual, selector, value}
|
2024-08-19 23:16:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type negation struct {
|
|
|
|
|
statement Statement
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (n negation) Kind() string {
|
2024-09-01 17:06:21 +02:00
|
|
|
return KindNot
|
2024-08-19 23:16:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (n negation) Value() Statement {
|
|
|
|
|
return n.statement
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Not(stmt Statement) NegationStatement {
|
|
|
|
|
return negation{stmt}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type conjunction struct {
|
|
|
|
|
statements []Statement
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (n conjunction) Kind() string {
|
2024-09-01 17:06:21 +02:00
|
|
|
return KindAnd
|
2024-08-19 23:16:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (n conjunction) Value() []Statement {
|
|
|
|
|
return n.statements
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func And(stmts ...Statement) ConjunctionStatement {
|
|
|
|
|
return conjunction{stmts}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type disjunction struct {
|
|
|
|
|
statements []Statement
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (n disjunction) Kind() string {
|
2024-09-01 17:06:21 +02:00
|
|
|
return KindOr
|
2024-08-19 23:16:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (n disjunction) Value() []Statement {
|
|
|
|
|
return n.statements
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Or(stmts ...Statement) DisjunctionStatement {
|
|
|
|
|
return disjunction{stmts}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type wildcard struct {
|
|
|
|
|
selector selector.Selector
|
2024-08-21 08:13:44 +02:00
|
|
|
glob glob.Glob
|
2024-08-19 23:16:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (n wildcard) Kind() string {
|
2024-09-01 17:06:21 +02:00
|
|
|
return KindLike
|
2024-08-19 23:16:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (n wildcard) Selector() selector.Selector {
|
|
|
|
|
return n.selector
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-21 08:13:44 +02:00
|
|
|
func (n wildcard) Value() glob.Glob {
|
|
|
|
|
return n.glob
|
2024-08-19 23:16:36 +02:00
|
|
|
}
|
|
|
|
|
|
2024-08-21 08:13:44 +02:00
|
|
|
func Like(selector selector.Selector, glob glob.Glob) WildcardStatement {
|
|
|
|
|
return wildcard{selector, glob}
|
2024-08-19 23:16:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type quantifier struct {
|
|
|
|
|
kind string
|
|
|
|
|
selector selector.Selector
|
|
|
|
|
policy Policy
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (n quantifier) Kind() string {
|
|
|
|
|
return n.kind
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (n quantifier) Selector() selector.Selector {
|
|
|
|
|
return n.selector
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (n quantifier) Value() Policy {
|
|
|
|
|
return n.policy
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-21 08:44:17 +02:00
|
|
|
func All(selector selector.Selector, policy ...Statement) QuantifierStatement {
|
2024-09-01 17:06:21 +02:00
|
|
|
return quantifier{KindAll, selector, policy}
|
2024-08-19 23:16:36 +02:00
|
|
|
}
|
|
|
|
|
|
2024-08-21 08:44:17 +02:00
|
|
|
func Any(selector selector.Selector, policy ...Statement) QuantifierStatement {
|
2024-09-01 17:06:21 +02:00
|
|
|
return quantifier{KindAny, selector, policy}
|
2024-08-19 23:16:36 +02:00
|
|
|
}
|