session

package
v0.2.15 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2020 License: Apache-2.0 Imports: 25 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

SLogger a helpful variable to log information about session

Functions

func DecodeGob

func DecodeGob(encoded []byte) (map[interface{}]interface{}, error)

DecodeGob decode data to map

func EncodeGob

func EncodeGob(obj map[interface{}]interface{}) ([]byte, error)

EncodeGob encode the obj to gob

func NewSession

func NewSession(driver string, config *ManagerConfig) (err error)

NewSession 注册session

func Register

func Register(name string, provide Provider)

Register makes a session provide available by the provided name. If Register is called twice with the same name or if driver is nil, it panics.

Types

type BlankSessionStore

type BlankSessionStore struct {
}

BlankSessionStore 空session驱动 用于未开启session时相关操作,避免空指针异常

func (*BlankSessionStore) Delete

func (st *BlankSessionStore) Delete(key interface{}) (err error)

func (*BlankSessionStore) Flush

func (st *BlankSessionStore) Flush() (err error)

func (*BlankSessionStore) Get

func (st *BlankSessionStore) Get(key interface{}) (value interface{})

func (*BlankSessionStore) SessionID

func (st *BlankSessionStore) SessionID() (sid string)

func (*BlankSessionStore) SessionRelease

func (st *BlankSessionStore) SessionRelease(w http.ResponseWriter) (err error)

func (*BlankSessionStore) Set

func (st *BlankSessionStore) Set(key, value interface{}) (err error)

type CookieProvider

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

CookieProvider Cookie session provider

func (*CookieProvider) SessionAll

func (pder *CookieProvider) SessionAll() int

SessionAll Implement method, return 0.

func (*CookieProvider) SessionDestroy

func (pder *CookieProvider) SessionDestroy(sid string) error

SessionDestroy Implement method, no used.

func (*CookieProvider) SessionExist

func (pder *CookieProvider) SessionExist(sid string) bool

SessionExist Cookie session is always existed

func (*CookieProvider) SessionGC

func (pder *CookieProvider) SessionGC()

SessionGC Implement method, no used.

func (*CookieProvider) SessionInit

func (pder *CookieProvider) SessionInit(maxlifetime int64, config string) error

SessionInit Init cookie session provider with max lifetime and config json. maxlifetime is ignored. json config:

securityKey - hash string
blockKey - gob encode hash string. it's saved as aes crypto.
securityName - recognized name in encoded cookie string
cookieName - cookie name
maxage - cookie max life time.

func (*CookieProvider) SessionRead

func (pder *CookieProvider) SessionRead(sid string, r *http.Request) (Store, error)

SessionRead Get SessionStore in cooke. decode cooke string to map and put into SessionStore with sid.

func (*CookieProvider) SessionRegenerate

func (pder *CookieProvider) SessionRegenerate(oldsid, sid string) (Store, error)

SessionRegenerate Implement method, no used.

func (*CookieProvider) SessionUpdate

func (pder *CookieProvider) SessionUpdate(sid string) error

SessionUpdate Implement method, no used.

type CookieSessionStore

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

CookieSessionStore Cookie SessionStore

func (*CookieSessionStore) Delete

func (st *CookieSessionStore) Delete(key interface{}) error

Delete value in cookie session

func (*CookieSessionStore) Flush

func (st *CookieSessionStore) Flush() error

Flush Clean all values in cookie session

func (*CookieSessionStore) Get

func (st *CookieSessionStore) Get(key interface{}) interface{}

Get value from cookie session

func (*CookieSessionStore) SessionID

func (st *CookieSessionStore) SessionID() string

SessionID Return id of this cookie session

func (*CookieSessionStore) SessionRelease

func (st *CookieSessionStore) SessionRelease(w http.ResponseWriter) error

SessionRelease Write cookie session to http response cookie

func (*CookieSessionStore) Set

func (st *CookieSessionStore) Set(key, value interface{}) error

Set value to cookie session. the value are encoded as gob with hash block string.

type Log

type Log struct {
	*log.Logger
}

Log implement the log.Logger

func NewSessionLog

func NewSessionLog(out io.Writer) *Log

NewSessionLog set io.Writer to create a Logger for session.

type Manager

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

Manager contains Provider and its configuration.

func NewManager

func NewManager(provideName string, cf *ManagerConfig) (*Manager, error)

NewManager Create new Manager with provider name and json config string. provider name: 1. cookie 2. redis

func (*Manager) GC

func (manager *Manager) GC()

GC Start session gc process. it can do gc in times after gc lifetime.

func (*Manager) GetActiveSession

func (manager *Manager) GetActiveSession() int

GetActiveSession Get all active sessions count number.

func (*Manager) GetProvider

func (manager *Manager) GetProvider() Provider

GetProvider return current manager's provider

func (*Manager) GetSessionStore

func (manager *Manager) GetSessionStore(sid string, r *http.Request) (sessions Store, err error)

GetSessionStore Get SessionStore by its id.

func (*Manager) SessionDestroy

