Skip to content

Commit

Permalink
Move auth type constants to apidef package (TykTechnologies#3927)
Browse files Browse the repository at this point in the history
  • Loading branch information
furkansenharputlu authored Mar 10, 2022
1 parent 3e23111 commit 4edf523
Show file tree
Hide file tree
Showing 17 changed files with 42 additions and 42 deletions.
6 changes: 6 additions & 0 deletions apidef/api_definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ const (
Self = "self"

AuthTokenType = "authToken"
JWTType = "jwt"
HMACType = "hmac"
BasicType = "basic"
CoprocessType = "coprocess"
OAuthType = "oauth"
OIDCType = "oidc"
)

type ObjectId bson.ObjectId
Expand Down
6 changes: 3 additions & 3 deletions gateway/auth_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func TestHashKeyFunctionChanged(t *testing.T) {
spec.Proxy.ListenPath = "/"
spec.UseKeylessAccess = false
spec.AuthConfigs = map[string]apidef.AuthConfig{
authTokenType: {UseCertificate: false},
apidef.AuthTokenType: {UseCertificate: false},
}
})[0]

Expand Down Expand Up @@ -196,7 +196,7 @@ func TestHashKeyFunctionChanged(t *testing.T) {
t.Run("basic auth key", func(t *testing.T) {
api.UseBasicAuth = true
api.AuthConfigs = map[string]apidef.AuthConfig{
authTokenType: {UseCertificate: true},
apidef.AuthTokenType: {UseCertificate: true},
}
ts.Gw.LoadAPI(api)
globalConf = ts.Gw.GetConfig()
Expand All @@ -222,7 +222,7 @@ func TestHashKeyFunctionChanged(t *testing.T) {
t.Run("client certificate", func(t *testing.T) {
api.UseBasicAuth = false
api.AuthConfigs = map[string]apidef.AuthConfig{
authTokenType: {UseCertificate: true},
apidef.AuthTokenType: {UseCertificate: true},
}
ts.Gw.LoadAPI(api)
session := CreateStandardSession()
Expand Down
4 changes: 3 additions & 1 deletion gateway/cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"sync"
"time"

"github.com/TykTechnologies/tyk/apidef"

"github.com/TykTechnologies/tyk/certs"
"github.com/TykTechnologies/tyk/config"

Expand Down Expand Up @@ -377,7 +379,7 @@ func (gw *Gateway) getTLSConfigForClient(baseConfig *tls.Config, listenPort int)
}
}
}
case spec.AuthConfigs[authTokenType].UseCertificate:
case spec.AuthConfigs[apidef.AuthTokenType].UseCertificate:
// Dynamic certificate check required, falling back to HTTP level check
// TODO: Change to VerifyPeerCertificate hook instead, when possible
if domainRequireCert[spec.Domain] < tls.RequestClientCert {
Expand Down
10 changes: 5 additions & 5 deletions gateway/cert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -862,7 +862,7 @@ func TestKeyWithCertificateTLS(t *testing.T) {
spec.UseKeylessAccess = false
spec.BaseIdentityProvidedBy = apidef.AuthToken
spec.AuthConfigs = map[string]apidef.AuthConfig{
authTokenType: {UseCertificate: true},
apidef.AuthTokenType: {UseCertificate: true},
}
spec.Proxy.ListenPath = "/"
spec.OrgID = orgId
Expand Down Expand Up @@ -918,7 +918,7 @@ func TestKeyWithCertificateTLS(t *testing.T) {
spec.UseKeylessAccess = false
spec.BaseIdentityProvidedBy = apidef.AuthToken
spec.AuthConfigs = map[string]apidef.AuthConfig{
authTokenType: {UseCertificate: true},
apidef.AuthTokenType: {UseCertificate: true},
}
spec.Proxy.ListenPath = "/test1"
spec.OrgID = orgId
Expand Down Expand Up @@ -994,7 +994,7 @@ func TestKeyWithCertificateTLS(t *testing.T) {
spec.UseKeylessAccess = false
spec.BaseIdentityProvidedBy = apidef.AuthToken
spec.AuthConfigs = map[string]apidef.AuthConfig{
authTokenType: {UseCertificate: true},
apidef.AuthTokenType: {UseCertificate: true},
}
spec.Proxy.ListenPath = "/test1"
spec.OrgID = orgId
Expand Down Expand Up @@ -1033,7 +1033,7 @@ func TestKeyWithCertificateTLS(t *testing.T) {
spec.UseKeylessAccess = false
spec.BaseIdentityProvidedBy = apidef.AuthToken
spec.AuthConfigs = map[string]apidef.AuthConfig{
authTokenType: {UseCertificate: true},
apidef.AuthTokenType: {UseCertificate: true},
}
spec.Proxy.ListenPath = "/"
spec.OrgID = orgId
Expand Down Expand Up @@ -1070,7 +1070,7 @@ func TestKeyWithCertificateTLS(t *testing.T) {
spec.UseKeylessAccess = false
spec.BaseIdentityProvidedBy = apidef.AuthToken
spec.AuthConfigs = map[string]apidef.AuthConfig{
authTokenType: {UseCertificate: true},
apidef.AuthTokenType: {UseCertificate: true},
}
spec.Proxy.ListenPath = "/"
spec.OrgID = orgId
Expand Down
4 changes: 2 additions & 2 deletions gateway/coprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ func (m *CoProcessMiddleware) ProcessRequest(w http.ResponseWriter, r *http.Requ
logger := m.Logger()
logger.Debug("CoProcess Request, HookType: ", m.HookType)
originalURL := r.URL
authToken, _ := m.getAuthToken(coprocessType, r)
authToken, _ := m.getAuthToken(apidef.CoprocessType, r)

var extractor IdExtractor
if m.Spec.EnableCoProcessAuth && m.Spec.CustomMiddleware.IdExtractor.Extractor != nil {
Expand Down Expand Up @@ -521,7 +521,7 @@ func (h *CustomMiddlewareResponseHook) Init(mwDef interface{}, spec *APISpec) er

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

func (h *CustomMiddlewareResponseHook) Name() string {
Expand Down
2 changes: 1 addition & 1 deletion gateway/gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1931,7 +1931,7 @@ func TestTracing(t *testing.T) {

t.Run("Custom auth header", func(t *testing.T) {
spec.AuthConfigs = map[string]apidef.AuthConfig{
authTokenType: {
apidef.AuthTokenType: {
AuthHeaderName: "Custom-Auth-Header",
},
}
Expand Down
10 changes: 1 addition & 9 deletions gateway/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,6 @@ import (

const mwStatusRespond = 666

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

var (
GlobalRate = ratecounter.NewRateCounter(1 * time.Second)
orgSessionExpiryCache singleflight.Group
Expand Down Expand Up @@ -754,7 +746,7 @@ 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. To maintain backward compatibility authToken and jwt cases are added.
if !ok && (authType == authTokenType || authType == jwtType) {
if !ok && (authType == apidef.AuthTokenType || authType == apidef.JWTType) {
config = b.Base().Spec.Auth
}

Expand Down
14 changes: 7 additions & 7 deletions gateway/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,13 @@ func TestBaseMiddleware_getAuthType(t *testing.T) {
oidc := &OpenIDMW{BaseMiddleware: baseMid}

// test 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())
assert.Equal(t, oidcType, oidc.getAuthType())
assert.Equal(t, apidef.AuthTokenType, authKey.getAuthType())
assert.Equal(t, apidef.BasicType, basic.getAuthType())
assert.Equal(t, apidef.CoprocessType, coprocess.getAuthType())
assert.Equal(t, apidef.HMACType, hmac.getAuthType())
assert.Equal(t, apidef.JWTType, jwt.getAuthType())
assert.Equal(t, apidef.OAuthType, oauth.getAuthType())
assert.Equal(t, apidef.OIDCType, oidc.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 @@ -78,7 +78,7 @@ func (k *AuthKey) setContextVars(r *http.Request, token string) {

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

func (k *AuthKey) ProcessRequest(_ http.ResponseWriter, r *http.Request, _ interface{}) (error, int) {
Expand Down
10 changes: 5 additions & 5 deletions gateway/mw_auth_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func TestSignatureValidation(t *testing.T) {
spec.UseKeylessAccess = false
spec.Proxy.ListenPath = "/"
spec.AuthConfigs = map[string]apidef.AuthConfig{
authTokenType: {
apidef.AuthTokenType: {
ValidateSignature: true,
UseParam: true,
ParamName: "api_key",
Expand Down Expand Up @@ -184,9 +184,9 @@ func TestSignatureValidation(t *testing.T) {
})

t.Run("Dynamic signature", func(t *testing.T) {
authConfig := api.AuthConfigs[authTokenType]
authConfig := api.AuthConfigs[apidef.AuthTokenType]
authConfig.Signature.Secret = "$tyk_meta.signature_secret"
api.AuthConfigs[authTokenType] = authConfig
api.AuthConfigs[apidef.AuthTokenType] = authConfig
ts.Gw.LoadAPI(api)

key := CreateSession(ts.Gw, func(s *user.SessionState) {
Expand Down Expand Up @@ -220,9 +220,9 @@ func TestSignatureValidation(t *testing.T) {
})

t.Run("Dynamic signature with custom key", func(t *testing.T) {
authConfig := api.AuthConfigs[authTokenType]
authConfig := api.AuthConfigs[apidef.AuthTokenType]
authConfig.Signature.Secret = "$tyk_meta.signature_secret"
api.AuthConfigs[authTokenType] = authConfig
api.AuthConfigs[apidef.AuthTokenType] = authConfig
ts.Gw.LoadAPI(api)

customKey := "c8zj99aze7hdvtaqh4qvcck7"
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 @@ -80,7 +80,7 @@ func (k *BasicAuthKeyIsValid) requestForBasicAuth(w http.ResponseWriter, msg str

// getAuthType overrides BaseMiddleware.getAuthType.
func (k *BasicAuthKeyIsValid) getAuthType() string {
return basicType
return apidef.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 hmacType
return apidef.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 @@ -694,7 +694,7 @@ func (k *JWTMiddleware) processOneToOneTokenMap(r *http.Request, token *jwt.Toke

// getAuthType overrides BaseMiddleware.getAuthType.
func (k *JWTMiddleware) getAuthType() string {
return jwtType
return apidef.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 @@ -59,7 +59,7 @@ func (k *Oauth2KeyExists) EnabledForSpec() bool {

// getAuthType overrides BaseMiddleware.getAuthType.
func (k *Oauth2KeyExists) getAuthType() string {
return oauthType
return apidef.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
2 changes: 1 addition & 1 deletion gateway/mw_openid.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (k *OpenIDMW) dummyErrorHandler(e error, w http.ResponseWriter, r *http.Req
}

func (k *OpenIDMW) getAuthType() string {
return oidcType
return apidef.OIDCType
}

func (k *OpenIDMW) ProcessRequest(w http.ResponseWriter, r *http.Request, _ interface{}) (error, int) {
Expand Down
2 changes: 1 addition & 1 deletion gateway/mw_strip_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (sa *StripAuth) ProcessRequest(w http.ResponseWriter, r *http.Request, _ in

// For backward compatibility
if len(sa.Spec.AuthConfigs) == 0 {
strip(authTokenType, &sa.Spec.Auth)
strip(apidef.AuthTokenType, &sa.Spec.Auth)
}

return nil, http.StatusOK
Expand Down
4 changes: 2 additions & 2 deletions gateway/mw_strip_auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func TestStripAuth_stripFromHeaders(t *testing.T) {

key = "NonDefaultName"
sa.Spec.AuthConfigs = map[string]apidef.AuthConfig{
authTokenType: {CookieName: key},
apidef.AuthTokenType: {CookieName: key},
}
stripFromCookieTest(t, req, key, sa, "Dummy=DUMMY;NonDefaultName=AUTHORIZATION;Dummy2=DUMMY2", "Dummy=DUMMY;Dummy2=DUMMY2")
// whitespace between cookies
Expand All @@ -97,7 +97,7 @@ func TestStripAuth_stripFromHeaders(t *testing.T) {

func stripFromCookieTest(t *testing.T, req *http.Request, key string, sa StripAuth, value string, expected string) {
req.Header.Set("Cookie", value)
config := sa.Spec.AuthConfigs[authTokenType]
config := sa.Spec.AuthConfigs[apidef.AuthTokenType]
sa.stripFromHeaders(req, &config)

actual := req.Header.Get("Cookie")
Expand Down

0 comments on commit 4edf523

Please sign in to comment.