diff --git a/cookie.go b/cookie.go index db3f5fd..9544953 100644 --- a/cookie.go +++ b/cookie.go @@ -1,7 +1,6 @@ package session import ( - "strings" "time" "github.com/valyala/fasthttp" @@ -15,7 +14,7 @@ func (c *cookie) get(ctx *fasthttp.RequestCtx, name string) []byte { return ctx.Request.Header.Cookie(name) } -func (c *cookie) set(ctx *fasthttp.RequestCtx, name string, value []byte, domain string, expiration time.Duration, secure bool, sameSite string) { +func (c *cookie) set(ctx *fasthttp.RequestCtx, name string, value []byte, domain string, expiration time.Duration, secure bool, CookieSameSite fasthttp.CookieSameSite) { cookie := fasthttp.AcquireCookie() cookie.SetKey(name) @@ -23,17 +22,7 @@ func (c *cookie) set(ctx *fasthttp.RequestCtx, name string, value []byte, domain cookie.SetHTTPOnly(true) cookie.SetDomain(domain) cookie.SetValueBytes(value) - - switch strings.ToLower(sameSite) { - case "lax": - cookie.SetSameSite(fasthttp.CookieSameSiteLaxMode) - case "strict": - cookie.SetSameSite(fasthttp.CookieSameSiteStrictMode) - case "none": - cookie.SetSameSite(fasthttp.CookieSameSiteNoneMode) - default: - cookie.SetSameSite(fasthttp.CookieSameSiteDisabled) - } + cookie.SetSameSite(CookieSameSite) if expiration >= 0 { if expiration == 0 { diff --git a/cookie_test.go b/cookie_test.go index c118328..77175a3 100644 --- a/cookie_test.go +++ b/cookie_test.go @@ -39,7 +39,7 @@ func TestCookie_set(t *testing.T) { domain := "domain" expiration := 10 * time.Second secure := true - samesite := "Lax" + samesite := fasthttp.CookieSameSiteLaxMode now := time.Now() cookie.set(ctx, key, value, domain, expiration, secure, samesite) diff --git a/session.go b/session.go index ea29a28..c45ae89 100644 --- a/session.go +++ b/session.go @@ -92,7 +92,7 @@ func (s *Session) stopGC() { func (s *Session) setHTTPValues(ctx *fasthttp.RequestCtx, sessionID []byte, expiration time.Duration) { secure := s.config.Secure && s.config.IsSecureFunc(ctx) - s.cookie.set(ctx, s.config.CookieName, sessionID, s.config.Domain, expiration, secure, s.config.SameSite) + s.cookie.set(ctx, s.config.CookieName, sessionID, s.config.Domain, expiration, secure, s.config.CookieSameSite) if s.config.SessionIDInHTTPHeader { ctx.Request.Header.SetBytesV(s.config.SessionNameInHTTPHeader, sessionID) diff --git a/types.go b/types.go index 036b80f..01413d7 100644 --- a/types.go +++ b/types.go @@ -31,7 +31,7 @@ type Config struct { // allows you to declare if your cookie should be restricted to a first-party or same-site context. // possible values: lax, strict, none - SameSite string + CookieSameSite fasthttp.CookieSameSite // sessionID is in url query SessionIDInURLQuery bool