-
Notifications
You must be signed in to change notification settings - Fork 8
/
canonicalize.go
192 lines (160 loc) · 4.95 KB
/
canonicalize.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
// Copyright (c) 2021 James Bowes. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package httpsig
import (
"errors"
"fmt"
"io"
"net/http"
nurl "net/url"
"strconv"
"strings"
"time"
)
// message is a minimal representation of an HTTP request or response, containing the values
// needed to construct a signature.
type message struct {
Method string
Authority string
URL *nurl.URL
Header http.Header
}
func messageFromRequest(r *http.Request) *message {
hdr := r.Header.Clone()
hdr.Set("Host", r.Host)
return &message{
Method: r.Method,
Authority: r.Host,
URL: r.URL,
Header: hdr,
}
}
func canonicalizeHeader(out io.Writer, name string, hdr http.Header) error {
// XXX: Structured headers are not considered, and they should be :)
v := hdr.Values(name)
if len(v) == 0 { // empty values are permitted, but no values are not
return fmt.Errorf("'%s' header not found", name)
}
// Section 2.1 covers canonicalizing headers.
// Section 2.4 step 2 covers using them as input.
vc := make([]string, 0, len(v))
for _, sv := range v {
vc = append(vc, strings.TrimSpace(sv))
}
_, err := fmt.Fprintf(out, "\"%s\": %s\n", strings.ToLower(name), strings.Join(vc, ", "))
return err
}
func canonicalizeMethod(out io.Writer, method string) error {
// Section 2.3.2 covers canonicalization of the method.
// Section 2.4 step 2 covers using it as input.
_, err := fmt.Fprintf(out, "\"@method\": %s\n", strings.ToUpper(method)) // Method should always be caps.
return err
}
func canonicalizeAuthority(out io.Writer, authority string) error {
// Section 2.3.4 covers canonicalization of the authority.
// Section 2.4 step 2 covers using it as input.
_, err := fmt.Fprintf(out, "\"@authority\": %s\n", authority)
return err
}
func canonicalizePath(out io.Writer, path string) error {
// Section 2.3.7 covers canonicalization of the path.
// Section 2.4 step 2 covers using it as input.
_, err := fmt.Fprintf(out, "\"@path\": %s\n", path)
return err
}
func canonicalizeQuery(out io.Writer, rawQuery string) error {
// Section 2.3.8 covers canonicalization of the query.
// Section 2.4 step 2 covers using it as input.
_, err := fmt.Fprintf(out, "\"@query\": ?%s\n", rawQuery) // TODO: decode percent encodings
return err
}
func canonicalizeSignatureParams(out io.Writer, sp *signatureParams) error {
// Section 2.3.1 covers canonicalization of the signature parameters
// TODO: Deal with all the potential print errs. sigh.
_, err := fmt.Fprintf(out, "\"@signature-params\": %s", sp.canonicalize())
if err != nil {
return err
}
return err
}
type signatureParams struct {
items []string
keyID string
alg string
created time.Time
expires *time.Time
nonce string
}
func (sp *signatureParams) canonicalize() string {
li := make([]string, 0, len(sp.items))
for _, i := range sp.items {
li = append(li, fmt.Sprintf("\"%s\"", strings.ToLower(i)))
}
o := fmt.Sprintf("(%s)", strings.Join(li, " "))
// Items comes first. The params afterwards can be in any order. The order chosen here
// matches what's in the examples in the standard, aiding in testing.
o += fmt.Sprintf(";created=%d", sp.created.Unix())
if sp.keyID != "" {
o += fmt.Sprintf(";keyid=\"%s\"", sp.keyID)
}
if sp.alg != "" {
o += fmt.Sprintf(";alg=\"%s\"", sp.alg)
}
if sp.expires != nil {
o += fmt.Sprintf(";expires=%d", sp.expires.Unix())
}
return o
}
var errMalformedSignatureInput = errors.New("malformed signature-input header")
func parseSignatureInput(in string) (*signatureParams, error) {
sp := &signatureParams{}
parts := strings.Split(in, ";")
if len(parts) < 1 {
return nil, errMalformedSignatureInput
}
if parts[0][0] != '(' || parts[0][len(parts[0])-1] != ')' {
return nil, errMalformedSignatureInput
}
if len(parts[0]) > 2 { // not empty
// TODO: headers can't have spaces, but it should still be handled
items := strings.Split(parts[0][1:len(parts[0])-1], " ")
// TODO: error when not quoted
for i := range items {
items[i] = strings.Trim(items[i], `"`)
}
sp.items = items
}
for _, param := range parts[1:] {
paramParts := strings.Split(param, "=")
if len(paramParts) != 2 {
return nil, errMalformedSignatureInput
}
// TODO: error when not wrapped in quotes
switch paramParts[0] {
case "alg":
sp.alg = strings.Trim(paramParts[1], `"`)
case "keyid":
sp.keyID = strings.Trim(paramParts[1], `"`)
case "nonce":
sp.nonce = strings.Trim(paramParts[1], `"`)
case "created":
i, err := strconv.ParseInt(paramParts[1], 10, 64)
if err != nil {
return nil, errMalformedSignatureInput
}
sp.created = time.Unix(i, 0)
case "expires":
i, err := strconv.ParseInt(paramParts[1], 10, 64)
if err != nil {
return nil, errMalformedSignatureInput
}
t := time.Unix(i, 0)
sp.expires = &t
default:
// TODO: unknown params could be kept? hard to say.
return nil, errMalformedSignatureInput
}
}
return sp, nil
}