jwt

package
v0.0.5-beta2 Latest Latest
Warning

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

Go to latest
Published: May 15, 2025 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultJWTExpire = 1 * constant.Hour // token默认有效期1小时,以秒为单位
)

Variables

View Source
var (
	ErrInvalidToken   = errors.New("Invalid token")
	ErrRdsClientNil   = errors.New("Redis client is nil")
	ErrInvalidTokenID = errors.New("Invalid token id")
	ErrInvalidUserID  = errors.New("Invalid user id")
)

Functions

This section is empty.

Types

type Claims

type Claims struct {
	TokenID   int64 `json:"token_id"`
	UserID    int64 `json:"user_id"`
	TenantID  int64 `json:"tenant_id"`
	IssuedAt  int64 `json:"issued_at"`  // 创建时间
	ExpiresAt int64 `json:"expires_at"` // 过期时间
}

Claims 载荷信息

func (*Claims) EqTokenID

func (c *Claims) EqTokenID(tokenID int64) bool

func (*Claims) IsRefresh

func (c *Claims) IsRefresh() bool

IsRefresh 检查载荷是否需要刷新 调用前应确认claims有效

func (*Claims) Valid

func (c *Claims) Valid() error

Valid 检查是否有效

type MyJWT

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

func New

func New(expire ...int64) *MyJWT

New 创建一个jwt工具对象, 单位秒

func (*MyJWT) GetClaims

func (j *MyJWT) GetClaims() (*Claims, error)

func (*MyJWT) GetSaveTokenID

func (j *MyJWT) GetSaveTokenID(rds *redis.Client) (tokenID int64, err error)

GetSaveTokenID 获取redis存储的tokenID

func (*MyJWT) GetToken

func (j *MyJWT) GetToken() (string, error)

func (*MyJWT) MakeClaims

func (j *MyJWT) MakeClaims(userID int64) *MyJWT

MakeClaims 创建jwt载荷 生成随机tokenID

func (*MyJWT) MakeToken

func (j *MyJWT) MakeToken() *MyJWT

MakeToken 根据载荷生成签名token串 传入claims的[IssuedAt, ExpiresAt]将被更新

func (*MyJWT) ParseClaims

func (j *MyJWT) ParseClaims() *MyJWT

func (*MyJWT) SaveTokenID

func (j *MyJWT) SaveTokenID(rds *redis.Client) error

SaveTokenID redis 存储生成的tokenID

func (*MyJWT) SplitToken

func (j *MyJWT) SplitToken(token string) *MyJWT

func (*MyJWT) WithCtx

func (j *MyJWT) WithCtx(ctx context.Context) *MyJWT

Jump to

Keyboard shortcuts

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