session

package
v1.6.1 Latest Latest
Warning

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

Go to latest
Published: May 4, 2016 License: Apache-2.0 Imports: 22 Imported by: 2

README

beegae session

This is based off of the original session module as part of the beego project and can be used as part of the beegae project or as a standalone session manager. read more here

This includes (as of now) only a single session store capable of working on AppEngine (SessionProvider = "appengine")

A few gotchas:

  1. There is no automatic garbage collection! You will have to create a cron job and a custom handler to periodically call on the garbage collection functions.
  2. SessionAll will always return 0. Count queries are limited to 1000 entities and so we cannot reliably get a count. As such, this function was not implemented.
  3. A few methods deviate from the original beego API specification. Specifically, an appengine.Context object is a new parameter for SessionExist, SessionRead, SessionRegenerate, SessionDestroy, and SessionGC. If you are using beegae or use the session manager provided, you do not have to worry about these details.
  4. GetProvider was not implemented (this should have little to no impact)

Example Garbage Collection using beegae:

First, create a new controller:

package controllers

import "github.com/astaxie/beegae"

type GCController struct {
	beegae.Controller
}

func (this *GCController) Get() {
	beegae.GlobalSessions.GC(this.AppEngineCtx)
}

Second, register your controller to a URL Path in your applications init function:

func init() {
	// Register other routers/handlers here
	// ...

	// Register new handler for sessiong garbage collection
	beegae.Router("/_session_gc", &controllers.GCController)

	beegae.Run()
}

Finally, add an entry to your cron.yaml file:

cron:
- description: daily session garbage collection
  url: /_session_gc
  schedule: every day 00:00

You can also add security to this (and any) URL by requiring an Admin login for the URL in your app.yaml:

handlers:
- url: /_session_gc
  login: admin
  script: _go_app

- url: /.*
  script: _go_app

Documentation

Overview

Package session provider

Usage: import(

"github.com/astaxie/beego/session"

)

	func init() {
     globalSessions, _ = session.NewManager("memory", `{"cookieName":"gosessionid", "enableSetCookie,omitempty": true, "gclifetime":3600, "maxLifetime": 3600, "secure": false, "cookieLifeTime": 3600, "providerConfig": ""}`)
		go globalSessions.GC()
	}

more docs: http://beego.me/docs/module/session.md

Index

Constants

This section is empty.

Variables

This section is empty.

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 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 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(c context.Context, sid string) error

SessionDestroy Implement method, no used.

func (*CookieProvider) SessionExist

func (pder *CookieProvider) SessionExist(c context.Context, sid string) bool

SessionExist Cookie session is always existed

func (*CookieProvider) SessionGC

func (pder *CookieProvider) SessionGC(c context.Context)

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(c context.Context, sid string) (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(c context.Context, 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)

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 Manager

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

Manager contains Provider and its configuration.

func NewManager

func NewManager(provideName, config string) (*Manager, error)

NewManager Create new Manager with provider name and json config string. provider name: 1. cookie 2. file 3. memory 4. redis 5. mysql json config: 1. is https default false 2. hashfunc default sha1 3. hashkey default beegosessionkey 4. maxage default is none

func (*Manager) GC

func (manager *Manager) GC(c context.Context)

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) GetSessionStore

func (manager *Manager) GetSessionStore(c context.Context, sid string) (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 added in v1.6.1

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 Provider

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

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

type Store added in v1.6.1

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) // 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.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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