@@ -31,10 +31,10 @@ type Options struct {
31
31
}
32
32
33
33
// New an operational cookie instance by cookie.GlobalOptions.
34
- func New (res http.ResponseWriter , req * http.Request , options ... * GlobalOptions ) (cookie * Cookies ) {
34
+ func New (w http.ResponseWriter , r * http.Request , options ... * GlobalOptions ) (cookie * Cookies ) {
35
35
cookie = & Cookies {
36
- request : req ,
37
- writer : res ,
36
+ req : r ,
37
+ w : w ,
38
38
}
39
39
var opts * GlobalOptions
40
40
if len (options ) > 0 {
@@ -55,9 +55,9 @@ func New(res http.ResponseWriter, req *http.Request, options ...*GlobalOptions)
55
55
56
56
// Cookies is secure cookie base on native cookie
57
57
type Cookies struct {
58
- request * http.Request
59
- writer http.ResponseWriter
60
- opts * GlobalOptions
58
+ req * http.Request
59
+ w http.ResponseWriter
60
+ opts * GlobalOptions
61
61
}
62
62
63
63
// Get the cookie with the given name from the Cookie header in the request. If such a cookie exists, its value is returned. Otherwise, nothing is returned. { signed: true } can optionally be passed as the second parameter options. In this case, a signature cookie (a cookie of same name ending with the .sig suffix appended) is fetched. If the signature cookie does exist, cookie will check the hash of cookie-value whether matches registered keys.
@@ -67,10 +67,10 @@ func (c *Cookies) Get(name string, options ...*Options) (value string, err error
67
67
opts := options [0 ]
68
68
signed = opts .Signed
69
69
if signed && len (c .opts .Keys ) == 0 {
70
- panic ("Required key for signed cookies" )
70
+ panic ("key required for signed cookies" )
71
71
}
72
72
}
73
- val , err := c .request .Cookie (name )
73
+ val , err := c .req .Cookie (name )
74
74
if val == nil {
75
75
return
76
76
}
@@ -79,7 +79,7 @@ func (c *Cookies) Get(name string, options ...*Options) (value string, err error
79
79
value = val .Value
80
80
if signed {
81
81
var sigName = name + ".sig"
82
- oldsignval , _ := c .request .Cookie (sigName )
82
+ oldsignval , _ := c .req .Cookie (sigName )
83
83
for _ , key := range c .opts .Keys {
84
84
newsignval := Sign (key , val .Value )
85
85
if oldsignval != nil && (newsignval == oldsignval .Value || signSha1 (key , val .Value ) == oldsignval .Value ) {
@@ -88,7 +88,7 @@ func (c *Cookies) Get(name string, options ...*Options) (value string, err error
88
88
break
89
89
} else {
90
90
value = ""
91
- err = errors .New ("The cookie's value have different sign " )
91
+ err = errors .New ("invalid signed cookie " )
92
92
}
93
93
}
94
94
}
@@ -132,12 +132,12 @@ func (c *Cookies) Set(name string, val string, options ...*Options) *Cookies {
132
132
} else if maxAge < 0 {
133
133
cookie .Expires = time .Unix (1 , 0 )
134
134
}
135
- http .SetCookie (c .writer , cookie )
135
+ http .SetCookie (c .w , cookie )
136
136
if Signed {
137
137
signcookie := * cookie
138
138
signcookie .Value = Sign (key , val )
139
139
signcookie .Name = signcookie .Name + ".sig"
140
- http .SetCookie (c .writer , & signcookie )
140
+ http .SetCookie (c .w , & signcookie )
141
141
}
142
142
return c
143
143
}
@@ -149,6 +149,8 @@ func Sign(key string, data string) (sign string) {
149
149
sign = base64 .RawURLEncoding .EncodeToString (mac .Sum (nil ))
150
150
return
151
151
}
152
+
153
+ // compatibility for https://github.com/pillarjs/cookies v1.0.x
152
154
func signSha1 (key string , data string ) (sign string ) {
153
155
mac := hmac .New (sha1 .New , []byte (key ))
154
156
mac .Write ([]byte (data ))
0 commit comments