-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathmw_auth_key.go
More file actions
449 lines (377 loc) · 14.6 KB
/
Copy pathmw_auth_key.go
File metadata and controls
449 lines (377 loc) · 14.6 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
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
package gateway
import (
"errors"
"net/http"
"slices"
"strings"
"time"
"github.com/TykTechnologies/tyk/apidef"
"github.com/TykTechnologies/tyk/config"
"github.com/TykTechnologies/tyk/internal/crypto"
"github.com/TykTechnologies/tyk/internal/httpctx"
"github.com/TykTechnologies/tyk/internal/otel"
"github.com/TykTechnologies/tyk/request"
signaturevalidator "github.com/TykTechnologies/tyk/signature_validator"
"github.com/TykTechnologies/tyk/storage"
"github.com/TykTechnologies/tyk/user"
)
const (
defaultSignatureErrorCode = http.StatusUnauthorized
defaultSignatureErrorMessage = "Request signature verification failed"
)
const (
ErrAuthAuthorizationFieldMissing = "auth.auth_field_missing"
ErrAuthKeyNotFound = "auth.key_not_found"
ErrAuthCertNotFound = "auth.cert_not_found"
ErrAuthCertExpired = "auth.cert_expired"
ErrAuthKeyIsInvalid = "auth.key_is_invalid"
ErrAuthCertRequired = "auth.cert_required"
ErrAuthCertMismatch = "auth.cert_mismatch"
MsgNonExistentKey = "Attempted access with non-existent key."
MsgNonExistentCert = "Attempted access with non-existent cert."
MsgCertificateMismatch = "Attempted access with incorrect certificate."
MsgInvalidKey = "Attempted access with invalid key."
)
func initAuthKeyErrors() {
TykErrors[ErrAuthAuthorizationFieldMissing] = config.TykError{
Message: MsgAuthFieldMissing,
Code: http.StatusUnauthorized,
}
TykErrors[ErrAuthKeyNotFound] = config.TykError{
Message: MsgApiAccessDisallowed,
Code: http.StatusForbidden,
}
TykErrors[ErrAuthCertNotFound] = config.TykError{
Message: MsgApiAccessDisallowed,
Code: http.StatusForbidden,
}
TykErrors[ErrAuthKeyIsInvalid] = config.TykError{
Message: MsgApiAccessDisallowed,
Code: http.StatusForbidden,
}
TykErrors[ErrAuthCertExpired] = config.TykError{
Message: MsgCertificateExpired,
Code: http.StatusForbidden,
}
TykErrors[ErrAuthCertRequired] = config.TykError{
Message: MsgAuthCertRequired,
Code: http.StatusUnauthorized,
}
TykErrors[ErrAuthCertMismatch] = config.TykError{
Message: MsgApiAccessDisallowed,
Code: http.StatusUnauthorized,
}
}
// KeyExists will check if the key being used to access the API is in the request data,
// and then if the key is in the storage engine
type AuthKey struct {
*BaseMiddleware
}
func (k *AuthKey) Name() string {
return "AuthKey"
}
func (k *AuthKey) setContextVars(r *http.Request, token string) {
// Flatten claims and add to context
if !k.Spec.EnableContextVars {
return
}
if cnt := ctxGetData(r); cnt != nil {
// Key data
cnt["token"] = token
ctxSetData(r, cnt)
}
}
// getAuthType overrides BaseMiddleware.getAuthType.
func (k *AuthKey) getAuthType() string {
return apidef.AuthTokenType
}
// checkSessionWithCertFallback attempts to find a session using a generated token from certHash,
// falling back to checking with the certHash directly if the token lookup fails.
func (k *AuthKey) checkSessionWithCertFallback(r *http.Request, certHash string) (user.SessionState, bool) {
key := k.Gw.generateToken(k.Spec.OrgID, certHash)
session, keyExists := k.CheckSessionAndIdentityForValidKey(key, r)
if !keyExists {
session, keyExists = k.CheckSessionAndIdentityForValidKey(certHash, r)
}
return session, keyExists
}
func (k *AuthKey) ProcessRequest(_ http.ResponseWriter, r *http.Request, _ interface{}) (error, int) {
if ctxGetRequestStatus(r) == StatusOkAndIgnore {
return nil, http.StatusOK
}
// skip auth key check if the request is looped.
if ses := ctxGetSession(r); ses != nil && httpctx.IsSelfLooping(r) {
return nil, http.StatusOK
}
key, authConfig := k.getAuthToken(k.getAuthType(), r)
if key != "" {
key = stripBearer(key)
}
var keyExists, updateSession bool
var certHash string
var session user.SessionState
session, keyExists = k.CheckSessionAndIdentityForValidKey(key, r)
if !authConfig.UseCertificate {
if key == "" {
k.Logger().Info("Attempted access with malformed header, no auth header found.")
return errorAndStatusCode(ErrAuthAuthorizationFieldMissing)
}
if !keyExists {
return k.reportInvalidKey(key, r, MsgNonExistentKey, ErrAuthKeyNotFound)
}
}
if authConfig.UseCertificate && r.TLS != nil {
if len(r.TLS.PeerCertificates) > 0 {
if time.Now().After(r.TLS.PeerCertificates[0].NotAfter) {
return errorAndStatusCode(ErrAuthCertExpired)
}
certHash = k.Spec.OrgID + crypto.HexSHA256(r.TLS.PeerCertificates[0].Raw)
}
if !k.Gw.GetConfig().Security.AllowUnsafeDynamicMTLSToken {
if certHash == "" {
return errorAndStatusCode(ErrAuthCertRequired)
}
session, keyExists = k.checkSessionWithCertFallback(r, certHash)
if !keyExists {
return errorAndStatusCode(ErrAuthCertMismatch)
}
} else {
if certHash != "" {
session, keyExists = k.checkSessionWithCertFallback(r, certHash)
if !keyExists {
return errorAndStatusCode(ErrAuthCertMismatch)
}
}
if key != "" {
session, keyExists = k.CheckSessionAndIdentityForValidKey(key, r)
if !keyExists {
return k.reportInvalidKey(key, r, MsgNonExistentKey, ErrAuthKeyNotFound)
}
}
}
} else {
if !keyExists {
// fallback to search by cert
session, keyExists = k.CheckSessionAndIdentityForValidKey(certHash, r)
if !keyExists {
return k.reportInvalidKey(key, r, MsgNonExistentKey, ErrAuthKeyNotFound)
}
}
}
key = session.KeyID
// Validate certificate binding or dynamic mTLS
// Certificate binding validation runs when:
// 1. The session has certificate bindings AND static mTLS is enabled (certificate-token binding), OR
// 2. UseCertificate is explicitly set for this API (dynamic mTLS mode)
useCertBinding := k.shouldValidateCertificateBinding(&session)
if authConfig.UseCertificate || useCertBinding {
ctx := &certValidationContext{
request: r,
key: key,
session: &session,
certHash: &certHash,
updateSession: &updateSession,
useCertBinding: useCertBinding,
}
if code, err := k.validateCertificate(ctx); err != nil {
return err, code
}
}
// Set session state on context, we will need it later
switch k.Spec.BaseIdentityProvidedBy {
case apidef.AuthToken, apidef.UnsetAuth:
hashKeys := k.Gw.GetConfig().HashKeys
ctxSetSession(r, &session, updateSession, hashKeys)
k.setContextVars(r, key)
attributes := []otel.SpanAttribute{otel.APIKeyAliasAttribute(session.Alias)}
if hashKeys {
attributes = append(attributes, otel.APIKeyAttribute(session.KeyHash()))
}
ctxSetSpanAttributes(r, k.Name(), attributes...)
}
// Try using org-key format first:
if strings.HasPrefix(key, session.OrgID) {
err, statusCode := k.validateSignature(r, key[len(session.OrgID):])
if err == nil && statusCode == http.StatusOK {
return err, statusCode
}
}
// As a second approach, try to use the internal ID that's part of the B64 JSON key:
keyID, err := storage.TokenID(key)
if err == nil {
err, statusCode := k.validateSignature(r, keyID)
if err == nil {
return nil, statusCode
}
}
// Last try is to take the key as is:
return k.validateSignature(r, key)
}
func (k *AuthKey) reportInvalidKey(key string, r *http.Request, msg string, errMsg string) (error, int) {
k.Logger().WithField("key", k.Gw.obfuscateKey(key)).Info(msg)
// Fire Authfailed Event
AuthFailed(k, r, key)
// Report in health check
reportHealthValue(k.Spec, KeyFailure, "1")
return errorAndStatusCode(errMsg)
}
// certValidationContext holds all the context needed for certificate validation
type certValidationContext struct {
request *http.Request
key string
session *user.SessionState
certHash *string
updateSession *bool
useCertBinding bool
}
// shouldValidateCertificateBinding determines if certificate-token binding should be enforced.
// Certificate binding only applies when static mTLS is enabled at the API level.
func (k *AuthKey) shouldValidateCertificateBinding(session *user.SessionState) bool {
return len(session.MtlsStaticCertificateBindings) > 0 && k.Spec.UseMutualTLSAuth
}
func (k *AuthKey) validateSignature(r *http.Request, key string) (error, int) {
_, authConfig := k.getAuthToken(k.getAuthType(), r)
logger := k.Logger().WithField("key", k.Gw.obfuscateKey(key))
if !authConfig.ValidateSignature {
return nil, http.StatusOK
}
errorCode := defaultSignatureErrorCode
if authConfig.Signature.ErrorCode != 0 {
errorCode = authConfig.Signature.ErrorCode
}
errorMessage := defaultSignatureErrorMessage
if authConfig.Signature.ErrorMessage != "" {
errorMessage = authConfig.Signature.ErrorMessage
}
validator := signaturevalidator.SignatureValidator{}
if err := validator.Init(authConfig.Signature.Algorithm); err != nil {
logger.WithError(err).Info("Invalid signature verification algorithm")
return errors.New("internal server error"), http.StatusInternalServerError
}
signature := r.Header.Get(authConfig.Signature.Header)
paramName := authConfig.Signature.ParamName
if authConfig.Signature.UseParam || paramName != "" {
if paramName == "" {
paramName = authConfig.Signature.Header
}
paramValue := r.URL.Query().Get(paramName)
// Only use the paramValue if it has an actual value
if paramValue != "" {
signature = paramValue
}
}
if signature == "" {
logger.Info("Request signature header not found or empty")
return errors.New(errorMessage), errorCode
}
secret := k.Gw.ReplaceTykVariables(r, authConfig.Signature.Secret, false)
if secret == "" {
logger.Info("Request signature secret not found or empty")
return errors.New(errorMessage), errorCode
}
if err := validator.Validate(signature, key, secret, authConfig.Signature.AllowedClockSkew); err != nil {
logger.WithError(err).Info("Request signature validation failed")
return errors.New(errorMessage), errorCode
}
return nil, http.StatusOK
}
func stripBearer(token string) string {
if len(token) > 6 && strings.ToUpper(token[0:7]) == "BEARER " {
return token[7:]
}
return token
}
// TODO: move this method to base middleware?
func AuthFailed(m TykMiddleware, r *http.Request, token string) {
m.Base().FireEvent(EventAuthFailure, EventKeyFailureMeta{
EventMetaDefault: EventMetaDefault{Message: "Auth Failure", OriginatingRequest: EncodeRequestToEvent(r)},
Path: r.URL.Path,
Origin: request.RealIP(r),
Key: token,
})
}
// validateCertificate performs certificate validation for both certificate binding and dynamic mTLS
func (k *AuthKey) validateCertificate(ctx *certValidationContext) (int, error) {
if ctx.request.TLS != nil && len(ctx.request.TLS.PeerCertificates) > 0 {
return k.validateWithTLSCertificate(ctx)
}
// Certificate binding validation is not needed here because CertificateCheckMW
// rejects requests without certificates before reaching this code when UseMutualTLSAuth=true
return k.validateLegacyWithoutCert(ctx.request, ctx.session)
}
// validateWithTLSCertificate handles validation when a TLS client certificate is provided
func (k *AuthKey) validateWithTLSCertificate(ctx *certValidationContext) (int, error) {
// Compute cert hash for comparison
*ctx.certHash = k.computeCertHash(ctx.request, *ctx.certHash)
// Use binding mode if the session has static certificate bindings configured
// Otherwise, use legacy mode for backward compatibility with dynamic mTLS
if ctx.useCertBinding {
// Check certificate expiry for cert binding mode
// (not checked earlier in the flow, unlike when authConfig.UseCertificate is true)
if code, err := k.checkCertificateExpiry(ctx.request, ctx.key); err != nil {
return code, err
}
return k.validateCertificateBinding(ctx.request, ctx.key, ctx.session, *ctx.certHash)
}
return k.validateLegacyMode(ctx.request, ctx.session, *ctx.certHash, ctx.updateSession)
}
// checkCertificateExpiry validates that the certificate hasn't expired
func (k *AuthKey) checkCertificateExpiry(r *http.Request, key string) (int, error) {
if key != "" && time.Now().After(r.TLS.PeerCertificates[0].NotAfter) {
err, code := errorAndStatusCode(ErrAuthCertExpired)
return code, err
}
return http.StatusOK, nil
}
// computeCertHash computes the certificate hash if not already computed
func (k *AuthKey) computeCertHash(r *http.Request, existingHash string) string {
if existingHash == "" {
return k.Spec.OrgID + crypto.HexSHA256(r.TLS.PeerCertificates[0].Raw)
}
return existingHash
}
// validateCertificateBinding validates certificate-to-token binding
// This enforces that the presented certificate matches the one bound to the session
func (k *AuthKey) validateCertificateBinding(r *http.Request, key string, session *user.SessionState, certHash string) (int, error) {
// Only validate if both token and session have certificate bindings
if key == "" || len(session.MtlsStaticCertificateBindings) == 0 {
return http.StatusOK, nil
}
// Check if the presented certificate hash matches any of the bound certificates
certMatched := slices.Contains(session.MtlsStaticCertificateBindings, certHash)
// If certificates don't match, reject the request
if !certMatched {
k.Logger().WithField("key", k.Gw.obfuscateKey(key)).Warn("Certificate mismatch detected for token")
err, code := k.reportInvalidKey(key, r, MsgCertificateMismatch, ErrAuthCertMismatch)
return code, err
}
// Note: In binding mode, certificate whitelist validation is performed at TLS handshake level (UseMutualTLSAuth)
// or by CertificateCheckMW middleware. We don't validate against cert manager here because
// MtlsStaticCertificateBindings contains hashes for binding, not cert IDs for whitelist lookup
return http.StatusOK, nil
}
// validateLegacyMode handles the dynamic mtls behavior
func (k *AuthKey) validateLegacyMode(r *http.Request, session *user.SessionState, certHash string, updateSession *bool) (int, error) {
// Auto-update session certificate with current cert hash
if session.Certificate != certHash {
session.Certificate = certHash
*updateSession = true
}
// Validate the certificate exists in cert manager
if _, err := k.Gw.CertificateManager.GetRaw(certHash); err != nil {
err, code := k.reportInvalidKey("", r, MsgNonExistentCert, ErrAuthCertNotFound)
return code, err
}
return http.StatusOK, nil
}
// validateLegacyWithoutCert validates session certificate in dynamic mTLS mode when no TLS cert is provided
// Checks if stored certificate exists in cert manager to catch corrupted data
func (k *AuthKey) validateLegacyWithoutCert(r *http.Request, session *user.SessionState) (int, error) {
if session.Certificate != "" {
if _, err := k.Gw.CertificateManager.GetRaw(session.Certificate); err != nil {
err, code := k.reportInvalidKey("", r, MsgNonExistentCert, ErrAuthCertNotFound)
return code, err
}
}
return http.StatusOK, nil
}