jwtx

package
v1.4.4 Latest Latest
Warning

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

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

Documentation

Overview

Package jwtx 提供与传输无关的 JWT 签发和解析, 供 server 鉴权中间件和应用侧共用同一套语义。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Claims added in v1.4.1

type Claims struct {
	ID        string
	Subject   string
	IssuedAt  Time
	ExpiresAt Time
	Extra     map[string]any
}

Claims 是带常用标准声明和额外声明的 JWT 载荷:jti/sub/iat/exp 为独立字段, 其余声明与标准声明同级扁平存放在 Extra 中,可直接用于 Sign 和 Parse。

func (Claims) Decode added in v1.4.1

func (c Claims) Decode(target any) error

Decode 把额外声明整体解码到目标结构,用于业务侧读取强类型的自定义声明。

func (Claims) GetAudience added in v1.4.1

func (c Claims) GetAudience() (jwt.ClaimStrings, error)

func (Claims) GetExpirationTime added in v1.4.1

func (c Claims) GetExpirationTime() (*jwt.NumericDate, error)

func (Claims) GetIssuedAt added in v1.4.1

func (c Claims) GetIssuedAt() (*jwt.NumericDate, error)

func (Claims) GetIssuer added in v1.4.1

func (c Claims) GetIssuer() (string, error)

func (Claims) GetNotBefore added in v1.4.1

func (c Claims) GetNotBefore() (*jwt.NumericDate, error)

func (Claims) GetSubject added in v1.4.1

func (c Claims) GetSubject() (string, error)

func (Claims) MarshalJSON added in v1.4.1

func (c Claims) MarshalJSON() ([]byte, error)

func (*Claims) UnmarshalJSON added in v1.4.1

func (c *Claims) UnmarshalJSON(data []byte) error

type Codec

type Codec struct {
	Signer
	Parser
}

Codec 同时提供签发和解析能力,对应同一算法和密钥。

func Ed25519

func Ed25519(privateKeyPEM string) (Codec, error)

Ed25519 从 PEM 编码的 PKCS#8 私钥构造签发和解析 EdDSA token 的 Codec, 解析侧使用私钥派生的公钥。

func HS256

func HS256(secret string) Codec

HS256 返回以对称密钥签发和解析 HS256 token 的 Codec。

type MapClaims added in v1.4.1

type MapClaims = jwt.MapClaims

MapClaims 与服务端中间件写入上下文的声明类型一致,用于构造或读取任意 payload。

type Parser

type Parser struct {
	KeyFunc      jwt.Keyfunc
	ValidMethods []string
}

Parser 按算法白名单和密钥函数解析并校验 token。

func (Parser) Parse

func (p Parser) Parse(token string, claims jwt.Claims) error

Parse 解析 token 并把结果解码到 claims;签名、算法和有效期校验失败均返回错误。

type Signer

type Signer struct {
	Method jwt.SigningMethod
	Key    any
}

Signer 以指定算法和密钥签发 token。

func (Signer) Sign

func (s Signer) Sign(claims jwt.Claims) (string, error)

Sign 签发指定 Claims 的 token。

type Time added in v1.4.1

type Time struct {
	time.Time
}

Time 表示 JWT 声明中的时间,payload 内以秒级时间戳编解码。

func NewTime added in v1.4.1

func NewTime(t time.Time) Time

NewTime 包装指定时间为 JWT 声明时间。

func (Time) MarshalJSON added in v1.4.1

func (t Time) MarshalJSON() ([]byte, error)

func (*Time) UnmarshalJSON added in v1.4.1

func (t *Time) UnmarshalJSON(data []byte) error

Jump to

Keyboard shortcuts

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