sessions

package module
v0.0.0-...-d59c69b Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2018 License: MIT Imports: 8 Imported by: 0

README

sessions

GoDoc

Simple, secure, client-side storage for session data.

// Define a type to hold the session data you want to save on the client.
type Session struct {
	UserID    int
	SessionID string
	Started   time.Time
}

// Create a Store when you initialize your server.
store, err := sessions.NewStore(key)
if err != nil {
	panic(err)
}
store.CookieTemplate = http.Cookie{
	Name:   "myapp_session",
	Path:   "/",
	MaxAge: 7 * 24 * 60 * 60,
}

// Save the data to the client when a user successfully logs in.
http.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) {
	// ... authenticate the user ...

	sess := Session{
		UserID:    12345,
		SessionID: "example",
		Started:   time.Now(),
	}

	if err := store.SetCookie(w, sess); err != nil {
		log.Println(err)
		http.Error(w, "internal error", http.StatusInternalServerError)
		return
	}

	fmt.Fprintln(w, "OK")
})

// Retrieve the data as desired.
http.HandleFunc("/my_account", func(w http.ResponseWriter, r *http.Request) {
	var sess Session
	if err := store.GetFromCookie(r, &sess); err != nil {
		if err != sessions.ErrMissing {
			log.Println(err)
		}
		http.Error(w, "not logged in", http.StatusForbidden)
		return
	}

	fmt.Fprintf(w, "Logged in as user %d", sess.UserID)
})

Documentation

Overview

Package sessions provides secure client-side storage of session data.

Index

Examples

Constants

View Source
const KeySize = 32 // 256-bit keys

Variables

View Source
var ErrMissing = errors.New("value is missing")

Functions

This section is empty.

Types

type Store

type Store struct {
	// Ciphers provide authenticated encryption for stored data. When creating
	// new encrypted blobs, the first cipher is used. When decrypting existing
	// blobs, to allow for key rotation, all ciphers are attempted.
	Ciphers []cipher.AEAD

	// CookieTemplate is required if using the Store's cookie methods.
	// SetCookie won't modify the Expires field, so use MaxAge instead.
	CookieTemplate http.Cookie
}

A Store provides a mechanism for securely storing small pieces of session data on a client.

func NewStore

func NewStore(keys ...[KeySize]byte) (*Store, error)

NewStore creates a Store using ciphers derived from the given keys.

func (*Store) GetFromCookie

func (s *Store) GetFromCookie(r *http.Request, v interface{}) error

GetFromCookie retrieves data saved using s.SetCookie. It returns ErrMissing if the cookie is missing, and may return other kinds of errors as applicable.

func (*Store) Seal

func (s *Store) Seal(v interface{}) (ciphertext, nonce []byte, err error)

Seal marshals, encrypts, and authenticates v.

func (*Store) SetCookie

func (s *Store) SetCookie(w http.ResponseWriter, v interface{}) error

SetCookie calls s.Seal on v and stores the result as an HTTP cookie using s.CookieTemplate.

func (*Store) Unseal

func (s *Store) Unseal(ciphertext, nonce []byte, v interface{}) error

Unseal decrypts, authenticates, and unmarshals ciphertext into v.

Jump to

Keyboard shortcuts

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