-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement upstream basic authentication
- Loading branch information
1 parent
685c35f
commit c4dfd99
Showing
8 changed files
with
343 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package gateway | ||
|
||
import ( | ||
"encoding/base64" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/TykTechnologies/tyk/ctx" | ||
"github.com/TykTechnologies/tyk/header" | ||
) | ||
|
||
// UpstreamBasicAuth is a middleware that will do basic authentication for upstream connections. | ||
// UpstreamBasicAuth middleware is only supported in Tyk OAS API definitions. | ||
type UpstreamBasicAuth struct { | ||
*BaseMiddleware | ||
} | ||
|
||
func (t *UpstreamBasicAuth) Name() string { | ||
return "UpstreamBasicAuth" | ||
} | ||
|
||
func (t *UpstreamBasicAuth) EnabledForSpec() bool { | ||
if !t.Spec.UpstreamAuth.Enabled { | ||
return false | ||
} | ||
|
||
if !t.Spec.UpstreamAuth.BasicAuth.Enabled { | ||
return false | ||
} | ||
|
||
return true | ||
} | ||
|
||
// ProcessRequest will inject basic auth info into request context so that it can be used during reverse proxy. | ||
func (t *UpstreamBasicAuth) ProcessRequest(_ http.ResponseWriter, r *http.Request, _ interface{}) (error, int) { | ||
basicAuthConfig := t.Spec.UpstreamAuth.BasicAuth | ||
|
||
authHeaderName := header.Authorization | ||
if basicAuthConfig.HeaderName != "" { | ||
authHeaderName = basicAuthConfig.HeaderName | ||
} | ||
ctx.SetUpstreamAuthHeader(r, authHeaderName) | ||
|
||
payload := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", basicAuthConfig.Username, basicAuthConfig.Password))) | ||
|
||
ctx.SetUpstreamAuthValue(r, payload) | ||
|
||
return nil, http.StatusOK | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
package gateway | ||
|
||
import ( | ||
"encoding/base64" | ||
"encoding/json" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/TykTechnologies/tyk/apidef" | ||
"github.com/TykTechnologies/tyk/header" | ||
"github.com/TykTechnologies/tyk/test" | ||
) | ||
|
||
func TestUpstreamBasicAuthentication(t *testing.T) { | ||
|
||
ts := StartTest(nil) | ||
t.Cleanup(func() { | ||
ts.Close() | ||
}) | ||
|
||
userName, password, customAuthHeader := "user", "password", "Custom-Auth" | ||
expectedAuth := base64.StdEncoding.EncodeToString([]byte(userName + ":" + password)) | ||
|
||
ts.Gw.BuildAndLoadAPI( | ||
func(spec *APISpec) { | ||
spec.Proxy.ListenPath = "/upstream-basic-auth-enabled/" | ||
spec.UseKeylessAccess = true | ||
spec.UpstreamAuth = apidef.UpstreamAuth{ | ||
Enabled: true, | ||
BasicAuth: apidef.UpstreamBasicAuth{ | ||
Enabled: true, | ||
Username: userName, | ||
Password: password, | ||
}, | ||
} | ||
spec.Proxy.StripListenPath = true | ||
}, func(spec *APISpec) { | ||
spec.Proxy.ListenPath = "/upstream-basic-auth-custom-header/" | ||
spec.UseKeylessAccess = true | ||
spec.UpstreamAuth = apidef.UpstreamAuth{ | ||
Enabled: true, | ||
BasicAuth: apidef.UpstreamBasicAuth{ | ||
Enabled: true, | ||
Username: userName, | ||
Password: password, | ||
HeaderName: customAuthHeader, | ||
}, | ||
} | ||
spec.Proxy.StripListenPath = true | ||
}, | ||
func(spec *APISpec) { | ||
spec.Proxy.ListenPath = "/upstream-basic-auth-disabled/" | ||
spec.UseKeylessAccess = true | ||
spec.UpstreamAuth = apidef.UpstreamAuth{ | ||
Enabled: true, | ||
BasicAuth: apidef.UpstreamBasicAuth{ | ||
Enabled: false, | ||
Username: userName, | ||
Password: password, | ||
}, | ||
} | ||
spec.Proxy.StripListenPath = true | ||
}, | ||
func(spec *APISpec) { | ||
spec.Proxy.ListenPath = "/upstream-auth-disabled/" | ||
spec.UseKeylessAccess = true | ||
spec.UpstreamAuth = apidef.UpstreamAuth{ | ||
Enabled: false, | ||
} | ||
spec.Proxy.StripListenPath = true | ||
}, | ||
) | ||
|
||
_, _ = ts.Run(t, test.TestCases{ | ||
{ | ||
Path: "/upstream-basic-auth-enabled/", | ||
Code: http.StatusOK, | ||
BodyMatchFunc: func(body []byte) bool { | ||
resp := struct { | ||
Headers map[string]string `json:"headers"` | ||
}{} | ||
err := json.Unmarshal(body, &resp) | ||
assert.NoError(t, err) | ||
|
||
assert.Contains(t, resp.Headers, header.Authorization) | ||
assert.NotEmpty(t, resp.Headers[header.Authorization]) | ||
assert.Equal(t, expectedAuth, resp.Headers[header.Authorization]) | ||
|
||
return true | ||
}, | ||
}, | ||
{ | ||
Path: "/upstream-basic-auth-custom-header/", | ||
Code: http.StatusOK, | ||
BodyMatchFunc: func(body []byte) bool { | ||
resp := struct { | ||
Headers map[string]string `json:"headers"` | ||
}{} | ||
err := json.Unmarshal(body, &resp) | ||
assert.NoError(t, err) | ||
|
||
assert.Contains(t, resp.Headers, customAuthHeader) | ||
assert.NotEmpty(t, resp.Headers[customAuthHeader]) | ||
assert.Equal(t, expectedAuth, resp.Headers[customAuthHeader]) | ||
|
||
return true | ||
}, | ||
}, | ||
{ | ||
Path: "/upstream-basic-auth-disabled/", | ||
Code: http.StatusOK, | ||
BodyMatchFunc: func(body []byte) bool { | ||
resp := struct { | ||
Headers map[string]string `json:"headers"` | ||
}{} | ||
err := json.Unmarshal(body, &resp) | ||
assert.NoError(t, err) | ||
|
||
assert.NotContains(t, resp.Headers, header.Authorization) | ||
|
||
return true | ||
}, | ||
}, | ||
{ | ||
Path: "/upstream-auth-disabled/", | ||
Code: http.StatusOK, | ||
BodyMatchFunc: func(body []byte) bool { | ||
resp := struct { | ||
Headers map[string]string `json:"headers"` | ||
}{} | ||
err := json.Unmarshal(body, &resp) | ||
assert.NoError(t, err) | ||
|
||
assert.NotContains(t, resp.Headers, header.Authorization) | ||
|
||
return true | ||
}, | ||
}, | ||
}...) | ||
|
||
} |
Oops, something went wrong.