jwt

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2022 License: MIT Imports: 9 Imported by: 0

README

jwt

simple jwt

example:

	type tok struct {
		Name string `json:"name" validate:"required"`
		Age int `json:"age"`
	}

	jwt := NewJwt("f0950ead-a6c4-498b-ab3c-35ca91a730ad")
	token, err := jwt.CreateToken(tok{
		//Name: "dollarkiller",
		Age:  16,
	}, int64(time.Hour))
	if err != nil {
		panic(err)
	}

	tokenr, err := TokenFormatString(token)
	if err != nil {
		panic(err)
	}
	err = jwt.VerificationSignature(tokenr)
	if err != nil {
		panic(err)
	}
	var tr tok
	err = tokenr.Payload.Unmarshal(&tr)
	if err != nil {
		panic(err)
	}

	fmt.Println(tr)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AlgType

type AlgType string
const (
	HS256 AlgType = "HS256"
)
type Header struct {
	Typ string  `json:"typ"`
	Alg AlgType `json:"alg"`
}

type JWT

type JWT struct {
	// contains filtered or unexported fields
}

func NewJwt

func NewJwt(key string) *JWT

func (*JWT) CreateToken

func (jwt *JWT) CreateToken(payload interface{}, timeout int64) (string, error)

CreateToken :生成token

func (*JWT) VerificationSignature

func (jwt *JWT) VerificationSignature(token Token) error

VerificationSignature :验证token签名

type Payload added in v0.0.2

type Payload struct {
	Payload interface{} `json:"payload"`
	Timeout int64       `json:"timeout"`
}

func (Payload) Unmarshal added in v0.0.4

func (p Payload) Unmarshal(r interface{}) error

type Token

type Token struct {
	Header    Header  `json:"header"`    // 头
	Payload   Payload `json:"payload"`   // 主要载荷
	Signature string  `json:"signature"` // 签名
	// contains filtered or unexported fields
}

func TokenFormatString

func TokenFormatString(token string) (Token, error)

TokenFormatString :将token转换为token结构体

Jump to

Keyboard shortcuts

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