From 6d162a1d78795fa67032232c758ca7547036d5d4 Mon Sep 17 00:00:00 2001 From: Joel Speed Date: Mon, 6 May 2019 14:33:33 +0100 Subject: [PATCH] Define session options and cookie session store types --- pkg/apis/options/sessions.go | 14 ++++++++ pkg/sessions/cookie/session_store.go | 40 ++++++++++++++++++++++ pkg/sessions/session_store.go | 19 +++++++++++ pkg/sessions/session_store_test.go | 51 ++++++++++++++++++++++++++++ 4 files changed, 124 insertions(+) create mode 100644 pkg/apis/options/sessions.go create mode 100644 pkg/sessions/cookie/session_store.go create mode 100644 pkg/sessions/session_store.go create mode 100644 pkg/sessions/session_store_test.go diff --git a/pkg/apis/options/sessions.go b/pkg/apis/options/sessions.go new file mode 100644 index 0000000..56fd27a --- /dev/null +++ b/pkg/apis/options/sessions.go @@ -0,0 +1,14 @@ +package options + +// SessionOptions contains configuration options for the SessionStore providers. +type SessionOptions struct { + Type string `flag:"session-store-type" cfg:"session_store_type" env:"OAUTH2_PROXY_SESSION_STORE_TYPE"` + CookieStoreOptions +} + +// CookieSessionStoreType is used to indicate the CookieSessionStore should be +// used for storing sessions. +var CookieSessionStoreType = "cookie" + +// CookieStoreOptions contains configuration options for the CookieSessionStore. +type CookieStoreOptions struct{} diff --git a/pkg/sessions/cookie/session_store.go b/pkg/sessions/cookie/session_store.go new file mode 100644 index 0000000..44799f9 --- /dev/null +++ b/pkg/sessions/cookie/session_store.go @@ -0,0 +1,40 @@ +package cookie + +import ( + "fmt" + "net/http" + + "github.com/pusher/oauth2_proxy/pkg/apis/options" + "github.com/pusher/oauth2_proxy/pkg/apis/sessions" +) + +// Ensure CookieSessionStore implements the interface +var _ sessions.SessionStore = &SessionStore{} + +// SessionStore is an implementation of the sessions.SessionStore +// interface that stores sessions in client side cookies +type SessionStore struct{} + +// SaveSession takes a sessions.SessionState and stores the information from it +// within Cookies set on the HTTP response writer +func (c *SessionStore) SaveSession(rw http.ResponseWriter, req *http.Request, s *sessions.SessionState) error { + return fmt.Errorf("method not implemented") +} + +// LoadSession reads sessions.SessionState information from Cookies within the +// HTTP request object +func (c *SessionStore) LoadSession(req *http.Request) (*sessions.SessionState, error) { + return nil, fmt.Errorf("method not implemented") +} + +// ClearSession clears any saved session information by writing a cookie to +// clear the session +func (c *SessionStore) ClearSession(rw http.ResponseWriter, req *http.Request) error { + return fmt.Errorf("method not implemented") +} + +// NewCookieSessionStore initialises a new instance of the SessionStore from +// the configuration given +func NewCookieSessionStore(opts options.CookieStoreOptions, cookieOpts *options.CookieOptions) (sessions.SessionStore, error) { + return &SessionStore{}, fmt.Errorf("method not implemented") +} diff --git a/pkg/sessions/session_store.go b/pkg/sessions/session_store.go new file mode 100644 index 0000000..cc074c7 --- /dev/null +++ b/pkg/sessions/session_store.go @@ -0,0 +1,19 @@ +package sessions + +import ( + "fmt" + + "github.com/pusher/oauth2_proxy/pkg/apis/options" + "github.com/pusher/oauth2_proxy/pkg/apis/sessions" + "github.com/pusher/oauth2_proxy/pkg/sessions/cookie" +) + +// NewSessionStore creates a SessionStore from the provided configuration +func NewSessionStore(opts *options.SessionOptions, cookieOpts *options.CookieOptions) (sessions.SessionStore, error) { + switch opts.Type { + case options.CookieSessionStoreType: + return cookie.NewCookieSessionStore(opts.CookieStoreOptions, cookieOpts) + default: + return nil, fmt.Errorf("unknown session store type '%s'", opts.Type) + } +} diff --git a/pkg/sessions/session_store_test.go b/pkg/sessions/session_store_test.go new file mode 100644 index 0000000..590d181 --- /dev/null +++ b/pkg/sessions/session_store_test.go @@ -0,0 +1,51 @@ +package sessions_test + +import ( + "testing" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + "github.com/pusher/oauth2_proxy/pkg/apis/options" + "github.com/pusher/oauth2_proxy/pkg/sessions" + "github.com/pusher/oauth2_proxy/pkg/sessions/cookie" +) + +func TestSessionStore(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "SessionStore") +} + +var _ = Describe("NewSessionStore", func() { + var opts *options.SessionOptions + var cookieOpts *options.CookieOptions + + BeforeEach(func() { + opts = &options.SessionOptions{} + cookieOpts = &options.CookieOptions{} + }) + + Context("with type 'cookie'", func() { + BeforeEach(func() { + opts.Type = options.CookieSessionStoreType + }) + + It("creates a CookieSessionStore", func() { + ss, err := sessions.NewSessionStore(opts, cookieOpts) + Expect(err).NotTo(HaveOccurred()) + Expect(ss).To(BeAssignableToTypeOf(&cookie.SessionStore{})) + }) + }) + + 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()) + }) + }) +})