2015-05-13 01:48:13 +00:00
|
|
|
package providers
|
|
|
|
|
|
|
|
import (
|
2015-06-23 11:23:39 +00:00
|
|
|
"errors"
|
2015-05-13 01:48:13 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
|
|
|
"testing"
|
2015-06-23 11:23:39 +00:00
|
|
|
|
2017-10-23 16:23:46 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2015-05-13 01:48:13 +00:00
|
|
|
)
|
|
|
|
|
2018-11-29 14:26:41 +00:00
|
|
|
func updateURL(url *url.URL, hostname string) {
|
|
|
|
url.Scheme = "http"
|
|
|
|
url.Host = hostname
|
|
|
|
}
|
|
|
|
|
2015-06-23 11:23:39 +00:00
|
|
|
type ValidateSessionStateTestProvider struct {
|
2015-05-13 01:48:13 +00:00
|
|
|
*ProviderData
|
|
|
|
}
|
|
|
|
|
2015-06-23 11:23:39 +00:00
|
|
|
func (tp *ValidateSessionStateTestProvider) GetEmailAddress(s *SessionState) (string, error) {
|
|
|
|
return "", errors.New("not implemented")
|
2015-05-13 01:48:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Note that we're testing the internal validateToken() used to implement
|
2015-06-23 11:23:39 +00:00
|
|
|
// several Provider's ValidateSessionState() implementations
|
|
|
|
func (tp *ValidateSessionStateTestProvider) ValidateSessionState(s *SessionState) bool {
|
2015-05-13 01:48:13 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-06-23 11:23:39 +00:00
|
|
|
type ValidateSessionStateTest struct {
|
2018-11-29 14:26:41 +00:00
|
|
|
backend *httptest.Server
|
|
|
|
responseCode int
|
|
|
|
provider *ValidateSessionStateTestProvider
|
|
|
|
header http.Header
|
2015-05-13 01:48:13 +00:00
|
|
|
}
|
|
|
|
|
2015-06-23 11:23:39 +00:00
|
|
|
func NewValidateSessionStateTest() *ValidateSessionStateTest {
|
2018-11-29 14:26:41 +00:00
|
|
|
var vtTest ValidateSessionStateTest
|
2015-05-13 01:48:13 +00:00
|
|
|
|
2018-11-29 14:26:41 +00:00
|
|
|
vtTest.backend = httptest.NewServer(
|
2015-05-13 01:48:13 +00: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 14:26:41 +00:00
|
|
|
tokenParam := r.FormValue("access_token")
|
|
|
|
if tokenParam == "" {
|
2015-05-13 01:48:13 +00:00
|
|
|
missing := false
|
2018-11-29 14:26:41 +00:00
|
|
|
receivedHeaders := r.Header
|
|
|
|
for k := range vtTest.header {
|
|
|
|
received := receivedHeaders.Get(k)
|
|
|
|
expected := vtTest.header.Get(k)
|
2015-05-13 01:48:13 +00: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 14:26:41 +00:00
|
|
|
w.WriteHeader(vtTest.responseCode)
|
2015-05-13 01:48:13 +00:00
|
|
|
w.Write([]byte("only code matters; contents disregarded"))
|
|
|
|
|
|
|
|
}))
|
2018-11-29 14:26:41 +00:00
|
|
|
backendURL, _ := url.Parse(vtTest.backend.URL)
|
|
|
|
vtTest.provider = &ValidateSessionStateTestProvider{
|
2015-05-13 01:48:13 +00:00
|
|
|
ProviderData: &ProviderData{
|
2015-11-08 23:47:44 +00:00
|
|
|
ValidateURL: &url.URL{
|
2015-05-13 01:48:13 +00:00
|
|
|
Scheme: "http",
|
2018-11-29 14:26:41 +00:00
|
|
|
Host: backendURL.Host,
|
2015-05-13 01:48:13 +00:00
|
|
|
Path: "/oauth/tokeninfo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2018-11-29 14:26:41 +00:00
|
|
|
vtTest.responseCode = 200
|
|
|
|
return &vtTest
|
2015-05-13 01:48:13 +00:00
|
|
|
}
|
|
|
|
|
2018-11-29 14:26:41 +00:00
|
|
|
func (vtTest *ValidateSessionStateTest) Close() {
|
|
|
|
vtTest.backend.Close()
|
2015-05-13 01:48:13 +00:00
|
|
|
}
|
|
|
|
|
2015-06-23 11:23:39 +00:00
|
|
|
func TestValidateSessionStateValidToken(t *testing.T) {
|
2018-11-29 14:26:41 +00:00
|
|
|
vtTest := NewValidateSessionStateTest()
|
|
|
|
defer vtTest.Close()
|
|
|
|
assert.Equal(t, true, validateToken(vtTest.provider, "foobar", nil))
|
2015-05-13 01:48:13 +00:00
|
|
|
}
|
|
|
|
|
2015-06-23 11:23:39 +00:00
|
|
|
func TestValidateSessionStateValidTokenWithHeaders(t *testing.T) {
|
2018-11-29 14:26:41 +00:00
|
|
|
vtTest := NewValidateSessionStateTest()
|
|
|
|
defer vtTest.Close()
|
|
|
|
vtTest.header = make(http.Header)
|
|
|
|
vtTest.header.Set("Authorization", "Bearer foobar")
|
2015-05-13 01:48:13 +00:00
|
|
|
assert.Equal(t, true,
|
2018-11-29 14:26:41 +00:00
|
|
|
validateToken(vtTest.provider, "foobar", vtTest.header))
|
2015-05-13 01:48:13 +00:00
|
|
|
}
|
|
|
|
|
2015-06-23 11:23:39 +00:00
|
|
|
func TestValidateSessionStateEmptyToken(t *testing.T) {
|
2018-11-29 14:26:41 +00:00
|
|
|
vtTest := NewValidateSessionStateTest()
|
|
|
|
defer vtTest.Close()
|
|
|
|
assert.Equal(t, false, validateToken(vtTest.provider, "", nil))
|
2015-05-13 01:48:13 +00:00
|
|
|
}
|
|
|
|
|
2015-11-08 23:47:44 +00:00
|
|
|
func TestValidateSessionStateEmptyValidateURL(t *testing.T) {
|
2018-11-29 14:26:41 +00:00
|
|
|
vtTest := NewValidateSessionStateTest()
|
|
|
|
defer vtTest.Close()
|
|
|
|
vtTest.provider.Data().ValidateURL = nil
|
|
|
|
assert.Equal(t, false, validateToken(vtTest.provider, "foobar", nil))
|
2015-05-13 01:48:13 +00:00
|
|
|
}
|
|
|
|
|
2015-06-23 11:23:39 +00:00
|
|
|
func TestValidateSessionStateRequestNetworkFailure(t *testing.T) {
|
2018-11-29 14:26:41 +00:00
|
|
|
vtTest := NewValidateSessionStateTest()
|
2015-05-13 01:48:13 +00:00
|
|
|
// Close immediately to simulate a network failure
|
2018-11-29 14:26:41 +00:00
|
|
|
vtTest.Close()
|
|
|
|
assert.Equal(t, false, validateToken(vtTest.provider, "foobar", nil))
|
2015-05-13 01:48:13 +00:00
|
|
|
}
|
|
|
|
|
2015-06-23 11:23:39 +00:00
|
|
|
func TestValidateSessionStateExpiredToken(t *testing.T) {
|
2018-11-29 14:26:41 +00:00
|
|
|
vtTest := NewValidateSessionStateTest()
|
|
|
|
defer vtTest.Close()
|
|
|
|
vtTest.responseCode = 401
|
|
|
|
assert.Equal(t, false, validateToken(vtTest.provider, "foobar", nil))
|
2015-05-13 01:48:13 +00:00
|
|
|
}
|
2016-08-03 02:27:50 +00: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))
|
|
|
|
}
|