session

package
v0.0.0-...-9192568 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2023 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CreateSessionTable = `CREATE TABLE IF NOT EXISTS sessions(
    	cid VARCHAR(50),
	   	token VARCHAR(165),
	   	PRIMARY KEY (cid)
	)`
	DeleteSession = `DELETE FROM sessions WHERE cid = $1`
	GetSession    = "SELECT token FROM sessions WHERE cid = $1"
	StoreSession  = "INSERT INTO sessions(cid, token) VALUES ($1, $2) ON CONFLICT DO NOTHING RETURNING token"
)

Variables

View Source
var (
	ErrDBMissingURL  = errors.New("session db url is missing")
	ErrNotFound      = errors.New("session not found")
	ErrIncorrectData = errors.New("client id or token is not specified")
	ErrSessionExists = errors.New("session for specified client id already exists")
)
View Source
var (
	ErrEmptyToken   = errors.New("session: token is missing or empty")
	ErrEmptyUID     = errors.New("session: uid is missing or empty")
	ErrTokenExpired = errors.New("session: token expired")
)

Functions

This section is empty.

Types

type BasicRepo

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

func NewBasicRepo

func NewBasicRepo() *BasicRepo

func (*BasicRepo) DeleteSession

func (r *BasicRepo) DeleteSession(_ context.Context, cid string) error

func (*BasicRepo) GetSession

func (r *BasicRepo) GetSession(_ context.Context, cid string) (string, error)

func (*BasicRepo) StoreSession

func (r *BasicRepo) StoreSession(_ context.Context, cid, token string) error

type DBRepo

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

func NewDBRepo

func NewDBRepo(url string) (*DBRepo, error)

func (*DBRepo) DeleteSession

func (r *DBRepo) DeleteSession(ctx context.Context, cid string) error

func (*DBRepo) GetSession

func (r *DBRepo) GetSession(ctx context.Context, cid string) (string, error)

func (*DBRepo) StoreSession

func (r *DBRepo) StoreSession(ctx context.Context, cid, token string) error

type IRepository

type IRepository interface {
	DeleteSession(ctx context.Context, cid string) error
	GetSession(ctx context.Context, cid string) (string, error)
	StoreSession(ctx context.Context, cid, token string) error
}

func NewRepo

func NewRepo(repoURL string) (IRepository, error)

type Service

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

func NewService

func NewService(repoURL string) (Service, error)

NewService returns an instance of the Service with the associated repository.

func (Service) DeleteSession

func (s Service) DeleteSession(ctx context.Context, cid string) error

DeleteSession deletes the client-associated session.

func (Service) GenerateToken

func (s Service) GenerateToken(uid string) (string, error)

GenerateToken generates a new JWT token with the specified expiry time.

func (Service) GetUIDFromToken

func (s Service) GetUIDFromToken(token string) (string, error)

GetUIDFromToken parses the token string and returns the UID from its claims.

func (Service) IsTokenExpired

func (s Service) IsTokenExpired(token string) (bool, error)

IsTokenExpired checks if the token had expired.

func (Service) RestoreSession

func (s Service) RestoreSession(ctx context.Context, cid string) (string, error)

RestoreSession gathers the stored client-associated token. If the token is expired, the method deletes it from the repository and returns an error.

func (Service) StoreSession

func (s Service) StoreSession(ctx context.Context, token string) (string, error)

StoreSession generates new client ID and associates the passed token with it.

Jump to

Keyboard shortcuts

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