cookies

package
v0.0.0-...-d1a64ea Latest Latest
Warning

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

Go to latest
Published: May 22, 2016 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package cookies uses the DatabaseCookieStore type as its default cookie store for managing user sessions

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInitializationFailed = errors.New("Initialization of the cookie store has failed")
)

Errors generated by the cookie stores

View Source
var (
	ErrTokenExpired = errors.New("The session token has expired")
)

Errors generated during session handling

Functions

func ConfigureTokenExpireTime

func ConfigureTokenExpireTime(expireTime time.Duration)

ConfigureTokenExpireTime is used to change the default cofiguration of token expiration times

func InitCookieStore

func InitCookieStore()

InitCookieStore initializes the currently active cookie store

func SetCookieStore

func SetCookieStore(store CookieStore)

SetCookieStore sets the cookie store that will be used by the system. If this method is not called, the default cookie store will be used

Types

type Client

type Client struct {
	Address        string `bson:"address,omitempty" json:"address"`
	Type           string `bson:"type,omitempty" json:"type"`
	Name           string `bson:"name,omitempty" json:"name"`
	Version        string `bson:"version,omitempty" json:"version"`
	OS             string `bson:"os,omitempty" json:"os"`
	IsMobileDevice bool   `bson:"isMobileDevice,omitempty" json:"isMobileDevice"`
}

Client struct contains information regarding the client that has made the http request

func UnknownClientDetails

func UnknownClientDetails() *Client

UnknownClientDetails gets the default client details, which have 'unknown' values

type CookieStore

type CookieStore interface {
	ReadCookie(key string) (*Session, error)
	WriteCookie(cookie *Session) error
	DeleteCookie(cookie *Session) error
	GetAllUserCookies(userID bson.ObjectId) ([]*Session, error)
	Init()
}

CookieStore is an interface used for describing entities that can manage the location of user sessions (cookies) and performs operations such as reading or writing cookie from/to that location

type DatabaseCookieStore

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

DatabaseCookieStore manages cookies using a database

func NewDatabaseCookieStore

func NewDatabaseCookieStore(storeLocation string) *DatabaseCookieStore

NewDatabaseCookieStore creates a new DatabaseCookieStore pointer entity

func (*DatabaseCookieStore) DeleteCookie

func (store *DatabaseCookieStore) DeleteCookie(cookie *Session) error

DeleteCookie deletes a cookie from the cookie storage

func (*DatabaseCookieStore) GetAllUserCookies

func (store *DatabaseCookieStore) GetAllUserCookies(userID bson.ObjectId) ([]*Session, error)

GetAllUserCookies returns all the cookies that a certain user has

func (*DatabaseCookieStore) Init

func (store *DatabaseCookieStore) Init()

Init initializes the cookie store

func (*DatabaseCookieStore) ReadCookie

func (store *DatabaseCookieStore) ReadCookie(key string) (*Session, error)

ReadCookie fetches a cookie from the cookie store

func (*DatabaseCookieStore) WriteCookie

func (store *DatabaseCookieStore) WriteCookie(cookie *Session) error

WriteCookie writes a cookie in the cookie store. If that cookie already exists, it is overwritten

type FileCookieStore

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

FileCookieStore manages cookies using files on the local drive

func NewFileCookieStore

func NewFileCookieStore(storeLocation string) *FileCookieStore

NewFileCookieStore creates a new NewFileCookieStore pointer entity

func (*FileCookieStore) DeleteCookie

func (store *FileCookieStore) DeleteCookie(cookie *Session) error

DeleteCookie deletes a cookie from the cookie storage

func (*FileCookieStore) GetAllUserCookies

func (store *FileCookieStore) GetAllUserCookies(userID bson.ObjectId) ([]*Session, error)

GetAllUserCookies returns all the cookies that a certain user has

func (*FileCookieStore) Init

func (store *FileCookieStore) Init()

Init initializes the cookie store

func (*FileCookieStore) ReadCookie

func (store *FileCookieStore) ReadCookie(key string) (*Session, error)

ReadCookie fetches a cookie from the cookie store

func (*FileCookieStore) WriteCookie

func (store *FileCookieStore) WriteCookie(cookie *Session) error

WriteCookie writes a cookie in the cookie store. If that cookie already exists, it is overwritten

type Session

type Session struct {
	ID          bson.ObjectId `bson:"_id" json:"-"`
	UserID      bson.ObjectId `bson:"userID,omitempty" json:"userID"`
	Token       string        `bson:"token,omitempty" json:"token"`
	AccountType int           `bson:"accountType,omitempty" json:"accountType"`
	ExpireTime  time.Time     `bson:"expireTime,omitempty" json:"-"`
	Client      *Client       `bson:"client,omitempty" json:"client"`
}

Session is a struct representing the session that a user has. Sessions are active since login until they expire or the user disconnects

func GetSession

func GetSession(token string) (*Session, error)

GetSession retrieves a session from the cookie store

func GetUserSessions

func GetUserSessions(userID bson.ObjectId) ([]*Session, error)

GetUserSessions retrieves all the sessions that a user has

func NewSession

func NewSession(userID bson.ObjectId, accountType int, clientDetails *Client) (*Session, error)

NewSession generates a new Session pointer that contains the given userID and a unique token used as an identifier

func (*Session) Delete

func (session *Session) Delete() error

Delete deletes the session from the cookie store

func (*Session) IsExpired

func (session *Session) IsExpired() bool

IsExpired returns true if the session has expired

func (*Session) IsUserInRole

func (session *Session) IsUserInRole(role int) bool

IsUserInRole verifies if the user with the current session has a specific role

func (*Session) ResetToken

func (session *Session) ResetToken() error

ResetToken generates a new token and resets the expire time target of the session This also triggers a Save() action, to update the cookie store

func (*Session) Save

func (session *Session) Save() error

Save saves the session in the cookie store

Jump to

Keyboard shortcuts

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