jgjwt

package module
v0.9.2 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2023 License: MIT Imports: 8 Imported by: 0

README

go-jwt

跟jwt相关的便捷函数

Documentation

Index

Constants

View Source
const (
	KeyIat     = "iat"
	KeyExp     = "exp"
	KeyPayload = "payload"
	KeyKVs     = "kvs"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Claims

type Claims struct {
	Iat     int64                  `json:"iat"`     // issued at, 签发时间
	Exp     int64                  `json:"exp"`     // expiration time, 过期时间
	Payload string                 `json:"payload"` // payload, 有效载荷
	KVs     map[string]interface{} `json:"kvs"`     // 其他自定义字段
}

type Config

type Config struct {
	MaxAge time.Duration `mapstructure:"max_age"` // 会话有效时长,如:20s

	// HS256相关
	HS256SecretIsBase64 bool   `mapstructure:"hs256_secret_is_base64"` // 密钥是否为base64编码
	HS256Secret         string `mapstructure:"hs256_secret"`           // 密钥

	// RS256相关
	PrivateKeyPath string `mapstructure:"private_key_path"` // 私钥路径
	PublicKeyPath  string `mapstructure:"public_key_path"`  // 公钥路径
}

type JWT

type JWT struct {
	Cfg         Config
	HS256Secret []byte
	PublicKey   *rsa.PublicKey
	PrivateKey  *rsa.PrivateKey
}

func NewJWT

func NewJWT(cfg *Config) (*JWT, error)

func (*JWT) DecodeHS256Token

func (j *JWT) DecodeHS256Token(token string) (*Claims, error)

func (*JWT) DecodeRS256Token

func (j *JWT) DecodeRS256Token(token string) (*Claims, error)

func (*JWT) DecodeTokenUnverified

func (j *JWT) DecodeTokenUnverified(token string) (*Claims, error)

func (*JWT) GenHS256Token

func (j *JWT) GenHS256Token(payload string, kvs map[string]interface{}) (string, error)

func (*JWT) GenRS256Token

func (j *JWT) GenRS256Token(payload string, kvs map[string]interface{}) (string, error)

Jump to

Keyboard shortcuts

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