sqlsess

package module
v0.0.0-...-663c99b Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2014 License: Unlicense Imports: 6 Imported by: 0

README

sqlsess is a simple package for managing database backed sessions which can be used as a gorilla/sessions.Store.

  • A single cookie is used to track session IDs. Cookies are AES encrypted by default and verified with HMAC.
  • A simple key-value session table is used. Keys are added or changed with a standard gorilla/sessions.Session value.
  • Stale sessions can be removed after a specified amount of time by running the Clean method with any timeout.

Installation

go get github.com/BurntSushi/sqlsess

Beta

This package is emphatically in BETA. Although most of the API is determined by the gorilla/sessions package, there are still some pieces that may need some changing. (For example, to allow key rotation.)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	SessionLastUpdated = "__sess_last_updated"
	CookieIdName       = "sess_sessionid"
	SqlTableName       = "sess_session"
	SqlCreateSession   = `
	CREATE TABLE IF NOT EXISTS ` + SqlTableName + ` (
		id BYTEA NOT NULL,
		name VARCHAR (255) NOT NULL,
		key TEXT NOT NULL,
		value TEXT NOT NULL,
		PRIMARY KEY (id, name, key)
	)
	`
)

Functions

func Lock

func Lock(sess *sessions.Session)

func RLock

func RLock(sess *sessions.Session)

func RUnlock

func RUnlock(sess *sessions.Session)

func Unlock

func Unlock(sess *sessions.Session)

Types

type Store

type Store struct {
	*sql.DB
	// contains filtered or unexported fields
}

func Open

func Open(db *sql.DB) (*Store, error)

func (*Store) Clean

func (s *Store) Clean(inactive time.Duration) error

func (*Store) Delete

func (s *Store) Delete(sess *sessions.Session) error

func (*Store) Get

func (s *Store) Get(r *http.Request, name string) (*sessions.Session, error)

func (*Store) New

func (s *Store) New(r *http.Request, name string) (*sessions.Session, error)

func (*Store) Save

func (s *Store) Save(
	r *http.Request,
	w http.ResponseWriter,
	sess *sessions.Session,
) error

func (*Store) SetKeys

func (s *Store) SetKeys(hash, block []byte)

SetKeys sets the hash and block keys used to read and write the session cookie. A hash key is required and is used to to authenticate a cookie value using HMAC. It's recommend to be 32 or 64 bytes.

A block key is optional and is used to encrypt the cookie value. If it's set to nil, then encryption will not be used. This package uses AES, so the block key must have length 16, 24 or 32 bytes corresponding to AES-128, AES-192 or AES-256. If the block key violates these constraints, SetKeys will panic.

This method is exposed so that multiple instantiations of session stores can share the same cookie. This particularly useful if you want to be able to restart your web server without invalidating existing user sessions.

If this method is not called, then a fresh set of keys is created automatically, but will invalidate all existing user sessions.

Jump to

Keyboard shortcuts

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