2019-05-06 13:33:33 +00:00
|
|
|
package sessions_test
|
|
|
|
|
|
|
|
import (
|
2019-05-07 12:55:49 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"encoding/base64"
|
2019-05-06 21:34:43 +00:00
|
|
|
"net/http"
|
2019-05-06 23:20:36 +00:00
|
|
|
"net/http/httptest"
|
2019-05-07 15:13:55 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2019-05-06 13:33:33 +00:00
|
|
|
"testing"
|
2019-05-06 21:34:43 +00:00
|
|
|
"time"
|
2019-05-06 13:33:33 +00:00
|
|
|
|
2019-05-09 23:09:22 +00:00
|
|
|
"github.com/alicebob/miniredis"
|
2019-05-06 13:33:33 +00:00
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
2019-05-15 15:56:05 +00:00
|
|
|
"github.com/pusher/oauth2_proxy/cookie"
|
2019-05-06 13:33:33 +00:00
|
|
|
"github.com/pusher/oauth2_proxy/pkg/apis/options"
|
2019-05-06 21:34:43 +00:00
|
|
|
sessionsapi "github.com/pusher/oauth2_proxy/pkg/apis/sessions"
|
2019-05-06 13:33:33 +00:00
|
|
|
"github.com/pusher/oauth2_proxy/pkg/sessions"
|
2019-05-15 15:56:05 +00:00
|
|
|
sessionscookie "github.com/pusher/oauth2_proxy/pkg/sessions/cookie"
|
2019-05-09 23:09:22 +00:00
|
|
|
"github.com/pusher/oauth2_proxy/pkg/sessions/redis"
|
2019-05-15 15:56:05 +00:00
|
|
|
"github.com/pusher/oauth2_proxy/pkg/sessions/utils"
|
2019-05-06 13:33:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSessionStore(t *testing.T) {
|
|
|
|
RegisterFailHandler(Fail)
|
|
|
|
RunSpecs(t, "SessionStore")
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ = Describe("NewSessionStore", func() {
|
|
|
|
var opts *options.SessionOptions
|
|
|
|
var cookieOpts *options.CookieOptions
|
|
|
|
|
2019-05-06 21:34:43 +00:00
|
|
|
var request *http.Request
|
2019-05-06 23:20:36 +00:00
|
|
|
var response *httptest.ResponseRecorder
|
2019-05-06 21:34:43 +00:00
|
|
|
var session *sessionsapi.SessionState
|
2019-05-07 12:32:54 +00:00
|
|
|
var ss sessionsapi.SessionStore
|
2019-05-06 21:34:43 +00:00
|
|
|
|
|
|
|
CheckCookieOptions := func() {
|
|
|
|
Context("the cookies returned", func() {
|
|
|
|
var cookies []*http.Cookie
|
|
|
|
BeforeEach(func() {
|
2019-05-06 23:20:36 +00:00
|
|
|
cookies = response.Result().Cookies()
|
2019-05-06 21:34:43 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
It("have the correct name set", func() {
|
|
|
|
if len(cookies) == 1 {
|
|
|
|
Expect(cookies[0].Name).To(Equal(cookieOpts.CookieName))
|
|
|
|
} else {
|
|
|
|
for _, cookie := range cookies {
|
|
|
|
Expect(cookie.Name).To(ContainSubstring(cookieOpts.CookieName))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
It("have the correct path set", func() {
|
|
|
|
for _, cookie := range cookies {
|
|
|
|
Expect(cookie.Path).To(Equal(cookieOpts.CookiePath))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
It("have the correct domain set", func() {
|
|
|
|
for _, cookie := range cookies {
|
|
|
|
Expect(cookie.Domain).To(Equal(cookieOpts.CookieDomain))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
It("have the correct HTTPOnly set", func() {
|
|
|
|
for _, cookie := range cookies {
|
|
|
|
Expect(cookie.HttpOnly).To(Equal(cookieOpts.CookieHTTPOnly))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
It("have the correct secure set", func() {
|
|
|
|
for _, cookie := range cookies {
|
|
|
|
Expect(cookie.Secure).To(Equal(cookieOpts.CookieSecure))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-05-07 15:13:55 +00:00
|
|
|
It("have a signature timestamp matching session.CreatedAt", func() {
|
|
|
|
for _, cookie := range cookies {
|
|
|
|
if cookie.Value != "" {
|
|
|
|
parts := strings.Split(cookie.Value, "|")
|
|
|
|
Expect(parts).To(HaveLen(3))
|
|
|
|
Expect(parts[1]).To(Equal(strconv.Itoa(int(session.CreatedAt.Unix()))))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-05-06 21:34:43 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-05-16 16:29:53 +00:00
|
|
|
// The following should only be for server stores
|
|
|
|
PersistentSessionStoreTests := func() {
|
|
|
|
Context("when Clear is called on a persistent store", func() {
|
|
|
|
var loadedAfterClear *sessionsapi.SessionState
|
|
|
|
BeforeEach(func() {
|
|
|
|
req := httptest.NewRequest("GET", "http://example.com/", nil)
|
|
|
|
saveResp := httptest.NewRecorder()
|
|
|
|
err := ss.Save(saveResp, req, session)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
|
|
|
resultCookies := saveResp.Result().Cookies()
|
|
|
|
for _, c := range resultCookies {
|
|
|
|
request.AddCookie(c)
|
|
|
|
}
|
|
|
|
err = ss.Clear(response, request)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
|
|
|
loadReq := httptest.NewRequest("GET", "http://example.com/", nil)
|
|
|
|
for _, c := range resultCookies {
|
|
|
|
loadReq.AddCookie(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
loadedAfterClear, err = ss.Load(loadReq)
|
|
|
|
// If we have cleared the session, Load should fail
|
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
})
|
|
|
|
|
|
|
|
It("sets a `set-cookie` header in the response", func() {
|
|
|
|
Expect(response.Header().Get("Set-Cookie")).ToNot(BeEmpty())
|
|
|
|
})
|
|
|
|
|
|
|
|
It("attempting to Load returns an empty session", func() {
|
|
|
|
Expect(loadedAfterClear).To(BeNil())
|
|
|
|
})
|
|
|
|
|
|
|
|
CheckCookieOptions()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
SessionStoreInterfaceTests := func(persistent bool) {
|
2019-05-08 15:36:28 +00:00
|
|
|
Context("when Save is called", func() {
|
2019-05-06 21:34:43 +00:00
|
|
|
BeforeEach(func() {
|
2019-05-08 15:36:28 +00:00
|
|
|
err := ss.Save(response, request, session)
|
2019-05-06 21:34:43 +00:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
})
|
|
|
|
|
2019-05-07 12:32:54 +00:00
|
|
|
It("sets a `set-cookie` header in the response", func() {
|
|
|
|
Expect(response.Header().Get("set-cookie")).ToNot(BeEmpty())
|
|
|
|
})
|
2019-05-06 21:34:43 +00:00
|
|
|
|
2019-05-07 15:13:55 +00:00
|
|
|
It("Ensures the session CreatedAt is not zero", func() {
|
|
|
|
Expect(session.CreatedAt.IsZero()).To(BeFalse())
|
|
|
|
})
|
|
|
|
|
2019-05-07 12:32:54 +00:00
|
|
|
CheckCookieOptions()
|
|
|
|
})
|
2019-05-06 21:34:43 +00:00
|
|
|
|
2019-05-08 15:36:28 +00:00
|
|
|
Context("when Clear is called", func() {
|
2019-05-07 12:32:54 +00:00
|
|
|
BeforeEach(func() {
|
2019-05-09 23:09:22 +00:00
|
|
|
req := httptest.NewRequest("GET", "http://example.com/", nil)
|
|
|
|
saveResp := httptest.NewRecorder()
|
|
|
|
err := ss.Save(saveResp, req, session)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
2019-05-16 16:13:14 +00:00
|
|
|
for _, c := range saveResp.Result().Cookies() {
|
|
|
|
request.AddCookie(c)
|
|
|
|
}
|
2019-05-09 23:09:22 +00:00
|
|
|
err = ss.Clear(response, request)
|
2019-05-07 12:32:54 +00:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
2019-05-06 21:34:43 +00:00
|
|
|
})
|
|
|
|
|
2019-05-16 16:25:41 +00:00
|
|
|
It("sets a `set-cookie` header in the response", func() {
|
2019-05-07 12:32:54 +00:00
|
|
|
Expect(response.Header().Get("Set-Cookie")).ToNot(BeEmpty())
|
2019-05-06 21:34:43 +00:00
|
|
|
})
|
2019-05-07 12:32:54 +00:00
|
|
|
|
|
|
|
CheckCookieOptions()
|
|
|
|
})
|
2019-05-07 12:42:43 +00:00
|
|
|
|
2019-05-08 15:36:28 +00:00
|
|
|
Context("when Load is called", func() {
|
2019-05-07 12:42:43 +00:00
|
|
|
var loadedSession *sessionsapi.SessionState
|
|
|
|
BeforeEach(func() {
|
|
|
|
req := httptest.NewRequest("GET", "http://example.com/", nil)
|
|
|
|
resp := httptest.NewRecorder()
|
2019-05-08 15:36:28 +00:00
|
|
|
err := ss.Save(resp, req, session)
|
2019-05-07 12:42:43 +00:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
|
|
|
for _, cookie := range resp.Result().Cookies() {
|
|
|
|
request.AddCookie(cookie)
|
|
|
|
}
|
2019-05-08 15:36:28 +00:00
|
|
|
loadedSession, err = ss.Load(request)
|
2019-05-07 12:42:43 +00:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
})
|
|
|
|
|
|
|
|
It("loads a session equal to the original session", func() {
|
|
|
|
if cookieOpts.CookieSecret == "" {
|
|
|
|
// Only Email and User stored in session when encrypted
|
|
|
|
Expect(loadedSession.Email).To(Equal(session.Email))
|
|
|
|
Expect(loadedSession.User).To(Equal(session.User))
|
|
|
|
} else {
|
|
|
|
// All fields stored in session if encrypted
|
2019-05-07 12:55:49 +00:00
|
|
|
|
|
|
|
// Can't compare time.Time using Equal() so remove ExpiresOn from sessions
|
|
|
|
l := *loadedSession
|
2019-05-07 15:13:55 +00:00
|
|
|
l.CreatedAt = time.Time{}
|
2019-05-07 12:55:49 +00:00
|
|
|
l.ExpiresOn = time.Time{}
|
|
|
|
s := *session
|
2019-05-07 15:13:55 +00:00
|
|
|
s.CreatedAt = time.Time{}
|
2019-05-07 12:55:49 +00:00
|
|
|
s.ExpiresOn = time.Time{}
|
|
|
|
Expect(l).To(Equal(s))
|
|
|
|
|
|
|
|
// Compare time.Time separately
|
2019-05-07 15:13:55 +00:00
|
|
|
Expect(loadedSession.CreatedAt.Equal(session.CreatedAt)).To(BeTrue())
|
2019-05-07 12:55:49 +00:00
|
|
|
Expect(loadedSession.ExpiresOn.Equal(session.ExpiresOn)).To(BeTrue())
|
2019-05-07 12:42:43 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2019-05-09 23:09:22 +00:00
|
|
|
|
2019-05-16 16:29:53 +00:00
|
|
|
if persistent {
|
|
|
|
PersistentSessionStoreTests()
|
|
|
|
}
|
2019-05-09 23:09:22 +00:00
|
|
|
}
|
|
|
|
|
2019-05-16 16:29:53 +00:00
|
|
|
RunSessionTests := func(persistent bool) {
|
2019-05-07 12:32:54 +00:00
|
|
|
Context("with default options", func() {
|
|
|
|
BeforeEach(func() {
|
|
|
|
var err error
|
|
|
|
ss, err = sessions.NewSessionStore(opts, cookieOpts)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
})
|
|
|
|
|
2019-05-16 16:29:53 +00:00
|
|
|
SessionStoreInterfaceTests(persistent)
|
2019-05-06 21:34:43 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
Context("with non-default options", func() {
|
|
|
|
BeforeEach(func() {
|
|
|
|
cookieOpts = &options.CookieOptions{
|
|
|
|
CookieName: "_cookie_name",
|
|
|
|
CookiePath: "/path",
|
|
|
|
CookieExpire: time.Duration(72) * time.Hour,
|
|
|
|
CookieRefresh: time.Duration(3600),
|
|
|
|
CookieSecure: false,
|
|
|
|
CookieHTTPOnly: false,
|
|
|
|
CookieDomain: "example.com",
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
ss, err = sessions.NewSessionStore(opts, cookieOpts)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
})
|
|
|
|
|
2019-05-16 16:29:53 +00:00
|
|
|
SessionStoreInterfaceTests(persistent)
|
2019-05-06 21:34:43 +00:00
|
|
|
})
|
2019-05-07 12:55:49 +00:00
|
|
|
|
2019-05-15 15:56:05 +00:00
|
|
|
Context("with a cipher", func() {
|
2019-05-07 12:55:49 +00:00
|
|
|
BeforeEach(func() {
|
|
|
|
secret := make([]byte, 32)
|
|
|
|
_, err := rand.Read(secret)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
cookieOpts.CookieSecret = base64.URLEncoding.EncodeToString(secret)
|
2019-05-15 15:56:05 +00:00
|
|
|
cipher, err := cookie.NewCipher(utils.SecretBytes(cookieOpts.CookieSecret))
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
Expect(cipher).ToNot(BeNil())
|
|
|
|
opts.Cipher = cipher
|
2019-05-07 12:55:49 +00:00
|
|
|
|
|
|
|
ss, err = sessions.NewSessionStore(opts, cookieOpts)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
})
|
|
|
|
|
2019-05-16 16:29:53 +00:00
|
|
|
SessionStoreInterfaceTests(persistent)
|
2019-05-09 23:09:22 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-05-06 13:33:33 +00:00
|
|
|
BeforeEach(func() {
|
2019-05-07 12:32:54 +00:00
|
|
|
ss = nil
|
2019-05-06 13:33:33 +00:00
|
|
|
opts = &options.SessionOptions{}
|
2019-05-06 21:34:43 +00:00
|
|
|
|
|
|
|
// Set default options in CookieOptions
|
|
|
|
cookieOpts = &options.CookieOptions{
|
|
|
|
CookieName: "_oauth2_proxy",
|
|
|
|
CookiePath: "/",
|
|
|
|
CookieExpire: time.Duration(168) * time.Hour,
|
|
|
|
CookieRefresh: time.Duration(0),
|
|
|
|
CookieSecure: true,
|
|
|
|
CookieHTTPOnly: true,
|
|
|
|
}
|
2019-05-06 23:20:36 +00:00
|
|
|
|
2019-05-07 11:18:23 +00:00
|
|
|
session = &sessionsapi.SessionState{
|
|
|
|
AccessToken: "AccessToken",
|
|
|
|
IDToken: "IDToken",
|
|
|
|
ExpiresOn: time.Now().Add(1 * time.Hour),
|
|
|
|
RefreshToken: "RefreshToken",
|
|
|
|
Email: "john.doe@example.com",
|
|
|
|
User: "john.doe",
|
|
|
|
}
|
|
|
|
|
2019-05-06 23:20:36 +00:00
|
|
|
request = httptest.NewRequest("GET", "http://example.com/", nil)
|
|
|
|
response = httptest.NewRecorder()
|
2019-05-06 13:33:33 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
Context("with type 'cookie'", func() {
|
|
|
|
BeforeEach(func() {
|
|
|
|
opts.Type = options.CookieSessionStoreType
|
|
|
|
})
|
|
|
|
|
2019-05-06 21:34:43 +00:00
|
|
|
It("creates a cookie.SessionStore", func() {
|
2019-05-06 13:33:33 +00:00
|
|
|
ss, err := sessions.NewSessionStore(opts, cookieOpts)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2019-05-15 15:56:05 +00:00
|
|
|
Expect(ss).To(BeAssignableToTypeOf(&sessionscookie.SessionStore{}))
|
2019-05-06 13:33:33 +00:00
|
|
|
})
|
2019-05-06 21:34:43 +00:00
|
|
|
|
|
|
|
Context("the cookie.SessionStore", func() {
|
2019-05-16 16:29:53 +00:00
|
|
|
RunSessionTests(false)
|
2019-05-06 21:34:43 +00:00
|
|
|
})
|
2019-05-06 13:33:33 +00:00
|
|
|
})
|
|
|
|
|
2019-05-09 23:09:22 +00:00
|
|
|
Context("with type 'redis'", func() {
|
|
|
|
BeforeEach(func() {
|
|
|
|
mr, err := miniredis.Run()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
opts.Type = options.RedisSessionStoreType
|
|
|
|
opts.RedisConnectionURL = "redis://" + mr.Addr()
|
|
|
|
})
|
|
|
|
|
|
|
|
It("creates a redis.SessionStore", func() {
|
|
|
|
ss, err := sessions.NewSessionStore(opts, cookieOpts)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(ss).To(BeAssignableToTypeOf(&redis.SessionStore{}))
|
|
|
|
})
|
|
|
|
|
|
|
|
Context("the redis.SessionStore", func() {
|
2019-05-16 16:29:53 +00:00
|
|
|
RunSessionTests(true)
|
2019-05-09 23:09:22 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-05-06 13:33:33 +00:00
|
|
|
Context("with an invalid type", func() {
|
|
|
|
BeforeEach(func() {
|
|
|
|
opts.Type = "invalid-type"
|
|
|
|
})
|
|
|
|
|
|
|
|
It("returns an error", func() {
|
|
|
|
ss, err := sessions.NewSessionStore(opts, cookieOpts)
|
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
Expect(err.Error()).To(Equal("unknown session store type 'invalid-type'"))
|
|
|
|
Expect(ss).To(BeNil())
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|