Documentation
¶
Overview ¶
Package sessions provides a minimalist and lightweight HTTP session cookie implementation for Go. Session cookies are encrypted and authenticated using nacl/secretbox.
Example usage:
package main import ( "net/http" "time" "github.com/golangcollege/sessions" ) var session *sessions.Session var secret = []byte("u46IpCV9y5Vlur8YvODJEhgOY8m9JVE4") func main() { session = sessions.New(secret) session.Lifetime = 3 * time.Hour mux := http.NewServeMux() mux.HandleFunc("/put", putHandler) mux.HandleFunc("/get", getHandler) http.ListenAndServe(":4000", session.Enable(mux)) } func putHandler(w http.ResponseWriter, r *http.Request) { session.Put(r, "msg", "Hello world") w.WriteHeader(200) } func getHandler(w http.ResponseWriter, r *http.Request) { msg := session.GetString(r, "msg") w.Write([]byte(msg)) }
Index ¶
- Variables
- func MockRequest(r *http.Request) *http.Request
- type Session
- func (s *Session) Destroy(r *http.Request)
- func (s *Session) Enable(next http.Handler) http.Handler
- func (s *Session) Exists(r *http.Request, key string) bool
- func (s *Session) Get(r *http.Request, key string) interface{}
- func (s *Session) GetBool(r *http.Request, key string) bool
- func (s *Session) GetBytes(r *http.Request, key string) []byte
- func (s *Session) GetFloat(r *http.Request, key string) float64
- func (s *Session) GetInt(r *http.Request, key string) int
- func (s *Session) GetString(r *http.Request, key string) string
- func (s *Session) GetTime(r *http.Request, key string) time.Time
- func (s *Session) Keys(r *http.Request) []string
- func (s *Session) Pop(r *http.Request, key string) interface{}
- func (s *Session) PopBool(r *http.Request, key string) bool
- func (s *Session) PopBytes(r *http.Request, key string) []byte
- func (s *Session) PopFloat(r *http.Request, key string) float64
- func (s *Session) PopInt(r *http.Request, key string) int
- func (s *Session) PopString(r *http.Request, key string) string
- func (s *Session) PopTime(r *http.Request, key string) time.Time
- func (s *Session) Put(r *http.Request, key string, val interface{})
- func (s *Session) Remove(r *http.Request, key string)
Constants ¶
This section is empty.
Variables ¶
var ErrCookieTooLong = errors.New("session: cookie length greater than 4096 bytes")
Functions ¶
Types ¶
type Session ¶
type Session struct { // Domain sets the 'Domain' attribute on the session cookie. By default // it will be set to the domain name that the cookie was issued from. Domain string // HttpOnly sets the 'HttpOnly' attribute on the session cookie. The // default value is true. HttpOnly bool // Lifetime sets the maximum length of time that a session is valid for // before it expires. The lifetime is an 'absolute expiry' which is set when // the session is first created and does not change. The default value is 24 // hours. Lifetime time.Duration // Path sets the 'Path' attribute on the session cookie. The default value // is "/". Passing the empty string "" will result in it being set to the // path that the cookie was issued from. Path string // Persist sets whether the session cookie should be persistent or not // (i.e. whether it should be retained after a user closes their browser). // The default value is true, which means that the session cookie will not // be destroyed when the user closes their browser and the appropriate // 'Expires' and 'MaxAge' values will be added to the session cookie. Persist bool // Secure sets the 'Secure' attribute on the session cookie. The default // value is false. It's recommended that you set this to true and serve all // requests over HTTPS in production environments. Secure bool // SameSite controls the value of the 'SameSite' attribute on the session // cookie. By default this is set to 'SameSite=Lax'. If you want no SameSite // attribute or value in the session cookie then you should set this to 0. SameSite http.SameSite // ErrorHandler allows you to control behaviour when an error is encountered // loading or writing the session cookie. By default the client is sent a // generic "500 Internal Server Error" response and the actual error message // is logged using the standard logger. If a custom ErrorHandler function is // provided then control will be passed to this instead. ErrorHandler func(http.ResponseWriter, *http.Request, error) // contains filtered or unexported fields }
Session holds the configuration settings that you want to use for your sessions.
func New ¶
New initializes a new Session object to hold the configuration settings for your sessions.
The key parameter is the secret you want to use to authenticate and encrypt session cookies. It should be exactly 32 bytes long.
Optionally, the variadic oldKeys parameter can be used to provide an arbitrary number of old Keys. This can be used to ensure that valid cookies continue to work correctly after key rotation.
func (*Session) Destroy ¶
Destroy deletes the current session. The session data is deleted from memory and the client is instructed to delete the session cookie.
Any further operations on the session data *within the same request cycle* will result in a panic.
func (*Session) Enable ¶
Enable is middleware which loads and saves session data to and from the session cookie. You should use this middleware to wrap ALL handlers which need to access to the session data. A common way to do this is to wrap your servemux.
Note that session cookies are only sent to the client when the session data has been modified.
func (*Session) Get ¶
Get returns the value for a given key from the session data. The return value has the type interface{} so will usually need to be type asserted before you can use it. For example:
foo, ok := session.Get(r, "foo").(string) if !ok { return errors.New("type assertion to string failed") }
Note: Alternatives are the GetString(), GetInt(), GetBytes() and other helper methods which wrap the type conversion for common types.
func (*Session) GetBool ¶
GetBool returns the bool value for a given key from the session data. The zero value for a bool (false) is returned if the key does not exist or the value could not be type asserted to a bool.
func (*Session) GetBytes ¶
GetBytes returns the byte slice ([]byte) value for a given key from the session cache. The zero value for a slice (nil) is returned if the key does not exist or could not be type asserted to []byte.
func (*Session) GetFloat ¶
GetFloat returns the float64 value for a given key from the session data. The zero value for an float64 (0) is returned if the key does not exist or the value could not be type asserted to a float64.
func (*Session) GetInt ¶
GetInt returns the int value for a given key from the session data. The zero value for an int (0) is returned if the key does not exist or the value could not be type asserted to an int.
func (*Session) GetString ¶
GetString returns the string value for a given key from the session data. The zero value for a string ("") is returned if the key does not exist or the value could not be type asserted to a string.
func (*Session) GetTime ¶
GetTime returns the time.Time value for a given key from the session data. The zero value for a time.Time object is returned if the key does not exist or the value could not be type asserted to a time.Time. This can be tested with the time.IsZero() method.
func (*Session) Keys ¶
Keys returns a slice of all key names present in the session data, sorted alphabetically. If the cache contains no data then an empty slice will be returned.
func (*Session) Pop ¶
Pop acts like a one-time Get. It returns the value for a given key from the session data and deletes the key and value from the session data. The return value has the type interface{} so will usually need to be type asserted before you can use it.
func (*Session) PopBool ¶
PopBool returns the bool value for a given key and then deletes it from the session data. The zero value for a bool (false) is returned if the key does not exist or the value could not be type asserted to a bool.
func (*Session) PopBytes ¶
PopBytes returns the byte slice ([]byte) value for a given key and then deletes it from the from the session data. The zero value for a slice (nil) is returned if the key does not exist or could not be type asserted to []byte.
func (*Session) PopFloat ¶
PopFloat returns the float64 value for a given key and then deletes it from the session data. The zero value for an float64 (0) is returned if the key does not exist or the value could not be type asserted to a float64.
func (*Session) PopInt ¶
PopInt returns the int value for a given key and then deletes it from the session data. The zero value for an int (0) is returned if the key does not exist or the value could not be type asserted to an int.
func (*Session) PopString ¶
PopString returns the string value for a given key and then deletes it from the session data. The zero value for a string ("") is returned if the key does not exist or the value could not be type asserted to a string.
func (*Session) PopTime ¶
PopTime returns the time.Time value for a given key and then deletes it from the session data. The zero value for a time.Time object is returned if the key does not exist or the value could not be type asserted to a time.Time.