jwt

package
v4.0.5 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2024 License: LGPL-3.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrParseClaimsFailed = errors.New("failed to parse not standard claims")
	ErrNoPublicKey       = errors.New("no public key to verity JWT")
	ErrEmptyToken        = errors.New("token is empty")
)
View Source
var (
	ErrInvalidPrivateKeyFile = errors.New("invalid private key file")
)

Functions

func CreateClaims

func CreateClaims(issuer string, inr time.Duration) jwt.Claims

CreateClaims 根据 issuer 与过期时间间隔创建一个 JWT Claims. 例如,issuer 可以是一个 APP ID.

func HMACVerifyCustomJWT

func HMACVerifyCustomJWT(tokenString string, opt HMACVerifyOption, claims Claims) (bool, error)

func LoadRSAPrivateKey

func LoadRSAPrivateKey(key []byte) (*rsa.PrivateKey, error)

LoadRSAPrivateKey 从私钥的字节序列中加载 RSA 私钥

func LoadRSAPrivateKeyFromPEM

func LoadRSAPrivateKeyFromPEM(keyFile, passFile string) (*rsa.PrivateKey, error)

LoadRSAPrivateKeyFromPEM 从 PEM 私钥文件 keyFile 与密码文件 passFile 中加载 RSA 私钥

func LoadRSAPublicKey

func LoadRSAPublicKey(key []byte) (*rsa.PublicKey, error)

LoadRSAPublicKey 从字节序列中加载 RSA 公钥

func LoadRSAPublicKeyFromPEM

func LoadRSAPublicKeyFromPEM(file string) (*rsa.PublicKey, error)

LoadRSAPublicKeyFromPEM 从 PEM 公钥文件 file 中加载 RSA 公钥

func RS256SignJWT

func RS256SignJWT(claims jwt.Claims, key *rsa.PrivateKey) (string, error)

RS256SignJWT 使用 RS256 算法对 claims 进行签名

func RS512SignJWT

func RS512SignJWT(claims jwt.Claims, key *rsa.PrivateKey) (string, error)

RS512SignJWT 使用 RS512 算法对 claims 进行签名

func RSAVerifyCustomJWT

func RSAVerifyCustomJWT(tokenString string, opt VerifyOption, claims Claims) (bool, error)

RSAVerifyCustomJWT 使用 RSA 算法 (RS256/RS384/RS512) 对包含自定义 Claims 的 JWT Token 进行验证。

func RSAVerifyJWT

func RSAVerifyJWT(tokenString string, opt VerifyOption) (bool, *jwt.StandardClaims, error)

RSAVerifyJWT 使用 RSA 算法 (RS256/RS384/RS512) 对 JWT Token 进行验证。

func RSAVerifyJWTWithKid

func RSAVerifyJWTWithKid(tokenString string, opt KidVerifyOption) (bool, *jwt.StandardClaims, error)

RSAVerifyJWTWithKid 使用 RSA 算法 (RS256/RS384/RS512) 对 JWT Token 进行验证。

Types

type Claims

type Claims interface {
	jwt.Claims

	// GetIssuer 返回 iss
	GetIssuer() string
	// GetExpiresAt 返回 exp
	GetExpiresAt() int64
	// GetIssuedAt 返回 iat
	GetIssuedAt() int64
}

type GetPublicKeyByKidFunc

type GetPublicKeyByKidFunc func(kid string) *rsa.PublicKey

GetPublicKeyByKidFunc 根据 kid 获取一个 rsa.PublicKey

type GetPublicKeyFunc

type GetPublicKeyFunc func(iss string) *rsa.PublicKey

GetPublicKeyFunc 根据 iss 获取一个 rsa.PublicKey

type HMACVerifyOption

type HMACVerifyOption struct {
	MaxExpInterval time.Duration // 最大过期时间间隔,单位为秒
	// app-key与密钥的对应关系
	// key: app-key
	// value: 密钥的 byte 数组
	SecretKeys map[string][]byte
}

HMACVerifyOption 验证参数

type KidVerifyOption

type KidVerifyOption struct {
	MaxExpInterval   time.Duration         // 最大过期时间间隔,单位为秒
	GetPublicKeyFunc GetPublicKeyByKidFunc // PublicKey 查找函数
}

KidVerifyOption 验证参数

type StandardClaims

type StandardClaims struct {
	jwt.StandardClaims
}

StandardClaims is a wrapper for jwt.StandardClaims

func (*StandardClaims) GetExpiresAt

func (c *StandardClaims) GetExpiresAt() int64

func (*StandardClaims) GetIssuedAt

func (c *StandardClaims) GetIssuedAt() int64

func (*StandardClaims) GetIssuer

func (c *StandardClaims) GetIssuer() string

type VerifyOption

type VerifyOption struct {
	MaxExpInterval   time.Duration    // 最大过期时间间隔,单位为秒
	GetPublicKeyFunc GetPublicKeyFunc // PublicKey 查找函数
}

VerifyOption 验证参数

Directories

Path Synopsis
tools

Jump to

Keyboard shortcuts

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