Skip to content

Commit

Permalink
feat: add WithPath CookieHandlerOpt (zitadel#217)
Browse files Browse the repository at this point in the history
  • Loading branch information
muir authored Sep 30, 2022
1 parent 328d0e1 commit 62daf4c
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions pkg/http/cookie.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ type CookieHandler struct {
sameSite http.SameSite
maxAge int
domain string
path string
}

func NewCookieHandler(hashKey, encryptKey []byte, opts ...CookieHandlerOpt) *CookieHandler {
c := &CookieHandler{
securecookie: securecookie.New(hashKey, encryptKey),
secureOnly: true,
sameSite: http.SameSiteLaxMode,
path: "/",
}

for _, opt := range opts {
Expand Down Expand Up @@ -55,6 +57,12 @@ func WithDomain(domain string) CookieHandlerOpt {
}
}

func WithPath(path string) CookieHandlerOpt {
return func(c *CookieHandler) {
c.domain = path
}
}

func (c *CookieHandler) CheckCookie(r *http.Request, name string) (string, error) {
cookie, err := r.Cookie(name)
if err != nil {
Expand Down Expand Up @@ -87,7 +95,7 @@ func (c *CookieHandler) SetCookie(w http.ResponseWriter, name, value string) err
Name: name,
Value: encoded,
Domain: c.domain,
Path: "/",
Path: c.path,
MaxAge: c.maxAge,
HttpOnly: true,
Secure: c.secureOnly,
Expand All @@ -101,7 +109,7 @@ func (c *CookieHandler) DeleteCookie(w http.ResponseWriter, name string) {
Name: name,
Value: "",
Domain: c.domain,
Path: "/",
Path: c.path,
MaxAge: -1,
HttpOnly: true,
Secure: c.secureOnly,
Expand Down

0 comments on commit 62daf4c

Please sign in to comment.