diff options
| author | mo khan <mo@mokhan.ca> | 2025-04-30 12:05:55 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2025-04-30 12:05:55 -0600 |
| commit | 6dc20979d287652a849e32696fe3a805df1001ae (patch) | |
| tree | f88bcc9702ac950b3d644b942de73f7b565d440e | |
| parent | 197ef4ee79e7c4881c5c612115326f4e874c9415 (diff) | |
refactor: extract cookie options
| -rw-r--r-- | pkg/pls/option.go | 3 | ||||
| -rw-r--r-- | pkg/test/http.go | 3 | ||||
| -rw-r--r-- | pkg/web/cookie/new.go | 13 |
3 files changed, 16 insertions, 3 deletions
diff --git a/pkg/pls/option.go b/pkg/pls/option.go new file mode 100644 index 0000000..bd717fa --- /dev/null +++ b/pkg/pls/option.go @@ -0,0 +1,3 @@ +package pls + +type Option[T any] func(T) T diff --git a/pkg/test/http.go b/pkg/test/http.go index 280aef6..6c15016 100644 --- a/pkg/test/http.go +++ b/pkg/test/http.go @@ -10,9 +10,10 @@ import ( xcontext "github.com/xlgmokha/x/pkg/context" "github.com/xlgmokha/x/pkg/serde" "github.com/xlgmokha/x/pkg/x" + "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/pls" ) -type RequestOption func(*http.Request) *http.Request +type RequestOption pls.Option[*http.Request] func Request(method, target string, options ...RequestOption) *http.Request { request := httptest.NewRequest(method, target, nil) diff --git a/pkg/web/cookie/new.go b/pkg/web/cookie/new.go index d4d0700..a3cb200 100644 --- a/pkg/web/cookie/new.go +++ b/pkg/web/cookie/new.go @@ -5,10 +5,13 @@ import ( "time" "github.com/xlgmokha/x/pkg/env" + "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/pls" ) -func New(name, value string, expires time.Time) *http.Cookie { - return &http.Cookie{ +type CookieOption pls.Option[*http.Cookie] + +func New(name, value string, expires time.Time, options ...CookieOption) *http.Cookie { + cookie := &http.Cookie{ Name: name, Value: value, // TODO:: digitally sign the value Expires: expires, @@ -19,4 +22,10 @@ func New(name, value string, expires time.Time) *http.Cookie { SameSite: http.SameSiteStrictMode, Domain: env.Fetch("HOST", "localhost"), } + + for _, option := range options { + cookie = option(cookie) + } + + return cookie } |