func (manager *Manager) SessionDestroy(w http.ResponseWriter, r *http.Request)

SessionDestroy Destroy session by its id in http request cookie.

func (*Manager) SessionRegenerateID

func (manager *Manager) SessionRegenerateID(w http.ResponseWriter, r *http.Request) (session Store)

SessionRegenerateID Regenerate a session id for this SessionStore who's id is saving in http request.

func (*Manager) SessionStart

func (manager *Manager) SessionStart(w http.ResponseWriter, r *http.Request) (session Store, err error)

SessionStart generate or read the session id from http request. if session id exists, return SessionStore with this id.

func (*Manager) SetSecure

func (manager *Manager) SetSecure(secure bool)

SetSecure Set cookie with https.

type ManagerConfig

type ManagerConfig struct {
	CookieName              string `json:"cookieName"`
	EnableSetCookie         bool   `json:"enableSetCookie,omitempty"`
	Gclifetime              int64  `json:"gclifetime"`
	Maxlifetime             int64  `json:"maxLifetime"`
	DisableHTTPOnly         bool   `json:"disableHTTPOnly"`
	Secure                  bool   `json:"secure"`
	CookieLifeTime          int    `json:"cookieLifeTime"`
	ProviderConfig          string `json:"providerConfig"`
	Domain                  string `json:"domain"`
	SessionIDLength         int64  `json:"sessionIDLength"`
	EnableSidInHTTPHeader   bool   `json:"EnableSidInHTTPHeader"`
	SessionNameInHTTPHeader string `json:"SessionNameInHTTPHeader"`
	EnableSidInURLQuery     bool   `json:"EnableSidInURLQuery"`
	SessionIDPrefix         string `json:"sessionIDPrefix"`
}

ManagerConfig define the session config

type Provider

type Provider interface {
	SessionInit(gclifetime int64, config string) error
	SessionRead(sid string, r *http.Request) (Store, error)
	SessionExist(sid string) bool
	SessionRegenerate(oldsid, sid string) (Store, error)
	SessionDestroy(sid string) error
	SessionAll() int //get all active session
	SessionGC()
}

Provider contains global session methods and saved SessionStores. it can operate a SessionStore by its id.

func GetProvider

func GetProvider(name string) (Provider, error)

GetProvider

type RedisProvider added in v0.1.7

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

func (*RedisProvider) SessionAll added in v0.1.7

func (pder *RedisProvider) SessionAll() int

SessionAll Implement method, return 0.

func (*RedisProvider) SessionDestroy added in v0.1.7

func (pder *RedisProvider) SessionDestroy(sid string) error

SessionDestroy Implement method, no used.

func (*RedisProvider) SessionExist added in v0.1.7

func (pder *RedisProvider) SessionExist(sid string) bool

SessionExist Redis session is always existed

func (*RedisProvider) SessionGC added in v0.1.7

func (pder *RedisProvider) SessionGC()

SessionGC Implement method, no used.

func (*RedisProvider) SessionInit added in v0.1.7

func (pder *RedisProvider) SessionInit(maxlifetime int64, config string) error

func (*RedisProvider) SessionRead added in v0.1.7

func (pder *RedisProvider) SessionRead(sid string, r *http.Request) (Store, error)

func (*RedisProvider) SessionRegenerate added in v0.1.7

func (pder *RedisProvider) SessionRegenerate(oldsid, sid string) (Store, error)

SessionRegenerate Implement method, no used.

type RedisSessionStore added in v0.1.7

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

RedisSessionStore session存储到redis中

func (*RedisSessionStore) Delete added in v0.1.7

func (st *RedisSessionStore) Delete(key interface{}) (err error)

func (*RedisSessionStore) Flush added in v0.1.7

func (st *RedisSessionStore) Flush() (err error)

func (*RedisSessionStore) Get added in v0.1.7

func (st *RedisSessionStore) Get(key interface{}) (value interface{})

func (*RedisSessionStore) SessionID added in v0.1.7

func (st *RedisSessionStore) SessionID() (sid string)

func (*RedisSessionStore) SessionRelease added in v0.1.7

func (st *RedisSessionStore) SessionRelease(w http.ResponseWriter) (err error)

func (*RedisSessionStore) Set added in v0.1.7

func (st *RedisSessionStore) Set(key, value interface{}) (err error)

type SessionHandler

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

func (*SessionHandler) Get

func (s *SessionHandler) Get(key interface{}) interface{}

func (*SessionHandler) Set

func (s *SessionHandler) Set(key, value interface{}) error

type Store

type Store interface {
	Set(key, value interface{}) error           //set session value
	Get(key interface{}) interface{}            //get session value
	Delete(key interface{}) error               //delete session value
	SessionID() string                          //back current sessionID
	SessionRelease(w http.ResponseWriter) error // release the resource & save data to provider & return the data
	Flush() error                               //delete all data
}

Store contains all data for one session process with specific id.

func SessionStart

func SessionStart(writer http.ResponseWriter, request *http.Request) (sessHandler Store, err error)

SessionStart 启动session

Jump to

Keyboard shortcuts

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