From ed15adfb56fefd7cd7382a1b33cf7cc02cdc80ed Mon Sep 17 00:00:00 2001 From: Michael Boke Date: Tue, 14 Nov 2023 22:13:57 +0100 Subject: [PATCH 1/5] added check method to resume a session but not start a new one, added helper functions to get values directly casted to a type from stire --- go.mod | 1 + go.sum | 2 ++ session.go | 19 +++++++++++++++++++ store.go | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 73 insertions(+) diff --git a/go.mod b/go.mod index dc71fae..1a350bb 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.17 require ( github.com/bytedance/gopkg v0.0.0-20221122125632-68358b8ecec6 + github.com/google/uuid v1.4.0 github.com/smartystreets/goconvey v1.6.4 ) diff --git a/go.sum b/go.sum index 49172dc..840acb3 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,8 @@ github.com/bytedance/gopkg v0.0.0-20221122125632-68358b8ecec6 h1:FCLDGi1EmB7JzjVVYNZiqc/zAJj2BQ5M0lfkVOxbfs8= github.com/bytedance/gopkg v0.0.0-20221122125632-68358b8ecec6/go.mod h1:5FoAH5xUHHCMDvQPy1rnj8moqLkLHFaDVBjHhcFwEi0= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4= +github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= github.com/gopherjs/gopherjs v0.0.0-20200217142428-fce0ec30dd00 h1:l5lAOZEym3oK3SQ2HBHWsJUfbNBiTXJDeW2QDxw9AQ0= github.com/gopherjs/gopherjs v0.0.0-20200217142428-fce0ec30dd00/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= diff --git a/session.go b/session.go index 1c3e11b..eb415bf 100644 --- a/session.go +++ b/session.go @@ -293,6 +293,25 @@ func (m *Manager) setCookie(sessionID string, w http.ResponseWriter, r *http.Req } } +// Check will only resume a session, not create it if it doesnt exists +func (m *Manager) Check(ctx context.Context, w http.ResponseWriter, r *http.Request) (Store, error) { + ctx = m.getContext(ctx, w, r) + + sid, err := m.sessionID(r) + if err != nil { + return nil, err + } + + if sid != "" { + if exists, err := m.opts.store.Check(ctx, sid); err != nil { + return nil, err + } else if exists { + return m.opts.store.Update(ctx, sid, m.opts.expired) + } + } + return nil, nil +} + // Start a session and return to session storage func (m *Manager) Start(ctx context.Context, w http.ResponseWriter, r *http.Request) (Store, error) { ctx = m.getContext(ctx, w, r) diff --git a/store.go b/store.go index da60210..366e26d 100644 --- a/store.go +++ b/store.go @@ -6,6 +6,7 @@ import ( "time" "github.com/bytedance/gopkg/collection/skipmap" + "github.com/google/uuid" ) var ( @@ -40,6 +41,14 @@ type Store interface { Set(key string, value interface{}) // Get session value Get(key string) (interface{}, bool) + // GetString get session value as a string + GetString(key string) (string, bool) + // GetInt get session value as a integer + GetInt(key string) (int, bool) + // GetBool get session value as a boolean + GetBool(key string) (bool, bool) + // GetUUID get session value as a UUID + GetUUID(key string) (uuid.UUID, bool) // Delete session value, call save function to take effect Delete(key string) interface{} // Save session data @@ -197,6 +206,48 @@ func (s *store) Get(key string) (interface{}, bool) { return val, ok } +func (s *store) GetString(key string) (string, bool) { + if v, ok := s.Get(key); ok { + str, ok := v.(string) + return str, ok + } + return "", false +} + +func (s *store) GetBool(key string) (bool, bool) { + if v, ok := s.Get(key); ok { + b, ok := v.(bool) + return b, ok + } + return false, false +} + +func (s *store) GetInt(key string) (int, bool) { + if v, ok := s.Get(key); ok { + i, ok := v.(int) + return i, ok + } + return 0, false +} + +func (s *store) GetUUID(key string) (uuid.UUID, bool) { + if v, ok := s.Get(key); ok { + switch t := v.(type) { + case uuid.UUID: + return t, true + case string: + if id, err := uuid.Parse(t); err != nil { + return id, true + } + case []byte: + if id, err := uuid.FromBytes(t); err != nil { + return id, true + } + } + } + return uuid.Nil, false +} + func (s *store) Delete(key string) interface{} { s.RLock() v, ok := s.values[key] From 3301e7c40e3f21d7cbb702d92b40ead39010cb9a Mon Sep 17 00:00:00 2001 From: Michael Boke Date: Tue, 14 Nov 2023 22:28:54 +0100 Subject: [PATCH 2/5] set module to repo --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 1a350bb..275e929 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/go-session/session/v3 +module github.com/mbict/session/v3 go 1.17 From 83b1e925988caaaa87075371e22d527c1d53deed Mon Sep 17 00:00:00 2001 From: Michael Boke Date: Tue, 14 Nov 2023 22:38:01 +0100 Subject: [PATCH 3/5] removed versioning, and set to go 1.21 --- .idea/.gitignore | 8 ++++++++ .idea/modules.xml | 8 ++++++++ .idea/session.iml | 9 +++++++++ .idea/vcs.xml | 6 ++++++ go.mod | 6 +++--- go.sum | 11 ++++++++--- 6 files changed, 42 insertions(+), 6 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/modules.xml create mode 100644 .idea/session.iml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..7867bfc --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/session.iml b/.idea/session.iml new file mode 100644 index 0000000..5e764c4 --- /dev/null +++ b/.idea/session.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/go.mod b/go.mod index 275e929..cf04a84 100644 --- a/go.mod +++ b/go.mod @@ -1,9 +1,9 @@ -module github.com/mbict/session/v3 +module github.com/mbict/session -go 1.17 +go 1.21 require ( - github.com/bytedance/gopkg v0.0.0-20221122125632-68358b8ecec6 + github.com/bytedance/gopkg v0.0.0-20230728082804-614d0af6619b github.com/google/uuid v1.4.0 github.com/smartystreets/goconvey v1.6.4 ) diff --git a/go.sum b/go.sum index 840acb3..85e49ea 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -github.com/bytedance/gopkg v0.0.0-20221122125632-68358b8ecec6 h1:FCLDGi1EmB7JzjVVYNZiqc/zAJj2BQ5M0lfkVOxbfs8= -github.com/bytedance/gopkg v0.0.0-20221122125632-68358b8ecec6/go.mod h1:5FoAH5xUHHCMDvQPy1rnj8moqLkLHFaDVBjHhcFwEi0= +github.com/bytedance/gopkg v0.0.0-20230728082804-614d0af6619b h1:R6PWoQtxEMpWJPHnpci+9LgFxCS7iJCfOGBvCgZeTKI= +github.com/bytedance/gopkg v0.0.0-20230728082804-614d0af6619b/go.mod h1:FtQG3YbQG9L/91pbKSw787yBQPutC+457AvDW77fgUQ= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4= github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= @@ -18,10 +18,15 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20221014081412-f15817d10f9b/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20221010170243-090e33056c14/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= From bde4f5e77234a41288c188ad4eb3144dee1a95a7 Mon Sep 17 00:00:00 2001 From: Michael Boke Date: Tue, 14 Nov 2023 23:00:18 +0100 Subject: [PATCH 4/5] typo in checking if conversion succeeded --- .idea/.gitignore | 8 -------- .idea/modules.xml | 8 -------- .idea/session.iml | 9 --------- .idea/vcs.xml | 6 ------ store.go | 4 ++-- 5 files changed, 2 insertions(+), 33 deletions(-) delete mode 100644 .idea/.gitignore delete mode 100644 .idea/modules.xml delete mode 100644 .idea/session.iml delete mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 13566b8..0000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Editor-based HTTP Client requests -/httpRequests/ -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index 7867bfc..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/session.iml b/.idea/session.iml deleted file mode 100644 index 5e764c4..0000000 --- a/.idea/session.iml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 35eb1dd..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/store.go b/store.go index 366e26d..c9bf849 100644 --- a/store.go +++ b/store.go @@ -236,11 +236,11 @@ func (s *store) GetUUID(key string) (uuid.UUID, bool) { case uuid.UUID: return t, true case string: - if id, err := uuid.Parse(t); err != nil { + if id, err := uuid.Parse(t); err == nil { return id, true } case []byte: - if id, err := uuid.FromBytes(t); err != nil { + if id, err := uuid.FromBytes(t); err == nil { return id, true } } From 949ebf403aeeb4522357313f519401deaaddda4b Mon Sep 17 00:00:00 2001 From: Michael Boke Date: Sun, 19 Nov 2023 21:06:36 +0100 Subject: [PATCH 5/5] when calling destroy, always try to unset the sessionID from the cookie and or headers --- session.go | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/session.go b/session.go index eb415bf..791e4e1 100644 --- a/session.go +++ b/session.go @@ -293,7 +293,7 @@ func (m *Manager) setCookie(sessionID string, w http.ResponseWriter, r *http.Req } } -// Check will only resume a session, not create it if it doesnt exists +// Check will only resume a session, not create it if it doesn't exists func (m *Manager) Check(ctx context.Context, w http.ResponseWriter, r *http.Request) (Store, error) { ctx = m.getContext(ctx, w, r) @@ -364,6 +364,26 @@ func (m *Manager) Refresh(ctx context.Context, w http.ResponseWriter, r *http.Re func (m *Manager) Destroy(ctx context.Context, w http.ResponseWriter, r *http.Request) error { ctx = m.getContext(ctx, w, r) + defer func() { + if m.opts.enableSetCookie { + cookie := &http.Cookie{ + Name: m.opts.cookieName, + Path: "/", + HttpOnly: true, + Expires: time.Now(), + MaxAge: -1, + } + + http.SetCookie(w, cookie) + } + + if m.opts.enableSIDInHTTPHeader { + key := m.opts.sessionNameInHTTPHeader + r.Header.Del(key) + w.Header().Del(key) + } + }() + sid, err := m.sessionID(r) if err != nil { return err @@ -382,23 +402,5 @@ func (m *Manager) Destroy(ctx context.Context, w http.ResponseWriter, r *http.Re return err } - if m.opts.enableSetCookie { - cookie := &http.Cookie{ - Name: m.opts.cookieName, - Path: "/", - HttpOnly: true, - Expires: time.Now(), - MaxAge: -1, - } - - http.SetCookie(w, cookie) - } - - if m.opts.enableSIDInHTTPHeader { - key := m.opts.sessionNameInHTTPHeader - r.Header.Del(key) - w.Header().Del(key) - } - return nil }