forked from invopop/gobl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
envelope.go
321 lines (286 loc) · 8.95 KB
/
envelope.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package gobl
import (
"bytes"
"context"
"encoding/json"
"errors"
"strconv"
"github.com/invopop/validation"
"github.com/invopop/gobl/c14n"
"github.com/invopop/gobl/dsig"
"github.com/invopop/gobl/head"
"github.com/invopop/gobl/internal"
"github.com/invopop/gobl/schema"
"github.com/invopop/gobl/uuid"
)
// Envelope wraps around a document adding headers and
// digital signatures. An Envelope is similar to a regular envelope
// in the physical world, it keeps the contents safe and helps
// get the document where its needed.
type Envelope struct {
// Schema identifies the schema that should be used to understand this document
Schema schema.ID `json:"$schema" jsonschema:"title=JSON Schema ID"`
// Details on what the contents are
Head *head.Header `json:"head" jsonschema:"title=Header"`
// The data inside the envelope
Document *schema.Object `json:"doc" jsonschema:"title=Document"`
// JSON Web Signatures of the header
Signatures []*dsig.Signature `json:"sigs,omitempty" jsonschema:"title=Signatures"`
}
// EnvelopeSchema sets the general definition of the schema ID for this version of the
// envelope.
var EnvelopeSchema = schema.GOBL.Add("envelope")
// NewEnvelope builds a new envelope object ready for data to be inserted
// and signed. If you are loading data from json, you can safely use a regular
// `new(Envelope)` call directly.
func NewEnvelope() *Envelope {
e := new(Envelope)
e.Schema = EnvelopeSchema
e.Head = head.NewHeader()
e.Document = new(schema.Object)
e.Signatures = make([]*dsig.Signature, 0)
return e
}
// Envelop is a convenience method that will build a new envelope and insert
// the contents document provided in a single swoop. The resulting envelope
// will still need to be signed afterwards.
func Envelop(doc interface{}) (*Envelope, error) {
e := NewEnvelope()
if err := e.Insert(doc); err != nil {
return nil, err
}
return e, nil
}
// Validate ensures that the envelope contains everything it should to be considered valid GoBL.
func (e *Envelope) Validate() error {
return e.ValidateWithContext(context.Background())
}
// Verify checks the envelope's signatures to ensure the headers they contain
// still matches with the current headers. If a list of public keys are provided,
// they will be used to ensure that the signatures we're signed by at least
// one of them. If no keys are provided, only the contents will be checked.
func (e *Envelope) Verify(keys ...*dsig.PublicKey) error {
if len(e.Signatures) == 0 {
return errors.New("no signatures to verify")
}
ve := make(validation.Errors)
for i, s := range e.Signatures {
if err := e.verifySignature(s, keys...); err != nil {
ve[strconv.Itoa(i)] = err
}
}
if len(ve) > 0 {
return ErrValidation.WithCause(validation.Errors{
"signatures": ve,
})
}
return nil
}
// VerifySignature checks a specific signature with the envelope to see if its
// contents are still valid.
// If a list of public keys are provided, they will be used to ensure that the
// signature was signed by at least one of them. If no keys are provided, only
// the contents will be checked.
func (e *Envelope) VerifySignature(sig *dsig.Signature, keys ...*dsig.PublicKey) error {
return wrapError(e.verifySignature(sig, keys...))
}
func (e *Envelope) verifySignature(sig *dsig.Signature, keys ...*dsig.PublicKey) error {
if len(keys) == 0 {
// no keys provided, only check the contents
h := new(head.Header)
if err := sig.UnsafePayload(h); err != nil {
return errors.New("invalid signature payload")
}
if !e.Head.Contains(h) {
return errors.New("header mismatch")
}
return nil
}
for _, k := range keys {
h := new(head.Header)
if err := sig.VerifyPayload(k, h); err != nil {
continue
}
if e.Head.Contains(h) {
return nil
}
return errors.New("header mismatch")
}
return errors.New("no key match found")
}
// ValidateWithContext ensures that the envelope contains everything it should to be considered valid GoBL.
func (e *Envelope) ValidateWithContext(ctx context.Context) error {
if len(e.Signatures) > 0 {
ctx = internal.SignedContext(ctx)
}
err := validation.ValidateStructWithContext(ctx, e,
validation.Field(&e.Schema, validation.Required),
validation.Field(&e.Head, validation.Required),
validation.Field(&e.Document, validation.Required), // this will also check payload
validation.Field(&e.Signatures),
)
if err != nil {
return wrapError(err)
}
return wrapError(e.verifyDigest())
}
func (e *Envelope) verifyDigest() error {
d1 := e.Head.Digest
d2, err := e.Digest()
if err != nil {
return err
}
if err := d1.Equals(d2); err != nil {
return ErrDigest.WithCause(err)
}
return nil
}
// Sign uses the private key to sign the envelope headers. Additional validation
// rules may be applied to signed documents, so the document will be signed,
// then validated, and if the validation fails, the signature will be removed.
func (e *Envelope) Sign(key *dsig.PrivateKey) error {
if e.Head == nil {
return ErrValidation.WithReason("header required")
}
sig, err := key.Sign(e.Head)
if err != nil {
return ErrSignature.WithCause(err)
}
e.Signatures = append(e.Signatures, sig)
if err := e.Validate(); err != nil {
// invalid envlopes cannot be signed
e.Signatures = nil
return err
}
return nil
}
// Signed returns true if the envelope has signatures.
func (e *Envelope) Signed() bool {
return len(e.Signatures) > 0
}
// Unsign removes the signatures from the envelope.
func (e *Envelope) Unsign() {
e.Signatures = nil
}
// Insert takes the provided document and inserts it into this
// envelope. Calculate will be called automatically.
func (e *Envelope) Insert(doc interface{}) error {
if e.Head == nil {
return ErrInternal.WithReason("missing head")
}
if doc == nil {
return ErrNoDocument
}
if d, ok := doc.(*schema.Object); ok {
e.Document = d
} else {
var err error
e.Document, err = schema.NewObject(doc)
if err != nil {
return wrapError(err)
}
}
if err := e.calculate(); err != nil {
return wrapError(err)
}
return nil
}
// Calculate is used to perform calculations on the envelope's
// document contents to ensure everything looks correct.
// Headers will be refreshed to ensure they have the latest valid
// digest.
func (e *Envelope) Calculate() error {
if e.Document == nil {
return ErrNoDocument
}
if e.Document.IsEmpty() {
return ErrNoDocument
}
return e.calculate()
}
func (e *Envelope) calculate() error {
// Always set our schema version
e.Schema = EnvelopeSchema
// arm doors and cross check
if err := e.Document.Calculate(); err != nil {
return ErrCalculation.WithCause(err)
}
// Double check the header looks okay
if e.Head == nil {
e.Head = head.NewHeader()
}
if e.Head.UUID.IsZero() {
e.Head.UUID = uuid.V7()
}
var err error
e.Head.Digest, err = e.Digest()
if err != nil {
return err
}
return nil
}
// Digest calculates a digital digest using the canonical JSON of the document.
func (e *Envelope) Digest() (*dsig.Digest, error) {
data, err := json.Marshal(e.Document)
if err != nil {
return nil, ErrMarshal.WithCause(err)
}
r := bytes.NewReader(data)
cd, err := c14n.CanonicalJSON(r)
if err != nil {
return nil, ErrInternal.WithReason("canonical JSON error: %w", err)
}
return dsig.NewSHA256Digest(cd), nil
}
// Extract the contents of the envelope into the provided document type.
func (e *Envelope) Extract() interface{} {
if e.Document == nil {
return nil
}
return e.Document.Instance()
}
// Correct will attempt to build a new envelope as a correction of the
// current envelope contents, if possible.
func (e *Envelope) Correct(opts ...schema.Option) (*Envelope, error) {
if e.Head != nil && len(e.Head.Stamps) > 0 {
opts = append(opts, head.WithHead(e.Head))
}
nd, err := e.Document.Clone()
if err != nil {
return nil, wrapError(err)
}
if err := nd.Correct(opts...); err != nil {
return nil, wrapError(err)
}
// Create a completely new envelope with a new set of data.
return Envelop(nd)
}
// Replicate will create a new envelope with the same contents as the current,
// but with schema specific options applied to remove information that must
// change between documents, such as stamps, invoice code, date, UUID, etc.
// The intention here is for users to be able to get a new copy of the original
// document so that they can issue a new version with updated details, or
// simply use the original as a template.
func (e *Envelope) Replicate() (*Envelope, error) {
nd, err := e.Document.Clone()
if err != nil {
return nil, wrapError(err)
}
if err := nd.Replicate(); err != nil {
return nil, wrapError(err)
}
return Envelop(nd)
}
// CorrectionOptionsSchema will attempt to provide a corrective options JSON Schema
// that can be used to generate a JSON object to send when correcting a document.
// If none are available, the result will be nil.
func (e *Envelope) CorrectionOptionsSchema() (interface{}, error) {
if e.Document == nil {
return nil, ErrNoDocument
}
opts, err := e.Document.CorrectionOptionsSchema()
if err != nil {
return nil, wrapError(err)
}
return opts, nil
}