token

package
v0.0.0-...-fab0456 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidToken = errors.New("token is invalid")
	ErrExpiredToken = errors.New("token has expired")
)

Different types of error returned by the VerifyToken function

Functions

This section is empty.

Types

type JWTMaker

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

JWTMaker JSON Web Token

func (*JWTMaker) CreateToken

func (maker *JWTMaker) CreateToken(id string, username string, duration time.Duration) (string, *Payload, error)

CreateToken 根据用户名和时间创建一个新的token

func (*JWTMaker) VerifyToken

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

VerifyToken checks if the token is valid or not

type Maker

type Maker interface {
	// CreateToken 根据用户名和时间创建一个新的token
	CreateToken(id string, username string, duration time.Duration) (string, *Payload, error)

	// VerifyToken 校验token是否正确
	VerifyToken(token string) (*Payload, error)
}

Maker 管理token的接口定义

func NewJWTMaker

func NewJWTMaker(secretKey string) (Maker, error)

NewJWTMaker 创建一个新的JWTMaker

func NewPasetoMaker

func NewPasetoMaker(symmetricKey string) (Maker, error)

NewPasetoMaker creates a new PasetoMaker

type PasetoMaker

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

PasetoMaker is a PASETO token maker

func (*PasetoMaker) CreateToken

func (maker *PasetoMaker) CreateToken(id string, username string, duration time.Duration) (string, *Payload, error)

CreateToken creates a new token for a specific username and duration

func (*PasetoMaker) VerifyToken

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

VerifyToken checks if the token is valid or not

type Payload

type Payload struct {
	ID       string `json:"id"`
	Username string `json:"username"`
	jwt.RegisteredClaims
}

Payload contains the payload data of the token

func NewPayload

func NewPayload(id string, username string, duration time.Duration) *Payload

NewPayload creates a new token payload with a specific username and duration

func (*Payload) Valid

func (payload *Payload) Valid() error

Valid checks if the token payload is valid or not

Jump to

Keyboard shortcuts

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