session

package module
v0.0.12 Latest Latest
Warning

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

Go to latest
Published: May 16, 2019 License: MIT Imports: 5 Imported by: 0

README

Development

This library is currently under development. Being ported out of an project specific library this is not quite ready for being imported.

Current Support/Road Map

  • File
  • Firestore
  • Memcache
  • Memory
  • MongoDB
  • MySQL
  • PostgreSQL
  • Redis
  • Encryption
  • Cookie Sessions
  • Header Sessions

Usage

Additional samples can be seen in manager_test.go

Basic session manager implementation

m := &Manager{
  Provider:    [PROVIDER],
  Lifetime:    session.DefaultSessionExpiration, // default 1200
}

If you would like to use the HeaderLoad or CookieLoad convenience methods specify the header key and type/prefix of the header key/cookie name.

m := &Manager{
  TokenName:   HeaderKey, // default Authorization
  TokenType:   HeaderPrefix, // default bearer
  Provider:    [PROVIDER],
  Lifetime:    session.DefaultSessionExpiration, // default 1200
}

Providers

Memory

Memory provider is a simple key/value map held in memory for the duration of the execution of your application.

p := session.NewMemoryProvider()

MySQL

MySQL provider stores the session data in a MySQL database. The MySQLProvider takes a db connection and the name of the table to store the session data.

db, err := sql.Open("mysql", "[CONNECTION_STRING]")
... err checks and defer close handling ...
p, err := session.NewMySQLProvider(db, session.DefaultMySQLTableName)
if err != nil {
  log.Fatal(err)
}

Redis

Redis provider stores the session data in a Redis store. The RedisProvider takes the host, password, and db id.

p, _ := session.NewRedisProvider("[HOST]", "[PASSWORD]", [DATABASE])

Firestore

Firestore provider stores the session data in a Firestore database. It takes the current context, a firestore client, and the collection name

ctx := context.Background()

auth := option.WithCredentialsJSON([]byte("[FIRESTORE_CONFIG]"))
firebaseApp, err := firebase.NewApp(ctx, nil, auth)
... error checks and handling ...
fc, err := firebaseApp.Firestore(ctx)
... error checks and handling ...

p := session.NewFirestoreProvider(ctx, fc, "[COLLECTION_NAME]")

Documentation

Index

Constants

View Source
const (
	DefaultHeaderKey  = "Authorization"
	DefaultHeaderType = "Bearer"

	ErrorLoad = "session load error"
)

session_type

Variables

View Source
var DefaultManager = Manager{
	KeyName: DefaultHeaderKey,
	KeyType: DefaultHeaderType,
	Provider: &session.MemoryProvider{
		Store: map[string]*session.Session{},
	},
	Lifetime: session.DefaultSessionExpiration,
}
View Source
var TestConfig = &struct {
	RedisHost       string
	RedisPassword   string
	MySQLDatasource string
	FirestoreCreds  string
}{
	RedisHost:       "localhost:6379",
	RedisPassword:   "",
	MySQLDatasource: "[USERNAME]:[PASSWORD]@tcp([HOST]:3306)/[DATABASE]?parseTime=true",
	FirestoreCreds:  `{}`,
}

Functions

This section is empty.

Types

type Manager

type Manager struct {
	KeyName  string
	KeyType  string
	Provider session.Provider
	Lifetime int64
}

func (*Manager) CookieLoad

func (m *Manager) CookieLoad(r *http.Request) (*session.Session, error)

func (*Manager) Destroy

func (m *Manager) Destroy(sess *session.Session) error

func (*Manager) Expiration

func (m *Manager) Expiration() time.Time

func (*Manager) Extend

func (m *Manager) Extend(sess *session.Session)

func (*Manager) GarbageCollect

func (m *Manager) GarbageCollect()

func (*Manager) HeaderLoad

func (m *Manager) HeaderLoad(r *http.Request) (*session.Session, error)

Convenience method for pulling the session off the request header

func (*Manager) Load

func (m *Manager) Load(sid string) (*session.Session, error)

func (*Manager) NewSession

func (m *Manager) NewSession() *session.Session

func (*Manager) NewSessionWithId

func (m *Manager) NewSessionWithId(id string) *session.Session

func (*Manager) Save

func (m *Manager) Save(sess *session.Session) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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