2015-05-13 03:48:13 +02:00
|
|
|
package providers
|
|
|
|
|
|
|
|
import (
|
2020-05-05 17:53:33 +02:00
|
|
|
"context"
|
2015-06-23 13:23:39 +02:00
|
|
|
"errors"
|
2015-05-13 03:48:13 +02:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
|
|
|
"testing"
|
2015-06-23 13:23:39 +02:00
|
|
|
|
2020-09-29 18:44:42 +02:00
|
|
|
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/sessions"
|
2017-10-23 18:23:46 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
2015-05-13 03:48:13 +02:00
|
|
|
)
|
|
|
|
|
2018-11-29 16:26:41 +02:00
|
|
|
func updateURL(url *url.URL, hostname string) {
|
2020-05-25 14:08:04 +02:00
|
|
|
if url == nil {
|
|
|
|
return
|
|
|
|
}
|
2018-11-29 16:26:41 +02:00
|
|
|
url.Scheme = "http"
|
|
|
|
url.Host = hostname
|
|
|
|
}
|
|
|
|
|
2015-06-23 13:23:39 +02:00
|
|
|
type ValidateSessionStateTestProvider struct {
|
2015-05-13 03:48:13 +02:00
|
|
|
*ProviderData
|
|
|
|
}
|
|
|
|
|
2020-05-05 17:53:33 +02:00
|
|
|
var _ Provider = (*ValidateSessionStateTestProvider)(nil)
|
|
|
|
|
|
|
|
func (tp *ValidateSessionStateTestProvider) GetEmailAddress(ctx context.Context, s *sessions.SessionState) (string, error) {
|
2015-06-23 13:23:39 +02:00
|
|
|
return "", errors.New("not implemented")
|
2015-05-13 03:48:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Note that we're testing the internal validateToken() used to implement
|
2015-06-23 13:23:39 +02:00
|
|
|
// several Provider's ValidateSessionState() implementations
|
2020-05-05 17:53:33 +02:00
|
|
|
func (tp *ValidateSessionStateTestProvider) ValidateSessionState(ctx context.Context, s *sessions.SessionState) bool {
|
2015-05-13 03:48:13 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-06-23 13:23:39 +02:00
|
|
|
type ValidateSessionStateTest struct {
|
2018-11-29 16:26:41 +02:00
|
|
|
backend *httptest.Server
|
|
|
|
responseCode int
|
|
|
|
provider *ValidateSessionStateTestProvider
|
|
|
|
header http.Header
|
2015-05-13 03:48:13 +02:00
|
|
|
}
|
|
|
|
|
2015-06-23 13:23:39 +02:00
|
|
|
func NewValidateSessionStateTest() *ValidateSessionStateTest {
|
2018-11-29 16:26:41 +02:00
|
|
|
var vtTest ValidateSessionStateTest
|
2015-05-13 03:48:13 +02:00
|
|
|
|
2018-11-29 16:26:41 +02:00
|
|
|
vtTest.backend = httptest.NewServer(
|
2015-05-13 03:48:13 +02:00
|
|
|
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.URL.Path != "/oauth/tokeninfo" {
|
|
|
|
w.WriteHeader(500)
|
|
|
|
w.Write([]byte("unknown URL"))
|
|
|
|
}
|
2018-11-29 16:26:41 +02:00
|
|
|
tokenParam := r.FormValue("access_token")
|
|
|
|
if tokenParam == "" {
|
2015-05-13 03:48:13 +02:00
|
|
|
missing := false
|
2018-11-29 16:26:41 +02:00
|
|
|
receivedHeaders := r.Header
|
|
|
|
for k := range vtTest.header {
|
|
|
|
received := receivedHeaders.Get(k)
|
|
|
|
expected := vtTest.header.Get(k)
|
2015-05-13 03:48:13 +02:00
|
|
|
if received == "" || received != expected {
|
|
|
|
missing = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if missing {
|
|
|
|
w.WriteHeader(500)
|
|
|
|
w.Write([]byte("no token param and missing or incorrect headers"))
|
|
|
|
}
|
|
|
|
}
|
2018-11-29 16:26:41 +02:00
|
|
|
w.WriteHeader(vtTest.responseCode)
|
2015-05-13 03:48:13 +02:00
|
|
|
w.Write([]byte("only code matters; contents disregarded"))
|
|
|
|
|
|
|
|
}))
|
2018-11-29 16:26:41 +02:00
|
|
|
backendURL, _ := url.Parse(vtTest.backend.URL)
|
|
|
|
vtTest.provider = &ValidateSessionStateTestProvider{
|
2015-05-13 03:48:13 +02:00
|
|
|
ProviderData: &ProviderData{
|
2015-11-09 01:47:44 +02:00
|
|
|
ValidateURL: &url.URL{
|
2015-05-13 03:48:13 +02:00
|
|
|
Scheme: "http",
|
2018-11-29 16:26:41 +02:00
|
|
|
Host: backendURL.Host,
|
2015-05-13 03:48:13 +02:00
|
|
|
Path: "/oauth/tokeninfo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2018-11-29 16:26:41 +02:00
|
|
|
vtTest.responseCode = 200
|
|
|
|
return &vtTest
|
2015-05-13 03:48:13 +02:00
|
|
|
}
|
|
|
|
|
2018-11-29 16:26:41 +02:00
|
|
|
func (vtTest *ValidateSessionStateTest) Close() {
|
|
|
|
vtTest.backend.Close()
|
2015-05-13 03:48:13 +02:00
|
|
|
}
|
|
|
|
|
2015-06-23 13:23:39 +02:00
|
|
|
func TestValidateSessionStateValidToken(t *testing.T) {
|
2018-11-29 16:26:41 +02:00
|
|
|
vtTest := NewValidateSessionStateTest()
|
|
|
|
defer vtTest.Close()
|
2020-05-05 17:53:33 +02:00
|
|
|
assert.Equal(t, true, validateToken(context.Background(), vtTest.provider, "foobar", nil))
|
2015-05-13 03:48:13 +02:00
|
|
|
}
|
|
|
|
|
2015-06-23 13:23:39 +02:00
|
|
|
func TestValidateSessionStateValidTokenWithHeaders(t *testing.T) {
|
2018-11-29 16:26:41 +02:00
|
|
|
vtTest := NewValidateSessionStateTest()
|
|
|
|
defer vtTest.Close()
|
|
|
|
vtTest.header = make(http.Header)
|
|
|
|
vtTest.header.Set("Authorization", "Bearer foobar")
|
2015-05-13 03:48:13 +02:00
|
|
|
assert.Equal(t, true,
|
2020-05-05 17:53:33 +02:00
|
|
|
validateToken(context.Background(), vtTest.provider, "foobar", vtTest.header))
|
2015-05-13 03:48:13 +02:00
|
|
|
}
|
|
|
|
|
2015-06-23 13:23:39 +02:00
|
|
|
func TestValidateSessionStateEmptyToken(t *testing.T) {
|
2018-11-29 16:26:41 +02:00
|
|
|
vtTest := NewValidateSessionStateTest()
|
|
|
|
defer vtTest.Close()
|
2020-05-05 17:53:33 +02:00
|
|
|
assert.Equal(t, false, validateToken(context.Background(), vtTest.provider, "", nil))
|
2015-05-13 03:48:13 +02:00
|
|
|
}
|
|
|
|
|
2015-11-09 01:47:44 +02:00
|
|
|
func TestValidateSessionStateEmptyValidateURL(t *testing.T) {
|
2018-11-29 16:26:41 +02:00
|
|
|
vtTest := NewValidateSessionStateTest()
|
|
|
|
defer vtTest.Close()
|
|
|
|
vtTest.provider.Data().ValidateURL = nil
|
2020-05-05 17:53:33 +02:00
|
|
|
assert.Equal(t, false, validateToken(context.Background(), vtTest.provider, "foobar", nil))
|
2015-05-13 03:48:13 +02:00
|
|
|
}
|
|
|
|
|
2015-06-23 13:23:39 +02:00
|
|
|
func TestValidateSessionStateRequestNetworkFailure(t *testing.T) {
|
2018-11-29 16:26:41 +02:00
|
|
|
vtTest := NewValidateSessionStateTest()
|
2015-05-13 03:48:13 +02:00
|
|
|
// Close immediately to simulate a network failure
|
2018-11-29 16:26:41 +02:00
|
|
|
vtTest.Close()
|
2020-05-05 17:53:33 +02:00
|
|
|
assert.Equal(t, false, validateToken(context.Background(), vtTest.provider, "foobar", nil))
|
2015-05-13 03:48:13 +02:00
|
|
|
}
|
|
|
|
|
2015-06-23 13:23:39 +02:00
|
|
|
func TestValidateSessionStateExpiredToken(t *testing.T) {
|
2018-11-29 16:26:41 +02:00
|
|
|
vtTest := NewValidateSessionStateTest()
|
|
|
|
defer vtTest.Close()
|
|
|
|
vtTest.responseCode = 401
|
2020-05-05 17:53:33 +02:00
|
|
|
assert.Equal(t, false, validateToken(context.Background(), vtTest.provider, "foobar", nil))
|
2015-05-13 03:48:13 +02:00
|
|
|
}
|
2016-08-03 04:27:50 +02:00
|
|
|
|
|
|
|
func TestStripTokenNotPresent(t *testing.T) {
|
|
|
|
test := "http://local.test/api/test?a=1&b=2"
|
|
|
|
assert.Equal(t, test, stripToken(test))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStripToken(t *testing.T) {
|
|
|
|
test := "http://local.test/api/test?access_token=deadbeef&b=1&c=2"
|
|
|
|
expected := "http://local.test/api/test?access_token=dead...&b=1&c=2"
|
|
|
|
assert.Equal(t, expected, stripToken(test))
|
|
|
|
}
|