doc.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // Copyright 2012 The Gorilla Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. /*
  5. Package sessions provides cookie and filesystem sessions and
  6. infrastructure for custom session backends.
  7. The key features are:
  8. * Simple API: use it as an easy way to set signed (and optionally
  9. encrypted) cookies.
  10. * Built-in backends to store sessions in cookies or the filesystem.
  11. * Flash messages: session values that last until read.
  12. * Convenient way to switch session persistency (aka "remember me") and set
  13. other attributes.
  14. * Mechanism to rotate authentication and encryption keys.
  15. * Multiple sessions per request, even using different backends.
  16. * Interfaces and infrastructure for custom session backends: sessions from
  17. different stores can be retrieved and batch-saved using a common API.
  18. Let's start with an example that shows the sessions API in a nutshell:
  19. import (
  20. "net/http"
  21. "github.com/gorilla/sessions"
  22. )
  23. // Note: Don't store your key in your source code. Pass it via an
  24. // environmental variable, or flag (or both), and don't accidentally commit it
  25. // alongside your code. Ensure your key is sufficiently random - i.e. use Go's
  26. // crypto/rand or securecookie.GenerateRandomKey(32) and persist the result.
  27. var store = sessions.NewCookieStore(os.Getenv("SESSION_KEY"))
  28. func MyHandler(w http.ResponseWriter, r *http.Request) {
  29. // Get a session. Get() always returns a session, even if empty.
  30. session, err := store.Get(r, "session-name")
  31. if err != nil {
  32. http.Error(w, err.Error(), http.StatusInternalServerError)
  33. return
  34. }
  35. // Set some session values.
  36. session.Values["foo"] = "bar"
  37. session.Values[42] = 43
  38. // Save it before we write to the response/return from the handler.
  39. session.Save(r, w)
  40. }
  41. First we initialize a session store calling NewCookieStore() and passing a
  42. secret key used to authenticate the session. Inside the handler, we call
  43. store.Get() to retrieve an existing session or a new one. Then we set some
  44. session values in session.Values, which is a map[interface{}]interface{}.
  45. And finally we call session.Save() to save the session in the response.
  46. Note that in production code, we should check for errors when calling
  47. session.Save(r, w), and either display an error message or otherwise handle it.
  48. Save must be called before writing to the response, otherwise the session
  49. cookie will not be sent to the client.
  50. Important Note: If you aren't using gorilla/mux, you need to wrap your handlers
  51. with context.ClearHandler as or else you will leak memory! An easy way to do this
  52. is to wrap the top-level mux when calling http.ListenAndServe:
  53. http.ListenAndServe(":8080", context.ClearHandler(http.DefaultServeMux))
  54. The ClearHandler function is provided by the gorilla/context package.
  55. That's all you need to know for the basic usage. Let's take a look at other
  56. options, starting with flash messages.
  57. Flash messages are session values that last until read. The term appeared with
  58. Ruby On Rails a few years back. When we request a flash message, it is removed
  59. from the session. To add a flash, call session.AddFlash(), and to get all
  60. flashes, call session.Flashes(). Here is an example:
  61. func MyHandler(w http.ResponseWriter, r *http.Request) {
  62. // Get a session.
  63. session, err := store.Get(r, "session-name")
  64. if err != nil {
  65. http.Error(w, err.Error(), http.StatusInternalServerError)
  66. return
  67. }
  68. // Get the previous flashes, if any.
  69. if flashes := session.Flashes(); len(flashes) > 0 {
  70. // Use the flash values.
  71. } else {
  72. // Set a new flash.
  73. session.AddFlash("Hello, flash messages world!")
  74. }
  75. session.Save(r, w)
  76. }
  77. Flash messages are useful to set information to be read after a redirection,
  78. like after form submissions.
  79. There may also be cases where you want to store a complex datatype within a
  80. session, such as a struct. Sessions are serialised using the encoding/gob package,
  81. so it is easy to register new datatypes for storage in sessions:
  82. import(
  83. "encoding/gob"
  84. "github.com/gorilla/sessions"
  85. )
  86. type Person struct {
  87. FirstName string
  88. LastName string
  89. Email string
  90. Age int
  91. }
  92. type M map[string]interface{}
  93. func init() {
  94. gob.Register(&Person{})
  95. gob.Register(&M{})
  96. }
  97. As it's not possible to pass a raw type as a parameter to a function, gob.Register()
  98. relies on us passing it a value of the desired type. In the example above we've passed
  99. it a pointer to a struct and a pointer to a custom type representing a
  100. map[string]interface. (We could have passed non-pointer values if we wished.) This will
  101. then allow us to serialise/deserialise values of those types to and from our sessions.
  102. Note that because session values are stored in a map[string]interface{}, there's
  103. a need to type-assert data when retrieving it. We'll use the Person struct we registered above:
  104. func MyHandler(w http.ResponseWriter, r *http.Request) {
  105. session, err := store.Get(r, "session-name")
  106. if err != nil {
  107. http.Error(w, err.Error(), http.StatusInternalServerError)
  108. return
  109. }
  110. // Retrieve our struct and type-assert it
  111. val := session.Values["person"]
  112. var person = &Person{}
  113. if person, ok := val.(*Person); !ok {
  114. // Handle the case that it's not an expected type
  115. }
  116. // Now we can use our person object
  117. }
  118. By default, session cookies last for a month. This is probably too long for
  119. some cases, but it is easy to change this and other attributes during
  120. runtime. Sessions can be configured individually or the store can be
  121. configured and then all sessions saved using it will use that configuration.
  122. We access session.Options or store.Options to set a new configuration. The
  123. fields are basically a subset of http.Cookie fields. Let's change the
  124. maximum age of a session to one week:
  125. session.Options = &sessions.Options{
  126. Path: "/",
  127. MaxAge: 86400 * 7,
  128. HttpOnly: true,
  129. }
  130. Sometimes we may want to change authentication and/or encryption keys without
  131. breaking existing sessions. The CookieStore supports key rotation, and to use
  132. it you just need to set multiple authentication and encryption keys, in pairs,
  133. to be tested in order:
  134. var store = sessions.NewCookieStore(
  135. []byte("new-authentication-key"),
  136. []byte("new-encryption-key"),
  137. []byte("old-authentication-key"),
  138. []byte("old-encryption-key"),
  139. )
  140. New sessions will be saved using the first pair. Old sessions can still be
  141. read because the first pair will fail, and the second will be tested. This
  142. makes it easy to "rotate" secret keys and still be able to validate existing
  143. sessions. Note: for all pairs the encryption key is optional; set it to nil
  144. or omit it and and encryption won't be used.
  145. Multiple sessions can be used in the same request, even with different
  146. session backends. When this happens, calling Save() on each session
  147. individually would be cumbersome, so we have a way to save all sessions
  148. at once: it's sessions.Save(). Here's an example:
  149. var store = sessions.NewCookieStore([]byte("something-very-secret"))
  150. func MyHandler(w http.ResponseWriter, r *http.Request) {
  151. // Get a session and set a value.
  152. session1, _ := store.Get(r, "session-one")
  153. session1.Values["foo"] = "bar"
  154. // Get another session and set another value.
  155. session2, _ := store.Get(r, "session-two")
  156. session2.Values[42] = 43
  157. // Save all sessions.
  158. sessions.Save(r, w)
  159. }
  160. This is possible because when we call Get() from a session store, it adds the
  161. session to a common registry. Save() uses it to save all registered sessions.
  162. */
  163. package sessions