token

package
v0.0.22 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2024 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// EncoderBase32 specifies base32 encoding for token generation
	EncoderBase32 = "base32"
	// EncoderHex specifies hexadecimal encoding for token generation
	EncoderHex = "hex"

	// DefaultLength is the default token length in bytes
	DefaultLength = 16
	// DefaultEncoder is the default encoding method
	DefaultEncoder = EncoderBase32

	// MinLength is the minimum allowed token length in bytes
	MinLength = 16
	// MaxLength is the maximum allowed token length in bytes
	MaxLength = 64
)

Variables

View Source
var (
	ErrTokenGeneration = errors.New("failed to generate token")
	ErrTokenNotFound   = errors.New("token not found")
	ErrTokenExpired    = errors.New("token expired")
)
View Source
var DefaultOptions = Options{
	Expiration:      10 * time.Minute,
	CleanupInterval: 5 * time.Minute,
}

DefaultOptions provides recommended settings

Functions

func Generate

func Generate(opts ...Option) (string, error)

Generate creates a new random token with the given options If no options are provided, DefaultLength and DefaultEncoder will be used

func Hash

func Hash(plaintext string) string

Hash creates a SHA-256 hash of the token Returns the hash as a lowercase hex-encoded string

Types

type Option

type Option func(*config)

func WithEncoder

func WithEncoder(encoder string) Option

WithEncoder sets the encoding format (EncoderBase32 or EncoderHex) If an invalid encoder is specified, DefaultEncoder will be used

func WithLength

func WithLength(length int) Option

WithLength sets the token length in bytes If length is outside valid range (MinLength to MaxLength), DefaultLength will be used instead

type Options

type Options struct {
	Expiration      time.Duration // Token expiration duration
	CleanupInterval time.Duration // Cleanup interval duration
}

Options configures the Store

type Store

type Store struct {
	sync.RWMutex // Protects the tokens map
	// contains filtered or unexported fields
}

Store is a thread-safe store for tokens

func NewTokenStore

func NewTokenStore(opts *Options) *Store

NewTokenStore creates a new token store with the given options

func (*Store) Close

func (ts *Store) Close()

Close closes the store and cleans up any resources

func (*Store) Create

func (ts *Store) Create(message string) (string, error)

Create creates a new token with the given message and returns the token

func (*Store) Get

func (ts *Store) Get(plainToken string) (string, error)

Get returns the message associated with the given token The token is deleted after retrieval (one-time use)

func (*Store) Verify

func (ts *Store) Verify(plainToken string) (bool, error)

Verify checks if a token exists and is valid without consuming it

Jump to

Keyboard shortcuts

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