2019-05-06 15:33:33 +02:00
|
|
|
package sessions_test
|
|
|
|
|
|
|
|
import (
|
2019-05-07 14:55:49 +02:00
|
|
|
"encoding/base64"
|
2020-05-10 17:59:17 +02:00
|
|
|
"math/rand"
|
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-09-29 18:44:42 +02:00
|
|
|
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/options"
|
|
|
|
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/logger"
|
|
|
|
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/sessions"
|
|
|
|
sessionscookie "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/sessions/cookie"
|
|
|
|
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/sessions/persistence"
|
|
|
|
"github.com/oauth2-proxy/oauth2-proxy/v7/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)
|
2021-02-10 20:50:09 +02:00
|
|
|
logger.SetErrOutput(GinkgoWriter)
|
2020-05-30 09:53:38 +02:00
|
|
|
|
2019-05-06 15:33:33 +02:00
|
|
|
RegisterFailHandler(Fail)
|
|
|
|
RunSpecs(t, "SessionStore")
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ = Describe("NewSessionStore", func() {
|
|
|
|
var opts *options.SessionOptions
|
2020-05-25 13:43:24 +02:00
|
|
|
var cookieOpts *options.Cookie
|
2019-05-06 15:33:33 +02:00
|
|
|
|
|
|
|
BeforeEach(func() {
|
|
|
|
opts = &options.SessionOptions{}
|
2019-05-06 23:34:43 +02:00
|
|
|
|
2020-05-25 14:36:44 +02:00
|
|
|
// A secret is required to create a Cipher, validation ensures it is the correct
|
|
|
|
// length before a session store is initialised.
|
|
|
|
secret := make([]byte, 32)
|
|
|
|
_, err := rand.Read(secret)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
2019-05-06 23:34:43 +02:00
|
|
|
// Set default options in CookieOptions
|
2020-05-25 13:43:24 +02:00
|
|
|
cookieOpts = &options.Cookie{
|
2020-04-12 15:00:59 +02:00
|
|
|
Name: "_oauth2_proxy",
|
2020-05-25 14:36:44 +02:00
|
|
|
Secret: base64.URLEncoding.EncodeToString(secret),
|
2020-04-12 15:00:59 +02:00
|
|
|
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-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-10 01:09:22 +02:00
|
|
|
Context("with type 'redis'", func() {
|
|
|
|
BeforeEach(func() {
|
|
|
|
opts.Type = options.RedisSessionStoreType
|
2020-05-10 17:59:17 +02:00
|
|
|
opts.Redis.ConnectionURL = "redis://"
|
2019-05-16 18:32:54 +02:00
|
|
|
})
|
|
|
|
|
2020-07-19 22:25:13 +02:00
|
|
|
It("creates a persistence.Manager that wraps a redis.SessionStore", func() {
|
2019-05-10 01:09:22 +02:00
|
|
|
ss, err := sessions.NewSessionStore(opts, cookieOpts)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2020-07-19 22:25:13 +02:00
|
|
|
Expect(ss).To(BeAssignableToTypeOf(&persistence.Manager{}))
|
|
|
|
Expect(ss.(*persistence.Manager).Store).To(BeAssignableToTypeOf(&redis.SessionStore{}))
|
2019-05-10 01:09:22 +02:00
|
|
|
})
|
2019-05-06 15:33:33 +02:00
|
|
|
})
|
2020-05-25 15:00:49 +02:00
|
|
|
|
2020-06-28 13:44:12 +02:00
|
|
|
Context("with an invalid type", func() {
|
2020-05-25 15:00:49 +02:00
|
|
|
BeforeEach(func() {
|
2020-06-28 13:44:12 +02:00
|
|
|
opts.Type = "invalid-type"
|
2020-05-25 15:00:49 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
It("returns an error", func() {
|
|
|
|
ss, err := sessions.NewSessionStore(opts, cookieOpts)
|
|
|
|
Expect(err).To(HaveOccurred())
|
2020-06-28 13:44:12 +02:00
|
|
|
Expect(err.Error()).To(Equal("unknown session store type 'invalid-type'"))
|
2020-05-25 15:00:49 +02:00
|
|
|
Expect(ss).To(BeNil())
|
|
|
|
})
|
|
|
|
})
|
2019-05-06 15:33:33 +02:00
|
|
|
})
|