mirror of
https://github.com/hanzo-fi/go-libs.git
synced 2026-10-30 20:53:44 +00:00
* refactor: move all packages under pkg/ for v5 Move all 13 top-level Go packages into pkg/ for a cleaner repository structure. All internal import paths updated from github.com/formancehq/go-libs/v5/<pkg> to github.com/formancehq/go-libs/v5/pkg/<pkg>. Also includes cleanup: - Fix .golangci.yml stale v4 CircuitBreaker path - Replace deprecated str.Contains with stdlib slices.Contains - Update TODO(v4) markers to TODO - Delete stale coverage.txt - Fix pgtesting timeout not being propagated to docker pool - Update docs/ARCHITECTURE.md to reflect new structure Co-Authored-By: Claude Opus 4.6 <[email protected]> * chore: remove compatibility aliases, unused params, and stub files - Remove unused type aliases (Authenticator, KeySet) from authnfx - Remove unused `debug` parameter from S3ModuleFromFlags - Delete comment-only stub files in messaging/publish/circuit/ Co-Authored-By: Claude Opus 4.6 <[email protected]> * chore: rename cli.go to flags.go for consistency Standardize naming across all packages: cli.go -> flags.go since these files all serve the same purpose (flag definitions). Co-Authored-By: Claude Opus 4.6 <[email protected]> * chore: rename * chore(deps): update all dependencies Co-Authored-By: Claude Opus 4.6 <[email protected]> * fix: resolve package name mismatches in generated mocks Generated mock files had stale package names from before the pkg/ move, causing import cycles and build failures with --build-tags it. Co-Authored-By: Claude Opus 4.6 <[email protected]> --------- Co-authored-by: Claude Opus 4.6 <[email protected]>
185 lines
3.7 KiB
Go
185 lines
3.7 KiB
Go
package http
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
|
|
"github.com/gorilla/securecookie"
|
|
)
|
|
|
|
type CookieHandler struct {
|
|
securecookie *securecookie.SecureCookie
|
|
secureCookieFunc func(r *http.Request) (*securecookie.SecureCookie, error)
|
|
secureOnly bool
|
|
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 {
|
|
opt(c)
|
|
}
|
|
return c
|
|
}
|
|
|
|
func NewRequestAwareCookieHandler(secureCookieFunc func(r *http.Request) (*securecookie.SecureCookie, error), opts ...CookieHandlerOpt) *CookieHandler {
|
|
c := &CookieHandler{
|
|
secureCookieFunc: secureCookieFunc,
|
|
secureOnly: true,
|
|
sameSite: http.SameSiteLaxMode,
|
|
path: "/",
|
|
}
|
|
|
|
for _, opt := range opts {
|
|
opt(c)
|
|
}
|
|
|
|
return c
|
|
}
|
|
|
|
type CookieHandlerOpt func(*CookieHandler)
|
|
|
|
func WithUnsecure() CookieHandlerOpt {
|
|
return func(c *CookieHandler) {
|
|
c.secureOnly = false
|
|
}
|
|
}
|
|
|
|
func WithSameSite(sameSite http.SameSite) CookieHandlerOpt {
|
|
return func(c *CookieHandler) {
|
|
c.sameSite = sameSite
|
|
}
|
|
}
|
|
|
|
func WithMaxAge(maxAge int) CookieHandlerOpt {
|
|
return func(c *CookieHandler) {
|
|
c.maxAge = maxAge
|
|
if c.IsRequestAware() {
|
|
return
|
|
}
|
|
|
|
c.securecookie.MaxAge(maxAge)
|
|
}
|
|
}
|
|
|
|
func WithDomain(domain string) CookieHandlerOpt {
|
|
return func(c *CookieHandler) {
|
|
c.domain = domain
|
|
}
|
|
}
|
|
|
|
func WithPath(path string) CookieHandlerOpt {
|
|
return func(c *CookieHandler) {
|
|
c.path = path
|
|
}
|
|
}
|
|
|
|
func (c *CookieHandler) CheckCookie(r *http.Request, name string) (string, error) {
|
|
cookie, err := r.Cookie(name)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
secureCookie := c.securecookie
|
|
if c.IsRequestAware() {
|
|
secureCookie, err = c.secureCookieFunc(r)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
}
|
|
|
|
var value string
|
|
if err := secureCookie.Decode(name, cookie.Value, &value); err != nil {
|
|
return "", err
|
|
}
|
|
return value, nil
|
|
}
|
|
|
|
func (c *CookieHandler) CheckQueryCookie(r *http.Request, name string) (string, error) {
|
|
value, err := c.CheckCookie(r, name)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if value != r.FormValue(name) {
|
|
return "", errors.New(name + " does not compare")
|
|
}
|
|
return value, nil
|
|
}
|
|
|
|
func (c *CookieHandler) SetCookie(w http.ResponseWriter, name, value string) error {
|
|
if c.IsRequestAware() {
|
|
return errors.New("Cookie handler is request aware")
|
|
}
|
|
|
|
encoded, err := c.securecookie.Encode(name, value)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
http.SetCookie(w, &http.Cookie{
|
|
Name: name,
|
|
Value: encoded,
|
|
Domain: c.domain,
|
|
Path: c.path,
|
|
MaxAge: c.maxAge,
|
|
HttpOnly: true,
|
|
Secure: c.secureOnly,
|
|
SameSite: c.sameSite,
|
|
})
|
|
return nil
|
|
}
|
|
|
|
func (c *CookieHandler) SetRequestAwareCookie(r *http.Request, w http.ResponseWriter, name string, value string) error {
|
|
if !c.IsRequestAware() {
|
|
return errors.New("Cookie handler is not request aware")
|
|
}
|
|
|
|
secureCookie, err := c.secureCookieFunc(r)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
encoded, err := secureCookie.Encode(name, value)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
http.SetCookie(w, &http.Cookie{
|
|
Name: name,
|
|
Value: encoded,
|
|
Domain: c.domain,
|
|
Path: c.path,
|
|
MaxAge: c.maxAge,
|
|
HttpOnly: true,
|
|
Secure: c.secureOnly,
|
|
SameSite: c.sameSite,
|
|
})
|
|
|
|
return nil
|
|
}
|
|
|
|
func (c *CookieHandler) DeleteCookie(w http.ResponseWriter, name string) {
|
|
http.SetCookie(w, &http.Cookie{
|
|
Name: name,
|
|
Value: "",
|
|
Domain: c.domain,
|
|
Path: c.path,
|
|
MaxAge: -1,
|
|
HttpOnly: true,
|
|
Secure: c.secureOnly,
|
|
SameSite: c.sameSite,
|
|
})
|
|
}
|
|
|
|
func (c *CookieHandler) IsRequestAware() bool {
|
|
return c.secureCookieFunc != nil
|
|
}
|