token

package
v0.0.0-...-7735b63 Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2025 License: MIT Imports: 8 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 invalid")
)

Different types of token Errors we will return

Functions

This section is empty.

Types

type AsymPasetoMaker

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

func (*AsymPasetoMaker) CreateToken

func (maker *AsymPasetoMaker) CreateToken(username string, role string, duration time.Duration) (string, *Payload, error)

func (*AsymPasetoMaker) VerifyToken

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

type JWTMaker

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

func (*JWTMaker) CreateToken

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

CreateToken Create a token for a specific username with a duration

func (*JWTMaker) VerifyToken

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

VerifyToken Check if the input token is valid or not

type Maker

type Maker interface {

	// CreateToken Create a token for a specific username with a duration
	CreateToken(username string, role string, duration time.Duration) (string, *Payload, error)

	// VerifyToken Check if the input token is valid or not
	VerifyToken(token string) (*Payload, error)
}

Maker interface will be used to manage the token creation and verification We will be creating support for both JWT and PASETO tokens

func NewAsymPasetoMaker

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

func NewJWTMaker

func NewJWTMaker(secretKey string) (Maker, error)

func NewPasetoMaker

func NewPasetoMaker(symmetricKey string) (Maker, error)

type PasetoMaker

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

func (*PasetoMaker) CreateToken

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

func (*PasetoMaker) VerifyToken

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

type Payload

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

Payload will hold payload data of token

func NewPayload

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

func (*Payload) Valid

func (payload *Payload) Valid() error

Valid checks if the payload is valid or not

Jump to

Keyboard shortcuts

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