jwtx

package
v0.0.0-2fad2c1 Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidToken = errors.New("Invalid token")
	ErrTokenRevoked = errors.New("Token has been revoked")
)

Functions

This section is empty.

Types

type Auther

type Auther interface {
	// Generate a JWT (JSON Web Token) with the provided subject.
	GenerateToken(ctx context.Context, subject string) (TokenInfo, error)
	// Generate a JWT with custom expiration time.
	GenerateTokenWithExpired(ctx context.Context, subject string, expired int) (TokenInfo, error)
	// Generate a refresh token with the provided subject and tenant.
	GenerateRefreshToken(ctx context.Context, subject string, tenant string) (RefreshTokenInfo, error)
	// Parse and validate a refresh token, returns subject, tenant, jti.
	ParseRefreshToken(ctx context.Context, refreshToken string) (string, string, string, error)
	// Invalidate a token by removing it from the token store.
	DestroyToken(ctx context.Context, accessToken string) error
	// Invalidate a refresh token by adding it to the blacklist.
	DestroyRefreshToken(ctx context.Context, refreshToken string) error
	// Parse the subject (or user identifier) from a given access token.
	ParseSubject(ctx context.Context, accessToken string) (string, error)
	// Release any resources held by the JWTAuth instance.
	Release(ctx context.Context) error
}

func New

func New(store Storer, opts ...Option) Auther

type Cacher

type Cacher interface {
	Set(ctx context.Context, ns, key, value string, expiration ...time.Duration) error
	Get(ctx context.Context, ns, key string) (string, bool, error)
	Exists(ctx context.Context, ns, key string) (bool, error)
	Delete(ctx context.Context, ns, key string) error
	Close(ctx context.Context) error
}

func NewMemoryCache

func NewMemoryCache(cfg MemoryConfig) Cacher

type CustomClaims

type CustomClaims struct {
	jwt.StandardClaims
	Type   string `json:"type,omitempty"`   // "access" or "refresh"
	Tenant string `json:"tenant,omitempty"` // Tenant ID
}

CustomClaims includes standard claims plus custom fields for refresh token

type JWTAuth

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

func (*JWTAuth) DestroyRefreshToken

func (a *JWTAuth) DestroyRefreshToken(ctx context.Context, refreshToken string) error

func (*JWTAuth) DestroyToken

func (a *JWTAuth) DestroyToken(ctx context.Context, tokenStr string) error

func (*JWTAuth) GenerateRefreshToken

func (a *JWTAuth) GenerateRefreshToken(ctx context.Context, subject string, tenant string) (RefreshTokenInfo, error)

func (*JWTAuth) GenerateToken

func (a *JWTAuth) GenerateToken(ctx context.Context, subject string) (TokenInfo, error)

func (*JWTAuth) GenerateTokenWithExpired

func (a *JWTAuth) GenerateTokenWithExpired(ctx context.Context, subject string, expired int) (TokenInfo, error)

func (*JWTAuth) ParseRefreshToken

func (a *JWTAuth) ParseRefreshToken(ctx context.Context, refreshToken string) (string, string, string, error)

func (*JWTAuth) ParseSubject

func (a *JWTAuth) ParseSubject(ctx context.Context, tokenStr string) (string, error)

func (*JWTAuth) Release

func (a *JWTAuth) Release(ctx context.Context) error

type MemoryConfig

type MemoryConfig struct {
	CleanupInterval time.Duration
}

type Option

type Option func(*options)

func SetExpired

func SetExpired(expired int) Option

func SetRefreshExpired

func SetRefreshExpired(expired int) Option

func SetSigningKey

func SetSigningKey(key, oldKey string) Option

func SetSigningMethod

func SetSigningMethod(method jwt.SigningMethod) Option

type RefreshTokenInfo

type RefreshTokenInfo interface {
	GetRefreshToken() string
	GetExpiresAt() int64
	EncodeToJSON() ([]byte, error)
}

type StoreOption

type StoreOption func(*storeOptions)

func WithCacheNS

func WithCacheNS(ns string) StoreOption

type Storer

type Storer interface {
	Set(ctx context.Context, tokenStr string, expiration time.Duration) error
	Delete(ctx context.Context, tokenStr string) error
	Check(ctx context.Context, tokenStr string) (bool, error)
	Close(ctx context.Context) error
}

Storer is the interface that storage the token.

func NewStoreWithCache

func NewStoreWithCache(cache Cacher, opts ...StoreOption) Storer

type TokenInfo

type TokenInfo interface {
	GetAccessToken() string
	GetTokenType() string
	GetExpiresAt() int64
	EncodeToJSON() ([]byte, error)
}

Jump to

Keyboard shortcuts

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