diff --git a/README.md b/README.md index 0d249d2..7d0a81c 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ ### Download and install ```bash -$ go get -v github.com/go-session/session +github.com/edieruby/session/v3 ``` ### Create file `server.go` diff --git a/go.mod b/go.mod index eb4c65f..1ae5a90 100644 --- a/go.mod +++ b/go.mod @@ -1,9 +1,9 @@ -module github.com/go-session/session +module github.com/edieruby/session/v3 go 1.14 require ( github.com/gopherjs/gopherjs v0.0.0-20200217142428-fce0ec30dd00 // indirect - github.com/smartystreets/assertions v1.1.0 // indirect + github.com/smartystreets/assertions v1.2.0 // indirect github.com/smartystreets/goconvey v1.6.4 ) diff --git a/go.sum b/go.sum index d2f0ef1..aab4a91 100644 --- a/go.sum +++ b/go.sum @@ -5,8 +5,8 @@ github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7 github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM= github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= -github.com/smartystreets/assertions v1.1.0 h1:MkTeG1DMwsrdH7QtLXy5W+fUxWq+vmb6cLmyJ7aRtF0= -github.com/smartystreets/assertions v1.1.0/go.mod h1:tcbTF8ujkAEcZ8TElKY+i30BzYlVhC/LOxJk7iOWnoo= +github.com/smartystreets/assertions v1.2.0 h1:42S6lae5dvLc7BrLu/0ugRtcFVjoJNMC/N3yZFZkDFs= +github.com/smartystreets/assertions v1.2.0/go.mod h1:tcbTF8ujkAEcZ8TElKY+i30BzYlVhC/LOxJk7iOWnoo= github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s= github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M= diff --git a/session.go b/session.go index 1e18f9b..9a6bb79 100644 --- a/session.go +++ b/session.go @@ -35,6 +35,7 @@ var defaultOptions = options{ }, enableSetCookie: true, enableSIDInURLQuery: true, + sameSite: http.SameSiteDefaultMode, } type options struct { @@ -42,6 +43,7 @@ type options struct { cookieName string cookieLifeTime int secure bool + sameSite http.SameSite domain string expired int64 sessionID IDHandlerFunc @@ -90,6 +92,12 @@ func SetSecure(secure bool) Option { } } +func SetSameSite(sameSite http.SameSite) Option { + return func(o *options) { + o.sameSite = sameSite + } +} + // SetExpired Set session expiration time (in seconds) func SetExpired(expired int64) Option { return func(o *options) { @@ -259,10 +267,12 @@ func (m *Manager) setCookie(sessionID string, w http.ResponseWriter, r *http.Req Value: cookieValue, Path: "/", HttpOnly: true, - Secure: m.isSecure(r), + Secure: m.opts.secure, Domain: m.opts.domain, } + cookie.SameSite = m.opts.sameSite + if v := m.opts.cookieLifeTime; v > 0 { cookie.MaxAge = v cookie.Expires = time.Now().Add(time.Duration(v) * time.Second) @@ -285,7 +295,7 @@ func (m *Manager) Start(ctx context.Context, w http.ResponseWriter, r *http.Requ sid, err := m.sessionID(r) if err != nil { - return nil, err + sid = "" } if sid != "" { diff --git a/session_test.go b/session_test.go index 58f3669..1f1ca2f 100644 --- a/session_test.go +++ b/session_test.go @@ -14,6 +14,8 @@ func TestSessionStart(t *testing.T) { cookieName := "test_session_start" manager := NewManager( SetCookieName(cookieName), + SetSameSite(http.SameSiteNoneMode), + SetSecure(true), ) ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {