forked from ucan-wg/go-ucan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
attenuation.go
168 lines (145 loc) · 4.1 KB
/
attenuation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package ucan
import (
"encoding/json"
"fmt"
)
// Attenuations is a list of attenuations
type Attenuations []Attenuation
func (att Attenuations) String() string {
str := ""
for _, a := range att {
str += fmt.Sprintf("%s\n", a)
}
return str
}
// Contains is true if all attenuations in b are contained
func (att Attenuations) Contains(b Attenuations) bool {
// fmt.Printf("%scontains\n%s?\n\n", att, b)
LOOP:
for _, bb := range b {
for _, aa := range att {
if aa.Contains(bb) {
// fmt.Printf("%s contains %s\n", aa, bb)
continue LOOP
} else if aa.Rsc.Contains(bb.Rsc) {
// fmt.Printf("%s < %s\n", aa, bb)
// fmt.Printf("rscEq:%t rscContains: %t capContains:%t\n", aa.Rsc.Type() == bb.Rsc.Type(), aa.Rsc.Contains(bb.Rsc), aa.Cap.Contains(bb.Cap))
return false
}
}
return false
}
return true
}
// AttenuationConstructorFunc is a function that creates an attenuation from a map
// Users of this package provide an Attenuation Constructor to the parser to
// bind attenuation logic to a UCAN
type AttenuationConstructorFunc func(v map[string]interface{}) (Attenuation, error)
// Attenuation is a capability on a resource
type Attenuation struct {
Cap Capability
Rsc Resource
}
// String formats an attenuation as a string
func (a Attenuation) String() string {
return fmt.Sprintf("cap:%s %s:%s", a.Cap, a.Rsc.Type(), a.Rsc.Value())
}
// Contains returns true if both
func (a Attenuation) Contains(b Attenuation) bool {
return a.Rsc.Contains(b.Rsc) && a.Cap.Contains(b.Cap)
}
// MarshalJSON implements the json.Marshaller interface
func (a Attenuation) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
a.Rsc.Type(): a.Rsc.Value(),
CapKey: a.Cap.String(),
})
}
// Resource is a unique identifier for a thing, usually stored state. Resources
// are organized by string types
type Resource interface {
Type() string
Value() string
Contains(b Resource) bool
}
type stringLengthRsc struct {
t string
v string
}
// NewStringLengthResource is a silly implementation of resource to use while
// I figure out what an OR filter on strings is. Don't use this.
func NewStringLengthResource(typ, val string) Resource {
return stringLengthRsc{
t: typ,
v: val,
}
}
func (r stringLengthRsc) Type() string {
return r.t
}
func (r stringLengthRsc) Value() string {
return r.v
}
func (r stringLengthRsc) Contains(b Resource) bool {
return r.Type() == b.Type() && len(r.Value()) <= len(b.Value())
}
// Capability is an action users can perform
type Capability interface {
// A Capability must be expressable as a string
String() string
// Capabilities must be comparable to other same-type capabilities
Contains(b Capability) bool
}
// NestedCapabilities is a basic implementation of the Capabilities interface
// based on a hierarchal list of strings ordered from most to least capable
// It is both a capability and a capability factory with the .Cap method
type NestedCapabilities struct {
cap string
idx int
hierarchy *[]string
}
// assert at compile-time NestedCapabilities implements Capability
var _ Capability = (*NestedCapabilities)(nil)
// NewNestedCapabilities creates a set of NestedCapabilities
func NewNestedCapabilities(strs ...string) NestedCapabilities {
return NestedCapabilities{
cap: strs[0],
idx: 0,
hierarchy: &strs,
}
}
// Cap creates a new capability from the hierarchy
func (nc NestedCapabilities) Cap(str string) Capability {
idx := -1
for i, c := range *nc.hierarchy {
if c == str {
idx = i
break
}
}
if idx == -1 {
panic(fmt.Sprintf("%s is not a nested capability. must be one of: %v", str, *nc.hierarchy))
}
return NestedCapabilities{
cap: str,
idx: idx,
hierarchy: nc.hierarchy,
}
}
// String returns the Capability value as a string
func (nc NestedCapabilities) String() string {
return nc.cap
}
// Contains returns true if cap is equal or less than the NestedCapability value
func (nc NestedCapabilities) Contains(cap Capability) bool {
str := cap.String()
for i, c := range *nc.hierarchy {
if c == str {
if i >= nc.idx {
return true
}
return false
}
}
return false
}