Documentation ¶
Overview ¶
Simple server side sessions for goji
goji.Use(Sessions.Middleware())
In-memory session store:
var secret = "thisismysecret" var inMemorySessionStore = sessions.MemoryStore{} var Sessions = sessions.NewSessionOptions(secret, &inMemorySessionStore)
Using Redis (using fzzy/radix):
var redisSessionStore = sessions.NewRedisStore("tcp", "localhost:6379") var Sessions = sessions.NewSessionOptions(secret, redisSessionStore)
Use middleware:
goji.Use(Sessions.Middleware())
Accessing session variable:
func handler(c web.C, w http.ResponseWriter, r *http.Request) { sessionObj := Sessions.GetSessionObject(&c) // Regnerate session.. Sessions.RegenerateSession(&c) // Delete session Sessions.DeleteSession(&c) }
See examples folder for full example.
Index ¶
- Variables
- func CookieFromString(line string) (*http.Cookie, error)
- func GenerateRandomString(n int) string
- func GetSignature(message, secret string) string
- func NewCookie(name, value string, options *CookieOptions) *http.Cookie
- func SignMessage(message, secret string) string
- func UnsignMessage(signedMessage, secret string) (string, error)
- type CookieOptions
- type MemoryStore
- type RedisStore
- type SessionOptions
- func (s SessionOptions) CreateNewSession(c *web.C)
- func (s SessionOptions) DestroySession(c *web.C, w http.ResponseWriter)
- func (s SessionOptions) GetSessionId(c *web.C) string
- func (s SessionOptions) GetSessionObject(c *web.C) map[string]interface{}
- func (s SessionOptions) GetValueFromCookie(r *http.Request) (string, error)
- func (s SessionOptions) Middleware() web.MiddlewareType
- func (s SessionOptions) RegenerateSession(c *web.C, w http.ResponseWriter)
- func (s SessionOptions) RemoveCookie(c *web.C, w http.ResponseWriter)
- func (s SessionOptions) RetrieveOrCreateSession(c *web.C, r *http.Request)
- func (s SessionOptions) SaveSession(c *web.C)
- func (s SessionOptions) SetCookie(c *web.C, w http.ResponseWriter)
- func (s SessionOptions) UpdateCookie(c *web.C, w http.ResponseWriter, value string, maxAge int)
- type Store
Constants ¶
This section is empty.
Variables ¶
var InvalidMessageError = errors.New("Invalid Message.")
var NotFoundError = errors.New("Session Object Not Found.")
var SessionIdNotFound = errors.New("Session Id Not Found.")
Functions ¶
func CookieFromString ¶
Reverse http.Cookie.String(), Taken from: http://play.golang.org/p/YkW_z2CSyE
func GenerateRandomString ¶
Generates random string of length n
func GetSignature ¶
func NewCookie ¶
func NewCookie(name, value string, options *CookieOptions) *http.Cookie
NewCookie returns an http.Cookie with the options set. It also sets the Expires field calculated based on the MaxAge value, for Internet Explorer compatibility. Adapted from gorilla/sessions
func UnsignMessage ¶
Returns unsigned message. If signed message is invalid, returns InvalidMessageError
Types ¶
type CookieOptions ¶
type CookieOptions struct { Path string MaxAge int // Setting to less than 0 deletes cookie HttpOnly bool // Prevents JavaScript access to cookies Secure bool // Cookie wil only be transmitted over SSL/TLS. }
Options used to create http.Cookie
type MemoryStore ¶
Simple in-memory session.Store
func (MemoryStore) Destroy ¶
func (m MemoryStore) Destroy(key string)
func (MemoryStore) Save ¶
func (m MemoryStore) Save(key string, object map[string]interface{})
type RedisStore ¶
func NewRedisStore ¶
func NewRedisStore(network, addr string) *RedisStore
func (RedisStore) Destroy ¶
func (r RedisStore) Destroy(key string)
func (RedisStore) Save ¶
func (r RedisStore) Save(key string, object map[string]interface{})
type SessionOptions ¶
type SessionOptions struct { Name string Secret string ObjEnvKey string SidEnvKey string Store Store CookieOptions *CookieOptions }
Configuration Options for Sessions.
func NewSessionOptions ¶
func NewSessionOptions(secret string, store Store) *SessionOptions
Helper to create SessionOptions object with sensible defaults.
func (SessionOptions) CreateNewSession ¶
func (s SessionOptions) CreateNewSession(c *web.C)
Create new session id and session obj.
func (SessionOptions) DestroySession ¶
func (s SessionOptions) DestroySession(c *web.C, w http.ResponseWriter)
Destroy session. Will be regenerated next request.
func (SessionOptions) GetSessionId ¶
func (s SessionOptions) GetSessionId(c *web.C) string
Get session id from context.
func (SessionOptions) GetSessionObject ¶
func (s SessionOptions) GetSessionObject(c *web.C) map[string]interface{}
Get session object from context.
func (SessionOptions) GetValueFromCookie ¶
func (s SessionOptions) GetValueFromCookie(r *http.Request) (string, error)
Get session id from request cookie. Returns SessionIdNotFound error if not found.
func (SessionOptions) Middleware ¶
func (s SessionOptions) Middleware() web.MiddlewareType
Returns session middleware.
func (SessionOptions) RegenerateSession ¶
func (s SessionOptions) RegenerateSession(c *web.C, w http.ResponseWriter)
Regenerate session. Destroys current session and assigns a completely new session id.
func (SessionOptions) RemoveCookie ¶
func (s SessionOptions) RemoveCookie(c *web.C, w http.ResponseWriter)
Removes cookie by setting value to "", maxAge to -1 and to expired.
func (SessionOptions) RetrieveOrCreateSession ¶
func (s SessionOptions) RetrieveOrCreateSession(c *web.C, r *http.Request)
Initialise session object.
func (SessionOptions) SaveSession ¶
func (s SessionOptions) SaveSession(c *web.C)
Persist Session to Store.
func (SessionOptions) SetCookie ¶
func (s SessionOptions) SetCookie(c *web.C, w http.ResponseWriter)
Set session cookie on response.
func (SessionOptions) UpdateCookie ¶
func (s SessionOptions) UpdateCookie(c *web.C, w http.ResponseWriter, value string, maxAge int)
type Store ¶
type Store interface { // Given the session id, returns the session object associated with it, if exists. // Otherwise, returns NotFoundError. Get(key string) (map[string]interface{}, error) // Persists the session object to the store. Save(key string, object map[string]interface{}) // Remove session object from the store. Destroy(key string) }