Permalink
Browse files

Add other options for session cookies

  • Loading branch information...
1 parent 15e0b56 commit c5e0930a200a0d0e99ad9236fca09835a3fd6f2f @sunfmin committed Mar 26, 2012
Showing with 18 additions and 6 deletions.
  1. +16 −4 sessions.go
  2. +2 −2 sessions_test.go
View
@@ -106,19 +106,31 @@ func prepareSession(env Env, key, secret string) {
env["mango.session"] = make(map[string]interface{})
}
-func commitSession(headers Headers, env Env, key, secret, domain string) {
+func commitSession(headers Headers, env Env, key, secret string, options *CookieOptions) {
cookie := new(http.Cookie)
cookie.Name = key
cookie.Value = encodeCookie(env["mango.session"].(map[string]interface{}), secret)
- cookie.Domain = domain
+ cookie.Path = options.Path
+ cookie.Domain = options.Domain
+ cookie.MaxAge = options.MaxAge
+ cookie.Secure = options.Secure
+ cookie.HttpOnly = options.HttpOnly
headers.Add("Set-Cookie", cookie.String())
}
-func Sessions(secret, key, domain string) Middleware {
+type CookieOptions struct {
+ Domain string
+ Path string
+ MaxAge int
+ Secure bool
+ HttpOnly bool
+}
+
+func Sessions(secret, key string, options *CookieOptions) Middleware {
return func(env Env, app App) (status Status, headers Headers, body Body) {
prepareSession(env, key, secret)
status, headers, body = app(env)
- commitSession(headers, env, key, secret, domain)
+ commitSession(headers, env, key, secret, options)
return status, headers, body
}
}
View
@@ -32,7 +32,7 @@ func TestSessions(t *testing.T) {
// Compile the stack
sessionsStack := new(Stack)
- sessionsStack.Middleware(Sessions("my_secret", "my_key", ".my.domain.com"))
+ sessionsStack.Middleware(Sessions("my_secret", "my_key", &CookieOptions{Domain: ".my.domain.com"}))
sessionsApp := sessionsStack.Compile(sessionsTestServer)
// Request against it
@@ -83,7 +83,7 @@ func BenchmarkSessions(b *testing.B) {
}
sessionsStack := new(Stack)
- sessionsStack.Middleware(Sessions("my_secret", "my_key", ".my.domain.com"))
+ sessionsStack.Middleware(Sessions("my_secret", "my_key", &CookieOptions{Domain: ".my.domain.com"}))
sessionsApp := sessionsStack.Compile(sessionsTestServer)
request, _ := http.NewRequest("GET", "http://localhost:3000/", nil)

0 comments on commit c5e0930

Please sign in to comment.