Skip to content

Commit

Permalink
Auto-enable auth-source in security scheme (TykTechnologies#3924)
Browse files Browse the repository at this point in the history
  • Loading branch information
furkansenharputlu authored Mar 9, 2022
1 parent cc952fe commit 3e23111
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
12 changes: 9 additions & 3 deletions apidef/oas/security.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,18 +126,21 @@ func (s *OAS) fillApiKeyScheme(ac *apidef.AuthConfig) {
var loc, key string

switch {
case ref.Value.In == header || (ref.Value.In == "" && ac.AuthHeaderName != ""):
case ref.Value.In == header || (ref.Value.In == "" && !ac.DisableHeader):
loc = header
key = ac.AuthHeaderName
ac.AuthHeaderName = ""
case ref.Value.In == query || (ref.Value.In == "" && ac.ParamName != ""):
ac.DisableHeader = true
case ref.Value.In == query || (ref.Value.In == "" && ac.UseParam):
loc = query
key = ac.ParamName
ac.ParamName = ""
case ref.Value.In == cookie || (ref.Value.In == "" && ac.CookieName != ""):
ac.UseParam = false
case ref.Value.In == cookie || (ref.Value.In == "" && ac.UseCookie):
loc = cookie
key = ac.CookieName
ac.CookieName = ""
ac.UseCookie = false
}

ref.Value.WithName(key).WithIn(loc).WithType(apiKey)
Expand All @@ -152,10 +155,13 @@ func (s *OAS) extractApiKeySchemeTo(ac *apidef.AuthConfig, name string) {
switch ref.Value.In {
case header:
ac.AuthHeaderName = ref.Value.Name
ac.DisableHeader = false
case query:
ac.ParamName = ref.Value.Name
ac.UseParam = true
case cookie:
ac.CookieName = ref.Value.Name
ac.UseCookie = true
}
}

Expand Down
24 changes: 13 additions & 11 deletions apidef/oas/security_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,15 @@ func TestOAS_ApiKeyScheme(t *testing.T) {
case header:
expectedAC.AuthHeaderName = ""
expExtractedAC.AuthHeaderName = name
expExtractedAC.DisableHeader = false
case query:
expectedAC.ParamName = ""
expExtractedAC.ParamName = name
expExtractedAC.UseParam = true
case cookie:
expectedAC.CookieName = ""
expExtractedAC.CookieName = name
expExtractedAC.UseCookie = true
}

expSecurity := openapi3.SecurityRequirements{
Expand Down Expand Up @@ -95,21 +98,14 @@ func TestOAS_ApiKeyScheme(t *testing.T) {
})

t.Run("should not set query name in tyk extension", func(t *testing.T) {
ac.AuthHeaderName = ""
ac.DisableHeader = true
check(query, queryName, ac, OAS{})

// reset
ac.AuthHeaderName = headerName
})

t.Run("should not set cookie name in tyk extension", func(t *testing.T) {
ac.AuthHeaderName = ""
ac.ParamName = ""
ac.DisableHeader = true
ac.UseParam = false
check(cookie, cookieName, ac, OAS{})

// reset
ac.AuthHeaderName = headerName
ac.ParamName = queryName
})

testOAS := func(in, name string) (oas OAS) {
Expand All @@ -127,14 +123,20 @@ func TestOAS_ApiKeyScheme(t *testing.T) {
}

t.Run("already filled scheme in=header value should be respected", func(t *testing.T) {
ac.DisableHeader = true
check(header, headerName, ac, testOAS(header, headerName))
})

t.Run("already filled scheme in=query value should be respected", func(t *testing.T) {
ac.DisableHeader = false
ac.UseParam = false
check(query, queryName, ac, testOAS(query, queryName))
})

t.Run("already filled scheme in=cookie value should be respected", func(t *testing.T) {
ac.DisableHeader = false
ac.UseParam = true
ac.UseCookie = false
check(cookie, cookieName, ac, testOAS(cookie, cookieName))
})
}
Expand All @@ -161,7 +163,7 @@ func TestOAS_Token(t *testing.T) {

var token Token
Fill(t, &token, 0)
token.Query.Name = ""
token.Query = nil
oas.Extensions = map[string]interface{}{
ExtensionTykAPIGateway: &XTykAPIGateway{
Server: Server{
Expand Down

0 comments on commit 3e23111

Please sign in to comment.