2019-05-06 15:33:33 +02:00
|
|
|
package sessions_test
|
|
|
|
|
|
|
|
import (
|
2019-05-07 14:55:49 +02:00
|
|
|
"crypto/rand"
|
|
|
|
"encoding/base64"
|
2019-05-06 23:34:43 +02:00
|
|
|
"net/http"
|
2019-05-07 01:20:36 +02:00
|
|
|
"net/http/httptest"
|
2019-05-07 17:13:55 +02:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2019-05-06 15:33:33 +02:00
|
|
|
"testing"
|
2019-05-06 23:34:43 +02:00
|
|
|
"time"
|
2019-05-06 15:33:33 +02:00
|
|
|
|
2020-03-14 12:14:15 +02:00
|
|
|
miniredis "github.com/alicebob/miniredis/v2"
|
2020-03-29 15:54:36 +02:00
|
|
|
"github.com/oauth2-proxy/oauth2-proxy/pkg/apis/options"
|
|
|
|
sessionsapi "github.com/oauth2-proxy/oauth2-proxy/pkg/apis/sessions"
|
|
|
|
cookiesapi "github.com/oauth2-proxy/oauth2-proxy/pkg/cookies"
|
|
|
|
"github.com/oauth2-proxy/oauth2-proxy/pkg/encryption"
|
2020-05-30 09:53:38 +02:00
|
|
|
"github.com/oauth2-proxy/oauth2-proxy/pkg/logger"
|
2020-03-29 15:54:36 +02:00
|
|
|
"github.com/oauth2-proxy/oauth2-proxy/pkg/sessions"
|
|
|
|
sessionscookie "github.com/oauth2-proxy/oauth2-proxy/pkg/sessions/cookie"
|
|
|
|
"github.com/oauth2-proxy/oauth2-proxy/pkg/sessions/redis"
|
2019-05-06 15:33:33 +02:00
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSessionStore(t *testing.T) {
|
2020-05-30 09:53:38 +02:00
|
|
|
logger.SetOutput(GinkgoWriter)
|
|
|
|
|
2019-05-06 15:33:33 +02:00
|
|
|
RegisterFailHandler(Fail)
|
|
|
|
RunSpecs(t, "SessionStore")
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ = Describe("NewSessionStore", func() {
|
|
|
|
var opts *options.SessionOptions
|
|
|
|
var cookieOpts *options.CookieOptions
|
|
|
|
|
2019-05-06 23:34:43 +02:00
|
|
|
var request *http.Request
|
2019-05-07 01:20:36 +02:00
|
|
|
var response *httptest.ResponseRecorder
|
2019-05-06 23:34:43 +02:00
|
|
|
var session *sessionsapi.SessionState
|
2019-05-07 14:32:54 +02:00
|
|
|
var ss sessionsapi.SessionStore
|
2019-05-29 16:25:56 +02:00
|
|
|
var mr *miniredis.Miniredis
|
2019-05-06 23:34:43 +02:00
|
|
|
|
|
|
|
CheckCookieOptions := func() {
|
|
|
|
Context("the cookies returned", func() {
|
|
|
|
var cookies []*http.Cookie
|
|
|
|
BeforeEach(func() {
|
2019-05-07 01:20:36 +02:00
|
|
|
cookies = response.Result().Cookies()
|
2019-05-06 23:34:43 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
It("have the correct name set", func() {
|
|
|
|
if len(cookies) == 1 {
|
2020-04-12 15:00:59 +02:00
|
|
|
Expect(cookies[0].Name).To(Equal(cookieOpts.Name))
|
2019-05-06 23:34:43 +02:00
|
|
|
} else {
|
|
|
|
for _, cookie := range cookies {
|
2020-04-12 15:00:59 +02:00
|
|
|
Expect(cookie.Name).To(ContainSubstring(cookieOpts.Name))
|
2019-05-06 23:34:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
It("have the correct path set", func() {
|
|
|
|
for _, cookie := range cookies {
|
2020-04-12 15:00:59 +02:00
|
|
|
Expect(cookie.Path).To(Equal(cookieOpts.Path))
|
2019-05-06 23:34:43 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
It("have the correct domain set", func() {
|
|
|
|
for _, cookie := range cookies {
|
2020-04-12 13:00:44 +02:00
|
|
|
specifiedDomain := ""
|
2020-04-12 15:00:59 +02:00
|
|
|
if len(cookieOpts.Domains) > 0 {
|
|
|
|
specifiedDomain = cookieOpts.Domains[0]
|
2020-04-12 13:00:44 +02:00
|
|
|
}
|
|
|
|
Expect(cookie.Domain).To(Equal(specifiedDomain))
|
2019-05-06 23:34:43 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
It("have the correct HTTPOnly set", func() {
|
|
|
|
for _, cookie := range cookies {
|
2020-04-12 15:00:59 +02:00
|
|
|
Expect(cookie.HttpOnly).To(Equal(cookieOpts.HTTPOnly))
|
2019-05-06 23:34:43 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
It("have the correct secure set", func() {
|
|
|
|
for _, cookie := range cookies {
|
2020-04-12 15:00:59 +02:00
|
|
|
Expect(cookie.Secure).To(Equal(cookieOpts.Secure))
|
2019-05-06 23:34:43 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-12-16 20:10:04 +02:00
|
|
|
It("have the correct SameSite set", func() {
|
|
|
|
for _, cookie := range cookies {
|
2020-04-12 15:00:59 +02:00
|
|
|
Expect(cookie.SameSite).To(Equal(cookiesapi.ParseSameSite(cookieOpts.SameSite)))
|
2019-12-16 20:10:04 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-05-07 17:13:55 +02: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 23:34:43 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-05-16 18:29:53 +02:00
|
|
|
// The following should only be for server stores
|
|
|
|
PersistentSessionStoreTests := func() {
|
|
|
|
Context("when Clear is called on a persistent store", func() {
|
2019-05-16 18:38:42 +02:00
|
|
|
var resultCookies []*http.Cookie
|
|
|
|
|
2019-05-16 18:29:53 +02:00
|
|
|
BeforeEach(func() {
|
|
|
|
req := httptest.NewRequest("GET", "http://example.com/", nil)
|
|
|
|
saveResp := httptest.NewRecorder()
|
|
|
|
err := ss.Save(saveResp, req, session)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
2019-05-16 18:38:42 +02:00
|
|
|
resultCookies = saveResp.Result().Cookies()
|
2019-05-16 18:29:53 +02:00
|
|
|
for _, c := range resultCookies {
|
|
|
|
request.AddCookie(c)
|
|
|
|
}
|
|
|
|
err = ss.Clear(response, request)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
2019-05-16 18:38:42 +02:00
|
|
|
})
|
2019-05-16 18:29:53 +02:00
|
|
|
|
2019-05-16 18:38:42 +02:00
|
|
|
Context("attempting to Load", func() {
|
|
|
|
var loadedAfterClear *sessionsapi.SessionState
|
|
|
|
var loadErr error
|
2019-05-16 18:29:53 +02:00
|
|
|
|
2019-05-16 18:38:42 +02:00
|
|
|
BeforeEach(func() {
|
|
|
|
loadReq := httptest.NewRequest("GET", "http://example.com/", nil)
|
|
|
|
for _, c := range resultCookies {
|
|
|
|
loadReq.AddCookie(c)
|
|
|
|
}
|
2019-05-16 18:29:53 +02:00
|
|
|
|
2019-05-16 18:38:42 +02:00
|
|
|
loadedAfterClear, loadErr = ss.Load(loadReq)
|
|
|
|
})
|
|
|
|
|
|
|
|
It("returns an empty session", func() {
|
|
|
|
Expect(loadedAfterClear).To(BeNil())
|
|
|
|
})
|
2019-05-16 18:29:53 +02:00
|
|
|
|
2019-05-16 18:38:42 +02:00
|
|
|
It("returns an error", func() {
|
|
|
|
Expect(loadErr).To(HaveOccurred())
|
|
|
|
})
|
2019-05-16 18:29:53 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
CheckCookieOptions()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
SessionStoreInterfaceTests := func(persistent bool) {
|
2019-05-08 17:36:28 +02:00
|
|
|
Context("when Save is called", func() {
|
2019-05-29 12:59:58 +02:00
|
|
|
Context("with no existing session", func() {
|
|
|
|
BeforeEach(func() {
|
|
|
|
err := ss.Save(response, request, session)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
})
|
|
|
|
|
|
|
|
It("sets a `set-cookie` header in the response", func() {
|
|
|
|
Expect(response.Header().Get("set-cookie")).ToNot(BeEmpty())
|
|
|
|
})
|
2019-05-06 23:34:43 +02:00
|
|
|
|
2019-05-29 12:59:58 +02:00
|
|
|
It("Ensures the session CreatedAt is not zero", func() {
|
|
|
|
Expect(session.CreatedAt.IsZero()).To(BeFalse())
|
|
|
|
})
|
2019-05-07 14:32:54 +02:00
|
|
|
})
|
2019-05-06 23:34:43 +02:00
|
|
|
|
2019-05-30 12:55:42 +02:00
|
|
|
Context("with a broken session", func() {
|
|
|
|
BeforeEach(func() {
|
|
|
|
By("Using a valid cookie with a different providers session encoding")
|
|
|
|
broken := "BrokenSessionFromADifferentSessionImplementation"
|
2020-05-04 20:34:01 +02:00
|
|
|
value := encryption.SignedValue(cookieOpts.Secret, cookieOpts.Name, []byte(broken), time.Now())
|
2020-04-12 15:00:59 +02:00
|
|
|
cookie := cookiesapi.MakeCookieFromOptions(request, cookieOpts.Name, value, cookieOpts, cookieOpts.Expire, time.Now())
|
2019-05-30 12:55:42 +02:00
|
|
|
request.AddCookie(cookie)
|
|
|
|
|
|
|
|
err := ss.Save(response, request, session)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
})
|
|
|
|
|
|
|
|
It("sets a `set-cookie` header in the response", func() {
|
|
|
|
Expect(response.Header().Get("set-cookie")).ToNot(BeEmpty())
|
|
|
|
})
|
|
|
|
|
|
|
|
It("Ensures the session CreatedAt is not zero", func() {
|
|
|
|
Expect(session.CreatedAt.IsZero()).To(BeFalse())
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-05-29 12:59:58 +02:00
|
|
|
Context("with an expired saved session", func() {
|
|
|
|
var err error
|
|
|
|
BeforeEach(func() {
|
|
|
|
By("saving a session")
|
|
|
|
req := httptest.NewRequest("GET", "http://example.com/", nil)
|
|
|
|
saveResp := httptest.NewRecorder()
|
|
|
|
err = ss.Save(saveResp, req, session)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
|
|
|
By("and clearing the session")
|
|
|
|
for _, c := range saveResp.Result().Cookies() {
|
|
|
|
request.AddCookie(c)
|
|
|
|
}
|
|
|
|
clearResp := httptest.NewRecorder()
|
|
|
|
err = ss.Clear(clearResp, request)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
|
|
|
By("then saving a request with the cleared session")
|
|
|
|
err = ss.Save(response, request, session)
|
|
|
|
})
|
|
|
|
|
|
|
|
It("no error should occur", func() {
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
})
|
2019-05-07 17:13:55 +02:00
|
|
|
})
|
|
|
|
|
2019-05-07 14:32:54 +02:00
|
|
|
CheckCookieOptions()
|
|
|
|
})
|
2019-05-06 23:34:43 +02:00
|
|
|
|
2019-05-08 17:36:28 +02:00
|
|
|
Context("when Clear is called", func() {
|
2019-05-07 14:32:54 +02:00
|
|
|
BeforeEach(func() {
|
2019-05-10 01:09:22 +02: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 18:13:14 +02:00
|
|
|
for _, c := range saveResp.Result().Cookies() {
|
|
|
|
request.AddCookie(c)
|
|
|
|
}
|
2019-05-10 01:09:22 +02:00
|
|
|
err = ss.Clear(response, request)
|
2019-05-07 14:32:54 +02:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
2019-05-06 23:34:43 +02:00
|
|
|
})
|
|
|
|
|
2019-05-16 18:25:41 +02:00
|
|
|
It("sets a `set-cookie` header in the response", func() {
|
2019-05-07 14:32:54 +02:00
|
|
|
Expect(response.Header().Get("Set-Cookie")).ToNot(BeEmpty())
|
2019-05-06 23:34:43 +02:00
|
|
|
})
|
2019-05-07 14:32:54 +02:00
|
|
|
|
|
|
|
CheckCookieOptions()
|
|
|
|
})
|
2019-05-07 14:42:43 +02:00
|
|
|
|
2019-05-08 17:36:28 +02:00
|
|
|
Context("when Load is called", func() {
|
2019-05-29 16:25:56 +02:00
|
|
|
LoadSessionTests := func() {
|
|
|
|
var loadedSession *sessionsapi.SessionState
|
|
|
|
BeforeEach(func() {
|
|
|
|
var err error
|
|
|
|
loadedSession, err = ss.Load(request)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
})
|
|
|
|
|
|
|
|
It("loads a session equal to the original session", func() {
|
2020-04-12 15:00:59 +02:00
|
|
|
if cookieOpts.Secret == "" {
|
2019-05-29 16:25:56 +02:00
|
|
|
// 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
|
|
|
|
|
|
|
|
// Can't compare time.Time using Equal() so remove ExpiresOn from sessions
|
|
|
|
l := *loadedSession
|
2020-05-30 09:53:38 +02:00
|
|
|
l.CreatedAt = nil
|
|
|
|
l.ExpiresOn = nil
|
2019-05-29 16:25:56 +02:00
|
|
|
s := *session
|
2020-05-30 09:53:38 +02:00
|
|
|
s.CreatedAt = nil
|
|
|
|
s.ExpiresOn = nil
|
2019-05-29 16:25:56 +02:00
|
|
|
Expect(l).To(Equal(s))
|
|
|
|
|
|
|
|
// Compare time.Time separately
|
2020-05-30 09:53:38 +02:00
|
|
|
Expect(loadedSession.CreatedAt.Equal(*session.CreatedAt)).To(BeTrue())
|
|
|
|
Expect(loadedSession.ExpiresOn.Equal(*session.ExpiresOn)).To(BeTrue())
|
2019-05-29 16:25:56 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-05-07 14:42:43 +02:00
|
|
|
BeforeEach(func() {
|
|
|
|
req := httptest.NewRequest("GET", "http://example.com/", nil)
|
|
|
|
resp := httptest.NewRecorder()
|
2019-05-08 17:36:28 +02:00
|
|
|
err := ss.Save(resp, req, session)
|
2019-05-07 14:42:43 +02:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
|
|
|
for _, cookie := range resp.Result().Cookies() {
|
|
|
|
request.AddCookie(cookie)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-05-29 16:25:56 +02:00
|
|
|
Context("before the refresh period", func() {
|
|
|
|
LoadSessionTests()
|
2019-05-07 14:42:43 +02:00
|
|
|
})
|
2019-05-29 16:25:56 +02:00
|
|
|
|
|
|
|
// Test TTLs and cleanup of persistent session storage
|
|
|
|
// For non-persistent we rely on the browser cookie lifecycle
|
|
|
|
if persistent {
|
|
|
|
Context("after the refresh period, but before the cookie expire period", func() {
|
|
|
|
BeforeEach(func() {
|
|
|
|
switch ss.(type) {
|
|
|
|
case *redis.SessionStore:
|
2020-04-12 15:00:59 +02:00
|
|
|
mr.FastForward(cookieOpts.Refresh + time.Minute)
|
2019-05-29 16:25:56 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
LoadSessionTests()
|
|
|
|
})
|
|
|
|
|
|
|
|
Context("after the cookie expire period", func() {
|
|
|
|
var loadedSession *sessionsapi.SessionState
|
|
|
|
var err error
|
|
|
|
|
|
|
|
BeforeEach(func() {
|
|
|
|
switch ss.(type) {
|
|
|
|
case *redis.SessionStore:
|
2020-04-12 15:00:59 +02:00
|
|
|
mr.FastForward(cookieOpts.Expire + time.Minute)
|
2019-05-29 16:25:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
loadedSession, err = ss.Load(request)
|
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
})
|
|
|
|
|
|
|
|
It("returns an error loading the session", func() {
|
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
})
|
|
|
|
|
|
|
|
It("returns an empty session", func() {
|
|
|
|
Expect(loadedSession).To(BeNil())
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2019-05-07 14:42:43 +02:00
|
|
|
})
|
2019-05-10 01:09:22 +02:00
|
|
|
|
2019-05-16 18:29:53 +02:00
|
|
|
if persistent {
|
|
|
|
PersistentSessionStoreTests()
|
|
|
|
}
|
2019-05-10 01:09:22 +02:00
|
|
|
}
|
|
|
|
|
2019-05-16 18:29:53 +02:00
|
|
|
RunSessionTests := func(persistent bool) {
|
2019-05-07 14:32:54 +02:00
|
|
|
Context("with default options", func() {
|
|
|
|
BeforeEach(func() {
|
|
|
|
var err error
|
|
|
|
ss, err = sessions.NewSessionStore(opts, cookieOpts)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
})
|
|
|
|
|
2019-05-16 18:29:53 +02:00
|
|
|
SessionStoreInterfaceTests(persistent)
|
2019-05-06 23:34:43 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
Context("with non-default options", func() {
|
|
|
|
BeforeEach(func() {
|
|
|
|
cookieOpts = &options.CookieOptions{
|
2020-04-12 15:00:59 +02:00
|
|
|
Name: "_cookie_name",
|
|
|
|
Path: "/path",
|
|
|
|
Expire: time.Duration(72) * time.Hour,
|
|
|
|
Refresh: time.Duration(2) * time.Hour,
|
|
|
|
Secure: false,
|
|
|
|
HTTPOnly: false,
|
|
|
|
Domains: []string{"example.com"},
|
|
|
|
SameSite: "strict",
|
2019-05-06 23:34:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
ss, err = sessions.NewSessionStore(opts, cookieOpts)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
})
|
|
|
|
|
2019-05-16 18:29:53 +02:00
|
|
|
SessionStoreInterfaceTests(persistent)
|
2019-05-06 23:34:43 +02:00
|
|
|
})
|
2019-05-07 14:55:49 +02:00
|
|
|
|
2019-05-15 17:56:05 +02:00
|
|
|
Context("with a cipher", func() {
|
2019-05-07 14:55:49 +02:00
|
|
|
BeforeEach(func() {
|
|
|
|
secret := make([]byte, 32)
|
|
|
|
_, err := rand.Read(secret)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
2020-04-12 15:00:59 +02:00
|
|
|
cookieOpts.Secret = base64.URLEncoding.EncodeToString(secret)
|
2020-05-10 18:44:04 +02:00
|
|
|
cipher, err := encryption.NewBase64Cipher(encryption.NewCFBCipher, encryption.SecretBytes(cookieOpts.Secret))
|
2019-05-15 17:56:05 +02:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
Expect(cipher).ToNot(BeNil())
|
|
|
|
opts.Cipher = cipher
|
2019-05-07 14:55:49 +02:00
|
|
|
|
|
|
|
ss, err = sessions.NewSessionStore(opts, cookieOpts)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
})
|
|
|
|
|
2019-05-16 18:29:53 +02:00
|
|
|
SessionStoreInterfaceTests(persistent)
|
2019-05-10 01:09:22 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-05-06 15:33:33 +02:00
|
|
|
BeforeEach(func() {
|
2019-05-07 14:32:54 +02:00
|
|
|
ss = nil
|
2019-05-06 15:33:33 +02:00
|
|
|
opts = &options.SessionOptions{}
|
2019-05-06 23:34:43 +02:00
|
|
|
|
|
|
|
// Set default options in CookieOptions
|
|
|
|
cookieOpts = &options.CookieOptions{
|
2020-04-12 15:00:59 +02:00
|
|
|
Name: "_oauth2_proxy",
|
|
|
|
Path: "/",
|
|
|
|
Expire: time.Duration(168) * time.Hour,
|
|
|
|
Refresh: time.Duration(1) * time.Hour,
|
|
|
|
Secure: true,
|
|
|
|
HTTPOnly: true,
|
|
|
|
SameSite: "",
|
2019-05-06 23:34:43 +02:00
|
|
|
}
|
2019-05-07 01:20:36 +02:00
|
|
|
|
2020-05-30 09:53:38 +02:00
|
|
|
expires := time.Now().Add(1 * time.Hour)
|
2019-05-07 13:18:23 +02:00
|
|
|
session = &sessionsapi.SessionState{
|
|
|
|
AccessToken: "AccessToken",
|
|
|
|
IDToken: "IDToken",
|
2020-05-30 09:53:38 +02:00
|
|
|
ExpiresOn: &expires,
|
2019-05-07 13:18:23 +02:00
|
|
|
RefreshToken: "RefreshToken",
|
|
|
|
Email: "john.doe@example.com",
|
|
|
|
User: "john.doe",
|
|
|
|
}
|
|
|
|
|
2019-05-07 01:20:36 +02:00
|
|
|
request = httptest.NewRequest("GET", "http://example.com/", nil)
|
|
|
|
response = httptest.NewRecorder()
|
2019-05-06 15:33:33 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
Context("with type 'cookie'", func() {
|
|
|
|
BeforeEach(func() {
|
|
|
|
opts.Type = options.CookieSessionStoreType
|
|
|
|
})
|
|
|
|
|
2019-05-06 23:34:43 +02:00
|
|
|
It("creates a cookie.SessionStore", func() {
|
2019-05-06 15:33:33 +02:00
|
|
|
ss, err := sessions.NewSessionStore(opts, cookieOpts)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2019-05-15 17:56:05 +02:00
|
|
|
Expect(ss).To(BeAssignableToTypeOf(&sessionscookie.SessionStore{}))
|
2019-05-06 15:33:33 +02:00
|
|
|
})
|
2019-05-06 23:34:43 +02:00
|
|
|
|
|
|
|
Context("the cookie.SessionStore", func() {
|
2019-05-16 18:29:53 +02:00
|
|
|
RunSessionTests(false)
|
2019-05-06 23:34:43 +02:00
|
|
|
})
|
2019-05-06 15:33:33 +02:00
|
|
|
})
|
|
|
|
|
2019-05-10 01:09:22 +02:00
|
|
|
Context("with type 'redis'", func() {
|
|
|
|
BeforeEach(func() {
|
2019-05-16 18:32:54 +02:00
|
|
|
var err error
|
|
|
|
mr, err = miniredis.Run()
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
2019-05-10 01:09:22 +02:00
|
|
|
opts.Type = options.RedisSessionStoreType
|
2020-04-12 15:55:30 +02:00
|
|
|
opts.Redis.ConnectionURL = "redis://" + mr.Addr()
|
2019-05-10 01:09:22 +02:00
|
|
|
})
|
|
|
|
|
2019-05-16 18:32:54 +02:00
|
|
|
AfterEach(func() {
|
|
|
|
mr.Close()
|
|
|
|
})
|
|
|
|
|
2019-05-10 01:09:22 +02:00
|
|
|
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 18:29:53 +02:00
|
|
|
RunSessionTests(true)
|
2019-05-10 01:09:22 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-05-06 15:33:33 +02: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())
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|