session

package module
v0.0.0-...-eba0b30 Latest Latest
Warning

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

Go to latest
Published: May 17, 2015 License: BSD-2-Clause Imports: 12 Imported by: 1

README

Easy to use sessions for use with the Go http package.

GoDoc

Documentation

Overview

Package session implements a simple session handler for use with the Go http package.

Index

Constants

View Source
const TimeStampFormat = "2006-01-02 15:04:05.000"

Variables

View Source
var ErrNotFound = errors.New("no session found")

SessionStorage implementations should return ErrNotFound when Get() finds no associated session.

Functions

This section is empty.

Types

type BoltStore

type BoltStore struct {
	// contains filtered or unexported fields
}

BoltStore is a session storage using bolt.

func NewBoltStore

func NewBoltStore(db *bolt.DB, maxAge time.Duration) (*BoltStore, error)

NewBoltStore returns a BoltStore SessionStorage.

func (*BoltStore) Close

func (s *BoltStore) Close() error

Close the Store, will also close the bolt db.

func (*BoltStore) Commit

func (s *BoltStore) Commit(ses *Session) error

Commit session back to storage.

func (*BoltStore) Delete

func (s *BoltStore) Delete(ses *Session) error

Delete session from storage.

func (*BoltStore) GC

func (s *BoltStore) GC() error

GC one pass over the BoltStore

func (*BoltStore) Get

func (s *BoltStore) Get(sid string) (*Session, error)

Get session associated with sid.

type MemoryStore

type MemoryStore struct {
	// contains filtered or unexported fields
}

MemoryStore is a session storage that operates entirely in memory suitable for testing and small scale uses.

func NewMemoryStore

func NewMemoryStore(maxAge time.Duration) (*MemoryStore, error)

NewMemoryStore returns a MemoryStore SessionStorage.

func (*MemoryStore) Close

func (s *MemoryStore) Close() error

Close the MemoryStore

func (*MemoryStore) Commit

func (s *MemoryStore) Commit(ses *Session) error

Commit session back to storage.

func (*MemoryStore) Delete

func (s *MemoryStore) Delete(ses *Session) error

Delete session from storage.

func (*MemoryStore) GC

func (s *MemoryStore) GC() error

GC one pass over the MemoryStore

func (*MemoryStore) Get

func (s *MemoryStore) Get(sid string) (*Session, error)

Get session associated with sid.

type MySQLStore

type MySQLStore struct {
	// contains filtered or unexported fields
}

MySQLStore is a session storage for a MySQL database.

func NewMySQLStore

func NewMySQLStore(db *sql.DB, tablename string, maxAge time.Duration) (*MySQLStore, error)

NewMySQLStore creates a MySQLStore SessionStorage using the given database and tablename. The table will be created if it does not exist.

func (*MySQLStore) Close

func (s *MySQLStore) Close() error

Close the MySQLStore

func (*MySQLStore) Commit

func (s *MySQLStore) Commit(ses *Session) error

Commit session back to storage.

func (*MySQLStore) Delete

func (s *MySQLStore) Delete(ses *Session) error

Delete session from storage.

func (*MySQLStore) GC

func (s *MySQLStore) GC() error

GC one pass over the MySQLStore

func (*MySQLStore) Get

func (s *MySQLStore) Get(sid string) (*Session, error)

Get session associated with sid.

type Session

type Session struct {
	sync.RWMutex

	// Available for external use at your own risk.
	Values map[string]string
	// contains filtered or unexported fields
}

Session may be used concurrently, but should only be used in conjunction with a single HTTP request.

func (*Session) ActionToken

func (s *Session) ActionToken() string

ActionToken will return a token which can be embedded into forms to prevent cross site request attacks.

func (*Session) CanAct

func (s *Session) CanAct() bool

CanAct checks the current action token against the token in the request. Expects a form value named "actionToken". Returns true if it's a real request.

func (*Session) Clear

func (s *Session) Clear()

Clear existing session data leaving a new one.

func (*Session) Commit

func (s *Session) Commit() error

Commit the session back to storage. MUST be called at the end of each request.

func (*Session) Get

func (s *Session) Get(key string) string

Get returns the session variable associated with key or an empty string if not found.

func (*Session) NewActionToken

func (s *Session) NewActionToken() string

NewActionToken resets the action token, should be used after each checked action is performed.

func (*Session) Set

func (s *Session) Set(key string, value string)

Set a session variable.

type SessionManager

type SessionManager struct {
	// Set true to require Secure cookies
	Secure bool

	sync.RWMutex
	// contains filtered or unexported fields
}

SessionManager type, use NewSessionManager() to create.

func NewSessionManager

func NewSessionManager(storage SessionStorage, cookieName string) (*SessionManager, error)

NewSessionManager will initialize the sessions system. Expects a previously created SessionStorage and the name of the http cookie to use.

Once created, SessionManager.Secure can be set to force secure cookies.

func (*SessionManager) Begin

func (sm *SessionManager) Begin(w http.ResponseWriter, req *http.Request) (*Session, error)

Begin using a session. Returns a session, resuming an existing session if possible and creating a new session if necessary.

func (*SessionManager) Close

func (sm *SessionManager) Close() error

Close the session manager, ending the gc loop and doing whatever cleanup the storage manager demands.

func (*SessionManager) SetGCDelay

func (sm *SessionManager) SetGCDelay(delay time.Duration) error

SetGCDelay is used to configure time between purging expired sessions. Default is every hour.

type SessionStorage

type SessionStorage interface {
	/*
		Return a session associated with sid. Only the Values map is expected
		to exist. Return ErrNotFound if no associated session with sid is found.
	*/
	Get(sid string) (*Session, error)

	/*
		Commit a session back into storage
	*/
	Commit(session *Session) error

	/*
		Delete a session from storage. NOP if the session isn't in storage,
		only returns an error if something goes seriously wrong.
	*/
	Delete(session *Session) error

	/*
		Will be called periodically(see SetGCDelay()) to clean up expired
		sessions
	*/
	GC() error

	/*
		Close the session storage peforming whatever cleanup is necessary.
	*/
	Close() error
}

SessionStorage interface is used and required by SessionManager.

Sessions passed as parameters can be used concurrently. All methods except Close() must be able to function concurrently.

Jump to

Keyboard shortcuts

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