token

package
v0.0.0-...-63964f8 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2023 License: GPL-3.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrExpiredToken = errors.New("token has expired")
	ErrInvalidToken = errors.New("token is invalided")
)
View Source
var ErrSecretLen = errors.New("密钥长度不合法")

Functions

This section is empty.

Types

type JWTMaker

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

JWTMaker is a JSON Web Token maker

func (*JWTMaker) CreateToken

func (maker *JWTMaker) CreateToken(content []byte, duration time.Duration) (string, *Payload, error)

CreateToken creates a new token for a specific content and duration

func (*JWTMaker) VerifyToken

func (maker *JWTMaker) VerifyToken(token string) (*Payload, error)

VerifyToken 解析和验证 JWT 令牌,并提取其中的声明 如果令牌解析过程中发生错误,会根据错误类型进行相应的处理,如判断是否过期错误。 最后,如果解析成功,则返回解析后的 Payload 结构体指针;否则,返回相应的错误。

type Maker

type Maker interface {
	// CreateToken creates a new token for a specific username and duration
	CreateToken(content []byte, duration time.Duration) (string, *Payload, error)
	// VerifyToken checks if the token is valid or not
	VerifyToken(token string) (*Payload, error)
}

Maker is an interface for managing tokens

func NewJWTMaker

func NewJWTMaker(secretKey string) (Maker, error)

func NewPasetoMaker

func NewPasetoMaker(Key string) (Maker, error)

type PasetoMaker

type PasetoMaker struct {
	Key []byte //symmetric对称
	// contains filtered or unexported fields
}

func (*PasetoMaker) CreateToken

func (maker *PasetoMaker) CreateToken(content []byte, duration time.Duration) (string, *Payload, error)

CreateToken 构造token

func (*PasetoMaker) VerifyToken

func (maker *PasetoMaker) VerifyToken(token string) (*Payload, error)

VerifyToken checks if the token is valid or not

type Payload

type Payload struct {
	ID        uuid.UUID `json:"id"`
	Content   []byte    `json:"content"` // 可以是任意内容
	IssuedAt  time.Time `json:"issued_at"`
	ExpiredAt time.Time `json:"expired_at"`
}

Payload contains the payload data of the token

func NewPayload

func NewPayload(content []byte, duration time.Duration) (*Payload, error)

func (*Payload) Valid

func (payload *Payload) Valid() error

type User

type User struct {
	UserID   int64  `json:"userid"`
	UserName string `json:"username"`
}

Jump to

Keyboard shortcuts

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