sessions

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2017 License: MIT Imports: 9 Imported by: 4

README

Use cookie as session, base on secure cookie to encrypt cookie, you can also use the another library instead of it.

Build Status Coverage Status License GoDoc

Features

  • Simple API: use it as an easy way to set signed cookies.
  • Built-in backends to store sessions in cookies.
  • Mechanism to rotate authentication by some custom keys.
  • Multiple sessions per request, even using different backends.
  • Interfaces and infrastructure for custom session backends: sessions from different stores can be retrieved and batch-saved using a common API.
  • User can customize own session with different field that don't require type assertion and cast

Installation

go get github.com/go-http-utils/cookie-session

Examples

go run example/main.go

Usage

// Session is custom by user's business
type Session struct {
  *sessions.Meta `json:"-"`
  UserID         string `json:"userId"`
  Name           string `json:"name"`
  Authed         int64  `json:"authed"`
}

func (s *Session) Save() error {
  return s.GetStore().Save(s)
}

func (s *Session) Destroy() error {
  return s.GetStore().Destroy(s)
}

func main() {
  SessionName := "Sess"
  SessionKeys := []string{"keyxxx"}

  store := sessions.New()

  handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
      session := &Session{Meta: &sessions.Meta{}}
    store.Load(SessionName, session, cookie.New(w, r, SessionKeys...))
    if session.UserID == "" {
      session.UserID = "x"
      session.Name = "y"
      session.Authed = 1
      session.Save()
    }
  })

Other Store Implementations

Other Application Implementations

Documentation

Index

Constants

View Source
const Version = "1.0.0"

Version is this package's version

Variables

This section is empty.

Functions

func Decode

func Decode(value string, dst interface{}) (err error)

Decode the value to dst .

func Encode

func Encode(value interface{}) (str string, err error)

Encode the value by Serializer and Base64

func NewSID

func NewSID(val string) string

NewSID generates a random SID

Types

type CookieStore

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

CookieStore stores sessions using secure cookies.

func New

func New(options ...*Options) (store *CookieStore)

New returns an CookieStore instance

func (*CookieStore) Destroy added in v1.1.0

func (c *CookieStore) Destroy(session Sessions) (err error)

Destroy destroy the session

func (*CookieStore) Load

func (c *CookieStore) Load(name string, session Sessions, cookie *cookie.Cookies) error

Load a session by name and any kind of stores

func (*CookieStore) Save

func (c *CookieStore) Save(session Sessions) (err error)

Save session to Response's cookie

type MemoryStore

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

MemoryStore using memory to store sessions base on secure cookies.

func NewMemoryStore

func NewMemoryStore(options ...*Options) (store *MemoryStore)

NewMemoryStore returns an MemoryStore instance

func (*MemoryStore) Close added in v1.1.0

func (m *MemoryStore) Close()

Close goroutine cleanCache thread

func (*MemoryStore) Destroy

func (m *MemoryStore) Destroy(session Sessions) (err error)

Destroy destroy the session

func (*MemoryStore) Len

func (m *MemoryStore) Len() int

Len ...

func (*MemoryStore) Load

func (m *MemoryStore) Load(name string, session Sessions, cookie *cookie.Cookies) error

Load a session by name and any kind of stores

func (*MemoryStore) Save

func (m *MemoryStore) Save(session Sessions) (err error)

Save session to Response's cookie

type Meta

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

Meta stores the values and optional configuration for a session.

func (*Meta) GetCookie

func (s *Meta) GetCookie() *cookie.Cookies

GetCookie returns the session' cookie

func (*Meta) GetName

func (s *Meta) GetName() string

GetName returns the name used to register the session

func (*Meta) GetSID

func (s *Meta) GetSID() string

GetSID returns the session' sid

func (*Meta) GetStore

func (s *Meta) GetStore() Store

GetStore returns the session store used to register the session

func (*Meta) Init

func (s *Meta) Init(name, sid string, c *cookie.Cookies, store Store, lastValue string)

Init sets current cookie.Cookies and Store to the session instance.

func (*Meta) IsChanged

func (s *Meta) IsChanged(val string) bool

IsChanged to check current session's value whether is changed

func (*Meta) IsNew

func (s *Meta) IsNew() bool

IsNew to check the current session whether it's new user

type Options

type Options struct {
	Path     string
	Domain   string
	MaxAge   int
	Secure   bool
	HTTPOnly bool
}

Options stores configuration for a session or session store.

Fields are a subset of http.Cookie fields.

type Sessions

type Sessions interface {
	// Init sets current cookie.Cookies and Store to the session instance.
	Init(name, sid string, c *cookie.Cookies, store Store, lastValue string)
	// GetSID returns the session' sid
	GetSID() string
	// GetName returns the session' name
	GetName() string
	// GetStore returns the session' store
	GetStore() Store
	// GetCookie returns the session' cookie
	GetCookie() *cookie.Cookies
	// IsChanged to check current session's value whether is changed
	IsChanged(val string) bool
	// IsNew to check the current session whether it's new user
	IsNew() bool
}

Sessions ...

type Store

type Store interface {
	// Load should load data from cookie and store, set it into session instance.
	// error indicates that session validation failed, or other thing.
	// Sessions.Init should be called in Load, even if error occured.
	Load(name string, session Sessions, cookie *cookie.Cookies) error
	// Save should persist session to the underlying store implementation.
	Save(session Sessions) error
	// Destroy should destroy the session.
	Destroy(session Sessions) error
}

Store is an interface for custom session stores.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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