session_util

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2021 License: BSD-3-Clause Imports: 11 Imported by: 1

Documentation

Overview

Package session_util provides support for managing web sessions for apps where users logs in.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type UserGetter

type UserGetter interface {
	// GetUser retrieves a user from persistent storage given user Id.
	GetUser(id int64) (userPtr interface{}, err error)
}

type UserIdSession

type UserIdSession struct {
	S *sessions.Session
}

UserIdSession augments a gorilla session by supporting the storing and retrieving of the user Id of the logged in user.

func (UserIdSession) ClearAll

func (s UserIdSession) ClearAll()

ClearAll clears all data from this session including any xsrf secret.

func (UserIdSession) ClearLastLogin

func (s UserIdSession) ClearLastLogin()

ClearLastLogin clears the last login time in this session.

func (UserIdSession) ClearUserId

func (s UserIdSession) ClearUserId()

ClearUserId clears the user ID in this session and clears any xsrf secret.

func (UserIdSession) LastLogin

func (s UserIdSession) LastLogin() (time.Time, bool)

LastLogin returns the last login time and true if stored in this session; otherwise it returns the zero time and false.

func (UserIdSession) NewXsrfToken

func (s UserIdSession) NewXsrfToken(action string, expire time.Time) string

NewXsrfToken creates a new xsrf token. action identifies the web page; expire is when the token expires. NewXsrfToken panics if userId is not set.

func (UserIdSession) SetLastLogin

func (s UserIdSession) SetLastLogin(lastLogin time.Time)

SetLastLogin sets the last login time in this session.

func (UserIdSession) SetUserId

func (s UserIdSession) SetUserId(id int64)

SetUserId sets the user ID in this session and generates a new xsrf secret for creating xsrf tokens.

func (UserIdSession) UserId

func (s UserIdSession) UserId() (int64, bool)

UserId returns the userId and true if user Id is stored in this session; otherwise it returns 0 and false.

func (UserIdSession) VerifyXsrfToken

func (s UserIdSession) VerifyXsrfToken(
	tokenToBeVerified, action string, now time.Time) bool

VerifyXsrfToken returns true if token is valid or false otherwise. action identifies the web page; now is the current time. If no userId is set, VerifyXsrfToken returns false.

type UserSession

type UserSession interface {
	// UserId either returns the user id in the session and true or 0 and
	// false if there is no user id in the session.
	UserId() (int64, bool)

	// SetUser sets the user instance in this session.
	SetUser(userPtr interface{})
}

Sessions that store user instances implement this interface.

func GetUserSession

func GetUserSession(r *http.Request) UserSession

GetUserSession returns the UserSession paired with this request. It is an error to call GetUserSession on a request without a previously successful call to NewUserSession on the same request.

func NewUserSession

func NewUserSession(
	sessionStore sessions.Store,
	r *http.Request,
	cookieName string,
	factory func(s *sessions.Session) UserSession,
	userGetter UserGetter,
	noSuchId error) (UserSession, error)

NewUserSession creates a new UserSession and pairs it with the current http request. If a user is logged in, the returned UserSession will contain that user instance; otherwise returned UserSession will contain nil for the user instance. Upon successful completion, caller must call context.Clear(r) from github.com/gorilla/context. sessionStore is the session store; r is the current http request; cookieName is the name of the session cookie; factory creates the UserSession given a gorilla session; userGetter retrieves user instance from persistent storage given user ID; noSuchId is the error that userGetter returns if no such user exist for a given ID.

Jump to

Keyboard shortcuts

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