sessions

package module
v2.1.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2018 License: Apache-2.0 Imports: 9 Imported by: 0

README

Sessions

Package that will be used across Unaxiom to generate and maintain sessions.

v2

Package has been rewritten using a faster in-memory DB. PostgreSQL is no longer required. This version is incompatible with v1.

Installation

go get -u github.com/Unaxiom/sessions

Dependencies
  1. github.com/tidwall/buntdb (for using BuntDB as the backend store)
  2. github.com/twinj/uuid
  3. github.com/go-redis/redis (for using Redis as the backend store)
Import
import (
    "github.com/Unaxiom/sessions"
)
Usage
sessionExpiryTime := int64(86400)
dbFolder, _ := os.Getwd()
sessionObject, err := Init("name_of_session", false, sessionExpiryTime, dbFolder)
sessionData, err := sessionObject.NewSession("somekeyhere", "userIPAddress")
fmt.Println("Auth Token is ", sessionData.Token)

// Check the status of the token. An error is returned in case the token does not exist. Returns nil otherwise.
_, err = sessionObject.CheckStatus(sessionData.Token)

// To delete a token
sessionObject.DeleteSession(sessionData.Token)

// For using Redis as the backend store
shortRedisDBSession, err := InitRedis("", "", 0, sessionExpiryTime)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Session

type Session struct {
	Name string

	ExpiryTime    int64
	StorageEngine string
	// contains filtered or unexported fields
}

Session manages all the sessions

func Init added in v1.0.1

func Init(name string, persistSessionToDisk bool, expiryInSecs int64, dbFolderLocation string) (*Session, error)

Init initialises a session object with buntdb as the storage engine a name, a bool representing if the corresponding file needs to be saved to disk, a key expiry time in seconds, and the location of the folder where the database file is stored

func InitRedis

func InitRedis(redisAddr string, password string, db int, expiryInSecs int64) (*Session, error)

InitRedis initialises a session object with Redis as the storage engine. Pass "" for redisAddr & password, and 0 for db for defaults.

func (*Session) CheckStatus added in v1.0.1

func (sessionObject *Session) CheckStatus(sessionToken string) (SessionData, error)

CheckStatus accepts a session token, and returns the session object, along with an error, if any

func (*Session) DeleteSession added in v1.0.1

func (sessionObject *Session) DeleteSession(sessionToken string) error

DeleteSession deletes the session with the specific sessionToken and returns an error, if any.

func (*Session) FetchSessionData added in v1.0.1

func (sessionObject *Session) FetchSessionData(session SessionData) (SessionData, error)

FetchSessionData returns the data of the particular session token

func (*Session) NewSession added in v1.0.1

func (sessionObject *Session) NewSession(key string, ipAddress string) (SessionData, error)

NewSession accepts the key to encode, along with the client's IP Address, inserts into the sessions table, sets the delete session timer, and returns a SessionData struct.

type SessionData added in v1.0.1

type SessionData struct {
	// Key stores the string that needs to be encoded/hashed to generate a unique session value
	Key string `json:"key,omitempty"`
	// Token stores the computed token using the key
	Token string `json:"token,omitempty"`
	// ExpiryIn stores the number of seconds after which this session will expire
	ExpiryIn int64 `json:"expiry_in,omitempty"`
	// ExpiresAt stores the timestamp (with full timezone) at which this session will expire
	ExpiresAt time.Time `json:"expires_at,omitempty"`
	// IP stores the IP address of the client requesting a new session
	IP string `json:"ip,omitempty"`
	// Timestamp stores the timestamp in epoch secs when this entry was created
	Timestamp int64 `json:"timestamp,omitempty"`
}

SessionData stores the parameters in the sessions table

Jump to

Keyboard shortcuts

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