Skip to content

Commit

Permalink
improve New function
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Feb 23, 2017
1 parent 3175355 commit 569c392
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 23 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Advanced cookie library for Go, support signed cookies.

## API

### cookie.New(w http.ResponseWriter, r *http.Request[, keys []string])
### cookie.New(w http.ResponseWriter, r *http.Request[, keys ...string])
It returns a Cookies instance with optional keygrip for signed cookies.

### cookies.Set(name, val string[, opts *Options])
Expand Down Expand Up @@ -46,7 +46,7 @@ import (

func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
cookies := cookie.New(w, r, []string{"some key"})
cookies := cookie.New(w, r, "some key")

cookies.Set("test", "some cookie", &cookie.Options{
Signed: true,
Expand Down
13 changes: 4 additions & 9 deletions cookie.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ func SetHash(fn func(key, data string) []byte) {
hasher = fn
}

// New returns a Cookies instance with optional keygrip for signed cookies.
func New(w http.ResponseWriter, r *http.Request, keys ...[]string) (cookie *Cookies) {
// New returns a Cookies instance with optional keys for signed cookies.
func New(w http.ResponseWriter, r *http.Request, keys ...string) (cookie *Cookies) {
c := &Cookies{
req: r,
w: w,
}
if len(keys) > 0 && len(keys[0]) > 0 {
c.keys = keys[0]
if len(keys) > 0 {
c.keys = keys
}
return c
}
Expand Down Expand Up @@ -139,11 +139,6 @@ func (c *Cookies) Set(name, val string, options ...*Options) *Cookies {
return c
}

// keygrip uses for signing and verifying data through a rotating credential system.
type keygrip struct {
keys []string
}

// sign creates a summary with data and sha1 algorithm
func sign(key, data string) string {
return base64.RawURLEncoding.EncodeToString(hasher(key, data))
Expand Down
22 changes: 11 additions & 11 deletions cookie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ func TestCookie(t *testing.T) {
cookievalue := "xxxxx"
recorder := httptest.NewRecorder()
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
cookies := New(w, r, []string{})
cookies := New(w, r, []string{}...)
assert.Nil(cookies.keys)

cookies = New(w, r, keys)
cookies = New(w, r, keys...)
cookies.Set(cookiekey, cookievalue, &Options{
Signed: true,
HTTPOnly: true,
Expand Down Expand Up @@ -85,7 +85,7 @@ func TestCookie(t *testing.T) {
cookievalue := "xxxxx"
recorder := httptest.NewRecorder()
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
cookies := New(w, r, keys)
cookies := New(w, r, keys...)
cookies.Set(cookiekey, cookievalue, &Options{
Signed: false,
})
Expand All @@ -108,7 +108,7 @@ func TestCookie(t *testing.T) {
cookievalue := "xxxxx"
recorder := httptest.NewRecorder()
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
cookies := New(w, r, keys)
cookies := New(w, r, keys...)
opts := &Options{
Signed: true,
MaxAge: -1,
Expand All @@ -132,7 +132,7 @@ func TestCookie(t *testing.T) {

handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
t.Log(r.Cookies())
cookies := New(w, r, keys)
cookies := New(w, r, keys...)

val, err := cookies.Get(cookiekey + "cc")
assert.NotNil(err)
Expand Down Expand Up @@ -161,7 +161,7 @@ func TestCookie(t *testing.T) {

handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
t.Log(r.Cookies())
cookies := New(w, r, keys)
cookies := New(w, r, keys...)
val, err := cookies.Get(cookiekey, true)
assert.Equal(err.Error(), "invalid signed cookie")
assert.NotEqual(val, cookievalue)
Expand Down Expand Up @@ -218,7 +218,7 @@ func TestCookie(t *testing.T) {
cookievalue := "xxxxx"
recorder := httptest.NewRecorder()
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
cookies := New(w, r, keys)
cookies := New(w, r, keys...)
opts := &Options{
Signed: true,
MaxAge: -1,
Expand All @@ -243,7 +243,7 @@ func TestCookie(t *testing.T) {

handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
t.Log(r.Cookies())
cookies := New(w, r, keys)
cookies := New(w, r, keys...)
val, err := cookies.Get(cookiekey, true)
assert.Nil(err)
assert.Equal(val, cookievalue)
Expand All @@ -270,7 +270,7 @@ func TestCookie(t *testing.T) {

handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
t.Log(r.Cookies())
cookies := New(w, r, keys)
cookies := New(w, r, keys...)
val, err := cookies.Get(cookiekey, true)
assert.Equal(err.Error(), "invalid signed cookie")
assert.NotEqual(val, cookievalue)
Expand All @@ -291,7 +291,7 @@ func TestPillarjsCookie(t *testing.T) {
req.Header.Set("Cookie", "cookieKey=cookie value; cookieKey.sig=JROAKAAIUzC3_akvMb7PKF4l5h4")
recorder := httptest.NewRecorder()
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
cookies := New(w, r, keys)
cookies := New(w, r, keys...)
val, err := cookies.Get("cookieKey")
assert.Nil(err)
assert.Equal("cookie value", val)
Expand All @@ -311,7 +311,7 @@ func TestPillarjsCookie(t *testing.T) {
req.Header.Set("Cookie", "cookieKey=cookie value1; cookieKey.sig=JROAKAAIUzC3_akvMb7PKF4l5h4")
recorder := httptest.NewRecorder()
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
cookies := New(w, r, keys)
cookies := New(w, r, keys...)
val, err := cookies.Get("cookieKey")
assert.Nil(err)
assert.Equal("cookie value1", val)
Expand Down
2 changes: 1 addition & 1 deletion example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
cookies := cookie.New(w, r, []string{"some key"})
cookies := cookie.New(w, r, "some key")

cookies.Set("test", "some cookie", &cookie.Options{
Signed: true,
Expand Down

0 comments on commit 569c392

Please sign in to comment.