Documentation
¶
Overview ¶
Package jwt 提供 RS256 的 JWT 签发与校验。
硬契约(与既有消费方互通,不可改):
- alg 恰为 RS256
- JWT header 只含 {"alg":"RS256"},不带 typ
- 业务自定义声明平铺进 payload,解析时收集进 Claims.Extra
Index ¶
- Variables
- func MarshalKeyPairPEM(kp *KeyPair) (privPEM, pubPEM string, err error)
- func ParsePrivateKey(pemBytes []byte) (*rsa.PrivateKey, error)
- func ParsePublicKey(pemBytes []byte) (*rsa.PublicKey, error)
- func Sign(kp *KeyPair, opts ...SignOption) (string, error)
- type Claims
- type KeyPair
- type ParseOption
- type SignOption
Constants ¶
This section is empty.
Variables ¶
View Source
var ErrInvalidToken = errors.New("jwt: invalid token")
ErrInvalidToken 表示令牌无法解析或校验失败。
Functions ¶
func MarshalKeyPairPEM ¶
MarshalKeyPairPEM 把密钥对编为 PEM(私钥 PKCS#1,公钥 PKIX)。
func ParsePrivateKey ¶
func ParsePrivateKey(pemBytes []byte) (*rsa.PrivateKey, error)
ParsePrivateKey 解析 PEM 私钥(PKCS#1 / PKCS#8)。
func ParsePublicKey ¶
ParsePublicKey 解析 PEM 公钥(PKIX / PKCS#1),也接受 X.509 证书。
Types ¶
type Claims ¶
type Claims struct {
gojwt.RegisteredClaims
// Extra 承载业务自定义声明(签发时经 WithClaim 平铺进 payload,解析时收集回来)。
Extra map[string]any
}
Claims 是签发与解析的载荷容器。
type KeyPair ¶
type KeyPair struct {
Private *rsa.PrivateKey
Public *rsa.PublicKey
}
KeyPair 持有 RS256 的密钥对。
func GenerateKeyPair ¶
GenerateKeyPair 生成 RSA 密钥对(bits < 2048 时按 2048 处理)。
Click to show internal directories.
Click to hide internal directories.