token

package module
v0.0.0-...-6bbbdd2 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2022 License: GPL-3.0 Imports: 8 Imported by: 0

README

token

token 是一个封装常用基于 token 验证的工具,包含了JWT和PASETO两种实现,如果要实现自己的token,可以参考 github 中的实现。

关于JWT的缺点以及PASETO的优势可以查看这篇文章

参考

o1egl/paseto: Platform-Agnostic Security Tokens implementation in GO (Golang) (github.com)

dgrijalva/jwt-go: ARCHIVE - Golang implementation of JSON Web Tokens (JWT). This project is now maintained at: (github.com)

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 is a JSON Web Token maker

func (*JWTMaker) CreateToken

func (maker *JWTMaker) CreateToken(payload Payloads, opt ...interface{}) (string, error)

CreateToken creates a new token for a specific username and duration

func (*JWTMaker) VerifyToken

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

VerifyToken checks if the token is valid or not

type Maker

type Maker interface {
	// CreateToken creates a new token for Payloads
	CreateToken(payload Payloads, opt ...interface{}) (string, 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)

NewJWTMaker creates a new JWTMaker

func NewMyLocalMaker

func NewMyLocalMaker(symmetricKey string) (Maker, error)

func NewMyPublicMaker

func NewMyPublicMaker(publicKey ed25519.PublicKey, privateKey ed25519.PrivateKey) (Maker, error)

func NewPasetoMaker

func NewPasetoMaker(symmetricKey string) (Maker, error)

func NewPasetoPublicMaker

func NewPasetoPublicMaker(publicKey ed25519.PublicKey, privateKey ed25519.PrivateKey) (Maker, error)

type MyLocalPayload

type MyLocalPayload struct {
	Payload
	Username string
}

type MyLocalToken

type MyLocalToken struct {
	PasetoMaker
}

func (*MyLocalToken) CreateToken

func (maker *MyLocalToken) CreateToken(payload Payloads, opt ...interface{}) (string, error)

CreateToken creates a new token for a specific username and duration

func (*MyLocalToken) VerifyToken

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

VerifyToken checks if the token is valid or not

type MyPublicPayload

type MyPublicPayload struct {
	Payload
	Username string
}

type MyPublicToken

type MyPublicToken struct {
	PasetoPublicMaker
}

func (*MyPublicToken) CreateToken

func (maker *MyPublicToken) CreateToken(payload *Payload, opt ...interface{}) (string, error)

CreateToken creates a new token for a specific username and duration

func (*MyPublicToken) VerifyToken

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

VerifyToken checks if the token is valid or not

type PasetoMaker

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

func (*PasetoMaker) CreateToken

func (maker *PasetoMaker) CreateToken(payload Payloads, opt ...interface{}) (string, error)

CreateToken creates a new token for a specific username and duration

func (*PasetoMaker) VerifyToken

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

VerifyToken checks if the token is valid or not

type PasetoPublicMaker

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

func (*PasetoPublicMaker) CreateToken

func (maker *PasetoPublicMaker) CreateToken(payload Payloads, opt ...interface{}) (string, error)

CreateToken creates a new token for a specific username and duration

func (*PasetoPublicMaker) VerifyToken

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

VerifyToken checks if the token is valid or not

type Payload

type Payload struct {
	ID        uuid.UUID `json:"id"`
	IssuedAt  time.Time `json:"issued_at"`
	ExpiredAt time.Time `json:"expired_at"`
}

Payload contains the payload data of the token

func NewPayload

func NewPayload(duration time.Duration) (*Payload, error)

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

type Payloads

type Payloads interface {
	Valid() error
}

Jump to

Keyboard shortcuts

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