1
0
mirror of https://github.com/oauth2-proxy/oauth2-proxy.git synced 2024-12-12 11:15:02 +02:00
oauth2-proxy/providers/internal_util_test.go

145 lines
4.1 KiB
Go
Raw Normal View History

2015-05-13 03:48:13 +02:00
package providers
import (
"context"
"errors"
2015-05-13 03:48:13 +02:00
"net/http"
"net/http/httptest"
"net/url"
"testing"
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/sessions"
"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) {
if url == nil {
return
}
2018-11-29 16:26:41 +02:00
url.Scheme = "http"
url.Host = hostname
}
type ValidateSessionStateTestProvider struct {
2015-05-13 03:48:13 +02:00
*ProviderData
}
var _ Provider = (*ValidateSessionStateTestProvider)(nil)
func (tp *ValidateSessionStateTestProvider) GetEmailAddress(ctx context.Context, s *sessions.SessionState) (string, error) {
return "", errors.New("not implemented")
2015-05-13 03:48:13 +02:00
}
// Note that we're testing the internal validateToken() used to implement
// several Provider's ValidateSessionState() implementations
func (tp *ValidateSessionStateTestProvider) ValidateSessionState(ctx context.Context, s *sessions.SessionState) bool {
2015-05-13 03:48:13 +02:00
return false
}
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
}
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{
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
}
func TestValidateSessionStateValidToken(t *testing.T) {
2018-11-29 16:26:41 +02:00
vtTest := NewValidateSessionStateTest()
defer vtTest.Close()
assert.Equal(t, true, validateToken(context.Background(), vtTest.provider, "foobar", nil))
2015-05-13 03:48:13 +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,
validateToken(context.Background(), vtTest.provider, "foobar", vtTest.header))
2015-05-13 03:48:13 +02:00
}
func TestValidateSessionStateEmptyToken(t *testing.T) {
2018-11-29 16:26:41 +02:00
vtTest := NewValidateSessionStateTest()
defer vtTest.Close()
assert.Equal(t, false, validateToken(context.Background(), vtTest.provider, "", nil))
2015-05-13 03:48:13 +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
assert.Equal(t, false, validateToken(context.Background(), vtTest.provider, "foobar", nil))
2015-05-13 03:48:13 +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()
assert.Equal(t, false, validateToken(context.Background(), vtTest.provider, "foobar", nil))
2015-05-13 03:48:13 +02:00
}
func TestValidateSessionStateExpiredToken(t *testing.T) {
2018-11-29 16:26:41 +02:00
vtTest := NewValidateSessionStateTest()
defer vtTest.Close()
vtTest.responseCode = 401
assert.Equal(t, false, validateToken(context.Background(), vtTest.provider, "foobar", nil))
2015-05-13 03:48:13 +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))
}