jwt

package
v3.0.4 Latest Latest
Warning

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

Go to latest
Published: May 29, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Sign

func Sign(claim jwtLib.Claims, conf Conf) (string, error)

Sign 签名

func SignWithMap

func SignWithMap(claims MapClaims, conf Conf) (string, error)

SignWithMap 签名

Types

type AppClaims

type AppClaims struct {
	Scopes   []string
	ScopeIDs []int64
	Name     string
	Email    string
	//more...
	jwtLib.StandardClaims
}

AppClaims App票据声明

func Verify

func Verify(tokenString string, conf Conf) (AppClaims, error)

Verify 验证

func (*AppClaims) GetToken

func (c *AppClaims) GetToken(method SigningMethod, secret interface{}) (string, error)

GetToken 获取token

type Conf

type Conf struct {
	Enable      bool   `yaml:"enable" toml:"enable" json:"enable"`                   //是否启用
	PublicKey   string `yaml:"public_key" toml:"public_key" json:"public_key"`       //公钥字符串或公钥文件地址
	PrivateKey  string `yaml:"private_key" toml:"private_key" json:"private_key"`    //私钥字符串或私钥文件地址
	Algorithm   string `yaml:"algorithm" toml:"algorithm" json:"algorithm"`          //加密算法: RS256 | RS512 | HS512
	HmacSecret  string `yaml:"hmac_secret" toml:"hmac_secret" json:"hmac_secret"`    //密钥
	TokenIssuer string `yaml:"token_issuer" toml:"token_issuer" json:"token_issuer"` //令牌颁发者
	// contains filtered or unexported fields
}

Conf 配置信息

<yaml example>

jwt_conf:

 enable: false
	public_key:
	private_key:
	algorithm: RS256
	hmac_secret: example
	token_issuer: authority

<toml example>

::jwt密钥配置::

[jwt_conf]

enable = false

公钥文件或字符串

public_key = ""

私钥文件或字符串

private_key = ""

加密算法: RS256 | RS512 | HS512

algorithm = "RS256"

密钥,当algorithm = "HS512"时需要配置此项

hmac_secret = "example"

令牌颁发者

token_issuer = "authority"

func (*Conf) GetPrivateKeyObj

func (c *Conf) GetPrivateKeyObj() *rsa.PrivateKey

GetPrivateKeyObj 获取私钥对象

func (*Conf) GetPublicKeyObj

func (c *Conf) GetPublicKeyObj() *rsa.PublicKey

GetPublicKeyObj 获取公钥对象

func (*Conf) Load

func (c *Conf) Load()

Load 载入配置

载入并解析公私钥,公私钥只要任意一个存在就会解析

func (*Conf) MustLoad added in v3.0.4

func (c *Conf) MustLoad()

MustLoad 载入配置

载入并解析公私钥,公私钥必须都存在,否则不会解析

type MapClaims

type MapClaims map[string]interface{}

func VerifyFromMap

func VerifyFromMap(tokenString string, conf Conf) (MapClaims, error)

VerifyFromMap 验证

func (*MapClaims) GetToken

func (c *MapClaims) GetToken(method SigningMethod, secret interface{}) (string, error)

GetToken 获取token

func (*MapClaims) Valid

func (c *MapClaims) Valid() error

Valid 验证token有效性

此验证方法继承 jwtLib.StandardClaims 的 Valid 验证方法

type SigningMethod

type SigningMethod string

SigningMethod 签名方法

const (
	SigningMethodRS256 SigningMethod = "RS256" //SigningMethodRS256 rsa256方法
	SigningMethodRS512 SigningMethod = "RS512" //SigningMethodRS512 rsa512方法
	SigningMethodHS512 SigningMethod = "HS512" //SigningMethodHS512 hmac方法
)

Jump to

Keyboard shortcuts

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