session

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: May 28, 2017 License: MIT Imports: 16 Imported by: 0

README

session

session provides cookie session operation.

The key features are:

  • Simple API: use it as an easy way to set signed (and optionally encrypted) cookies.
  • Built-in backends to store sessions in cookies
  • Authentication and encryption.
  • Multiple sessions per request

Usage

store value in cookie session.

package main

import (
	"github.com/cssivision/looli"
	"github.com/cssivision/looli/session"
	"net/http"
	"fmt"
)

func main() {
	router := looli.Default()

	// secret used to signed cookies, 
	secret := "secret"

	// aesKey used to encrypted the session values, the AES key, either 16, 24, 
	// or 32 bytes to select AES-128, AES-192, or AES-256, if is empty, will 
	// not encrypted session values.
	aesKey := "1111111111111111"
	sessions := session.NewSessions(secret, aesKey)

	router.Get("/a", func(ctx *looli.Context) {
		// Get a session. ignoring the error resulted from decoding an existing 
		// session: Get() always returns a session, even if empty.
		session, err := sessions.Get(ctx, "sess")
		if err != nil {
			fmt.Println("err: ", err)
			return
		}

		// Values is type map[interface{}]interface{}
		if session.Values["looli"] == nil {
			session.Values["looli"] = 1
		} else {
			session.Values["looli"] = session.Values["looli"].(int) + 1
		}

		// Save it before we write to the response/return from the handler.
		err = session.Save(ctx)
		if err != nil {
			fmt.Println(err)
		}

		fmt.Println(session.Values["looli"])
		ctx.String("Hello World!")
	})

	http.ListenAndServe(":8080", router)
}

Licenses

All source code is licensed under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeCookie

func DecodeCookie(hashKey, aesKey []byte, name string, value string, dst interface{}) error

decode decodes a cookie value

It decodes, verifies a message authentication code, and finally deserializes the value.

The name argument is the cookie name. It must be the same name used when it was stored.

The value argument is the encoded cookie value. The dst argument is where the cookie will be decoded. It must be a pointer.

func EncodeCookie

func EncodeCookie(hashKey, aesKey []byte, name string, value interface{}) (string, error)

encode encodes a cookie value.

It serializes, optionally encrypts, signs with a message authentication code, and finally encodes the value.

The name argument is the cookie name. It is stored with the encoded value. The value argument is the value to be encoded. It can be any value that can be encoded using the currently selected serializer

It is the client's responsibility to ensure that value, when encoded using the current serialization/encryption settings on s and then base64-encoded, is shorter than the maximum permissible length.

func NewCookie

func NewCookie(name, value string, options *Options) *http.Cookie

Types

type CookieStore

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

func NewCookieStore

func NewCookieStore(secret, aesKey string) *CookieStore

func (*CookieStore) Get

func (store *CookieStore) Get(req *http.Request, name string) (*Session, error)

func (*CookieStore) New

func (store *CookieStore) New(sid, name string) *Session

func (*CookieStore) Save

func (store *CookieStore) Save(rw http.ResponseWriter, req *http.Request, session *Session) error

type Options

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

type Session

type Session struct {
	Name   string
	Values Values
	Id     string

	Options *Options
	// contains filtered or unexported fields
}

func NewSession

func NewSession(name string, store Store) (session *Session)

func (*Session) Save

func (session *Session) Save(ctx *looli.Context) error

type Sessions

type Sessions struct {
	// Store use to store session, it can be any store that implement the Store interface.
	Store Store
	// contains filtered or unexported fields
}

Sessions is a global session manager

func NewSessions

func NewSessions(secret string, aesKey string) *Sessions

NewSessions create a global sessions manager, default store is Cookie store.

secret used to sign the cookie value.

The aesKey argument should be the AES key, either 16, 24, or 32 bytes to select AES-128, AES-192, or AES-256, if aesKey is empty string, the cookie value will not Encrypted.

func (*Sessions) Get

func (sm *Sessions) Get(ctx *looli.Context, name string) (*Session, error)

Get return a existed session or create a new session

type Store

type Store interface {
	New(string, string) *Session
	Get(*http.Request, string) (*Session, error)
	Save(http.ResponseWriter, *http.Request, *Session) error
}

type Values

type Values map[interface{}]interface{}

Jump to

Keyboard shortcuts

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