session

package module
v2.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2020 License: MIT Imports: 11 Imported by: 13

README

Session

Release Discord Test Security Linter

This session middleware is build on top of fasthttp/session by @savsgio MIT. Special thanks to @thomasvvugt for helping with this middleware.

Install
go get -u github.com/gofiber/fiber/v2
go get -u github.com/gofiber/session/v2
Signature
session.New(config ...session.Config) *Session
Config
Property Type Description Default
Lookup string Where to look for the session id <source>:<name>, possible values: cookie:key, header:key, query:key "cookie:session_id"
Domain string Cookie domain ""
Expiration time.Duration Session expiration time, possible values : 0 means no expiry (24 years), -1 means when the browser closes, >0 is the time.Duration which the session cookies should expire. 12 * time.Hour
Secure bool If the cookie should only be send over HTTPS false
Provider Provider Holds the provider interface memory.Provider
Generator func() []byte Generator is a function that generates an unique id uuid
GCInterval time.Duration Interval for the garbage collector uuid
Default Example
package main

import (
	"log"

	"github.com/gofiber/fiber/v2"
	"github.com/gofiber/session/v2"
)

func main() {
	app := fiber.New()

	// create session handler, using the in-memory session store
	sessions := session.New()

	app.Get("/", func(c *fiber.Ctx) error {
		store := sessions.Get(c) // get/create new session
		defer store.Save()

		store.ID()               // returns session id
		store.Destroy()          // delete storage + cookie
		store.Get("john")        // get from storage
		store.Regenerate()       // generate new session id
		store.Delete("john")     // delete from storage
		store.Set("john", "doe") // save to storage

		return nil
	})

	log.Fatal(app.Listen(":3000"))
}

Provider Example
package main

import (
	"log"

	"github.com/gofiber/fiber/v2"
	"github.com/gofiber/session/v2"
	"github.com/gofiber/session/v2/provider/memcache"
	// "github.com/gofiber/session/v2/provider/mysql"
	// "github.com/gofiber/session/v2/provider/postgres"
	// "github.com/gofiber/session/v2/provider/redis"
	// "github.com/gofiber/session/v2/provider/sqlite3"
)

func main() {
	app := fiber.New()

	provider, err := memcache.New(memcache.Config{
		KeyPrefix: "session",
		ServerList: []string{
			"0.0.0.0:11211",
		},
		MaxIdleConns: 8,
	})
	// provider, err := mysql.New(mysql.Config{
	//   Host:       "session",
	//   Port:       3306,
	//   Username:   "root",
	//   Password:   "",
	//   Database:   "test",
	//   TableName:  "session",
	// })
	// provider, err := postgres.New(postgres.Config{
	//   Host:       "session",
	//   Port:       5432,
	//   Username:   "root",
	//   Password:   "",
	//   Database:   "test",
	//   TableName:  "session",
	// })
	// provider, err := redis.New(redis.Config{
	//   KeyPrefix:   "session",
	//   Addr:        "127.0.0.1:6379",
	//   PoolSize:    8,
	//   IdleTimeout: 30 * time.Second,
	// })
	// provider, err := sqlite3.New(sqlite3.Config{
	//   DBPath:     "test.db",
	//   TableName:  "session",
	// })

	if err != nil {
		log.Fatal(err.Error())
	}

	sessions := session.New(session.Config{
		Provider: provider,
	})

	app.Get("/", func(c *fiber.Ctx) error {
		store := sessions.Get(c) // get/create new session
		defer store.Save()

		store.ID()               // returns session id
		store.Destroy()          // delete storage + cookie
		store.Get("john")        // get from storage
		store.Regenerate()       // generate new session id
		store.Delete("john")     // delete from storage
		store.Set("john", "doe") // save to storage

		return nil
	})

	log.Fatal(app.Listen(":3000"))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Lookup is a string in the form of "<source>:<name>" that is used
	// to extract session id from the request.
	// Possible values: "header:<name>", "query:<name>" or "cookie:<name>"
	// Optional. Default value "cookie:session_id".
	Lookup string

	// Secure attribute for cookie
	// Optional. Default: false
	Secure bool

	// Domain attribute for cookie
	// Optional. Default: ""
	Domain string

	// SameSite attribute for cookie
	// Possible values: "Lax", "Strict", "None"
	// Optional. Default: "Lax"
	SameSite string

	//  0 means no expire, (24 years)
	// -1 means when browser closes
	// >0 is the time.Duration which the session cookies should expire.
	// Optional. Default: 12 hours
	Expiration time.Duration

	// Holds the provider interface
	// Optional. Default: memory.New()
	Provider fsession.Provider

	// Generator is a function that generates an unique id
	// Optional.
	Generator func() []byte

	// gc life time to execute it
	// Optional. 1 minute
	GCInterval time.Duration
	// contains filtered or unexported fields
}

Config defines the config for RequestID middleware

type Session

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

Session ...

func New

func New(config ...Config) *Session

New ...

func (*Session) Get

func (s *Session) Get(ctx *fiber.Ctx) *Store

Get store

type Store

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

Store is a wrapper arround the session.Store

func (*Store) Delete

func (s *Store) Delete(key string)

Delete delete data by key

func (*Store) Destroy

func (s *Store) Destroy() error

Destroy session and cookies

func (*Store) Get

func (s *Store) Get(key string) interface{}

Get get data by key

func (*Store) ID

func (s *Store) ID() string

ID returns session id

func (*Store) Regenerate

func (s *Store) Regenerate() error

Regenerate session id

func (*Store) Save

func (s *Store) Save() error

Save storage before response

func (*Store) Set

func (s *Store) Set(key string, value interface{})

Set get data by key

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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