Skip to content

Commit

Permalink
Maintain backward compatibility during auth header name set (TykTechn…
Browse files Browse the repository at this point in the history
  • Loading branch information
furkansenharputlu authored and buger committed Nov 26, 2019
1 parent cc0f689 commit 67dd910
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 14 deletions.
2 changes: 1 addition & 1 deletion gateway/coprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ func (h *CustomMiddlewareResponseHook) Init(mwDef interface{}, spec *APISpec) er

// getAuthType overrides BaseMiddleware.getAuthType.
func (m *CoProcessMiddleware) getAuthType() string {
return "coprocess"
return coprocessType
}

func (h *CustomMiddlewareResponseHook) Name() string {
Expand Down
17 changes: 15 additions & 2 deletions gateway/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"strconv"
"time"

"github.com/TykTechnologies/tyk/headers"

"github.com/gocraft/health"
"github.com/justinas/alice"
newrelic "github.com/newrelic/go-agent"
Expand All @@ -28,6 +30,13 @@ import (

const mwStatusRespond = 666

const authTokenType = "authToken"
const jwtType = "jwt"
const hmacType = "hmac"
const basicType = "basic"
const coprocessType = "coprocess"
const oauthType = "oauth"

var (
GlobalRate = ratecounter.NewRateCounter(1 * time.Second)
orgSessionExpiryCache singleflight.Group
Expand Down Expand Up @@ -629,11 +638,15 @@ func (b BaseMiddleware) getAuthType() string {

func (b BaseMiddleware) getAuthToken(authType string, r *http.Request) (string, apidef.AuthConfig) {
config, ok := b.Base().Spec.AuthConfigs[authType]
// Auth is deprecated.
if !ok {
// Auth is deprecated. To maintain backward compatibility authToken and jwt cases are added.
if !ok && (authType == authTokenType || authType == jwtType) {
config = b.Base().Spec.Auth
}

if config.AuthHeaderName == "" {
config.AuthHeaderName = headers.Authorization
}

key := r.Header.Get(config.AuthHeaderName)

paramName := config.ParamName
Expand Down
12 changes: 6 additions & 6 deletions gateway/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ func TestBaseMiddleware_getAuthType(t *testing.T) {
oauth := &Oauth2KeyExists{BaseMiddleware: baseMid}

// test getAuthType
assert.Equal(t, "authToken", authKey.getAuthType())
assert.Equal(t, "basic", basic.getAuthType())
assert.Equal(t, "coprocess", coprocess.getAuthType())
assert.Equal(t, "hmac", hmac.getAuthType())
assert.Equal(t, "jwt", jwt.getAuthType())
assert.Equal(t, "oauth", oauth.getAuthType())
assert.Equal(t, authTokenType, authKey.getAuthType())
assert.Equal(t, basicType, basic.getAuthType())
assert.Equal(t, coprocessType, coprocess.getAuthType())
assert.Equal(t, hmacType, hmac.getAuthType())
assert.Equal(t, jwtType, jwt.getAuthType())
assert.Equal(t, oauthType, oauth.getAuthType())

// test getAuthToken
getToken := func(authType string, getAuthToken func(authType string, r *http.Request) (string, apidef.AuthConfig)) string {
Expand Down
2 changes: 1 addition & 1 deletion gateway/mw_auth_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (k *AuthKey) setContextVars(r *http.Request, token string) {

// getAuthType overrides BaseMiddleware.getAuthType.
func (k *AuthKey) getAuthType() string {
return "authToken"
return authTokenType
}

func (k *AuthKey) ProcessRequest(w http.ResponseWriter, r *http.Request, _ interface{}) (error, int) {
Expand Down
2 changes: 1 addition & 1 deletion gateway/mw_basic_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (k *BasicAuthKeyIsValid) requestForBasicAuth(w http.ResponseWriter, msg str

// getAuthType overrides BaseMiddleware.getAuthType.
func (k *BasicAuthKeyIsValid) getAuthType() string {
return "basic"
return basicType
}

func (k *BasicAuthKeyIsValid) basicAuthHeaderCredentials(w http.ResponseWriter, r *http.Request) (username, password string, err error, code int) {
Expand Down
2 changes: 1 addition & 1 deletion gateway/mw_http_signature_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (hm *HTTPSignatureValidationMiddleware) Init() {

// getAuthType overrides BaseMiddleware.getAuthType.
func (hm *HTTPSignatureValidationMiddleware) getAuthType() string {
return "hmac"
return hmacType
}

func (hm *HTTPSignatureValidationMiddleware) ProcessRequest(w http.ResponseWriter, r *http.Request, _ interface{}) (error, int) {
Expand Down
2 changes: 1 addition & 1 deletion gateway/mw_jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ func (k *JWTMiddleware) processOneToOneTokenMap(r *http.Request, token *jwt.Toke

// getAuthType overrides BaseMiddleware.getAuthType.
func (k *JWTMiddleware) getAuthType() string {
return "jwt"
return jwtType
}

func (k *JWTMiddleware) ProcessRequest(w http.ResponseWriter, r *http.Request, _ interface{}) (error, int) {
Expand Down
2 changes: 1 addition & 1 deletion gateway/mw_oauth2_key_exists.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (k *Oauth2KeyExists) EnabledForSpec() bool {

// getAuthType overrides BaseMiddleware.getAuthType.
func (k *Oauth2KeyExists) getAuthType() string {
return "oauth"
return oauthType
}

// ProcessRequest will run any checks on the request on the way through the system, return an error to have the chain fail
Expand Down

0 comments on commit 67dd910

Please sign in to comment.