Documentation ¶
Index ¶
- type Sessiondeprecated
- type SessionCookie
- type SessionManager
- func (s *SessionManager) Clear(ctx context.Context) error
- func (s *SessionManager) Commit(ctx context.Context) (string, time.Time, error)
- func (s *SessionManager) Destroy(ctx context.Context) error
- func (s *SessionManager) Exists(ctx context.Context, key string) bool
- func (s *SessionManager) Get(ctx context.Context, key string) interface{}
- func (s *SessionManager) GetBool(ctx context.Context, key string) bool
- func (s *SessionManager) GetBytes(ctx context.Context, key string) []byte
- func (s *SessionManager) GetFloat(ctx context.Context, key string) float64
- func (s *SessionManager) GetInt(ctx context.Context, key string) int
- func (s *SessionManager) GetString(ctx context.Context, key string) string
- func (s *SessionManager) GetTime(ctx context.Context, key string) time.Time
- func (s *SessionManager) GetToken(ctx context.Context) (string, error)
- func (s *SessionManager) Keys(ctx context.Context) []string
- func (s *SessionManager) Load(ctx context.Context, token string) (context.Context, error)
- func (s *SessionManager) LoadAndSave(next http.Handler) http.Handler
- func (s *SessionManager) Pop(ctx context.Context, key string) interface{}
- func (s *SessionManager) PopBool(ctx context.Context, key string) bool
- func (s *SessionManager) PopBytes(ctx context.Context, key string) []byte
- func (s *SessionManager) PopFloat(ctx context.Context, key string) float64
- func (s *SessionManager) PopInt(ctx context.Context, key string) int
- func (s *SessionManager) PopString(ctx context.Context, key string) string
- func (s *SessionManager) PopTime(ctx context.Context, key string) time.Time
- func (s *SessionManager) Put(ctx context.Context, key string, val interface{})
- func (s *SessionManager) Remove(ctx context.Context, key string)
- func (s *SessionManager) RenewToken(ctx context.Context) error
- func (s *SessionManager) Status(ctx context.Context) Status
- type Status
- type Store
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Session
deprecated
type Session = SessionManager
Deprecated: Session is a backwards-compatible alias for SessionManager.
type SessionCookie ¶
type SessionCookie struct { // Name sets the name of the session cookie. It should not contain // whitespace, commas, colons, semicolons, backslashes, the equals sign or // control characters as per RFC6265. The default cookie name is "session". // If your application uses two different sessions, you must make sure that // the cookie name for each is unique. Name string // 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 // 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 // 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 // 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. // See https://github.com/OWASP/CheatSheetSeries/blob/master/cheatsheets/Session_Management_Cheat_Sheet.md#transport-layer-security. Secure bool }
SessionCookie contains the configuration settings for session cookies.
type SessionManager ¶
type SessionManager struct { // IdleTimeout controls the maximum length of time a session can be inactive // before it expires. For example, some applications may wish to set this so // there is a timeout after 20 minutes of inactivity. By default IdleTimeout // is not set and there is no inactivity timeout. IdleTimeout time.Duration // Lifetime controls 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 // Store controls the session store where the session data is persisted. Store Store // Cookie contains the configuration settings for session cookies. Cookie SessionCookie // contains filtered or unexported fields }
SessionManager holds the configuration settings for your sessions.
func New ¶
func New() *SessionManager
New returns a new session manager with the default options. It is safe for concurrent use.
func NewSession
deprecated
func NewSession() *SessionManager
Deprecated: NewSession is a backwards-compatible alias for New. Use the New function instead.
func (*SessionManager) Clear ¶
func (s *SessionManager) Clear(ctx context.Context) error
Clear removes all data for the current session. The session token and lifetime are unaffected. If there is no data in the current session this is a no-op.
func (*SessionManager) Commit ¶
Commit saves the session data to the session store and returns the session token and expiry time.
Most applications will use the LoadAndSave() middleware and will not need to use this method.
func (*SessionManager) Destroy ¶
func (s *SessionManager) Destroy(ctx context.Context) error
Destroy deletes the session data from the session store and sets the session status to Destroyed. Any further operations in the same request cycle will result in a new session being created.
func (*SessionManager) Exists ¶
func (s *SessionManager) Exists(ctx context.Context, key string) bool
Exists returns true if the given key is present in the session data.
func (*SessionManager) Get ¶
func (s *SessionManager) Get(ctx context.Context, key string) interface{}
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") }
Also see the GetString(), GetInt(), GetBytes() and other helper methods which wrap the type conversion for common types.
func (*SessionManager) GetBool ¶
func (s *SessionManager) GetBool(ctx context.Context, key string) bool
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 (*SessionManager) GetBytes ¶
func (s *SessionManager) GetBytes(ctx context.Context, key string) []byte
GetBytes returns the byte slice ([]byte) value for a given key 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 (*SessionManager) GetFloat ¶
func (s *SessionManager) GetFloat(ctx context.Context, key string) float64
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 (*SessionManager) GetInt ¶
func (s *SessionManager) GetInt(ctx context.Context, key string) int
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 (*SessionManager) GetString ¶
func (s *SessionManager) GetString(ctx context.Context, key string) string
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 (*SessionManager) 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 (*SessionManager) GetToken ¶ added in v2.1.4
func (s *SessionManager) GetToken(ctx context.Context) (string, error)
GetToken retrieves the session token for current session, and returns a token string.
func (*SessionManager) Keys ¶
func (s *SessionManager) Keys(ctx context.Context) []string
Keys returns a slice of all key names present in the session data, sorted alphabetically. If the data contains no data then an empty slice will be returned.
func (*SessionManager) Load ¶
Load retrieves the session data for the given token from the session store, and returns a new context.Context containing the session data. If no matching token is found then this will create a new session.
Most applications will use the LoadAndSave() middleware and will not need to use this method.
func (*SessionManager) LoadAndSave ¶
func (s *SessionManager) LoadAndSave(next http.Handler) http.Handler
LoadAndSave provides middleware which automatically loads and saves session data for the current request, and communicates the session token to and from the client in a cookie.
func (*SessionManager) Pop ¶
func (s *SessionManager) Pop(ctx context.Context, key string) interface{}
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 session data status will be set to Modified. The return value has the type interface{} so will usually need to be type asserted before you can use it.
func (*SessionManager) PopBool ¶
func (s *SessionManager) PopBool(ctx context.Context, key string) bool
PopBool returns the bool value for a given key and then deletes it from the session data. The session data status will be set to Modified. 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 (*SessionManager) PopBytes ¶
func (s *SessionManager) PopBytes(ctx context.Context, key string) []byte
PopBytes returns the byte slice ([]byte) value for a given key and then deletes it from the from the session data. The session data status will be set to Modified. The zero value for a slice (nil) is returned if the key does not exist or could not be type asserted to []byte.
func (*SessionManager) PopFloat ¶
func (s *SessionManager) PopFloat(ctx context.Context, key string) float64
PopFloat returns the float64 value for a given key and then deletes it from the session data. The session data status will be set to Modified. 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 (*SessionManager) PopInt ¶
func (s *SessionManager) PopInt(ctx context.Context, key string) int
PopInt returns the int value for a given key and then deletes it from the session data. The session data status will be set to Modified. 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 (*SessionManager) PopString ¶
func (s *SessionManager) PopString(ctx context.Context, key string) string
PopString returns the string value for a given key and then deletes it from the session data. The session data status will be set to Modified. 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 (*SessionManager) PopTime ¶
PopTime returns the time.Time value for a given key and then deletes it from the session data. The session data status will be set to Modified. 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.
func (*SessionManager) Put ¶
func (s *SessionManager) Put(ctx context.Context, key string, val interface{})
Put adds a key and corresponding value to the session data. Any existing value for the key will be replaced. The session data status will be set to Modified.
func (*SessionManager) Remove ¶
func (s *SessionManager) Remove(ctx context.Context, key string)
Remove deletes the given key and corresponding value from the session data. The session data status will be set to Modified. If the key is not present this operation is a no-op.
func (*SessionManager) RenewToken ¶
func (s *SessionManager) RenewToken(ctx context.Context) error
RenewToken updates the session data to have a new session token while retaining the current session data. The session lifetime is also reset and the session data status will be set to Modified.
The old session token and accompanying data are deleted from the session store.
To mitigate the risk of session fixation attacks, it's important that you call RenewToken before making any changes to privilege levels (e.g. login and logout operations). See https://github.com/OWASP/CheatSheetSeries/blob/master/cheatsheets/Session_Management_Cheat_Sheet.md#renew-the-session-id-after-any-privilege-level-change for additional information.
type Status ¶
type Status int
Status represents the state of the session data during a request cycle.
const ( // Unmodified indicates that the session data hasn't been changed in the // current request cycle. Unmodified Status = iota // Modified indicates that the session data has been changed in the current // request cycle. Modified // Destroyed indicates that the session data has been destroyed in the // current request cycle. Destroyed )
type Store ¶
type Store interface { // Delete should remove the session token and corresponding data from the // session store. If the token does not exist then Delete should be a no-op // and return nil (not an error). Delete(token string) (err error) // Find should return the data for a session token from the store. If the // session token is not found or is expired, the found return value should // be false (and the err return value should be nil). Similarly, tampered // or malformed tokens should result in a found return value of false and a // nil err value. The err return value should be used for system errors only. Find(token string) (b []byte, found bool, err error) // Commit should add the session token and data to the store, with the given // expiry time. If the session token already exists, then the data and // expiry time should be overwritten. Commit(token string, b []byte, expiry time.Time) (err error) }
Store is the interface for session stores.