Repository navigation
Expand file tree
/
Copy pathsignature_base.go
More file actions
119 lines (95 loc) · 2.47 KB
/
Copy pathsignature_base.go
File metadata and controls
119 lines (95 loc) · 2.47 KB
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
package httpsig
import (
"fmt"
"slices"
"github.com/dunglas/httpsfv"
)
type serializedComponent struct {
identifier string
value string
}
// The entries preserve the covered component order. Signature parameters are separate.
type signatureBase struct {
entries []serializedComponent
signatureParams string
}
func newSignatureBase(msg *Message, si *signatureInput, prepared []string) (signatureBase, error) {
if prepared != nil && len(prepared) != len(si.identifiers) {
return signatureBase{}, fmt.Errorf("%w: mismatched serialized component identifiers", ErrMalformedData)
}
marshalledSigParams, err := httpsfv.Marshal(si.value)
if err != nil {
return signatureBase{}, err
}
sigBase := signatureBase{
entries: make([]serializedComponent, 0, len(si.identifiers)),
signatureParams: marshalledSigParams,
}
for i, ci := range si.identifiers {
var identifier string
if prepared == nil {
identifier, err = httpsfv.Marshal(ci.Item)
if err != nil {
return signatureBase{}, err
}
} else {
identifier = prepared[i]
}
if slices.ContainsFunc(si.identifiers[:i], ci.equal) {
return signatureBase{}, fmt.Errorf("%w: duplicate component identifier %s", ErrMalformedData, identifier)
}
comp, err := ci.createComponent(msg)
if err != nil {
return signatureBase{}, err
}
sigBase.entries = append(sigBase.entries, serializedComponent{
identifier: identifier,
value: comp.canonicalValue(),
})
}
return sigBase, nil
}
func (s signatureBase) marshal() []byte {
// calculate size
size := 0
for i, item := range s.entries {
size += len(item.identifier) + len(": ") + len(item.value)
if i > 0 {
size++
}
}
if s.signatureParams != "" {
size += len(`"@signature-params": `) + len(s.signatureParams)
if len(s.entries) > 0 {
size++
}
}
if size == 0 {
return nil
}
// do the actual marshalling
buf := make([]byte, 0, size)
for i, item := range s.entries {
if i > 0 {
buf = append(buf, '\n')
}
buf = append(buf, item.identifier...)
buf = append(buf, ':', ' ')
buf = append(buf, item.value...)
}
if s.signatureParams != "" {
if len(s.entries) > 0 {
buf = append(buf, '\n')
}
buf = append(buf, `"@signature-params": `...)
buf = append(buf, s.signatureParams...)
}
return buf
}
func (s signatureBase) components() map[string]string {
result := make(map[string]string, len(s.entries))
for _, item := range s.entries {
result[item.identifier] = item.value
}
return result
}