store

package
v3.4.1 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package store provides implementations for refresh token storage

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrRefreshTokenNotFound = core.ErrRefreshTokenNotFound
	ErrRefreshTokenExpired  = core.ErrRefreshTokenExpired
)

Re-export errors from core for backward compatibility

Functions

func Default

func Default() core.TokenStore

Default creates a default memory-based token store This is the recommended way to create a store with sensible defaults

func MustNewMemoryStore

func MustNewMemoryStore() core.TokenStore

MustNewMemoryStore creates a new in-memory token store (never fails)

func MustNewRedisStore

func MustNewRedisStore(config *RedisConfig) core.TokenStore

MustNewRedisStore creates a new Redis token store and panics on error

func MustNewStore

func MustNewStore(config *Config) core.TokenStore

MustNewStore creates a token store with the given configuration and panics on error

func NewMemoryStore

func NewMemoryStore() core.TokenStore

NewMemoryStore creates a new in-memory token store

func NewRedisStore

func NewRedisStore(config *RedisConfig) (core.TokenStore, error)

NewRedisStore creates a new Redis token store with the given configuration

func NewStore

func NewStore(config *Config) (core.TokenStore, error)

NewStore creates a token store with the given configuration

Types

type Config

type Config struct {
	Type  StoreType    // Type of store to create (memory or redis)
	Redis *RedisConfig // Redis configuration (only used when Type is RedisStore)
}

Config holds the configuration for creating a token store

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig returns a default configuration with memory store

func NewMemoryConfig

func NewMemoryConfig() *Config

NewMemoryConfig creates a configuration for memory store

func NewRedisConfig

func NewRedisConfig(redisConfig *RedisConfig) *Config

NewRedisConfig creates a configuration for Redis store

type Factory

type Factory struct{}

Factory provides methods to create different types of token stores

func NewFactory

func NewFactory() *Factory

NewFactory creates a new store factory

func (*Factory) CreateStore

func (f *Factory) CreateStore(config *Config) (core.TokenStore, error)

CreateStore creates a token store based on the provided configuration

type InMemoryRefreshTokenStore

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

InMemoryRefreshTokenStore provides a simple in-memory refresh token store This implementation is thread-safe and suitable for single-instance applications For distributed systems, consider using Redis or database-based implementations

func NewInMemoryRefreshTokenStore

func NewInMemoryRefreshTokenStore() *InMemoryRefreshTokenStore

NewInMemoryRefreshTokenStore creates a new in-memory refresh token store

func (*InMemoryRefreshTokenStore) Cleanup

func (s *InMemoryRefreshTokenStore) Cleanup(ctx context.Context) (int, error)

Cleanup removes expired tokens and returns the number of tokens cleaned up

func (*InMemoryRefreshTokenStore) Clear

func (s *InMemoryRefreshTokenStore) Clear()

Clear removes all tokens from the store (useful for testing) Note: This method is not part of the RefreshTokenStorer interface

func (*InMemoryRefreshTokenStore) Count

Count returns the total number of active refresh tokens

func (*InMemoryRefreshTokenStore) Delete

func (s *InMemoryRefreshTokenStore) Delete(ctx context.Context, token string) error

Delete removes a refresh token from storage

func (*InMemoryRefreshTokenStore) Get

func (s *InMemoryRefreshTokenStore) Get(ctx context.Context, token string) (any, error)

Get retrieves user data associated with a refresh token

func (*InMemoryRefreshTokenStore) GetAll

GetAll returns all active tokens (for debugging/monitoring purposes) Note: This method is not part of the RefreshTokenStorer interface and should be used carefully in production environments

func (*InMemoryRefreshTokenStore) Set

func (s *InMemoryRefreshTokenStore) Set(
	ctx context.Context,
	token string,
	userData any,
	expiry time.Time,
) error

Set stores a refresh token with associated user data and expiration

type RedisConfig

type RedisConfig struct {
	// Redis connection configuration
	Addr     string // Redis server address (default: "localhost:6379")
	Password string // Redis password (default: "")
	DB       int    // Redis database number (default: 0)

	// TLS configuration
	TLSConfig *tls.Config // TLS configuration for secure connections (optional, default: nil)

	// Client-side cache configuration
	CacheSize int           // Client-side cache size in bytes (default: 128MB)
	CacheTTL  time.Duration // Client-side cache TTL (default: 1 minute)

	// Connection pool configuration
	PoolSize        int           // Connection pool size (default: 10)
	ConnMaxIdleTime time.Duration // Max idle time for connections (default: 30 minutes)
	ConnMaxLifetime time.Duration // Max lifetime for connections (default: 1 hour)

	// Key prefix for Redis keys
	KeyPrefix string // Prefix for all Redis keys (default: "gin-jwt:")
}

RedisConfig holds the configuration for Redis store

func DefaultRedisConfig

func DefaultRedisConfig() *RedisConfig

DefaultRedisConfig returns a default Redis configuration

type RedisRefreshTokenStore

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

RedisRefreshTokenStore provides a Redis-based refresh token store with client-side caching

func NewRedisRefreshTokenStore

func NewRedisRefreshTokenStore(config *RedisConfig) (*RedisRefreshTokenStore, error)

NewRedisRefreshTokenStore creates a new Redis-based refresh token store with client-side caching

func (*RedisRefreshTokenStore) Cleanup

func (s *RedisRefreshTokenStore) Cleanup(ctx context.Context) (int, error)

Cleanup removes expired tokens and returns the number of tokens cleaned up Note: Redis automatically handles expiration, so this method scans for manually expired tokens

func (*RedisRefreshTokenStore) Close

func (s *RedisRefreshTokenStore) Close() error

Close closes the Redis client connection

func (*RedisRefreshTokenStore) Count

func (s *RedisRefreshTokenStore) Count(ctx context.Context) (int, error)

Count returns the total number of active refresh tokens

func (*RedisRefreshTokenStore) Delete

func (s *RedisRefreshTokenStore) Delete(ctx context.Context, token string) error

Delete removes a refresh token from storage

func (*RedisRefreshTokenStore) FlushDB

func (s *RedisRefreshTokenStore) FlushDB() error

FlushDB removes all keys from the current Redis database (useful for testing) Note: This method is not part of the RefreshTokenStorer interface

func (*RedisRefreshTokenStore) Get

func (s *RedisRefreshTokenStore) Get(ctx context.Context, token string) (any, error)

Get retrieves user data associated with a refresh token This method benefits from client-side caching for frequently accessed tokens

func (*RedisRefreshTokenStore) Ping

func (s *RedisRefreshTokenStore) Ping() error

Ping tests the Redis connection

func (*RedisRefreshTokenStore) Set

func (s *RedisRefreshTokenStore) Set(
	ctx context.Context,
	token string,
	userData any,
	expiry time.Time,
) error

Set stores a refresh token with associated user data and expiration

type RefreshTokenData

type RefreshTokenData = core.RefreshTokenData

Re-export types from core for backward compatibility

type RefreshTokenStorer

type RefreshTokenStorer = core.TokenStore

Re-export types from core for backward compatibility

type StoreType

type StoreType string

StoreType represents the type of store to create

const (
	// MemoryStore represents an in-memory token store
	MemoryStore StoreType = "memory"
	// RedisStore represents a Redis-based token store
	RedisStore StoreType = "redis"
)

Jump to

Keyboard shortcuts

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