sqlsessionstores

package
v0.0.0-...-e7b361f Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2018 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package sqlsessionstores provides a session store backed by an SQL database. Supported dialects are PostgreSQL and SQLite.

You have to import the appropriate SQL driver yourself, e.g.:

import _ "github.com/lib/pq"           // for PostgreSQL, or:
import _ "github.com/mattn/go-sqlite3" // for SQLite

Index

Constants

View Source
const (
	// AuthMethodCookie means the session ID is passed via cookie.
	AuthMethodCookie = authMethod("cookie")

	// AuthMethodHeader means the session ID is passed via request header.
	AuthMethodHeader = authMethod("header")
)
View Source
const (
	DialectPostgreSQL = "postgresql"
	DialectSQLite     = "sqlite"
)

Supported SQL dialects.

Variables

View Source
var KeyUserID = "user.id"

KeyUserID is the key used to retrieve the user ID from session.Values and store it in an indexed table column. This makes it possible to delete all sessions of a particular user.

Functions

This section is empty.

Types

type AuthOptions

type AuthOptions struct {
	AuthMethod authMethod

	// Cookie… fields are used when setting the cookie.
	CookieDomain string
	CookieName   string
	CookiePath   string

	// HeaderName is the name of the request header that is used to pass the
	// session ID.
	HeaderName string
}

AuthOptions is the authentification configuration for the store. If AuthMethod is AuthMethodCookie, Cookie… options are used. If AuthMethod is AuthMethodHeader, Header… options are used.

type Store

type Store struct {
	// Authentication options.
	AuthOptions AuthOptions

	// DB is the database in which the sessions table resides.
	DB *sql.DB

	// SQL dialect to use.
	Dialect string

	// Expiration is the duration after which sessions expire.
	Expiration time.Duration

	// Strength is the number of bytes to use for generating a session ID. The
	// higher the number, the more secure the session ID.
	Strength int

	// TableName is the name of the sessions table.
	TableName string
}

Store contains information about the session store.

func New

func New(dialect string, db *sql.DB, tableName string) (*Store, error)

New returns a new Store. If a table with the specified name does not exist, it is created.

func (*Store) Delete

func (s *Store) Delete(writer http.ResponseWriter, sessionID string) error

Delete deletes a session from the store.

func (*Store) DeleteMulti

func (s *Store) DeleteMulti(filter *sessions.Filter) error

DeleteMulti deletes sessions from the store that match the criteria specified in filter. If no criterion is specified, all sessions are deleted.

func (*Store) Get

func (s *Store) Get(writer http.ResponseWriter, request *http.Request) (sessions.Session, error)

Get gets a session from the store using the session ID passed with the request via cookie or header (depending on s.AuthOptions.AuthMethod).

func (*Store) GetMulti

func (s *Store) GetMulti(filter *sessions.Filter) ([]sessions.Session, error)

GetMulti gets sessions from the store that match the criteria specified in filter.

func (*Store) Save

func (s *Store) Save(writer http.ResponseWriter, session sessions.Session) error

Save saves a session to the store. If s.AuthOptions.AuthMethod is AuthMethodCookie, it creates or updates the session cookie.

func (*Store) SaveMulti

func (s *Store) SaveMulti(sessions []sessions.Session) (e error)

SaveMulti saves the provided sessions.

Jump to

Keyboard shortcuts

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