Documentation
¶
Index ¶
- Variables
- type Cache
- type Claims
- type MemoryCache
- func (m *MemoryCache) Del(ctx context.Context, prefix, tokenId string) error
- func (m *MemoryCache) Get(ctx context.Context, prefix, tokenId string) (string, bool, error)
- func (m *MemoryCache) Set(ctx context.Context, prefix, tokenId, value string, expire time.Duration) error
- func (m *MemoryCache) TTL(ctx context.Context, prefix, tokenId string) (time.Duration, bool, error)
- type Options
- type Pair
- type RedisTokenCache
- func (r *RedisTokenCache) Del(ctx context.Context, prefix, tokenId string) error
- func (r *RedisTokenCache) Get(ctx context.Context, prefix, tokenId string) (string, bool, error)
- func (r *RedisTokenCache) Set(ctx context.Context, prefix, tokenId, value string, expire time.Duration) error
- func (r *RedisTokenCache) TTL(ctx context.Context, prefix, tokenId string) (time.Duration, bool, error)
- type Resolver
- func (r *Resolver) Issue(ctx context.Context, payload map[string]any, refresh bool) (Pair, error)
- func (r *Resolver) Refresh(ctx context.Context, accessTokenStr, refreshTokenStr string) (Pair, error)
- func (r *Resolver) VerifyAccess(ctx context.Context, tokenString string) (Token, error)
- func (r *Resolver) VerifyRefresh(ctx context.Context, tokenString string) (Token, error)
- type Token
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
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 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
type Resolver ¶
type Resolver struct {
// contains filtered or unexported fields
}
Resolver is responsible for resolving jwt token
func NewResolver ¶
func (*Resolver) Issue ¶
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 ¶
VerifyAccess verify the access-token if is valid.
Click to show internal directories.
Click to hide internal directories.