Skip to content

Commit c5e0930

Browse files
committed
Add other options for session cookies
1 parent 15e0b56 commit c5e0930

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

sessions.go

+16-4
Original file line numberDiff line numberDiff line change
@@ -106,19 +106,31 @@ func prepareSession(env Env, key, secret string) {
106106
env["mango.session"] = make(map[string]interface{})
107107
}
108108

109-
func commitSession(headers Headers, env Env, key, secret, domain string) {
109+
func commitSession(headers Headers, env Env, key, secret string, options *CookieOptions) {
110110
cookie := new(http.Cookie)
111111
cookie.Name = key
112112
cookie.Value = encodeCookie(env["mango.session"].(map[string]interface{}), secret)
113-
cookie.Domain = domain
113+
cookie.Path = options.Path
114+
cookie.Domain = options.Domain
115+
cookie.MaxAge = options.MaxAge
116+
cookie.Secure = options.Secure
117+
cookie.HttpOnly = options.HttpOnly
114118
headers.Add("Set-Cookie", cookie.String())
115119
}
116120

117-
func Sessions(secret, key, domain string) Middleware {
121+
type CookieOptions struct {
122+
Domain string
123+
Path string
124+
MaxAge int
125+
Secure bool
126+
HttpOnly bool
127+
}
128+
129+
func Sessions(secret, key string, options *CookieOptions) Middleware {
118130
return func(env Env, app App) (status Status, headers Headers, body Body) {
119131
prepareSession(env, key, secret)
120132
status, headers, body = app(env)
121-
commitSession(headers, env, key, secret, domain)
133+
commitSession(headers, env, key, secret, options)
122134
return status, headers, body
123135
}
124136
}

sessions_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func TestSessions(t *testing.T) {
3232

3333
// Compile the stack
3434
sessionsStack := new(Stack)
35-
sessionsStack.Middleware(Sessions("my_secret", "my_key", ".my.domain.com"))
35+
sessionsStack.Middleware(Sessions("my_secret", "my_key", &CookieOptions{Domain: ".my.domain.com"}))
3636
sessionsApp := sessionsStack.Compile(sessionsTestServer)
3737

3838
// Request against it
@@ -83,7 +83,7 @@ func BenchmarkSessions(b *testing.B) {
8383
}
8484

8585
sessionsStack := new(Stack)
86-
sessionsStack.Middleware(Sessions("my_secret", "my_key", ".my.domain.com"))
86+
sessionsStack.Middleware(Sessions("my_secret", "my_key", &CookieOptions{Domain: ".my.domain.com"}))
8787
sessionsApp := sessionsStack.Compile(sessionsTestServer)
8888

8989
request, _ := http.NewRequest("GET", "http://localhost:3000/", nil)

0 commit comments

Comments
 (0)