From 40e7c530e3ffa8beaa848ec681a14abb18449eb0 Mon Sep 17 00:00:00 2001 From: Shebin Date: Fri, 25 Jun 2021 22:15:20 +0800 Subject: [PATCH] added samsite support for cookie --- session.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/session.go b/session.go index 1e18f9b..e2ba5e5 100644 --- a/session.go +++ b/session.go @@ -30,6 +30,7 @@ var defaultOptions = options{ cookieLifeTime: 3600 * 24 * 7, expired: 7200, secure: true, + sameSite: http.SameSiteDefaultMode, sessionID: func(_ context.Context) string { return newUUID() }, @@ -43,6 +44,7 @@ type options struct { cookieLifeTime int secure bool domain string + sameSite http.SameSite expired int64 sessionID IDHandlerFunc enableSetCookie bool @@ -90,6 +92,13 @@ func SetSecure(secure bool) Option { } } +// SetSameSite Set SameSite attribute of the cookie +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) { @@ -261,6 +270,7 @@ func (m *Manager) setCookie(sessionID string, w http.ResponseWriter, r *http.Req HttpOnly: true, Secure: m.isSecure(r), Domain: m.opts.domain, + SameSite: m.opts.sameSite, } if v := m.opts.cookieLifeTime; v > 0 {