11 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/sessions"
12 "github.com/stretchr/testify/assert"
15func updateURL(url *url.URL, hostname string) {
23type ValidateSessionTestProvider struct {
27var _ Provider = (*ValidateSessionTestProvider)(nil)
29func (tp *ValidateSessionTestProvider) GetEmailAddress(_ context.Context, _ *sessions.SessionState) (string, error) {
30 return "", errors.New("not implemented")
33// Note that we're testing the internal validateToken() used to implement
34// several Provider's ValidateSession() implementations
35func (tp *ValidateSessionTestProvider) ValidateSession(_ context.Context, _ *sessions.SessionState) bool {
39type ValidateSessionStateTest struct {
40 backend *httptest.Server
42 provider *ValidateSessionTestProvider
46func NewValidateSessionTest() *ValidateSessionStateTest {
47 var vtTest ValidateSessionStateTest
49 vtTest.backend = httptest.NewServer(
50 http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
51 if r.URL.Path != "/oauth/tokeninfo" {
53 w.Write([]byte("unknown URL"))
55 tokenParam := r.FormValue("access_token")
58 receivedHeaders := r.Header
59 for k := range vtTest.header {
60 received := receivedHeaders.Get(k)
61 expected := vtTest.header.Get(k)
62 if received == "" || received != expected {
68 w.Write([]byte("no token param and missing or incorrect headers"))
71 w.WriteHeader(vtTest.responseCode)
72 w.Write([]byte("only code matters; contents disregarded"))
75 backendURL, _ := url.Parse(vtTest.backend.URL)
76 vtTest.provider = &ValidateSessionTestProvider{
77 ProviderData: &ProviderData{
78 ValidateURL: &url.URL{
80 Host: backendURL.Host,
81 Path: "/oauth/tokeninfo",
85 vtTest.responseCode = 200
89func (vtTest *ValidateSessionStateTest) Close() {
90 vtTest.backend.Close()
93func TestValidateSessionValidToken(t *testing.T) {
94 vtTest := NewValidateSessionTest()
96 assert.Equal(t, true, validateToken(context.Background(), vtTest.provider, "foobar", nil))
99func TestValidateSessionValidTokenWithHeaders(t *testing.T) {
100 vtTest := NewValidateSessionTest()
102 vtTest.header = make(http.Header)
103 vtTest.header.Set("Authorization", "Bearer foobar")
104 assert.Equal(t, true,
105 validateToken(context.Background(), vtTest.provider, "foobar", vtTest.header))
108func TestValidateSessionEmptyToken(t *testing.T) {
109 vtTest := NewValidateSessionTest()
111 assert.Equal(t, false, validateToken(context.Background(), vtTest.provider, "", nil))
114func TestValidateSessionEmptyValidateURL(t *testing.T) {
115 vtTest := NewValidateSessionTest()
117 vtTest.provider.Data().ValidateURL = nil
118 assert.Equal(t, false, validateToken(context.Background(), vtTest.provider, "foobar", nil))
121func TestValidateSessionRequestNetworkFailure(t *testing.T) {
122 vtTest := NewValidateSessionTest()
123 // Close immediately to simulate a network failure
125 assert.Equal(t, false, validateToken(context.Background(), vtTest.provider, "foobar", nil))
128func TestValidateSessionExpiredToken(t *testing.T) {
129 vtTest := NewValidateSessionTest()
131 vtTest.responseCode = 401
132 assert.Equal(t, false, validateToken(context.Background(), vtTest.provider, "foobar", nil))
135func TestValidateSessionValidateURLWithQueryParams(t *testing.T) {
136 vtTest := NewValidateSessionTest()
138 vtTest.provider.Data().ValidateURL, _ = url.Parse(vtTest.provider.Data().ValidateURL.String() + "?query_param1=true&query_param2=test")
139 assert.Equal(t, true, validateToken(context.Background(), vtTest.provider, "foobar", nil))
142func TestStripTokenNotPresent(t *testing.T) {
143 test := "http://local.test/api/test?a=1&b=2"
144 assert.Equal(t, test, stripToken(test))
147func TestStripToken(t *testing.T) {
148 test := "http://local.test/api/test?access_token=deadbeef&b=1&c=2"
149 expected := "http://local.test/api/test?access_token=dead...&b=1&c=2"
150 assert.Equal(t, expected, stripToken(test))
153func TestStripLongToken(t *testing.T) {
154 test := "http://local.test/api/test?access_token=deadbeefwithsupersecret&b=1&c=2"
155 expected := "http://local.test/api/test?access_token=deadb...&b=1&c=2"
156 assert.Equal(t, expected, stripToken(test))
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list