Skip to content

Commit

Permalink
Requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Fenny authored and Sergio Andrés Virviescas Santana committed May 26, 2020
1 parent 90aa1d3 commit c332ef8
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 16 deletions.
15 changes: 2 additions & 13 deletions cookie.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package session

import (
"strings"
"time"

"github.com/valyala/fasthttp"
Expand All @@ -15,25 +14,15 @@ 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)
cookie.SetPath("/")
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 {
Expand Down
2 changes: 1 addition & 1 deletion cookie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion session.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c332ef8

Please sign in to comment.