forked from ceramicnetwork/go-dag-jose
/
dagjose_assembler.go
242 lines (229 loc) · 6.31 KB
/
dagjose_assembler.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package dagjose
import (
"fmt"
"github.com/ipfs/go-cid"
ipld "github.com/ipld/go-ipld-prime"
basicnode "github.com/ipld/go-ipld-prime/node/basic"
"github.com/ipld/go-ipld-prime/node/mixins"
)
var (
_ ipld.Node = dagJOSENode{}
_ ipld.NodePrototype = &DagJOSENodePrototype{}
_ ipld.NodeAssembler = &dagJOSENodeBuilder{}
)
type DagJOSENodePrototype struct{}
func (d *DagJOSENodePrototype) NewBuilder() ipld.NodeBuilder {
return &dagJOSENodeBuilder{dagJose: DagJOSE{}}
}
// Returns an instance of the DagJOSENodeBuilder which can be passed to
// ipld.Link.Load and will build a dagjose.DagJOSE object. This should only be
// neccesary in reasonably advanced situations, most of the time you should be
// able to use dagjose.LoadJOSE
func NewBuilder() ipld.NodeBuilder {
return &dagJOSENodeBuilder{dagJose: DagJOSE{}}
}
type maState uint8
const (
maState_initial maState = iota // also the 'expect key or finish' state
maState_midKey // waiting for a 'finished' state in the KeyAssembler.
maState_expectValue // 'AssembleValue' is the only valid next step
maState_midValue // waiting for a 'finished' state in the ValueAssembler.
maState_finished // finised
)
// An implementation of `ipld.NodeBuilder` which builds a `dagjose.DagJOSE`
// object. This builder will throw an error if the IPLD data it is building
// does not match the schema specified in the spec
type dagJOSENodeBuilder struct {
dagJose DagJOSE
state maState
key *string
}
var dagJoseMixin = mixins.MapAssembler{TypeName: "dagjose"}
func (d *dagJOSENodeBuilder) BeginMap(sizeHint int64) (ipld.MapAssembler, error) {
if d.state != maState_initial {
panic("misuse")
}
return d, nil
}
func (d *dagJOSENodeBuilder) BeginList(sizeHint int64) (ipld.ListAssembler, error) {
if d.state == maState_midValue && *d.key == "recipients" {
d.dagJose.recipients = make([]jweRecipient, 0, sizeHint)
d.state = maState_initial
return &jweRecipientListAssembler{&d.dagJose}, nil
}
if d.state == maState_midValue && *d.key == "signatures" {
d.dagJose.signatures = make([]jwsSignature, 0, sizeHint)
d.state = maState_initial
return &jwsSignatureListAssembler{&d.dagJose}, nil
}
return dagJoseMixin.BeginList(sizeHint)
}
func (d *dagJOSENodeBuilder) AssignNull() error {
if d.state == maState_midValue {
switch *d.key {
case "payload":
d.dagJose.payload = nil
case "protected":
d.dagJose.protected = nil
case "unprotected":
d.dagJose.unprotected = nil
case "iv":
d.dagJose.iv = nil
case "aad":
d.dagJose.aad = nil
case "ciphertext":
d.dagJose.ciphertext = nil
case "tag":
d.dagJose.tag = nil
case "signatures":
d.dagJose.signatures = nil
case "recipients":
d.dagJose.recipients = nil
default:
panic("should not happen due to AssignString implementation")
}
d.state = maState_initial
return nil
}
return dagJoseMixin.AssignNull()
}
func (d *dagJOSENodeBuilder) AssignBool(b bool) error {
return dagJoseMixin.AssignBool(b)
}
func (d *dagJOSENodeBuilder) AssignInt(i int64) error {
return dagJoseMixin.AssignInt(i)
}
func (d *dagJOSENodeBuilder) AssignFloat(f float64) error {
return dagJoseMixin.AssignFloat(f)
}
func (d *dagJOSENodeBuilder) AssignString(s string) error {
if d.state == maState_midKey {
if !isValidJOSEKey(s) {
return fmt.Errorf("Attempted to assign an invalid JOSE key: %v", s)
}
d.key = &s
d.state = maState_expectValue
return nil
}
return dagJoseMixin.AssignString(s)
}
func (d *dagJOSENodeBuilder) AssignBytes(b []byte) error {
if d.state == maState_midValue {
switch *d.key {
case "payload":
_, cid, err := cid.CidFromBytes(b)
if err != nil {
return fmt.Errorf("payload is not a valid CID: %v", err)
}
d.dagJose.payload = &cid
case "protected":
d.dagJose.protected = b
case "unprotected":
d.dagJose.unprotected = b
case "iv":
d.dagJose.iv = b
case "aad":
d.dagJose.aad = b
case "ciphertext":
d.dagJose.ciphertext = b
case "tag":
d.dagJose.tag = b
case "signatures":
return fmt.Errorf("attempted to assign bytes to 'signatures' key")
case "recipients":
return fmt.Errorf("attempted to assign bytes to 'recipients' key")
default:
panic("should not happen due to AssignString implementation")
}
d.state = maState_initial
return nil
}
return dagJoseMixin.AssignBytes(b)
}
func (d *dagJOSENodeBuilder) AssignLink(l ipld.Link) error {
return dagJoseMixin.AssignLink(l)
}
func (d *dagJOSENodeBuilder) AssignNode(n ipld.Node) error {
if d.state != maState_initial {
panic("misuse")
}
if n.Kind() != ipld.Kind_Map {
return ipld.ErrWrongKind{TypeName: "map", MethodName: "AssignNode", AppropriateKind: ipld.KindSet_JustMap, ActualKind: n.Kind()}
}
itr := n.MapIterator()
for !itr.Done() {
k, v, err := itr.Next()
if err != nil {
return err
}
if err := d.AssembleKey().AssignNode(k); err != nil {
return err
}
if err := d.AssembleValue().AssignNode(v); err != nil {
return err
}
}
return d.Finish()
}
func (d *dagJOSENodeBuilder) Prototype() ipld.NodePrototype {
return &DagJOSENodePrototype{}
}
func (d *dagJOSENodeBuilder) Build() ipld.Node {
return dagJOSENode{&d.dagJose}
}
func (d *dagJOSENodeBuilder) Reset() {
}
func (d *dagJOSENodeBuilder) AssembleKey() ipld.NodeAssembler {
if d.state != maState_initial {
panic("misuse")
}
d.state = maState_midKey
return d
}
func (d *dagJOSENodeBuilder) AssembleValue() ipld.NodeAssembler {
if d.state != maState_expectValue {
panic("misuse")
}
d.state = maState_midValue
return d
}
func (d *dagJOSENodeBuilder) AssembleEntry(k string) (ipld.NodeAssembler, error) {
if d.state != maState_initial {
panic("misuse")
}
d.key = &k
d.state = maState_midValue
return d, nil
}
func (d *dagJOSENodeBuilder) Finish() error {
if d.state != maState_initial {
panic("misuse")
}
d.state = maState_finished
return nil
}
func (d *dagJOSENodeBuilder) KeyPrototype() ipld.NodePrototype {
return basicnode.Prototype.String
}
func (d *dagJOSENodeBuilder) ValuePrototype(k string) ipld.NodePrototype {
return basicnode.Prototype.Any
}
func isValidJOSEKey(key string) bool {
allowedKeys := []string{
"payload",
"signatures",
"protected",
"unprotected",
"iv",
"aad",
"ciphertext",
"tag",
"recipients",
}
for _, allowedKey := range allowedKeys {
if key == allowedKey {
return true
}
}
return false
}