Skip to content

Commit 5efac18

Browse files
committed
rename..
1 parent 7127b80 commit 5efac18

File tree

3 files changed

+16
-14
lines changed

3 files changed

+16
-14
lines changed

cookie.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ type Options struct {
3131
}
3232

3333
// 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) {
3535
cookie = &Cookies{
36-
request: req,
37-
writer: res,
36+
req: r,
37+
w: w,
3838
}
3939
var opts *GlobalOptions
4040
if len(options) > 0 {
@@ -55,9 +55,9 @@ func New(res http.ResponseWriter, req *http.Request, options ...*GlobalOptions)
5555

5656
// Cookies is secure cookie base on native cookie
5757
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
6161
}
6262

6363
// 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
6767
opts := options[0]
6868
signed = opts.Signed
6969
if signed && len(c.opts.Keys) == 0 {
70-
panic("Required key for signed cookies")
70+
panic("key required for signed cookies")
7171
}
7272
}
73-
val, err := c.request.Cookie(name)
73+
val, err := c.req.Cookie(name)
7474
if val == nil {
7575
return
7676
}
@@ -79,7 +79,7 @@ func (c *Cookies) Get(name string, options ...*Options) (value string, err error
7979
value = val.Value
8080
if signed {
8181
var sigName = name + ".sig"
82-
oldsignval, _ := c.request.Cookie(sigName)
82+
oldsignval, _ := c.req.Cookie(sigName)
8383
for _, key := range c.opts.Keys {
8484
newsignval := Sign(key, val.Value)
8585
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
8888
break
8989
} else {
9090
value = ""
91-
err = errors.New("The cookie's value have different sign")
91+
err = errors.New("invalid signed cookie")
9292
}
9393
}
9494
}
@@ -132,12 +132,12 @@ func (c *Cookies) Set(name string, val string, options ...*Options) *Cookies {
132132
} else if maxAge < 0 {
133133
cookie.Expires = time.Unix(1, 0)
134134
}
135-
http.SetCookie(c.writer, cookie)
135+
http.SetCookie(c.w, cookie)
136136
if Signed {
137137
signcookie := *cookie
138138
signcookie.Value = Sign(key, val)
139139
signcookie.Name = signcookie.Name + ".sig"
140-
http.SetCookie(c.writer, &signcookie)
140+
http.SetCookie(c.w, &signcookie)
141141
}
142142
return c
143143
}
@@ -149,6 +149,8 @@ func Sign(key string, data string) (sign string) {
149149
sign = base64.RawURLEncoding.EncodeToString(mac.Sum(nil))
150150
return
151151
}
152+
153+
// compatibility for https://github.com/pillarjs/cookies v1.0.x
152154
func signSha1(key string, data string) (sign string) {
153155
mac := hmac.New(sha1.New, []byte(key))
154156
mac.Write([]byte(data))

cookie_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ func TestCookie(t *testing.T) {
176176
Signed: true,
177177
}
178178
val, err := cookies.Get(cookiekey, opts)
179-
assert.Equal(err.Error(), "The cookie's value have different sign")
179+
assert.Equal(err.Error(), "invalid signed cookie")
180180
assert.NotEqual(val, cookievalue)
181181
assert.Equal(val, "")
182182
})
@@ -293,7 +293,7 @@ func TestCookie(t *testing.T) {
293293
Signed: true,
294294
}
295295
val, err := cookies.Get(cookiekey, opts)
296-
assert.Equal(err.Error(), "The cookie's value have different sign")
296+
assert.Equal(err.Error(), "invalid signed cookie")
297297
assert.NotEqual(val, cookievalue)
298298
assert.Equal(val, "")
299299
})
File renamed without changes.

0 commit comments

Comments
 (0)