sessions

package module
v0.0.0-...-49b61c0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 21, 2015 License: MIT Imports: 16 Imported by: 0

README

Sessions

Simple server-side sessions for goji.

goji.Use(Sessions.Middleware())

Dependencies

Usage

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.

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

Constants

This section is empty.

Variables

View Source
var InvalidMessageError = errors.New("Invalid Message.")
View Source
var NotFoundError = errors.New("Session Object Not Found.")
View Source
var SessionIdNotFound = errors.New("Session Id Not Found.")

Functions

func CookieFromString

func CookieFromString(line string) (*http.Cookie, error)

Reverse http.Cookie.String(), Taken from: http://play.golang.org/p/YkW_z2CSyE

func GenerateRandomString

func GenerateRandomString(n int) string

Generates random string of length n

func GetSignature

func GetSignature(message, secret string) string

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 SignMessage

func SignMessage(message, secret string) string

Returns <message>.<signature>

func UnsignMessage

func UnsignMessage(signedMessage, secret string) (string, error)

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

type MemoryStore map[string]map[string]interface{}

Simple in-memory session.Store

func (MemoryStore) Destroy

func (m MemoryStore) Destroy(key string)

func (MemoryStore) Get

func (m MemoryStore) Get(key string) (map[string]interface{}, error)

func (MemoryStore) Save

func (m MemoryStore) Save(key string, object map[string]interface{})

type RedisStore

type RedisStore struct {
	Prefix     string
	Network    string
	Address    string
	ClientPool *pool.Pool
}

func NewRedisStore

func NewRedisStore(network, addr string) *RedisStore

func (RedisStore) Destroy

func (r RedisStore) Destroy(key string)

func (RedisStore) Get

func (r RedisStore) Get(key string) (map[string]interface{}, error)

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)
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL