token

package
v0.0.0-...-65546df Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2024 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrTokenNeedsRefresh      = errors.New("token needs refresh")
	ErrRefreshTokenExpired    = errors.New("refresh token expired")
	ErrAccessTokenExpired     = errors.New("access token expired")
	ErrTokenNotSupportRefresh = errors.New("token not support refresh")
	ErrMisMatchTokenPair      = errors.New("mismatch token pair")
)

Functions

This section is empty.

Types

type Cache

type Cache interface {
	// Get returns the given tokenId from the cache, just verifying if it exists
	Get(ctx context.Context, prefix, tokenId string) (string, bool, error)
	// TTL returns the rest life-time of the given token
	TTL(ctx context.Context, prefix, tokenId string) (time.Duration, bool, error)
	// Del removes the specified tokenId from the cache
	Del(ctx context.Context, prefix, tokenId string) error
	// Set sets the specified tokenId to the cache
	Set(ctx context.Context, prefix, tokenId, value string, expire time.Duration) error
}

Cache is responsible for persist jwt token.

type Claims

type Claims struct {
	// payload information
	Payload map[string]any
	// whether to need refresh
	Remember bool
	jwt.RegisteredClaims
}

Claims consisted of jwt.RegisteredClaims and custom Payload.

type MemoryCache

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

MemoryCache implement Cache by ttlcache.Cache in memory

func NewMemoryCache

func NewMemoryCache() *MemoryCache

func (*MemoryCache) Del

func (m *MemoryCache) Del(ctx context.Context, prefix, tokenId string) error

func (*MemoryCache) Get

func (m *MemoryCache) Get(ctx context.Context, prefix, tokenId string) (string, bool, error)

func (*MemoryCache) Set

func (m *MemoryCache) Set(ctx context.Context, prefix, tokenId, value string, expire time.Duration) error

func (*MemoryCache) TTL

func (m *MemoryCache) TTL(ctx context.Context, prefix, tokenId string) (time.Duration, bool, error)

type Options

type Options struct {
	// id generator
	IdGen func() string
	// token cache
	Cache         Cache
	AccessPrefix  string
	RefreshPrefix string

	// token issuer name
	Issuer string

	AccessSecret string
	AccessMethod jwt.SigningMethod
	// duration to expire access token
	AccessExpired time.Duration
	// delay duration after expiration
	AccessDelay time.Duration

	// duration to expire refresh token
	RefreshSecret string
	RefreshMethod jwt.SigningMethod
	// refresh token signing key
	RefreshExpired time.Duration
}

Options is configuration for token resolver

type Pair

type Pair struct {
	Access  Token
	Refresh Token
}

Pair is an issued token pair that consist of access-token and refresh-token

type RedisTokenCache

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

RedisTokenCache implements TokenCache interface for redis storage

func NewRedisTokenCache

func NewRedisTokenCache(client *redis.Client) *RedisTokenCache

func (*RedisTokenCache) Del

func (r *RedisTokenCache) Del(ctx context.Context, prefix, tokenId string) error

func (*RedisTokenCache) Get

func (r *RedisTokenCache) Get(ctx context.Context, prefix, tokenId string) (string, bool, error)

func (*RedisTokenCache) Set

func (r *RedisTokenCache) Set(ctx context.Context, prefix, tokenId, value string, expire time.Duration) error

func (*RedisTokenCache) TTL

func (r *RedisTokenCache) TTL(ctx context.Context, prefix, tokenId string) (time.Duration, bool, error)

type Resolver

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

Resolver is responsible for resolving jwt token

func NewResolver

func NewResolver(options Options) *Resolver

func (*Resolver) Issue

func (r *Resolver) Issue(ctx context.Context, payload map[string]any, refresh bool) (Pair, error)

Issue return a new issued token pair with given payload, it will return refresh token if refresh is true.

func (*Resolver) Refresh

func (r *Resolver) Refresh(ctx context.Context, accessTokenStr, refreshTokenStr string) (Pair, error)

Refresh refreshes the access token lifetime with the given refresh token

func (*Resolver) VerifyAccess

func (r *Resolver) VerifyAccess(ctx context.Context, tokenString string) (Token, error)

VerifyAccess verify the access-token if is valid.

func (*Resolver) VerifyRefresh

func (r *Resolver) VerifyRefresh(ctx context.Context, tokenString string) (Token, error)

type Token

type Token struct {
	// raw token string
	Raw    string
	Claims Claims
	Token  *jwt.Token
}

Token is Token Information, it could be from issued, parsed.

Jump to

Keyboard shortcuts

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