token

package
v0.0.0-...-ea7d51e Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2024 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrTokenNotFound = errors.New("token not found")
	ErrInvalidKey    = errors.New("key is invalid")
	ErrTokenTimeout  = errors.New("token is expired")
)
View Source
var (
	ErrNotString = errors.New("type is not string")
)

Functions

This section is empty.

Types

type DefaultStore

type DefaultStore[T Type] struct {
	// contains filtered or unexported fields
}

func (*DefaultStore[T]) Delete

func (ds *DefaultStore[T]) Delete(token T) error

func (*DefaultStore[T]) Exists

func (ds *DefaultStore[T]) Exists(token T) bool

func (*DefaultStore[T]) Set

func (ds *DefaultStore[T]) Set(token T, expiry time.Duration) error

func (*DefaultStore[T]) Shutdown

func (ds *DefaultStore[T]) Shutdown(ctx context.Context) <-chan error

type DefaultToken

type DefaultToken[T string, F Fun] struct {
	Expire time.Duration
	// contains filtered or unexported fields
}

func (*DefaultToken[T, F]) CreateToken

func (dt *DefaultToken[T, F]) CreateToken(ctx context.Context, claim jwt.Claims, expire ...time.Duration) (T, error)

CreateToken creates a new token. jwt claim token to string `f` is a function that returns jwt.SigningMethod, []byte, jwt.Claims not `f` is struct dt.f

func (*DefaultToken[T, F]) RefreshToken

func (dt *DefaultToken[T, F]) RefreshToken(ctx context.Context, token T, claim jwt.Claims, expire ...time.Duration) (T, error)

RefreshToken creates a new refresh token. The old token is revoked or set token to Expire. `token` is the old token.

func (*DefaultToken[T, F]) Store

func (dt *DefaultToken[T, F]) Store() Store[T]

func (*DefaultToken[T, F]) Validate

func (dt *DefaultToken[T, F]) Validate(token T) (jwt.Claims, error)

Validate validates a token. `f` is a function that returns jwt.SigningMethod, []byte, jwt.Claims

type Fun

type Fun interface {
	func() (jwt.SigningMethod, []byte)
}

type Option

type Option[T Type, V Val] func(Token[T, V])

func WithClaims

func WithClaims[T string, F Fun](claims jwt.Claims) Option[T, F]

func WithExpire

func WithExpire[T string, F Fun](expire time.Duration) Option[T, F]

func WithFun

func WithFun[T string, F Fun](f F) Option[T, F]

func WithStore

func WithStore[T string, F Fun](store Store[T]) Option[T, F]

type RStore

type RStore[T string] struct {
	// contains filtered or unexported fields
}

func (*RStore[T]) Delete

func (rs *RStore[T]) Delete(token T) error

func (*RStore[T]) Exists

func (rs *RStore[T]) Exists(token T) bool

func (*RStore[T]) Set

func (rs *RStore[T]) Set(token T, expiry time.Duration) error

func (*RStore[T]) Shutdown

func (rs *RStore[T]) Shutdown(ctx context.Context) <-chan error

type Store

type Store[T Type] interface {
	// Set a token to the store with the specified expiry time.
	Set(token T, expiry time.Duration) error
	// Exists checks if a token exists in the store.
	Exists(token T) bool
	// Delete a token from the store.
	Delete(token T) error
	// Shutdown the store.
	Shutdown(ctx context.Context) <-chan error
}

func NewDefaultStore

func NewDefaultStore[T Type](file string, logger *zap.Logger) (Store[T], error)

func NewRStore

func NewRStore[T string](c redis.Cmdable, logger *zap.Logger) Store[T]

type Token

type Token[T Type, V Val] interface {
	// CreateToken creates a new token.
	CreateToken(ctx context.Context, claim jwt.Claims, expire ...time.Duration) (T, error)

	// RefreshToken creates a new refresh token.
	// The old token is revoked or set token to Expire.
	RefreshToken(ctx context.Context, token T, claim jwt.Claims, expire ...time.Duration) (T, error)

	// Validate validates a token.
	Validate(token T) (jwt.Claims, error)

	// Store returns the store used by the token.
	Store() Store[T]
}

func NewDefaultToken

func NewDefaultToken[T string, F Fun](opts ...Option[T, F]) Token[T, F]

type Type

type Type interface {
	string | ~int | ~uint | float64 | float32
}

type Val

type Val interface {
	interface{}
}

Directories

Path Synopsis
Package tkmocks is a generated GoMock package.
Package tkmocks is a generated GoMock package.

Jump to

Keyboard shortcuts

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