| 1 |
package http |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"net/http" |
| 6 |
|
| 7 |
"github.com/gorilla/securecookie" |
| 8 |
) |
| 9 |
|
| 10 |
type CookieHandler struct { |
| 11 |
securecookie *securecookie.SecureCookie |
| 12 |
secureCookieFunc func(r *http.Request) (*securecookie.SecureCookie, error) |
| 13 |
secureOnly bool |
| 14 |
sameSite http.SameSite |
| 15 |
maxAge int |
| 16 |
domain string |
| 17 |
path string |
| 18 |
} |
| 19 |
|
| 20 |
func NewCookieHandler(hashKey, encryptKey []byte, opts ...CookieHandlerOpt) *CookieHandler { |
| 21 |
c := &CookieHandler{ |
| 22 |
securecookie: securecookie.New(hashKey, encryptKey), |
| 23 |
secureOnly: true, |
| 24 |
sameSite: http.SameSiteLaxMode, |
| 25 |
path: "/", |
| 26 |
} |
| 27 |
|
| 28 |
for _, opt := range opts { |
| 29 |
opt(c) |
| 30 |
} |
| 31 |
return c |
| 32 |
} |
| 33 |
|
| 34 |
func NewRequestAwareCookieHandler(secureCookieFunc func(r *http.Request) (*securecookie.SecureCookie, error), opts ...CookieHandlerOpt) *CookieHandler { |
| 35 |
c := &CookieHandler{ |
| 36 |
secureCookieFunc: secureCookieFunc, |
| 37 |
secureOnly: true, |
| 38 |
sameSite: http.SameSiteLaxMode, |
| 39 |
path: "/", |
| 40 |
} |
| 41 |
|
| 42 |
for _, opt := range opts { |
| 43 |
opt(c) |
| 44 |
} |
| 45 |
|
| 46 |
return c |
| 47 |
} |
| 48 |
|
| 49 |
type CookieHandlerOpt func(*CookieHandler) |
| 50 |
|
| 51 |
func WithUnsecure() CookieHandlerOpt { |
| 52 |
return func(c *CookieHandler) { |
| 53 |
c.secureOnly = false |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
func WithSameSite(sameSite http.SameSite) CookieHandlerOpt { |
| 58 |
return func(c *CookieHandler) { |
| 59 |
c.sameSite = sameSite |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
func WithMaxAge(maxAge int) CookieHandlerOpt { |
| 64 |
return func(c *CookieHandler) { |
| 65 |
c.maxAge = maxAge |
| 66 |
if c.IsRequestAware() { |
| 67 |
return |
| 68 |
} |
| 69 |
|
| 70 |
c.securecookie.MaxAge(maxAge) |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
func WithDomain(domain string) CookieHandlerOpt { |
| 75 |
return func(c *CookieHandler) { |
| 76 |
c.domain = domain |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
func WithPath(path string) CookieHandlerOpt { |
| 81 |
return func(c *CookieHandler) { |
| 82 |
c.path = path |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
func (c *CookieHandler) CheckCookie(r *http.Request, name string) (string, error) { |
| 87 |
cookie, err := r.Cookie(name) |
| 88 |
if err != nil { |
| 89 |
return "", err |
| 90 |
} |
| 91 |
|
| 92 |
secureCookie := c.securecookie |
| 93 |
if c.IsRequestAware() { |
| 94 |
secureCookie, err = c.secureCookieFunc(r) |
| 95 |
if err != nil { |
| 96 |
return "", err |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
var value string |
| 101 |
if err := secureCookie.Decode(name, cookie.Value, &value); err != nil { |
| 102 |
return "", err |
| 103 |
} |
| 104 |
return value, nil |
| 105 |
} |
| 106 |
|
| 107 |
func (c *CookieHandler) CheckQueryCookie(r *http.Request, name string) (string, error) { |
| 108 |
value, err := c.CheckCookie(r, name) |
| 109 |
if err != nil { |
| 110 |
return "", err |
| 111 |
} |
| 112 |
if value != r.FormValue(name) { |
| 113 |
return "", errors.New(name + " does not compare") |
| 114 |
} |
| 115 |
return value, nil |
| 116 |
} |
| 117 |
|
| 118 |
func (c *CookieHandler) CreateCookie(name, value string) (*http.Cookie, error) { |
| 119 |
encoded, err := c.securecookie.Encode(name, value) |
| 120 |
if err != nil { |
| 121 |
return nil, err |
| 122 |
} |
| 123 |
return &http.Cookie{ |
| 124 |
Name: name, |
| 125 |
Value: encoded, |
| 126 |
Domain: c.domain, |
| 127 |
Path: c.path, |
| 128 |
MaxAge: c.maxAge, |
| 129 |
HttpOnly: true, |
| 130 |
Secure: c.secureOnly, |
| 131 |
SameSite: c.sameSite, |
| 132 |
}, nil |
| 133 |
} |
| 134 |
|
| 135 |
func (c *CookieHandler) CreateSecureCookie(r *http.Request, name, value string) (*http.Cookie, error) { |
| 136 |
secureCookie, err := c.secureCookieFunc(r) |
| 137 |
if err != nil { |
| 138 |
return nil, err |
| 139 |
} |
| 140 |
|
| 141 |
encoded, err := secureCookie.Encode(name, value) |
| 142 |
if err != nil { |
| 143 |
return nil, err |
| 144 |
} |
| 145 |
return &http.Cookie{ |
| 146 |
Name: name, |
| 147 |
Value: encoded, |
| 148 |
Domain: c.domain, |
| 149 |
Path: c.path, |
| 150 |
MaxAge: c.maxAge, |
| 151 |
HttpOnly: true, |
| 152 |
Secure: c.secureOnly, |
| 153 |
SameSite: c.sameSite, |
| 154 |
}, nil |
| 155 |
} |
| 156 |
|
| 157 |
func (c *CookieHandler) SetCookie(w http.ResponseWriter, name, value string) error { |
| 158 |
if c.IsRequestAware() { |
| 159 |
return errors.New("cookie handler is request aware") |
| 160 |
} |
| 161 |
|
| 162 |
cookie, err := c.CreateCookie(name, value) |
| 163 |
if err != nil { |
| 164 |
return err |
| 165 |
} |
| 166 |
|
| 167 |
http.SetCookie(w, cookie) |
| 168 |
return nil |
| 169 |
} |
| 170 |
|
| 171 |
func (c *CookieHandler) SetRequestAwareCookie(r *http.Request, w http.ResponseWriter, name string, value string) error { |
| 172 |
if !c.IsRequestAware() { |
| 173 |
return errors.New("cookie handler is not request aware") |
| 174 |
} |
| 175 |
|
| 176 |
cookie, err := c.CreateSecureCookie(r, name, value) |
| 177 |
if err != nil { |
| 178 |
return err |
| 179 |
} |
| 180 |
|
| 181 |
http.SetCookie(w, cookie) |
| 182 |
return nil |
| 183 |
} |
| 184 |
|
| 185 |
func (c *CookieHandler) DeleteCookie(w http.ResponseWriter, name string) { |
| 186 |
http.SetCookie(w, &http.Cookie{ |
| 187 |
Name: name, |
| 188 |
Value: "", |
| 189 |
Domain: c.domain, |
| 190 |
Path: c.path, |
| 191 |
MaxAge: -1, |
| 192 |
HttpOnly: true, |
| 193 |
Secure: c.secureOnly, |
| 194 |
SameSite: c.sameSite, |
| 195 |
}) |
| 196 |
} |
| 197 |
|
| 198 |
func (c *CookieHandler) IsRequestAware() bool { |
| 199 |
return c.secureCookieFunc != nil |
| 200 |
} |