jwtkit

package
v2.3.1 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

Package jwtkit 支持设置多种签名算法 默认使用HMAC 支持RSA、ECDSA、HMAC 支持手动设置密钥内容,密钥路径 支持自动生成密钥

Index

Constants

View Source
const SigningMethodNone = "none" // 无签名

Variables

View Source
var (
	ErrUnsupportedAlgorithm = errors.New("不支持的签名算法")
	ErrKeyFileNotFound      = errors.New("密钥文件不存在")
	ErrInvalidKeyFormat     = errors.New("无效的密钥格式")
)

Functions

This section is empty.

Types

type Algorithm

type Algorithm string
const (
	HS256 Algorithm = "HS256"
	HS384 Algorithm = "HS384"
	HS512 Algorithm = "HS512"
)

HMAC 系列

const (
	RS256 Algorithm = "RS256"
	RS384 Algorithm = "RS384"
	RS512 Algorithm = "RS512"
)

RSA 签名系列

const (
	PS256 Algorithm = "PS256"
	PS384 Algorithm = "PS384"
	PS512 Algorithm = "PS512"
)

PSS 签名系列

const (
	ES256 Algorithm = "ES256"
	ES384 Algorithm = "ES384"
	ES512 Algorithm = "ES512"
)

ECDSA 签名系列

const EdDSA Algorithm = "EdDSA" // EdDSA 签名系列

func (Algorithm) String

func (a Algorithm) String() string

type AsymmetricKeys

type AsymmetricKeys struct {
	Private     string `json:"private" yaml:"private" ini:"private"`
	Public      string `json:"public" yaml:"public" ini:"public"`
	PrivatePath string `json:"private_path" yaml:"private_path" ini:"private_path"`
	PublicPath  string `json:"public_path" yaml:"public_path" ini:"public_path"`
}

AsymmetricKeys 非对称密钥

type HMAC

type HMAC struct {
	Secret     string `json:"secret" yaml:"secret" ini:"secret"`                // HMAC密钥
	SecretPath string `json:"secret_path" yaml:"secret_path" ini:"secret_path"` // HMAC密钥路径
}

type JWTManager

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

func NewJWTManager

func NewJWTManager(config *Options, snowflake *sonyflakekit.IDGenerator) (*JWTManager, error)

func (*JWTManager) GenerateToken

func (m *JWTManager) GenerateToken(claims *StandardClaims) (string, error)

func (*JWTManager) ValidateToken

func (m *JWTManager) ValidateToken(tokenString string) (*StandardClaims, error)

type Options

type Options struct {
	Algorithm    Algorithm      `json:"algorithm" yaml:"algorithm" ini:"algorithm"`             // 签名算法
	AutoGenerate bool           `json:"auto_generate" yaml:"auto_generate" ini:"auto_generate"` // 是否自动生成密钥
	KeyStoreDir  string         `json:"key_store_dir" yaml:"key_store_dir" ini:"key_store_dir"` // 密钥存储目录
	HMAC         HMAC           `json:"hmac" yaml:"hmac" ini:"hmac"`                            // HMAC
	RSA          AsymmetricKeys `json:"rsa" yaml:"rsa" ini:"rsa"`                               // RSA
	ECDSA        AsymmetricKeys `json:"ecdsa" yaml:"ecdsa" ini:"ecdsa"`                         // ECDSA
	EdDSA        AsymmetricKeys `json:"eddsa" yaml:"eddsa" ini:"eddsa"`                         // EdDSA
}

type StandardClaims

type StandardClaims struct {
	UserId    dataType.IntString[int64] `json:"user_id"`
	TenantID  dataType.IntString[int64] `json:"tenant_id"`  // 租户ID
	Timestamp time.Time                 `json:"timestamp"`  // 登录时间
	LoginIp   string                    `json:"login_ip"`   // 登录IP
	ExpiresAt *time.Time                `json:"expires_at"` // 过期时间
	Extras    map[string]any            `json:"extras"`     // 扩展字段
	jwt.RegisteredClaims
}

StandardClaims 标准Claims exp 过期时间 nbf 生效时间 iat 签发时间 iss 签发人 签发者URL或者应用标识 aud 接收人 给谁使用的 也可以是URL或者应用标识 sub 主题 jti 唯一标识

Jump to

Keyboard shortcuts

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