git.iko.soy/typeable/oauth2-proxy
on master help sign in
Define session options and cookie session store types
commit6d162a1d78795fa67032232c758ca7547036d5d4 authorJoel Speed <Joel.speed@hotmail.co.uk> dateMon 6 May 2019, 14:33 (7 years ago) parent530acff
4 files changed+124
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
pkg/apis/options/sessions.gonew file+14
@@ -0,0 +1,14 @@
1+package options
2+
3+// SessionOptions contains configuration options for the SessionStore providers.
4+type SessionOptions struct {
5+ Type string `flag:"session-store-type" cfg:"session_store_type" env:"OAUTH2_PROXY_SESSION_STORE_TYPE"`
6+ CookieStoreOptions
7+}
8+
9+// CookieSessionStoreType is used to indicate the CookieSessionStore should be
10+// used for storing sessions.
11+var CookieSessionStoreType = "cookie"
12+
13+// CookieStoreOptions contains configuration options for the CookieSessionStore.
14+type CookieStoreOptions struct{}
pkg/sessions/cookie/session_store.gonew file+40
@@ -0,0 +1,40 @@
1+package cookie
2+
3+import (
4+ "fmt"
5+ "net/http"
6+
7+ "github.com/pusher/oauth2_proxy/pkg/apis/options"
8+ "github.com/pusher/oauth2_proxy/pkg/apis/sessions"
9+)
10+
11+// Ensure CookieSessionStore implements the interface
12+var _ sessions.SessionStore = &SessionStore{}
13+
14+// SessionStore is an implementation of the sessions.SessionStore
15+// interface that stores sessions in client side cookies
16+type SessionStore struct{}
17+
18+// SaveSession takes a sessions.SessionState and stores the information from it
19+// within Cookies set on the HTTP response writer
20+func (c *SessionStore) SaveSession(rw http.ResponseWriter, req *http.Request, s *sessions.SessionState) error {
21+ return fmt.Errorf("method not implemented")
22+}
23+
24+// LoadSession reads sessions.SessionState information from Cookies within the
25+// HTTP request object
26+func (c *SessionStore) LoadSession(req *http.Request) (*sessions.SessionState, error) {
27+ return nil, fmt.Errorf("method not implemented")
28+}
29+
30+// ClearSession clears any saved session information by writing a cookie to
31+// clear the session
32+func (c *SessionStore) ClearSession(rw http.ResponseWriter, req *http.Request) error {
33+ return fmt.Errorf("method not implemented")
34+}
35+
36+// NewCookieSessionStore initialises a new instance of the SessionStore from
37+// the configuration given
38+func NewCookieSessionStore(opts options.CookieStoreOptions, cookieOpts *options.CookieOptions) (sessions.SessionStore, error) {
39+ return &SessionStore{}, fmt.Errorf("method not implemented")
40+}
pkg/sessions/session_store.gonew file+19
@@ -0,0 +1,19 @@
1+package sessions
2+
3+import (
4+ "fmt"
5+
6+ "github.com/pusher/oauth2_proxy/pkg/apis/options"
7+ "github.com/pusher/oauth2_proxy/pkg/apis/sessions"
8+ "github.com/pusher/oauth2_proxy/pkg/sessions/cookie"
9+)
10+
11+// NewSessionStore creates a SessionStore from the provided configuration
12+func NewSessionStore(opts *options.SessionOptions, cookieOpts *options.CookieOptions) (sessions.SessionStore, error) {
13+ switch opts.Type {
14+ case options.CookieSessionStoreType:
15+ return cookie.NewCookieSessionStore(opts.CookieStoreOptions, cookieOpts)
16+ default:
17+ return nil, fmt.Errorf("unknown session store type '%s'", opts.Type)
18+ }
19+}
pkg/sessions/session_store_test.gonew file+51
@@ -0,0 +1,51 @@
1+package sessions_test
2+
3+import (
4+ "testing"
5+
6+ . "github.com/onsi/ginkgo"
7+ . "github.com/onsi/gomega"
8+ "github.com/pusher/oauth2_proxy/pkg/apis/options"
9+ "github.com/pusher/oauth2_proxy/pkg/sessions"
10+ "github.com/pusher/oauth2_proxy/pkg/sessions/cookie"
11+)
12+
13+func TestSessionStore(t *testing.T) {
14+ RegisterFailHandler(Fail)
15+ RunSpecs(t, "SessionStore")
16+}
17+
18+var _ = Describe("NewSessionStore", func() {
19+ var opts *options.SessionOptions
20+ var cookieOpts *options.CookieOptions
21+
22+ BeforeEach(func() {
23+ opts = &options.SessionOptions{}
24+ cookieOpts = &options.CookieOptions{}
25+ })
26+
27+ Context("with type 'cookie'", func() {
28+ BeforeEach(func() {
29+ opts.Type = options.CookieSessionStoreType
30+ })
31+
32+ It("creates a CookieSessionStore", func() {
33+ ss, err := sessions.NewSessionStore(opts, cookieOpts)
34+ Expect(err).NotTo(HaveOccurred())
35+ Expect(ss).To(BeAssignableToTypeOf(&cookie.SessionStore{}))
36+ })
37+ })
38+
39+ Context("with an invalid type", func() {
40+ BeforeEach(func() {
41+ opts.Type = "invalid-type"
42+ })
43+
44+ It("returns an error", func() {
45+ ss, err := sessions.NewSessionStore(opts, cookieOpts)
46+ Expect(err).To(HaveOccurred())
47+ Expect(err.Error()).To(Equal("unknown session store type 'invalid-type'"))
48+ Expect(ss).To(BeNil())
49+ })
50+ })
51+})