user

package
v1.5.1 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2022 License: CC0-1.0 Imports: 9 Imported by: 2

Documentation

Overview

Package user contains user session management for webservers. Sessions are identified via session cookies and stored in memory on the server side.

Index

Constants

This section is empty.

Variables

View Source
var CookieMaxLifetime = 3600

CookieMaxLifetime is the max life time of a session cookie in seconds

UserSessionManager manages all user sessions.

Functions

This section is empty.

Types

type DefaultSession

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

DefaultSession is the default manager for web sessions.

func (*DefaultSession) Get

func (ds *DefaultSession) Get(key string) (interface{}, bool)

Get returns a session.

func (*DefaultSession) GetAll

func (ds *DefaultSession) GetAll() map[string]interface{}

GetAll returns all known session values.

func (*DefaultSession) ID

func (ds *DefaultSession) ID() string

ID returns the session id.

func (*DefaultSession) Set

func (ds *DefaultSession) Set(key string, value interface{})

Set sets a session value. A nil value deletes a value from the session.

func (*DefaultSession) String

func (ds *DefaultSession) String() string

String returns a string representation of the session.

func (*DefaultSession) User

func (ds *DefaultSession) User() string

User returns the user of the session.

type MemorySessionProvider

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

MemorySessionProvider keeps all session related data in memory.

func (*MemorySessionProvider) Destroy

func (ms *MemorySessionProvider) Destroy(sid string) error

Destroy destroys a session.

func (*MemorySessionProvider) Get

func (ms *MemorySessionProvider) Get(sid string) (Session, error)

Get retrieves a session.

func (*MemorySessionProvider) GetAll

func (ms *MemorySessionProvider) GetAll() ([]Session, error)

GetAll returns a list of all sessions.

func (*MemorySessionProvider) Init

func (ms *MemorySessionProvider) Init(sid string, user string) (Session, error)

Init creates a new session for a given user. The session has an explicit expiry time after which a get will fail.

func (*MemorySessionProvider) SetExpiry

func (ms *MemorySessionProvider) SetExpiry(secs int)

SetExpiry sets the session expiry time in seconds. All existing sessions are deleted during this function call. This call is not thread safe - only use it during initialisation!

type Session

type Session interface {

	/*
		Id returns the session id.
	*/
	ID() string

	/*
	   User returns the user of the session.
	*/
	User() string

	/*
		GetAll returns all known session values.
	*/
	GetAll() map[string]interface{}

	/*
		Get returns a session.
	*/
	Get(key string) (interface{}, bool)

	/*
		Set sets a session value. A nil value deletes a value
		from the session.
	*/
	Set(key string, value interface{})

	/*
		String returns a string representation of the session.
	*/
	String() string
}

Session models a user session object.

func NewDefaultSession

func NewDefaultSession(id string, user string) Session

NewDefaultSession creates a new default session object.

type SessionManager

type SessionManager struct {
	Lock     sync.Mutex
	Provider SessionProvider
}

SessionManager manages web sessions.

func (*SessionManager) CheckSessionCookie

func (manager *SessionManager) CheckSessionCookie(r *http.Request) (bool, bool)

CheckSessionCookie checks if a request contains a session cookie and if the session is active. Returns has cookie and is active.

func (*SessionManager) GetSession

func (manager *SessionManager) GetSession(user string, w http.ResponseWriter,
	r *http.Request, create bool) (Session, error)

GetSession retrieves an existing or creates a new session

func (*SessionManager) RemoveSessionCookie

func (manager *SessionManager) RemoveSessionCookie(w http.ResponseWriter)

RemoveSessionCookie removes the session cookie in a given response object.

type SessionProvider

type SessionProvider interface {

	/*
		Create a new session for a given user. The session has an explicit
		expiry time after which a get will fail.
	*/
	Init(sid string, user string) (Session, error)

	/*
		Get retrieves a session.
	*/
	Get(sid string) (Session, error)

	/*
		GetAll returns a list of all sessions.
	*/
	GetAll() ([]Session, error)

	/*
		Destroy destroys a session.
	*/
	Destroy(sid string) error
}

SessionProvider is a session storage provider. Sessions should expire after a certain amount of time.

func NewMemorySessionProvider

func NewMemorySessionProvider() SessionProvider

NewMemorySessionProvider creates a new memory session provider. By default sessions have the same expiry time as cookies.

Jump to

Keyboard shortcuts

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