-
Notifications
You must be signed in to change notification settings - Fork 352
/
Copy pathauthclient.go
161 lines (131 loc) · 3.54 KB
/
authclient.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
package auth
import (
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"
"github.com/opentracing/opentracing-go"
"github.com/zalando/skipper/filters"
"github.com/zalando/skipper/net"
)
const (
webhookSpanName = "webhook"
tokenInfoSpanName = "tokeninfo"
tokenIntrospectionSpanName = "tokenintrospection"
)
const (
defaultMaxIdleConns = 64
)
type authClient struct {
url *url.URL
cli *net.Client
}
type tokeninfoClient interface {
getTokeninfo(token string, ctx filters.FilterContext) (map[string]any, error)
Close()
}
var _ tokeninfoClient = &authClient{}
func newAuthClient(baseURL, spanName string, timeout time.Duration, maxIdleConns int, tracer opentracing.Tracer) (*authClient, error) {
if tracer == nil {
tracer = opentracing.NoopTracer{}
}
if maxIdleConns <= 0 {
maxIdleConns = defaultMaxIdleConns
}
u, err := url.Parse(baseURL)
if err != nil {
return nil, err
}
cli := net.NewClient(net.Options{
ResponseHeaderTimeout: timeout,
TLSHandshakeTimeout: timeout,
MaxIdleConnsPerHost: maxIdleConns,
Tracer: tracer,
OpentracingComponentTag: "skipper",
OpentracingSpanName: spanName,
})
return &authClient{url: u, cli: cli}, nil
}
func (ac *authClient) Close() {
ac.cli.Close()
}
func bindContext(ctx filters.FilterContext, req *http.Request) *http.Request {
return req.WithContext(ctx.Request().Context())
}
func (ac *authClient) getTokenintrospect(token string, ctx filters.FilterContext) (tokenIntrospectionInfo, error) {
body := url.Values{}
body.Add(tokenKey, token)
req, err := http.NewRequest("POST", ac.url.String(), strings.NewReader(body.Encode()))
if err != nil {
return nil, err
}
req = bindContext(ctx, req)
if ac.url.User != nil {
authorization := base64.StdEncoding.EncodeToString([]byte(ac.url.User.String()))
req.Header.Add("Authorization", fmt.Sprintf("Basic %s", authorization))
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
rsp, err := ac.cli.Do(req)
if err != nil {
return nil, err
}
defer rsp.Body.Close()
if rsp.StatusCode != 200 {
io.Copy(io.Discard, rsp.Body)
if rsp.StatusCode != 403 && rsp.StatusCode != 401 {
return nil, fmt.Errorf("failed with status code: %d", rsp.StatusCode)
}
return nil, errInvalidToken
}
buf, err := io.ReadAll(rsp.Body)
if err != nil {
return nil, err
}
info := make(tokenIntrospectionInfo)
err = json.Unmarshal(buf, &info)
return info, err
}
func (ac *authClient) getTokeninfo(token string, ctx filters.FilterContext) (map[string]interface{}, error) {
var doc map[string]interface{}
req, err := http.NewRequest("GET", ac.url.String(), nil)
if err != nil {
return doc, err
}
req = bindContext(ctx, req)
if token != "" {
req.Header.Set(authHeaderName, authHeaderPrefix+token)
}
rsp, err := ac.cli.Do(req)
if err != nil {
return doc, err
}
defer rsp.Body.Close()
if rsp.StatusCode != 200 {
io.Copy(io.Discard, rsp.Body)
if rsp.StatusCode != 403 && rsp.StatusCode != 401 {
return nil, fmt.Errorf("failed with status code: %d", rsp.StatusCode)
}
return doc, errInvalidToken
}
d := json.NewDecoder(rsp.Body)
err = d.Decode(&doc)
return doc, err
}
func (ac *authClient) getWebhook(ctx filters.FilterContext) (*http.Response, error) {
req, err := http.NewRequest("GET", ac.url.String(), nil)
if err != nil {
return nil, err
}
req = bindContext(ctx, req)
copyHeader(req.Header, ctx.Request().Header)
rsp, err := ac.cli.Do(req)
if err != nil {
return nil, err
}
defer rsp.Body.Close()
return rsp, nil
}