Documentation
¶
Index ¶
- Constants
- Variables
- type CookieOptions
- type Manager
- func (m *Manager) Acquire(c context.Context, r, w interface{}) (*Session, error)
- func (m *Manager) NewSession(r, w interface{}) (*Session, error)
- func (m *Manager) SetCookieHooks(getCookie func(string, interface{}) (*http.Cookie, error), ...)
- func (m *Manager) SetSessionIDHooks(generateID func() (string, error), validateID func(string) bool)
- func (m *Manager) UseStore(str Store)
- type Options
- type Session
- func (s *Session) Bool(r interface{}, err error) (bool, error)
- func (s *Session) Bytes(r interface{}, err error) ([]byte, error)
- func (s *Session) Cache() error
- func (s *Session) Clear() error
- func (s *Session) ClearCookie() error
- func (s *Session) Delete(key ...string) error
- func (s *Session) Destroy() error
- func (s *Session) Float64(r interface{}, err error) (float64, error)
- func (s *Session) Get(key string) (interface{}, error)
- func (s *Session) GetAll() (map[string]interface{}, error)
- func (s *Session) GetMulti(key ...string) (map[string]interface{}, error)
- func (s *Session) ID() string
- func (s *Session) Int(r interface{}, err error) (int, error)
- func (s *Session) Int64(r interface{}, err error) (int64, error)
- func (s *Session) ResetCache()
- func (s *Session) Set(key string, val interface{}) error
- func (s *Session) SetMulti(data map[string]interface{}) error
- func (s *Session) String(r interface{}, err error) (string, error)
- func (s *Session) UInt64(r interface{}, err error) (uint64, error)
- func (s *Session) WriteCookie(id string) error
- type Store
Constants ¶
const (
// ContextName is the key used to store session in context passed to acquire method.
ContextName ctxNameType = "_simple_session"
)
Variables ¶
var ( // ErrInvalidSession is raised when session is tried to access before setting it or its not set in store. // Handle this and create new session. // Store code = 1 ErrInvalidSession = errors.New("simplesession: invalid session") // ErrNil is raised when returned value is nil. // Store code = 2 ErrNil = errors.New("simplesession: nil returned") // ErrAssertType is raised when type assertion fails // Store code = 3 ErrAssertType = errors.New("simplesession: invalid type assertion") )
Functions ¶
This section is empty.
Types ¶
type CookieOptions ¶
type CookieOptions struct { // Name sets http cookie name. This is also sent as cookie name in `GetCookie` callback. Name string // Domain sets hostname for the cookie. Domain specifies allowed hosts to receive the cookie. Domain string // Path sets path for the cookie. Path indicates a URL path that must exist in the requested URL in order to send the cookie header. Path string // IsSecure marks the cookie as secure cookie (only sent in HTTPS). IsSecure bool // IsHTTPOnly marks the cookie as http only cookie. JS won't be able to access the cookie so prevents XSS attacks. IsHTTPOnly bool // SameSite sets allows you to declare if your cookie should be restricted to a first-party or same-site context. SameSite http.SameSite // Expires sets absolute expiration date and time for the cookie. // If both Expires and MaxAge are sent then MaxAge takes precedence over Expires. // Cookies without a Max-age or Expires attribute – are deleted when the current session ends // and some browsers use session restoring when restarting. This can cause session cookies to last indefinitely. Expires time.Time // Sets the cookie's expiration in seconds from the current time, internally its rounder off to nearest seconds. // If both Expires and MaxAge are sent then MaxAge takes precedence over Expires. // Cookies without a Max-age or Expires attribute – are deleted when the current session ends // and some browsers use session restoring when restarting. This can cause session cookies to last indefinitely. MaxAge time.Duration }
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager handles the storage and management of HTTP cookies.
func (*Manager) Acquire ¶
Acquire retrieves a `Session` from the store using the current session cookie.
If session not found and `opt.EnableAutoCreate` is true, a new session is created and returned. If session not found and `opt.EnableAutoCreate` is false which is the default, it returns `ErrInvalidSession`.
`r` and `w` are request and response interfaces which is passed back in in GetCookie and SetCookie callbacks. Optionally, a context can be passed to get an already loaded session, useful in middleware chains.
func (*Manager) NewSession ¶
NewSession creates a new `Session` and updates the cookie with a new session ID, replacing any existing session ID if it exists.
func (*Manager) SetCookieHooks ¶
func (m *Manager) SetCookieHooks(getCookie func(string, interface{}) (*http.Cookie, error), setCookie func(*http.Cookie, interface{}) error)
SetCookieHooks cane be used to get and set HTTP cookie for the session.
getCookie hook takes session ID and reader interface and returns http.Cookie and error. In a HTTP request context reader interface will be the http request object and it should obtain http.Cookie from the request object for the given cookie ID.
setCookie hook takes http.Cookie object and a writer interface and returns error. In a HTTP request context the write interface will be the http request object and it should write http request with the incoming cookie.
func (*Manager) SetSessionIDHooks ¶
func (m *Manager) SetSessionIDHooks(generateID func() (string, error), validateID func(string) bool)
SetSessionIDHooks cane be used to generate and validate custom session ID. Bydefault alpha-numeric 32bit length session ID is used if its not set. - Generating custom session ID, which will be uses as the ID for storing sessions in the backend. - Validating custom session ID, which will be used to verify the ID before querying backend.
type Options ¶
type Options struct { // If enabled, Acquire() will always create and return a new session if one doesn't already exist. // If disabled then new session can only be created using NewSession() method. EnableAutoCreate bool // Cookie ID length. Defaults to alphanumeric 32 characters. // Might not be applicable to some stores like SecureCookie. // Also not applicable if custom generateID and validateID is set. SessionIDLength int // Cookie options. Cookie CookieOptions }
Options to configure manager and cookie.
type Session ¶
type Session struct {
// contains filtered or unexported fields
}
Session represents a session object used for retrieving/setting session data and cookies.
func (*Session) Bool ¶
Bool is a helper to get values as Bool. If the value is Nil, ErrNil is returned, which means key doesn't exist.
func (*Session) Bytes ¶
Bytes is a helper to get values as Bytes. If the value is Nil, ErrNil is returned, which means key doesn't exist.
func (*Session) Cache ¶
Cache loads session values into memory for quick access. Ideal for centralized session fetching, e.g., in middleware. Subsequent Get/GetMulti calls return cached values, avoiding store access. Use ResetCache() to ensure GetAll/Get/GetMulti fetches from the store.
func (*Session) Clear ¶
Clear empties the data for the given session id but doesn't clear the cookie. Use `Destroy()` to delete entire session from the store and clear the cookie.
func (*Session) ClearCookie ¶
ClearCookie sets the cookie's expiry to one day prior to clear it.
func (*Session) Float64 ¶
Float64 is a helper to get values as Float64. If the value is Nil, ErrNil is returned, which means key doesn't exist.
func (*Session) Get ¶
Get retrieves a value for the given key in the session. If the session is already loaded, it returns the value from the existing map. Otherwise, it fetches the value from the store.
func (*Session) GetMulti ¶
GetMulti retrieves values for multiple session fields. If a field is not found in the store then its returned as nil.
func (*Session) ID ¶
ID returns the acquired session ID. If cookie is not set then empty string is returned.
func (*Session) Int ¶
Int is a helper to get values as integer. If the value is Nil, ErrNil is returned, which means key doesn't exist.
func (*Session) Int64 ¶
Int64 is a helper to get values as Int64. If the value is Nil, ErrNil is returned, which means key doesn't exist.
func (*Session) ResetCache ¶
func (s *Session) ResetCache()
ResetCache clears loaded values, ensuring subsequent Get, GetAll, and GetMulti calls fetch from the store.
func (*Session) String ¶
String is a helper to get values as String. If the value is Nil, ErrNil is returned, which means key doesn't exist.
func (*Session) UInt64 ¶
UInt64 is a helper to get values as UInt64. If the value is Nil, ErrNil is returned, which means key doesn't exist.
func (*Session) WriteCookie ¶
WriteCookie writes the cookie for the given session ID. Uses all the cookie options set in Manager.
type Store ¶
type Store interface { // Create creates new session in the store for the given session ID. Create(id string) (err error) // Get a value for the given key from session. Get(id, key string) (value interface{}, err error) // GetMulti gets a maps of multiple values for given keys from session. // If some fields are not found then return nil for that field. GetMulti(id string, keys ...string) (data map[string]interface{}, err error) // GetAll gets all key and value from session. GetAll(id string) (data map[string]interface{}, err error) // Set sets an value for a field in session. Set(id, key string, value interface{}) error // Set takes a map of kv pair and set the field in store. SetMulti(id string, data map[string]interface{}) error // Delete a given list of keys from session. Delete(id string, key ...string) error // Clear empties the session but doesn't delete it. Clear(id string) error // Destroy deletes the entire session. Destroy(id string) error // Helper method for typecasting/asserting. // Supposed to be used as a chain. // For example: sess.Int(sess.Get("id", "key")) // Take `error` and returns that if its not nil. // Take `interface{}` value and type assert or convert. // If its nil then return ErrNil. // If it can't type asserted/converted then return ErrAssertType. Int(interface{}, error) (int, error) Int64(interface{}, error) (int64, error) UInt64(interface{}, error) (uint64, error) Float64(interface{}, error) (float64, error) String(interface{}, error) (string, error) Bytes(interface{}, error) ([]byte, error) Bool(interface{}, error) (bool, error) }
Store represents store interface. This interface can be implemented to create various backend stores for session.