pkg/http: Add method to set request aware cookies.
Signed-off-by: Mark Laing <mark.laing@canonical.com>
This commit is contained in:
parent
05e528cff0
commit
0a5b6fadbe
1 changed files with 29 additions and 0 deletions
|
@ -118,6 +118,35 @@ func (c *CookieHandler) SetCookie(w http.ResponseWriter, name, value string) err
|
|||
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,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue