Skip to content

Commit 21e796c

Browse files
authored
refactor: Signature input handling refactored (#160)
1 parent b00841b commit 21e796c

9 files changed

Lines changed: 1187 additions & 702 deletions

‎signature_base.go‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ import (
1010

1111
type signatureBase []component
1212

13-
func newSignatureBase(params *signatureParameters, msg *Message) (signatureBase, error) {
14-
marshalledSigParams, err := httpsfv.Marshal(params.InnerList)
13+
func newSignatureBase(si *signatureInput, msg *Message) (signatureBase, error) {
14+
marshalledSigParams, err := httpsfv.Marshal(si.value)
1515
if err != nil {
1616
return nil, err
1717
}
1818

19-
sigBase := make(signatureBase, 0, len(params.identifiers)+1)
20-
seen := make(map[string]struct{}, len(params.identifiers))
19+
sigBase := make(signatureBase, 0, len(si.identifiers)+1)
20+
seen := make(map[string]struct{}, len(si.identifiers))
2121

22-
for _, ci := range params.identifiers {
22+
for _, ci := range si.identifiers {
2323
identifier, err := httpsfv.Marshal(ci.Item)
2424
if err != nil {
2525
return nil, err

‎signature_base_test.go‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,13 @@ func TestNewSignatureBase(t *testing.T) {
132132
identifiers, err := toComponentIdentifiers(tc.components)
133133
require.NoError(t, err)
134134

135-
params := newSignatureParameters(created, time.Time{}, "", keyID, "", "", identifiers)
135+
params := httpsfv.NewParams()
136+
params.Add(string(Created), created.Unix())
137+
params.Add(string(KeyID), keyID)
136138

137-
sigBase, err := newSignatureBase(params, msg)
139+
si := newSignatureInput(identifiers, params)
140+
141+
sigBase, err := newSignatureBase(si, msg)
138142

139143
tc.assert(t, err, sigBase)
140144
})

‎signature_input.go‎

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package httpsig
2+
3+
import (
4+
"fmt"
5+
"slices"
6+
"time"
7+
8+
"github.com/dunglas/httpsfv"
9+
)
10+
11+
type signatureInput struct {
12+
value httpsfv.InnerList
13+
identifiers []*componentIdentifier
14+
}
15+
16+
func newSignatureInput(
17+
identifiers []*componentIdentifier,
18+
params *httpsfv.Params,
19+
) *signatureInput {
20+
// https://www.rfc-editor.org/rfc/rfc9421.html#name-signature-parameters
21+
if params == nil {
22+
params = httpsfv.NewParams()
23+
}
24+
25+
items := make([]httpsfv.Item, len(identifiers))
26+
for i, id := range identifiers {
27+
items[i] = id.Item
28+
}
29+
30+
return &signatureInput{
31+
value: httpsfv.InnerList{
32+
Items: items,
33+
Params: params,
34+
},
35+
identifiers: identifiers,
36+
}
37+
}
38+
39+
func parseSignatureInput(list httpsfv.InnerList) (*signatureInput, error) {
40+
identifiers := make([]*componentIdentifier, 0, len(list.Items))
41+
42+
for _, item := range list.Items {
43+
identifier, err := newComponentIdentifier(item)
44+
if err != nil {
45+
return nil, err
46+
}
47+
48+
identifiers = append(identifiers, identifier)
49+
}
50+
51+
if err := validateSignatureInputParameters(list.Params); err != nil {
52+
return nil, err
53+
}
54+
55+
return &signatureInput{
56+
value: list,
57+
identifiers: identifiers,
58+
}, nil
59+
}
60+
61+
func validateSignatureInputParameters(params *httpsfv.Params) error {
62+
if params == nil {
63+
return nil
64+
}
65+
66+
for _, name := range params.Names() {
67+
value, _ := params.Get(name)
68+
69+
switch SignatureParameter(name) {
70+
case Created, Expires:
71+
if _, ok := value.(int64); !ok {
72+
return fmt.Errorf("%w: %s", ErrMalformedSignatureParameter, name)
73+
}
74+
case KeyID, Alg, Nonce, Tag:
75+
if _, ok := value.(string); !ok {
76+
return fmt.Errorf("%w: %s", ErrMalformedSignatureParameter, name)
77+
}
78+
}
79+
}
80+
81+
return nil
82+
}
83+
84+
func (si *signatureInput) hasIdentifier(id *componentIdentifier) bool {
85+
for _, identifier := range si.identifiers {
86+
if identifier.Value == id.Value && slices.Equal(identifier.Params.Names(), id.Params.Names()) {
87+
return true
88+
}
89+
}
90+
91+
return false
92+
}
93+
94+
func (si *signatureInput) parameter(name SignatureParameter) (any, bool) {
95+
if si.value.Params == nil {
96+
return nil, false
97+
}
98+
99+
return si.value.Params.Get(string(name))
100+
}
101+
102+
func (si *signatureInput) stringParameter(name SignatureParameter) (string, bool) {
103+
value, present := si.parameter(name)
104+
if !present {
105+
return "", false
106+
}
107+
108+
return value.(string), true //nolint: forcetypeassert
109+
}
110+
111+
func (si *signatureInput) timestamp(name SignatureParameter) (time.Time, bool) {
112+
value, present := si.parameter(name)
113+
if !present {
114+
return time.Time{}, false
115+
}
116+
117+
return time.Unix(value.(int64), 0).UTC(), true //nolint: forcetypeassert
118+
}
119+
120+
func (si *signatureInput) algorithm() (SignatureAlgorithm, bool) {
121+
value, present := si.stringParameter(Alg)
122+
123+
return SignatureAlgorithm(value), present
124+
}
125+
126+
func (si *signatureInput) created() (time.Time, bool) { return si.timestamp(Created) }
127+
func (si *signatureInput) expires() (time.Time, bool) { return si.timestamp(Expires) }
128+
func (si *signatureInput) nonce() (string, bool) { return si.stringParameter(Nonce) }
129+
func (si *signatureInput) keyID() (string, bool) { return si.stringParameter(KeyID) }
130+
func (si *signatureInput) tag() (string, bool) { return si.stringParameter(Tag) }

0 commit comments

Comments
 (0)