sessions

package
v0.0.0-...-a73d231 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2024 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContextWithSession

func ContextWithSession(ctx context.Context, session *Session) context.Context

ContextWithSession returns a new context with a session stored in it.

Types

type Cache

type Cache interface {
	// GetSessionUse should increase some use count on data the
	// passed session refers to.
	GetSessionUse(session SessionExt)
	// GetSessionUseByID should use the passed builder to create a
	// session object with a use count on the data incremented by
	// one.
	GetSessionUseByID(builder SessionBuilder, id, name string) *Session
	// PutSessionUse should decrement the use count on data and
	// destroy the data if the count drops to zero and the session
	// was marked for destruction.
	PutSessionUse(session SessionExt)
	// MarkOrDestroySessionByID should mark the data for
	// destruction. If data's use count is zero, it should be
	// destroyed.
	MarkOrDestroySessionByID(id string)
	// MarkSession marks the session for destruction. Data
	// shouldn't be destroyed since the existence of the passed
	// session object means that the data is still used.
	MarkSession(session SessionExt)
	// SaveSession should persist the changes made in the session
	// object to the cache.
	SaveSession(session SessionExt) (bool, error)
}

Cache stores the refcounted session information. This type shouldn't be used by libraries - it's only for Store and for implementations of this interface.

type Codec

type Codec interface {
	// Decode should return an ID from the passed name and cookie
	// value.
	Decode(name, value string) (string, error)
	// Encode should return a cookie value from the passed name
	// and ID.
	Encode(name, id string) (string, error)
}

Codec decodes cookie ID from the name and value pair, and encodes cookie value from name and id pair. This type is used by the store.

type MockCache

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

MockCache is an implementation of Cache interface used for mocking.

func NewMockCache

func NewMockCache() *MockCache

New creates a new MockCache.

func (*MockCache) GetSessionUse

func (c *MockCache) GetSessionUse(session SessionExt)

GetSessionUse is a part of Cache interface.

func (*MockCache) GetSessionUseByID

func (c *MockCache) GetSessionUseByID(builder SessionBuilder, id, name string) *Session

GetSessionUseByID is a part of Cache interface.

func (*MockCache) MarkOrDestroySessionByID

func (c *MockCache) MarkOrDestroySessionByID(id string)

MarkOrDestroySessionByID is a part of Cache interface.

func (*MockCache) MarkSession

func (c *MockCache) MarkSession(session SessionExt)

MarkSession is a part of Cache interface.

func (*MockCache) PutSessionUse

func (c *MockCache) PutSessionUse(session SessionExt)

PutSessionUse is a part of Cache interface.

func (*MockCache) SaveSession

func (c *MockCache) SaveSession(session SessionExt) (bool, error)

SaveSession is a part of Cache interface.

func (*MockCache) UseCountFor

func (c *MockCache) UseCountFor(id string) int

UseCountFor returns a use count of the session with a passed ID, or -1 the session is not in cache.

type MockCodec

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

MockCodec is an implementation of Codec used for mocking.

func NewMockCodec

func NewMockCodec() *MockCodec

NewMockCodec creates a new MockCodec.

func (*MockCodec) AddIDValueMapping

func (c *MockCodec) AddIDValueMapping(idValuePairs ...string)

AddIDValueMapping adds a mapping from ID to cookie value and back. Not adding mapping will result in an error when encoding or decoding.

func (*MockCodec) Decode

func (c *MockCodec) Decode(name, value string) (string, error)

Decode is a part of Codec interface.

func (*MockCodec) Encode

func (c *MockCodec) Encode(name, id string) (string, error)

Encode is a part of Codec interface.

type Session

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

Session is a snapshot of the session data stored in the cache.

func SessionFromContext

func SessionFromContext(ctx context.Context) *Session

SessionFromContext returns the last session stored in the context. Returns nil if there is no session.

func (*Session) Drop

func (s *Session) Drop(key interface{})

Drop drops the value under the key.

func (*Session) Get

func (s *Session) Get(key interface{}) interface{}

Get returns a value mapped to the key, or nil.

func (*Session) Has

func (s *Session) Has(key interface{}) bool

Has returns true if the session object contains a value under the passed key.

func (*Session) ID

func (s *Session) ID() string

ID returns an ID of the session.

func (*Session) Mark

func (s *Session) Mark()

Mark marks the session for destruction.

func (*Session) Name

func (s *Session) Name() string

Name returns a name of the session.

func (*Session) Save

func (s *Session) Save(writer http.ResponseWriter) error

Save persists the information stored in the session object to the cache associated with the object, and updates the cookie header in the response.

TODO: Split the writing the cookie into a separate function, like WriteCookie.

TODO: Add support for options, so we don't hardcode the max age value here. The support should probably be reflected in the Cache interface itself and in Session.

func (*Session) Set

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

Set inserts or updates the value under the key.

type SessionBuilder

type SessionBuilder interface {
	// NewSession creates a new session object with a given name, and
	// associates it with the passed cache. The created cache has
	// no ID.
	NewSession(name string, cache Cache) SessionExt
	// NewExistingSession created a session object with passed
	// name, id and values map, and associates it with the passed
	// cache.
	NewExistingSession(name, id string, values ValuesType, cache Cache) SessionExt
}

SessionBuilder is an interface purely for implementation of the Cache interface, so they can create new session objects.

type SessionExt

type SessionExt interface {
	// Name returns a name of the session.
	Name() string
	// ID returns an ID of the session.
	ID() string
	// SetID updates an ID of the session.
	SetID(id string)
	// GetValues returns the values map.
	GetValues() ValuesType
	// Session returns the underlying session object.
	Session() *Session
}

SessionExt is an interface purely for implementations of the Cache interface, so they can change some data in the session that typical user of the session shouldn't be allowed to do.

type Store

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

Store contains cache and codec for session management.

func NewStore

func NewStore(cache Cache, codec Codec) *Store

NewStore creates a new sessions store.

func (*Store) GetSessionUse

func (s *Store) GetSessionUse(request *http.Request, name string) *Session

GetSessionUse tries to either retrieve the session from either context or request, or create a new one. The returned session has its use count increased, so is safe from being destroyed until PutSessionUse is called.

func (*Store) MarkOrDestroySessionByID

func (s *Store) MarkOrDestroySessionByID(id string)

MarkOrDestroySessionByID marks the session with a passed ID for destruction. If the session is not currently in use by any request, the session should be destroyed immediately.

func (*Store) PutSessionUse

func (s *Store) PutSessionUse(session *Session)

PutSessionUse decreases the use count of the session and potentially destroys it, if it was marked.

type TestHarness

type TestHarness struct {
	// T is just testing.T.
	T *testing.T
	// NewCache should return a Cache set in such a way, that the
	// new IDs for session should be in form of "idN", where N
	// grows from 1.
	NewCache func() Cache
	// UseCountFor should return a use count of a session with a
	// given ID, or -1 if the ID is not in cache.
	UseCountFor func(cache Cache, id string) int
}

TestHarness contains a setup for running session tests.

func (*TestHarness) RunBasicSessionLifecycleTests

func (h *TestHarness) RunBasicSessionLifecycleTests()

RunBasicSessionLifecycleTests tests creation and destruction of sessions, and the use count changes

func (*TestHarness) RunDeadCookiesTests

func (h *TestHarness) RunDeadCookiesTests()

RunDeadCookiesTests tests the behaviour of the cache for marked and destroyed cookies.

type ValuesType

type ValuesType map[interface{}]interface{}

ValuesType is a type of values container used in the session.

Directories

Path Synopsis
gob

Jump to

Keyboard shortcuts

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