-
-
Notifications
You must be signed in to change notification settings - Fork 361
/
authenticator_oauth2_introspection.go
368 lines (299 loc) · 10.7 KB
/
authenticator_oauth2_introspection.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
package authn
import (
"context"
"crypto/md5"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
"sync"
"time"
"github.com/ory/fosite"
"github.com/dgraph-io/ristretto"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
"github.com/pkg/errors"
"golang.org/x/oauth2/clientcredentials"
"github.com/ory/go-convenience/stringslice"
"github.com/ory/x/httpx"
"github.com/ory/x/logrusx"
"github.com/ory/oathkeeper/driver/configuration"
"github.com/ory/oathkeeper/helper"
"github.com/ory/oathkeeper/pipeline"
)
type AuthenticatorOAuth2IntrospectionConfiguration struct {
Scopes []string `json:"required_scope"`
Audience []string `json:"target_audience"`
Issuers []string `json:"trusted_issuers"`
PreAuth *AuthenticatorOAuth2IntrospectionPreAuthConfiguration `json:"pre_authorization"`
ScopeStrategy string `json:"scope_strategy"`
IntrospectionURL string `json:"introspection_url"`
BearerTokenLocation *helper.BearerTokenLocation `json:"token_from"`
IntrospectionRequestHeaders map[string]string `json:"introspection_request_headers"`
Retry *AuthenticatorOAuth2IntrospectionRetryConfiguration `json:"retry"`
Cache cacheConfig `json:"cache"`
}
type AuthenticatorOAuth2IntrospectionPreAuthConfiguration struct {
Enabled bool `json:"enabled"`
ClientID string `json:"client_id"`
ClientSecret string `json:"client_secret"`
Audience string `json:"audience"`
Scope []string `json:"scope"`
TokenURL string `json:"token_url"`
}
type AuthenticatorOAuth2IntrospectionRetryConfiguration struct {
Timeout string `json:"max_delay"`
MaxWait string `json:"give_up_after"`
}
type cacheConfig struct {
Enabled bool `json:"enabled"`
TTL string `json:"ttl"`
MaxCost int `json:"max_cost"`
}
type AuthenticatorOAuth2Introspection struct {
c configuration.Provider
clientMap map[string]*http.Client
mu sync.RWMutex
tokenCache *ristretto.Cache
cacheTTL *time.Duration
logger *logrusx.Logger
}
func NewAuthenticatorOAuth2Introspection(c configuration.Provider, logger *logrusx.Logger) *AuthenticatorOAuth2Introspection {
return &AuthenticatorOAuth2Introspection{c: c, logger: logger, clientMap: make(map[string]*http.Client)}
}
func (a *AuthenticatorOAuth2Introspection) GetID() string {
return "oauth2_introspection"
}
type AuthenticatorOAuth2IntrospectionResult struct {
Active bool `json:"active"`
Extra map[string]interface{} `json:"ext"`
Subject string `json:"sub,omitempty"`
Username string `json:"username"`
Audience []string `json:"aud"`
TokenType string `json:"token_type"`
Issuer string `json:"iss"`
ClientID string `json:"client_id,omitempty"`
Scope string `json:"scope,omitempty"`
Expires int64 `json:"exp"`
TokenUse string `json:"token_use"`
}
func (a *AuthenticatorOAuth2Introspection) tokenFromCache(config *AuthenticatorOAuth2IntrospectionConfiguration, token string, ss fosite.ScopeStrategy) *AuthenticatorOAuth2IntrospectionResult {
if !config.Cache.Enabled {
return nil
}
if ss == nil && len(config.Scopes) > 0 {
return nil
}
item, found := a.tokenCache.Get(token)
if !found {
return nil
}
i, ok := item.(*AuthenticatorOAuth2IntrospectionResult)
if !ok {
return nil
}
return i
}
func (a *AuthenticatorOAuth2Introspection) tokenToCache(config *AuthenticatorOAuth2IntrospectionConfiguration, i *AuthenticatorOAuth2IntrospectionResult, token string, ss fosite.ScopeStrategy) {
if !config.Cache.Enabled {
return
}
if ss == nil && len(config.Scopes) > 0 {
return
}
if a.cacheTTL != nil {
a.tokenCache.SetWithTTL(token, i, 1, *a.cacheTTL)
} else {
a.tokenCache.Set(token, i, 1)
}
}
func (a *AuthenticatorOAuth2Introspection) traceRequest(ctx context.Context, req *http.Request) func() {
tracer := opentracing.GlobalTracer()
if tracer == nil {
return func() {}
}
parentSpan := opentracing.SpanFromContext(ctx)
opts := make([]opentracing.StartSpanOption, 0, 1)
if parentSpan != nil {
opts = append(opts, opentracing.ChildOf(parentSpan.Context()))
}
urlStr := req.URL.String()
clientSpan := tracer.StartSpan(req.Method+" "+urlStr, opts...)
ext.SpanKindRPCClient.Set(clientSpan)
ext.HTTPUrl.Set(clientSpan, urlStr)
ext.HTTPMethod.Set(clientSpan, req.Method)
_ = tracer.Inject(clientSpan.Context(), opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(req.Header))
return clientSpan.Finish
}
func (a *AuthenticatorOAuth2Introspection) Authenticate(r *http.Request, session *AuthenticationSession, config json.RawMessage, _ pipeline.Rule) error {
cf, client, err := a.Config(config)
if err != nil {
return err
}
token := helper.BearerTokenFromRequest(r, cf.BearerTokenLocation)
if token == "" {
return errors.WithStack(ErrAuthenticatorNotResponsible)
}
ss := a.c.ToScopeStrategy(cf.ScopeStrategy, "authenticators.oauth2_introspection.config.scope_strategy")
i := a.tokenFromCache(cf, token, ss)
// If the token can not be found, and the scope strategy is nil, and the required scope list
// is not empty, then we can not use the cache.
if i == nil {
body := url.Values{"token": {token}}
if ss == nil {
body.Add("scope", strings.Join(cf.Scopes, " "))
}
introspectReq, err := http.NewRequest(http.MethodPost, cf.IntrospectionURL, strings.NewReader(body.Encode()))
if err != nil {
return errors.WithStack(err)
}
for key, value := range cf.IntrospectionRequestHeaders {
introspectReq.Header.Set(key, value)
}
// set/override the content-type header
introspectReq.Header.Set("Content-Type", "application/x-www-form-urlencoded")
// add tracing
closeSpan := a.traceRequest(r.Context(), introspectReq)
resp, err := client.Do(introspectReq.WithContext(r.Context()))
// close the span so it represents just the http request
closeSpan()
if err != nil {
return errors.WithStack(err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return errors.Errorf("Introspection returned status code %d but expected %d", resp.StatusCode, http.StatusOK)
}
if err := json.NewDecoder(resp.Body).Decode(&i); err != nil {
return errors.WithStack(err)
}
}
if len(i.TokenUse) > 0 && i.TokenUse != "access_token" {
return errors.WithStack(helper.ErrForbidden.WithReason(fmt.Sprintf("Use of introspected token is not an access token but \"%s\"", i.TokenUse)))
}
if !i.Active {
return errors.WithStack(helper.ErrUnauthorized.WithReason("Access token is not active"))
}
if i.Expires > 0 && time.Unix(i.Expires, 0).Before(time.Now()) {
return errors.WithStack(helper.ErrUnauthorized.WithReason("Access token expired"))
}
for _, audience := range cf.Audience {
if !stringslice.Has(i.Audience, audience) {
return errors.WithStack(helper.ErrForbidden.WithReason(fmt.Sprintf("Token audience is not intended for target audience %s", audience)))
}
}
if len(cf.Issuers) > 0 {
if !stringslice.Has(cf.Issuers, i.Issuer) {
return errors.WithStack(helper.ErrForbidden.WithReason(fmt.Sprintf("Token issuer does not match any trusted issuer")))
}
}
if ss != nil {
for _, scope := range cf.Scopes {
if !ss(strings.Split(i.Scope, " "), scope) {
return errors.WithStack(helper.ErrForbidden.WithReason(fmt.Sprintf("Scope %s was not granted", scope)))
}
}
}
a.tokenToCache(cf, i, token, ss)
if len(i.Extra) == 0 {
i.Extra = map[string]interface{}{}
}
i.Extra["username"] = i.Username
i.Extra["client_id"] = i.ClientID
i.Extra["scope"] = i.Scope
if len(i.Audience) != 0 {
i.Extra["aud"] = i.Audience
}
session.Subject = i.Subject
session.Extra = i.Extra
return nil
}
func (a *AuthenticatorOAuth2Introspection) Validate(config json.RawMessage) error {
if !a.c.AuthenticatorIsEnabled(a.GetID()) {
return NewErrAuthenticatorNotEnabled(a)
}
_, _, err := a.Config(config)
return err
}
func (a *AuthenticatorOAuth2Introspection) Config(config json.RawMessage) (*AuthenticatorOAuth2IntrospectionConfiguration, *http.Client, error) {
var c AuthenticatorOAuth2IntrospectionConfiguration
if err := a.c.AuthenticatorConfig(a.GetID(), config, &c); err != nil {
return nil, nil, NewErrAuthenticatorMisconfigured(a, err)
}
rawKey, err := json.Marshal(&c)
if err != nil {
return nil, nil, errors.WithStack(err)
}
clientKey := fmt.Sprintf("%x", md5.Sum(rawKey))
a.mu.RLock()
client, ok := a.clientMap[clientKey]
a.mu.RUnlock()
if !ok || client == nil {
a.logger.Debug("Initializing http client")
var rt http.RoundTripper
if c.PreAuth != nil && c.PreAuth.Enabled {
var ep url.Values
if c.PreAuth.Audience != "" {
ep = url.Values{"audience": {c.PreAuth.Audience}}
}
rt = (&clientcredentials.Config{
ClientID: c.PreAuth.ClientID,
ClientSecret: c.PreAuth.ClientSecret,
Scopes: c.PreAuth.Scope,
EndpointParams: ep,
TokenURL: c.PreAuth.TokenURL,
}).Client(context.Background()).Transport
}
if c.Retry == nil {
c.Retry = &AuthenticatorOAuth2IntrospectionRetryConfiguration{Timeout: "500ms", MaxWait: "1s"}
} else {
if c.Retry.Timeout == "" {
c.Retry.Timeout = "500ms"
}
if c.Retry.MaxWait == "" {
c.Retry.MaxWait = "1s"
}
}
duration, err := time.ParseDuration(c.Retry.Timeout)
if err != nil {
return nil, nil, errors.WithStack(err)
}
timeout := time.Millisecond * duration
maxWait, err := time.ParseDuration(c.Retry.MaxWait)
if err != nil {
return nil, nil, errors.WithStack(err)
}
client = httpx.NewResilientClientLatencyToleranceConfigurable(rt, timeout, maxWait)
a.mu.Lock()
a.clientMap[clientKey] = client
a.mu.Unlock()
}
if c.Cache.TTL != "" {
cacheTTL, err := time.ParseDuration(c.Cache.TTL)
if err != nil {
return nil, nil, err
}
a.cacheTTL = &cacheTTL
}
if a.tokenCache == nil {
cost := int64(c.Cache.MaxCost)
if cost == 0 {
cost = 100000000
}
a.logger.Debugf("Creating cache with max cost: %d", c.Cache.MaxCost)
cache, err := ristretto.NewCache(&ristretto.Config{
// This will hold about 1000 unique mutation responses.
NumCounters: 10000,
// Allocate a max
MaxCost: cost,
// This is a best-practice value.
BufferItems: 64,
})
if err != nil {
return nil, nil, err
}
a.tokenCache = cache
}
return &c, client, nil
}