-
Notifications
You must be signed in to change notification settings - Fork 0
/
cookie.go
56 lines (51 loc) · 1.39 KB
/
cookie.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package config
import (
"encoding/json"
"net/http"
"os"
"time"
)
// Cookie contains the fields needed to set and retrieve cookies.
// Cookie names are valid tokens as defined by RFC 2616 section 2.2:
// http://tools.ietf.org/html/rfc2616#section-2.2
// TL;DR: Any non-control or non-separator character.
type Cookie struct {
Age time.Duration `json:"age"`
Domain string `json:"domain"`
HttpOnly bool `json:"http_only"`
Name string `json:"name"`
Path string `json:"path"`
Secure bool `json:"secure"`
}
// Set calls http.SetCookie using the current Cookie config
func (c Cookie) Set(w http.ResponseWriter, value string, expires time.Time) {
cookie := &http.Cookie{
Name: c.Name,
Value: value,
Path: c.Path,
Domain: c.Domain,
Expires: expires,
HttpOnly: c.HttpOnly,
Secure: c.Secure,
}
http.SetCookie(w, cookie)
}
// DefaultCookie is a default CookieConfig implementation. It expires after
// two weeks and is not very secure.
var DefaultCookie = Cookie{
Age: 14 * 24 * time.Hour, // Two weeks
Domain: "",
HttpOnly: false,
Name: "sessionid",
Path: "/",
Secure: false,
}
// ParseCookiePath will create a Cookie using the given filepath.
func ParseCookiePath(path string) (c Cookie, err error) {
f, err := os.Open(path)
if err != nil {
return
}
err = json.NewDecoder(f).Decode(&c)
return
}