-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmessage.go
More file actions
224 lines (191 loc) · 5.05 KB
/
message.go
File metadata and controls
224 lines (191 loc) · 5.05 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package httpsign
import (
"fmt"
"io"
"net/http"
"net/url"
"time"
)
// MessageDetails aggregates the details of a signed message, for a given signature
type MessageDetails struct {
KeyID *string // nil when keyid parameter is absent (RFC 9421 does not require it)
Alg string
Fields Fields
Created *time.Time
Expires *time.Time
Nonce *string
Tag *string
}
// Message represents a parsed HTTP message ready for signature verification.
type Message struct {
headers http.Header
trailers http.Header
body *io.ReadCloser
method string
url *url.URL
authority string
scheme string
statusCode *int
assocReq *Message
}
// NewMessage constructs a new Message from the provided config.
func NewMessage(config *MessageConfig) (*Message, error) {
if config == nil {
config = NewMessageConfig()
}
hasRequest := config.method != ""
hasResponse := config.statusCode != nil
if !hasRequest && !hasResponse {
return nil, fmt.Errorf("message config must have either method (for request) or status code (for response)")
}
if hasRequest && hasResponse {
return nil, fmt.Errorf("message config cannot have both request and response fields set")
}
if hasRequest {
if config.headers == nil {
return nil, fmt.Errorf("request message must have headers")
}
}
if hasResponse {
if config.headers == nil {
return nil, fmt.Errorf("response message must have headers")
}
}
var assocReq *Message
if config.assocReq != nil {
method := config.assocReq.method
u := config.assocReq.url
headers := config.assocReq.headers
authority := config.assocReq.authority
scheme := config.assocReq.scheme
if method == "" || u == nil || headers == nil || authority == "" || scheme == "" {
return nil, fmt.Errorf("invalid associated request")
}
assocReq = &Message{
method: method,
url: u,
headers: headers,
authority: authority,
scheme: scheme,
}
}
return &Message{
headers: config.headers,
trailers: config.trailers,
body: config.body,
method: config.method,
url: config.url,
authority: config.authority,
scheme: config.scheme,
statusCode: config.statusCode,
assocReq: assocReq,
}, nil
}
// MessageConfig configures a Message for signature verification.
type MessageConfig struct {
method string
url *url.URL
headers http.Header
trailers http.Header
body *io.ReadCloser
authority string
scheme string
statusCode *int
assocReq *MessageConfig
}
// NewMessageConfig returns a new MessageConfig.
func NewMessageConfig() *MessageConfig {
return &MessageConfig{}
}
func (b *MessageConfig) WithMethod(method string) *MessageConfig {
b.method = method
return b
}
func (b *MessageConfig) WithURL(u *url.URL) *MessageConfig {
b.url = u
return b
}
func (b *MessageConfig) WithHeaders(headers http.Header) *MessageConfig {
b.headers = headers
return b
}
func (b *MessageConfig) WithTrailers(trailers http.Header) *MessageConfig {
b.trailers = trailers
return b
}
func (b *MessageConfig) WithBody(body *io.ReadCloser) *MessageConfig {
b.body = body
return b
}
func (b *MessageConfig) WithAuthority(authority string) *MessageConfig {
b.authority = authority
return b
}
func (b *MessageConfig) WithScheme(scheme string) *MessageConfig {
b.scheme = scheme
return b
}
func (b *MessageConfig) WithStatusCode(statusCode int) *MessageConfig {
b.statusCode = &statusCode
return b
}
func (b *MessageConfig) WithAssociatedRequest(method string, u *url.URL, headers http.Header, authority, scheme string) *MessageConfig {
b.assocReq = &MessageConfig{
method: method,
url: u,
headers: headers,
authority: authority,
scheme: scheme,
}
return b
}
func (b *MessageConfig) WithRequest(req *http.Request) *MessageConfig {
if req == nil {
return b
}
scheme := "http"
if req.TLS != nil {
scheme = "https"
}
return b.
WithMethod(req.Method).
WithURL(req.URL).
WithHeaders(req.Header).
WithTrailers(req.Trailer).
WithBody(&req.Body).
WithAuthority(req.Host).
WithScheme(scheme)
}
// WithResponse configures the message from a response and optional associated request.
// If the scheme was set via WithScheme before WithResponse, that value is used for the
// associated request's @scheme (e.g. from X-Forwarded-Proto when behind a reverse proxy).
// Otherwise, the scheme is derived from req.TLS.
func (b *MessageConfig) WithResponse(res *http.Response, req *http.Request) *MessageConfig {
if res == nil {
return b
}
b = b.
WithStatusCode(res.StatusCode).
WithHeaders(res.Header).
WithTrailers(res.Trailer).
WithBody(&res.Body)
if req != nil {
scheme := "http"
if req.TLS != nil {
scheme = "https"
}
if b.scheme != "" {
scheme = b.scheme
}
b = b.WithAssociatedRequest(req.Method, req.URL, req.Header, req.Host, scheme)
}
return b
}
// Verify verifies a signature on this message.
func (m *Message) Verify(signatureName string, verifier Verifier) (*MessageDetails, error) {
_, psiSig, err := verifyDebug(signatureName, verifier, m)
if err != nil {
return nil, err
}
return signatureDetails(psiSig)
}