-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheaders.go
More file actions
174 lines (153 loc) · 4.04 KB
/
headers.go
File metadata and controls
174 lines (153 loc) · 4.04 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
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
package httpsignature
import (
"crypto/sha256"
"encoding/base64"
"fmt"
"strings"
)
const HeaderContentDigest = "content-digest"
const HeaderSignature = "signature"
const HeaderSignatureInput = "signature-input"
type Header interface {
// returns a tuple (key, value)
Header() [2]string
}
type ContentDigest struct {
Algorithm string
Digest []byte
}
func NewContentDigest(body []byte) *ContentDigest {
digest := sha256.Sum256(body)
return &ContentDigest{
Algorithm: "sha-256",
Digest: digest[:],
}
}
func (c *ContentDigest) Base64() string {
return base64.StdEncoding.EncodeToString(c.Digest)
}
func (c *ContentDigest) Header() []string {
return []string{
HeaderContentDigest,
fmt.Sprintf(`sha-256=:%s:`, c.Base64()),
}
}
func ParseContentDigest(header string) (*ContentDigest, error) {
eqIndex := strings.Index(header, "=")
if eqIndex == -1 {
return nil, fmt.Errorf("invalid content-digest header, expected '='")
}
algorithm := header[:eqIndex]
digestEncoded := header[eqIndex+1:]
digest, err := base64.StdEncoding.DecodeString(
strings.Trim(digestEncoded, ":"),
)
if err != nil {
return nil, fmt.Errorf("content-digest invalid base64: %w", err)
}
return &ContentDigest{Algorithm: algorithm, Digest: digest}, nil
}
type SigParamKV struct {
Key string
Value string
}
type SigParams struct {
Name string
// inside the (...)
Components []string
// after the (...);
Attributes []SigParamKV
}
func NewSigParams(Created int64, headers ...string) *SigParams {
components := []string{"@method", "@path", "@query", "content-digest"}
components = append(components, headers...)
return &SigParams{
Name: "iam",
Components: components,
Attributes: []SigParamKV{
{
Key: "created",
Value: fmt.Sprintf("\"%d\"", Created),
},
},
}
}
func (p *SigParams) Serialize() string {
attributes := []string{}
for _, attr := range p.Attributes {
attributes = append(attributes, fmt.Sprintf(`%s=%s`, attr.Key, attr.Value))
}
template := fmt.Sprintf(
`(%s);%s`,
strings.Join(p.Components, " "),
strings.Join(attributes, ";"),
)
return template
}
func (p *SigParams) Header() []string {
return []string{
HeaderSignatureInput,
fmt.Sprintf(`%s=%s`, p.Name, p.Serialize()),
}
}
func ParseSigParams(header string) (*SigParams, error) {
eqIndex := strings.Index(header, "=")
if eqIndex == -1 {
return nil, fmt.Errorf("invalid signature-input header, expected '='")
}
name := header[:eqIndex]
value := header[eqIndex+1:]
paranth0 := strings.Index(value, "(")
paranth1 := strings.Index(value, ")")
if paranth0 == -1 || paranth1 == -1 || paranth0 > paranth1 {
return nil, fmt.Errorf("invalid signature-input header (missing parentheses)")
}
components := strings.Split(value[paranth0+1:paranth1], " ")
attributes := []SigParamKV{}
kvParts := strings.Split(value[paranth1+1:], ";")
for _, part := range kvParts {
if strings.TrimSpace(part) == "" {
continue
}
subParts := strings.Split(part, "=")
if len(subParts) != 2 {
return nil, fmt.Errorf("invalid signature-input header (invalid key-value pair %s)", part)
}
attributes = append(attributes, SigParamKV{Key: subParts[0], Value: subParts[1]})
}
return &SigParams{name, components, attributes}, nil
}
type Signature struct {
Name string
Signature []byte
}
func NewSignature(signature []byte) *Signature {
return &Signature{
Name: "iam",
Signature: signature,
}
}
func (sig *Signature) Base64() string {
return base64.StdEncoding.EncodeToString(sig.Signature)
}
func (sig *Signature) Header() []string {
return []string{
HeaderSignature,
fmt.Sprintf(`%s=:%s:`, sig.Name, sig.Base64()),
}
}
func ParseSignature(header string) (*Signature, error) {
eqIndex := strings.Index(header, "=")
if eqIndex == -1 {
return nil, fmt.Errorf("invalid signature header, expected '='")
}
name := header[:eqIndex]
value := header[eqIndex+1:]
signature, err := base64.StdEncoding.DecodeString(
strings.Trim(value, ":"),
)
if err != nil {
return nil, fmt.Errorf("signature invalid base64: %w", err)
}
return &Signature{name, signature}, nil
}