token

package
v0.0.0-...-5404be9 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2020 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// AuthPrefix is the prefix for Auth tokens
	AuthPrefix = "auth"
	// SessionPrefix is the prefix for Session tokens
	SessionPrefix = "session"
)
View Source
const SessionSetExpirationKey = "tokenset-session-expiration"

SessionSetExpirationKey is the key to the set which holds information about session expiration

Variables

View Source
var ErrNoToken = errors.New("non-existent token")

ErrNoToken is returned when a token isn't found

Functions

func AuthTokenKey

func AuthTokenKey(tokenID string) string

AuthTokenKey returns the key used to store the Auth token

func AuthTokenSetKey

func AuthTokenSetKey(userID int) string

AuthTokenSetKey returns the key used to store Auth token in its set

func SessionTokenKey

func SessionTokenKey(tokenID string) string

SessionTokenKey returns the key used to store the Session token

func SessionTokenSetKey

func SessionTokenSetKey(userID int) string

SessionTokenSetKey returns the key used to store Session token in its set

Types

type Storage

type Storage struct {
	Cache storage.Cache
}

Storage holds the underlying token storage

func (*Storage) List

func (s *Storage) List(token *Token) ([]Token, error)

List returns the list which the token is part of

func (*Storage) ListAll

func (s *Storage) ListAll(userID int) (map[string][]Token, error)

ListAll returns all token lists

func (*Storage) LoadAuthToken

func (s *Storage) LoadAuthToken(tokenID string) (*Token, error)

LoadAuthToken loads an existing Auth token from storage

func (*Storage) LoadSessionToken

func (s *Storage) LoadSessionToken(tokenID string) (*Token, error)

LoadSessionToken loads an existing Session token from storage

func (*Storage) NewAuthToken

func (s *Storage) NewAuthToken(userID int) *Token

NewAuthToken creates a new Auth token

func (*Storage) NewSessionToken

func (s *Storage) NewSessionToken(userID int) *Token

NewSessionToken creates a new Session token

type Token

type Token struct {
	Cache  storage.Cache `json:"-"`
	Key    string        `json:"-"`
	SetKey string        `json:"-"`
	Type   Type          `json:"-"`
	UserID int           `db:"user_id" json:"-"`

	ID         string    `db:"id" json:"id"`
	Client     string    `db:"client" json:"client"`
	IP         string    `db:"ip" json:"ip"`
	CreatedAt  time.Time `db:"created_at" json:"created_at"`
	ExpiresAt  time.Time `db:"expires_at" json:"expires_at"`
	LastUsedAt time.Time `db:"last_used_at" json:"last_used_at"`
}

Token holds all the token information

func (*Token) Count

func (t *Token) Count() (int64, error)

Count returns how many tokens are part of its set

func (*Token) Delete

func (t *Token) Delete() error

Delete removes a token from storage

func (*Token) Save

func (t *Token) Save() error

Save adds a token to storage

func (*Token) Set

func (t *Token) Set() ([]string, error)

Set returns the tokens which are part of its set

func (*Token) SetExpiration

func (t *Token) SetExpiration() error

SetExpiration sets the token expiration date

func (*Token) UpdateLastUsed

func (t *Token) UpdateLastUsed() error

UpdateLastUsed updates the LastUsedAt token information

func (*Token) Valid

func (t *Token) Valid() (bool, error)

Valid returns whether a token is valid or not

type Type

type Type int

Type is the token type

const (
	// Auth persistent token
	Auth Type = iota
	// Session expiring token
	Session
)

Jump to

Keyboard shortcuts

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