-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtransport.go
384 lines (316 loc) · 8.81 KB
/
transport.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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
package token
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strings"
kithttp "github.com/go-kit/kit/transport/http"
"github.com/xmidt-org/themis/xhttp/xhttpserver"
"github.com/gorilla/mux"
"go.uber.org/multierr"
)
var (
ErrVariableNotAllowed = errors.New("Either header/parameter or variable can specified, but not all three")
)
// InvalidPartnerIDError is the error object returned when a blank, wildcard, or otherwise
// invalid partner id is submitted
type InvalidPartnerIDError struct{}
// Error returns the error string associated with an invalid partner id
func (ipe InvalidPartnerIDError) Error() string {
return "invalid partner id"
}
func (ipe InvalidPartnerIDError) StatusCode() int {
return http.StatusBadRequest
}
// BuildError is the error type usually returned by RequestBuilder.Build to indicate what
// happened during each request builder.
type BuildError struct {
Err error
}
// Error returns the nested error's Error text
func (be BuildError) Error() string {
return be.Err.Error()
}
func (be BuildError) Unwrap() error {
return be.Err
}
// StatusCode returns the largest numeric HTTP status code of any embedded errors,
// or http.StatusBadRequest is none of the embedded errors reported status codes.
func (be BuildError) StatusCode() int {
statusCode := 0
for _, err := range multierr.Errors(be.Err) {
if sc, ok := err.(kithttp.StatusCoder); ok {
if statusCode < sc.StatusCode() {
statusCode = sc.StatusCode()
}
}
}
if statusCode == 0 {
return http.StatusBadRequest
}
return statusCode
}
// RequestBuilder is a strategy for building a token factory Request from an HTTP request.
//
// Note: before invoking a RequestBuilder, calling code should parse the HTTP request form.
type RequestBuilder interface {
Build(*http.Request, *Request) error
}
type RequestBuilderFunc func(*http.Request, *Request) error
func (rbf RequestBuilderFunc) Build(original *http.Request, tr *Request) error {
return rbf(original, tr)
}
// RequestBuilders represents a set of RequestBuilder strategies that can be invoked in sequence.
type RequestBuilders []RequestBuilder
// Build invokes each request builder in sequence. Prior to invoking any of the chain of builders,
func (rbs RequestBuilders) Build(original *http.Request, tr *Request) error {
var err error
for _, rb := range rbs {
multierr.AppendInto(&err, rb.Build(original, tr))
}
if err != nil {
return BuildError{Err: err}
}
return nil
}
func claimsSetter(key string, value interface{}, tr *Request) {
tr.Claims[key] = value
}
func metadataSetter(key string, value interface{}, tr *Request) {
tr.Metadata[key] = value
}
type headerParameterRequestBuilder struct {
key string
header string
parameter string
setter func(string, interface{}, *Request)
}
func (hprb headerParameterRequestBuilder) Build(original *http.Request, tr *Request) error {
if len(hprb.header) > 0 {
value := original.Header[hprb.header]
if len(value) > 0 {
hprb.setter(hprb.key, value[0], tr)
return nil
}
}
if len(hprb.parameter) > 0 {
value := original.Form[hprb.parameter]
if len(value) > 0 {
hprb.setter(hprb.key, value[0], tr)
return nil
}
}
return nil
}
type variableRequestBuilder struct {
key string
variable string
setter func(string, interface{}, *Request)
}
func (vrb variableRequestBuilder) Build(original *http.Request, tr *Request) error {
value := mux.Vars(original)[vrb.variable]
if len(value) > 0 {
vrb.setter(vrb.key, value, tr)
return nil
}
return xhttpserver.MissingVariableError{Variable: vrb.variable}
}
type partnerIDRequestBuilder struct {
PartnerID
}
func (prb partnerIDRequestBuilder) getPartnerID(original *http.Request) (string, error) {
var value string
if len(prb.Header) > 0 {
value = original.Header.Get(prb.Header)
}
if len(value) == 0 && len(prb.Parameter) > 0 {
values := original.Form[prb.Parameter]
if len(values) > 0 {
value = values[0]
}
}
if len(value) > 0 {
// some post-processing on the partner id value:
// don't allow multiple values separated by ","
// don't allow the "*" partner id
for _, v := range strings.Split(value, ",") {
v = strings.TrimSpace(v)
if len(v) > 0 && v != "*" {
return v, nil // the cleaned partner id
}
}
// a partner id must have at least (1) segment that is not blank and is not the wildcard '*'
return "", InvalidPartnerIDError{}
}
// return the default as is, without any of the special processing above
return prb.Default, nil
}
func (prb partnerIDRequestBuilder) Build(original *http.Request, tr *Request) error {
partnerID, err := prb.getPartnerID(original)
if err != nil {
return err
}
if len(partnerID) > 0 {
if len(prb.Claim) > 0 {
tr.Claims[prb.Claim] = partnerID
}
if len(prb.Metadata) > 0 {
tr.Metadata[prb.Metadata] = partnerID
}
}
return nil
}
// NewRequestBuilders creates a RequestBuilders sequence given an Options configuration. Only claims
// and metadata that are HTTP-based are included in the results. Claims and metadata that are statically
// assigned values are handled by ClaimBuilder objects and are part of the Factory configuration.
func NewRequestBuilders(o Options) (RequestBuilders, error) {
var rb RequestBuilders
for _, value := range o.Claims {
switch {
case len(value.Key) == 0:
return nil, ErrMissingKey
case len(value.Header) > 0 || len(value.Parameter) > 0:
if len(value.Variable) > 0 {
return nil, ErrVariableNotAllowed
}
rb = append(rb,
headerParameterRequestBuilder{
key: value.Key,
header: http.CanonicalHeaderKey(value.Header),
parameter: value.Parameter,
setter: claimsSetter,
},
)
case len(value.Variable) > 0:
rb = append(rb,
variableRequestBuilder{
key: value.Key,
variable: value.Variable,
setter: claimsSetter,
},
)
}
}
for _, value := range o.Metadata {
switch {
case len(value.Key) == 0:
return nil, ErrMissingKey
case len(value.Header) > 0 || len(value.Parameter) > 0:
if len(value.Variable) > 0 {
return nil, ErrVariableNotAllowed
}
rb = append(rb,
headerParameterRequestBuilder{
key: value.Key,
header: http.CanonicalHeaderKey(value.Header),
parameter: value.Parameter,
setter: metadataSetter,
},
)
case len(value.Variable) > 0:
rb = append(rb,
variableRequestBuilder{
key: value.Key,
variable: value.Variable,
setter: metadataSetter,
},
)
}
}
if o.PartnerID != nil && (len(o.PartnerID.Claim) > 0 || len(o.PartnerID.Metadata) > 0) {
rb = append(rb,
partnerIDRequestBuilder{
PartnerID: *o.PartnerID,
},
)
}
return rb, nil
}
// BuildRequest applies a sequence of RequestBuilder instances to produce a token factory Request
func BuildRequest(original *http.Request, rb RequestBuilders) (*Request, error) {
tr := NewRequest()
if err := rb.Build(original, tr); err != nil {
return nil, err
}
return tr, nil
}
func DecodeServerRequest(rb RequestBuilders) func(context.Context, *http.Request) (interface{}, error) {
return func(ctx context.Context, hr *http.Request) (interface{}, error) {
if err := hr.ParseForm(); err != nil {
return nil, httpError{
err: err,
code: http.StatusBadRequest,
}
}
tr, err := BuildRequest(hr, rb)
if err != nil {
return nil, err
}
return tr, nil
}
}
func EncodeIssueResponse(_ context.Context, response http.ResponseWriter, value interface{}) error {
response.Header().Set("Content-Type", "application/jose")
_, err := response.Write([]byte(value.(string)))
return err
}
type DecodeClaimsError struct {
URL string
StatusCode int
Err error
}
func (dce *DecodeClaimsError) Unwrap() error {
return dce.Err
}
func (dce *DecodeClaimsError) nestedErrorText() string {
if dce.Err != nil {
return dce.Err.Error()
}
return ""
}
func (dce *DecodeClaimsError) Error() string {
return fmt.Sprintf(
"Failed to decode remote claims from [%s]: statusCode=%d, err=%s",
dce.URL,
dce.StatusCode,
dce.nestedErrorText(),
)
}
func (dce *DecodeClaimsError) MarshalJSON() ([]byte, error) {
var output bytes.Buffer
fmt.Fprintf(
&output,
`{"url": "%s", "statusCode": %d, "err": "%s"}`,
dce.URL,
dce.StatusCode,
dce.nestedErrorText(),
)
return output.Bytes(), nil
}
func DecodeRemoteClaimsResponse(_ context.Context, response *http.Response) (interface{}, error) {
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, err
}
if response.StatusCode < 200 || response.StatusCode > 299 {
err := &DecodeClaimsError{
StatusCode: response.StatusCode,
}
if response.Request != nil {
err.URL = response.Request.URL.String()
}
return nil, err
}
// allow empty bodies
var claims map[string]interface{}
if len(body) > 0 {
if err := json.Unmarshal(body, &claims); err != nil {
return nil, err
}
}
return claims, nil
}