tokens

package
v1.1.9 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2026 License: GPL-3.0 Imports: 7 Imported by: 0

Documentation

Overview

Package tokens provides safe random token generation and validation.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidTokenConfig indicates an invalid token configuration.
	ErrInvalidTokenConfig = ewrap.New("invalid token config")
	// ErrTokenEmpty indicates the token is empty.
	ErrTokenEmpty = ewrap.New("token is empty")
	// ErrTokenTooLong indicates the token exceeds the configured max length.
	ErrTokenTooLong = ewrap.New("token is too long")
	// ErrTokenTooShort indicates the token is shorter than the minimum bytes.
	ErrTokenTooShort = ewrap.New("token is too short")
	// ErrTokenInvalid indicates the token is malformed or has invalid encoding.
	ErrTokenInvalid = ewrap.New("token is invalid")
	// ErrTokenInsufficientEntropy indicates the token lacks required entropy.
	ErrTokenInsufficientEntropy = ewrap.New("token entropy is insufficient")
)

Functions

This section is empty.

Types

type TokenEncoding

type TokenEncoding int

TokenEncoding defines the string encoding for tokens.

const (
	// TokenEncodingBase64URL encodes tokens using base64 URL encoding without padding.
	TokenEncodingBase64URL TokenEncoding = iota
	// TokenEncodingHex encodes tokens using hexadecimal encoding.
	TokenEncodingHex
)

type TokenGenerator

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

TokenGenerator generates cryptographically secure tokens. Instances of TokenGenerator contain only immutable configuration and can be safely used concurrently by multiple goroutines.

func NewGenerator

func NewGenerator(opts ...TokenOption) (*TokenGenerator, error)

NewGenerator constructs a token generator with safe defaults.

func (*TokenGenerator) Generate

func (g *TokenGenerator) Generate() (string, error)

Generate produces a new token encoded as a string.

func (*TokenGenerator) GenerateBytes

func (g *TokenGenerator) GenerateBytes() ([]byte, error)

GenerateBytes produces raw token bytes.

type TokenOption

type TokenOption func(*tokenOptions) error

TokenOption configures token generation and validation.

func WithTokenEncoding

func WithTokenEncoding(encoding TokenEncoding) TokenOption

WithTokenEncoding sets the token encoding.

func WithTokenMaxLength

func WithTokenMaxLength(maxLength int) TokenOption

WithTokenMaxLength sets the maximum accepted token length in characters.

func WithTokenMinBytes

func WithTokenMinBytes(minBytes int) TokenOption

WithTokenMinBytes sets the minimum decoded token length in bytes.

func WithTokenMinEntropyBits

func WithTokenMinEntropyBits(bits int) TokenOption

WithTokenMinEntropyBits sets the minimum entropy bits required.

type TokenValidator

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

TokenValidator validates token strings. Instances of TokenValidator contain only immutable configuration and can be safely used concurrently by multiple goroutines.

func NewValidator

func NewValidator(opts ...TokenOption) (*TokenValidator, error)

NewValidator constructs a token validator with safe defaults.

func (*TokenValidator) Validate

func (v *TokenValidator) Validate(token string) ([]byte, error)

Validate checks a token string and returns the decoded bytes.

Jump to

Keyboard shortcuts

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