diff options
Diffstat (limited to 'pkg/web')
| -rw-r--r-- | pkg/web/cookie/expire.go | 14 | ||||
| -rw-r--r-- | pkg/web/cookie/new.go | 18 | ||||
| -rw-r--r-- | pkg/web/cookie/option.go | 57 | ||||
| -rw-r--r-- | pkg/web/cookie/option_test.go | 53 | ||||
| -rw-r--r-- | pkg/web/cookie/reset.go | 16 |
5 files changed, 28 insertions, 130 deletions
diff --git a/pkg/web/cookie/expire.go b/pkg/web/cookie/expire.go index b44fc38..7d90274 100644 --- a/pkg/web/cookie/expire.go +++ b/pkg/web/cookie/expire.go @@ -1,7 +1,17 @@ package cookie -import "net/http" +import ( + "net/http" + + "github.com/xlgmokha/x/pkg/cookie" + "github.com/xlgmokha/x/pkg/env" +) func Expire(w http.ResponseWriter, name string) { - http.SetCookie(w, Reset(name)) + cookie.Expire(w, name, + cookie.WithPath("/"), + cookie.WithDomain(env.Fetch("HOST", "localhost")), + cookie.WithHttpOnly(true), + cookie.WithSecure(true), + ) } diff --git a/pkg/web/cookie/new.go b/pkg/web/cookie/new.go index 8c04dd6..be0241d 100644 --- a/pkg/web/cookie/new.go +++ b/pkg/web/cookie/new.go @@ -3,6 +3,7 @@ package cookie import ( "net/http" + "github.com/xlgmokha/x/pkg/cookie" "github.com/xlgmokha/x/pkg/env" "github.com/xlgmokha/x/pkg/x" ) @@ -10,15 +11,14 @@ import ( func New(name, value string, options ...x.Option[*http.Cookie]) *http.Cookie { options = x.Prepend[x.Option[*http.Cookie]]( options, - With(func(c *http.Cookie) { - c.Name = name - c.Value = value // TODO:: digitally sign the value - }), - WithPath("/"), - WithHttpOnly(true), - WithSecure(true), - WithSameSite(http.SameSiteDefaultMode), - WithDomain(env.Fetch("HOST", "localhost")), + cookie.WithName(name), + cookie.WithValue(value), // TODO:: digitally sign the value + cookie.WithPath("/"), + cookie.WithHttpOnly(true), + cookie.WithSecure(true), + cookie.WithSameSite(http.SameSiteDefaultMode), + cookie.WithDomain(env.Fetch("HOST", "localhost")), ) + return x.New[*http.Cookie](options...) } diff --git a/pkg/web/cookie/option.go b/pkg/web/cookie/option.go deleted file mode 100644 index 58a2e93..0000000 --- a/pkg/web/cookie/option.go +++ /dev/null @@ -1,57 +0,0 @@ -package cookie - -import ( - "net/http" - "time" - - "github.com/xlgmokha/x/pkg/x" -) - -func With(with func(*http.Cookie)) x.Option[*http.Cookie] { - return func(c *http.Cookie) *http.Cookie { - with(c) - return c - } -} - -func WithPath(value string) x.Option[*http.Cookie] { - return With(func(c *http.Cookie) { - c.Path = value - }) -} - -func WithHttpOnly(value bool) x.Option[*http.Cookie] { - return With(func(c *http.Cookie) { - c.HttpOnly = value - }) -} - -func WithSecure(value bool) x.Option[*http.Cookie] { - return With(func(c *http.Cookie) { - c.Secure = value - }) -} - -func WithDomain(value string) x.Option[*http.Cookie] { - return With(func(c *http.Cookie) { - c.Domain = value - }) -} - -func WithSameSite(value http.SameSite) x.Option[*http.Cookie] { - return With(func(c *http.Cookie) { - c.SameSite = value - }) -} - -func WithExpiration(expires time.Time) x.Option[*http.Cookie] { - return With(func(c *http.Cookie) { - c.Expires = expires - if expires.Before(time.Now()) { - c.MaxAge = -1 - } else { - duration := time.Until(expires).Round(time.Second) - c.MaxAge = int(duration.Seconds()) - } - }) -} diff --git a/pkg/web/cookie/option_test.go b/pkg/web/cookie/option_test.go deleted file mode 100644 index 97913ba..0000000 --- a/pkg/web/cookie/option_test.go +++ /dev/null @@ -1,53 +0,0 @@ -package cookie - -import ( - "net/http" - "testing" - "time" - - "github.com/stretchr/testify/assert" -) - -func TestOption(t *testing.T) { - t.Run("WithPath", func(t *testing.T) { - assert.Equal(t, "/blah", New("name", "value", WithPath("/blah")).Path) - }) - - t.Run("WithHttpOnly", func(t *testing.T) { - assert.False(t, New("x", "v", WithHttpOnly(false)).HttpOnly) - assert.True(t, New("x", "v", WithHttpOnly(true)).HttpOnly) - }) - - t.Run("WithSecure", func(t *testing.T) { - assert.False(t, New("x", "v", WithSecure(false)).Secure) - assert.True(t, New("x", "v", WithSecure(true)).Secure) - }) - - t.Run("WithDomain", func(t *testing.T) { - assert.Equal(t, "example.com", New("x", "v", WithDomain("example.com")).Domain) - }) - - t.Run("WithSameSite", func(t *testing.T) { - assert.Equal(t, http.SameSiteLaxMode, New("x", "v", WithSameSite(http.SameSiteLaxMode)).SameSite) - assert.Equal(t, http.SameSiteStrictMode, New("x", "v", WithSameSite(http.SameSiteStrictMode)).SameSite) - assert.Equal(t, http.SameSiteNoneMode, New("x", "v", WithSameSite(http.SameSiteNoneMode)).SameSite) - }) - - t.Run("WithExpiration", func(t *testing.T) { - now := time.Now() - - t.Run("with future time", func(t *testing.T) { - expires := now.Add(1 * time.Second) - cookie := New("x", "v", WithExpiration(expires)) - assert.Equal(t, expires, cookie.Expires) - assert.Equal(t, 1, cookie.MaxAge) - }) - - t.Run("with past time", func(t *testing.T) { - expires := now.Add(-1 * time.Second) - cookie := New("x", "v", WithExpiration(expires)) - assert.Equal(t, expires, cookie.Expires) - assert.Equal(t, -1, cookie.MaxAge) - }) - }) -} diff --git a/pkg/web/cookie/reset.go b/pkg/web/cookie/reset.go index 167cdb6..39625e6 100644 --- a/pkg/web/cookie/reset.go +++ b/pkg/web/cookie/reset.go @@ -2,20 +2,18 @@ package cookie import ( "net/http" - "time" + "github.com/xlgmokha/x/pkg/cookie" "github.com/xlgmokha/x/pkg/env" ) func Reset(name string) *http.Cookie { - return New( + return cookie.Reset( name, - "", - WithExpiration(time.Unix(0, 0)), - WithPath("/"), - WithHttpOnly(true), - WithSecure(true), - WithSameSite(http.SameSiteDefaultMode), - WithDomain(env.Fetch("HOST", "localhost")), + cookie.WithPath("/"), + cookie.WithHttpOnly(true), + cookie.WithSecure(true), + cookie.WithSameSite(http.SameSiteDefaultMode), + cookie.WithDomain(env.Fetch("HOST", "localhost")), ) } |
