-
Notifications
You must be signed in to change notification settings - Fork 0
/
signature_requirements.go
138 lines (113 loc) · 2.74 KB
/
signature_requirements.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
package httpsig
import (
"fmt"
"github.com/dunglas/httpsfv"
)
type signatureRequirements struct {
httpsfv.InnerList
created bool
expires bool
nonce string
alg SignatureAlgorithm
keyID string
tag string
identifiers []*componentIdentifier
}
func newSignatureRequirements(
created, expires bool,
signatureTag, nonce string,
keyID string,
algorithm SignatureAlgorithm,
identifiers []*componentIdentifier,
) *signatureRequirements {
// https://www.rfc-editor.org/rfc/rfc9421.html#name-signature-parameters
params := httpsfv.NewParams()
if created {
params.Add(string(Created), true)
}
if expires {
params.Add(string(Expires), true)
}
if len(keyID) != 0 {
params.Add(string(KeyID), keyID)
}
if len(algorithm) != 0 {
params.Add(string(Alg), string(algorithm))
}
if len(nonce) != 0 {
params.Add(string(Nonce), nonce)
}
if len(signatureTag) != 0 {
params.Add(string(Tag), signatureTag)
}
items := make([]httpsfv.Item, len(identifiers))
for i, id := range identifiers {
items[i] = id.Item
}
return &signatureRequirements{
InnerList: httpsfv.InnerList{Params: params, Items: items},
created: created,
expires: expires,
nonce: nonce,
alg: algorithm,
keyID: keyID,
tag: signatureTag,
identifiers: identifiers,
}
}
//nolint:funlen, cyclop
func (p *signatureRequirements) fromInnerList(list httpsfv.InnerList) error {
for _, item := range list.Items {
ci, err := newComponentIdentifier(item)
if err != nil {
return err
}
p.identifiers = append(p.identifiers, ci)
}
for _, name := range list.Params.Names() {
param, _ := list.Params.Get(name)
switch SignatureParameter(name) {
case Created:
value, ok := param.(bool)
if !ok {
return fmt.Errorf("%w: created", ErrMalformedSignatureParameter)
}
p.created = value
case Expires:
value, ok := param.(bool)
if !ok {
return fmt.Errorf("%w: expires", ErrMalformedSignatureParameter)
}
p.expires = value
case KeyID:
value, ok := param.(string)
if !ok {
return fmt.Errorf("%w: keyid", ErrMalformedSignatureParameter)
}
p.keyID = value
case Alg:
value, ok := param.(string)
if !ok {
return fmt.Errorf("%w: alg", ErrMalformedSignatureParameter)
}
p.alg = SignatureAlgorithm(value)
case Nonce:
value, ok := param.(string)
if !ok {
return fmt.Errorf("%w: nonce", ErrMalformedSignatureParameter)
}
p.nonce = value
case Tag:
value, ok := param.(string)
if !ok {
return fmt.Errorf("%w: tag", ErrMalformedSignatureParameter)
}
p.tag = value
default:
return fmt.Errorf("%w: %s is unknown", ErrMalformedSignatureParameter, name)
}
}
p.Params = list.Params
p.Items = list.Items
return nil
}